
This course includes our updated coding exercises so you can practice your skills as you learn.
See a demo
What this course is all about and how you will benefit from it
Using real example let's get familiar with how PHPUnit works and why it is very useful thing
Brief description of each section of the course
Take a look at the real world application that we will be building during the course
Basic steps you should follow in order to get the most out of this course
And why you will see almost no errors in my courses
Install PHPUnit
How to use PHPUnit command line - run one test or all from a test folder. Generate configuration file
Follow naming conventions and basic steps when creating tests in PHPUnit - create our first test!
PHPUnit command-line test runner shows us different statuses of our tests - success, failure and others
Write Agile test results to file & show on the console
Write most common assertions in PHPUnit and test it
Write most common annotations in PHPUnit and test it
Delegate testing set of data to another method - data provider
If we want to use data changed in previous tests - use test dependencies
Test errors and exceptions in PHPUnit. Use trigger_error built in PHP function
Apply coloring console test output and testdox Agile format automatically without specifing options in command line
You can make shorter console command when running tests thanks to PHPUnit XML configuration file
In order to write less code and not make code duplication, we can create constants and other stuff in XML configuration file to later use in tests
Setup initial state before testing and reset it after testing. Use traits
Use closures and anonymous classes to test private and protected attributes
Use anonymous classes to mock objects without setter method - in poorly designed or legacy code
Use closures and anonymous classes to test private and protected methods
Use the power of interfaces to write tests easily
Instead of mocking objects you can utilize PHP 7 anonymous classes
Use PHP callable to make untestable code testable! (in isolation)
If you have no time to implement a test to the end you can mark it as incomplete in order not to forget about it. You can also skip tests if there are no custom extension for PHP. Also about risky tests
If you have some complex data structure to be tested a lot - write custom assertion for it
Code coverage report helps us to find part of our code that was not tested yet. Ignore code that you do not want to be included in code coverage report
If you want to replace real object methods for testing use stubs
If you want to not only replace real object methods for testing but also check (test) how many times real method should be called, which arguments should be passed in etc. - use mock objects
How to mock traits and abstract classes without dealing with its child classes
If your class calls external web service you usually do not want to use it when testing internal class functionalities - so mock the web service!
About unit, integration, functional and acceptance testing
We use Selenium Web Driver to automate clicking on links, buttons on a website, etc.
You can read values of different types of html elements using css selectors, by tag name, id, etc.
Click links (two methods using Selenium). Use commands for back, forward and refresh the page
Provide values for html input text, textarea, select item in a list, checkboxes etc. Then send the form using Selenium
Other useful PHPUnit Selenium WebDriver stuff
Explicit waiting for data to be asserted
Using TDD approach, first we write a test, then the code that makes the test pass as soon as possible, at the end refactor the code
We will build example TDD (not TODO :) app with Slim PHP micro framework. But this is not mandatory, you can build the app using just plain PHP or framework of your choice
Use Zurb Foundation to build html, css and js packed layout for our sample app
Install and configure PHPUnit & Selenium Web Driver. Fire up Selenium server
Let's start with frontend stuff. Our first test - make it pass by moving the template to the Slim app
Test confirmation box when trying to delete a category and see message after deleting a category
Assert existing links to edit and delete a category after we click the category link
Click edit link and see proper stuff for this on the website. Fill inputs with edited category data. Mark test as incomplete
Test validation of the html form and see if correct messages are displayed
Test visibility of categories in the menu. Locate nested elements using Xpath method
In order to display nested categories on the website, first convert database result into nested PHP array
Refactor the code from the previous step. Use recursion to create final solution for multilevel nested array
Use setUp method and data provider
Let's build ul li html list based on converted database result
Produce data for html option list
Install Laravel Eloquent ORM to manage database operations for categories. Make first acceptance test involving database operation
Using Laravel models makes it easier to interact with the database especially when it comes to relationships between models
Create global variable to show categories on all web pages
And make integration test to test creating html string based on database result
Convert categories array from database to html nested ul li list. Display it dynamically on the website
We have to improve some of the tests because after changing code they no longer pass
Delete parent category and check if also child categories were deleted
Show chosen category dynamically. Make integration test for this purpose. Use mocking environment in Slim to avoid acceptance test if necessary
Make input values dynamic when a category is edited. Create an acceptance test for this
After that we have to change rest of the code to make all tests pass
Make browser acceptance test for that
Make it possible to change not only name and description but also parent category for edited one
And refactor our code a bit
Add redirection after deleting a category for example
There were some code duplications while building this app. Refactor it.
If you want you can build this app again with your framework or in plain PHP
And configure PHPUnit
Make GET and POST requests to test live public api
Create ApiClient class to test rest api
If we still test live api, we can mock responses in order to not actually hit live api
With Json Server we can easily setup environment for testing our api
Test POST and DELETE requests using Json Server
Use patch request to update record, put request to replace entire record in the database
Do you want to be able to test your code and thus become a better programmer?
Welcome to the very comprehensive PHPUnit (testing framework for PHP) and Selenium for PHPUnit course. PHPUnit is a tool for testing code written in PHP language. And Selenium is a tool for automating web browsers that we will use in this course to test application interfaces. You will learn not only about unit tests but also what integration, functional and acceptance tests are. Particular emphasis will be placed on php unit testing.
Code testing is very important. Here there are most important benefits:
you don't waste time as a web developer for manual testing in a web browser
you find mistakes in the code faster
what's more, by mastering PHPUnit you will start to write code that is testable (you will learn in the course what is non-testable code!)
and your code will be easier to maintain, i.e. it will be easier to adapt it to the changes you might need
In short: if you haven't known PHPUnit yet, you'll simply be a better PHP programmer after completing this course. PHPUnit is a very important skill and if you're looking for a job as a PHP developer, it will allow you to increase your chances to get hired.
During the course, we will be discussing the most important aspects of PHPUnit and Selenium for PHPUnit on practical examples with hands-on coding, such as assertions, data providers, mocks and others (see the curriculum for more information). You will also learn how to test api clients.
The most interesting part of this course is building from scratch a real MVC-based application in PHP and PHPUnit using TDD - Test Driven Development approach. This is an application for managing product multi-level categories - which is not easy to implement. PHPUnit will be particularly useful here. It will be a very comprehensive TDD - not only unit tests but we will also test the user interface of this application using Selenium WebDriver. The HTML template of this application built using the Zurb Foundation Framework will be available to download (not because I think it is the best, just the template for this particular application was easiest to build using Zurb). We will be coding the backend from scratch using Slim Microframework for PHP. We will also use a great package from Laravel called illuminate/database for Laravel Eloquent ORM, thanks to which database operations are very easy.
To better consolidate the acquired knowledge the course includes, in addition to lectures, coding exercises, quizzes, assignments and other practical activities.
For each lecture that requires coding, there are file resources that you can download and copy & paste, but I encourage you to write code from the screen.
I promise that you'll be a better PHP developer after completing this PHPUnit course. And even if for some reason you will not like this course, Udemy always offers you a 30-day money back guarantee. But I doubt that you will not like it. I am 100% sure that you will be happy with this course and your PHP code will be better from now!