Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We don't always have the option to build things perfectly the first time, and we're always learning, so that perfect way is going to change. A great thing about PHPUnit is its wide adoption. Almost every framework comes shipped with PHPUnit or allows for easy integration. You can also add PHPUnit as a separate package for any object-oriented application.
composer.json
composer.json file.
{
"require-dev": {
"phpunit/phpunit": "^8.0",
"codedungeon/phpunit-result-printer": "^0.26.1"
}
}
phpunit.xml configuration
<phpunit
bootstrap="tests/bootstrap.php"
colors="true"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
>
</phpunit>
[MUSIC]
0:00
[SOUND] We don't always have the option
to build things perfectly the first time,
0:03
and we're always learning so
that perfect way is going to change.
0:08
A great thing about PHPUnit
is its wide adoption.
0:13
Almost every framework comes shipped with
PHPUnit or allows for easy integration.
0:18
You can also add PHPUnit
as a separate package for
0:24
any object oriented application.
0:28
Once PHPUnit is installed, you can start
adding tests a little bit at a time.
0:32
When you need to fix a bug,
0:38
unit testing can be used to demonstrate
that incorrect functionality.
0:40
And then keep that bug from being
reintroduced after it's fixed.
0:45
When adding a new feature, unit testing
can be used to verify the current
0:49
functionality before you add new features.
0:54
This provides confidence
that the new feature
0:58
doesn't break the existing functionality.
1:00
Let's add PHPUnit to an existing codebase.
1:03
I'm going to start with a very
basic object oriented project,
1:08
a simple cookbook.
1:12
There is a lot of functionality
that could be added.
1:13
But before that,
1:16
it would be helpful to make sure
that things are working as expected.
1:18
To start this process we're going to add
PHPUnit and the code dungeon printer.
1:21
We could add them one at
a time with Composer,
1:28
but I'm going to add them
directly to a Composer.json file.
1:30
Now I can run composer install.
1:38
Let's take a look at how this application
is put together, I'll open cookbook.php.
1:45
This is the main application file.
1:51
And we can see that it includes
our classes and a recipe file.
1:54
These classes are what we'll need for
our testing boot strap.
1:58
Let's copy those lines and
create a new test folder.
2:02
Within this folder, we can create
a bootstrap.php and we'll paste our lines.
2:08
We're going to be adding a phpunit.xml
file to our main directory and
2:16
calling our test from there.
2:22
So these paths will work,
2:24
even though the bootstrap file
is within the test's folder.
2:26
You could also update the paths to
be relative to this bootstrap file.
2:29
Let's add our phpunit.xml file.
2:43
We add PHPUnit and then we're going to
add our bootstrap, set this to tests.
2:49
Bootstrap.php, turn on our colors,
2:58
equals true and set printerClass equals
3:03
our Codedungeon PHPUnit,
3:09
PrettyResultPrinter\Printer.
3:17
Now we're ready to create our first test.
3:27
In the test directory,
we'll create a new file and
3:30
we'll name this RecipesTest.php.
3:34
We'll use PHPUnit\Framework\TestCase.
3:41
And then we'll add class
3:50
RecipesTest extends TestCase.
3:55
We're going to create our first test.
4:03
We'll set the test and
4:05
then we'll name this
canBeCreatedWithEmptyTitle,
4:09
very descriptive.
4:19
Then we'll set recipe equal to new Recipe.
4:22
We can then assert that the recipe
is an instance of the recipe class.
4:28
This assertInstanceOf Recipe.
4:34
Recipe.
4:43
We can also assert that
the title is an empty string.
4:45
This assertEquals an empty string and
4:49
recipe, getTitle.
4:56
Let's run our test.
5:00
Vendor/bin/phpunit and our test folder.
5:05
Helps if you spell it right.
5:18
That's also one reason it's good to
copy and paste from the documentation.
5:20
But now we can see that
we're running our tests.
5:24
As we start testing our code, it would be
helpful to know which code is tested and
5:27
which is not.
5:32
Let's set up code coverage.
5:34
You need to sign up for Treehouse in order to download course files.
Sign up