Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Getting Started with PHP Unit Testing!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Fixtures allow you to setup all the code and data you'll need to run your test and share that set up across multiple tests.
Documentation for Fixtures
tests/FullRecipeTest.php
<?php
use the PHPUnit\Framework\TestCase;
class FullRecipeTest extends TestCase
{
protected $recipe;
protected function setUp(): void
{
$this->recipe = new Recipe("Belgian Waffles");
$this->recipe->addIngredient("Egg", 2);
$this->recipe->addIngredient("Butter", 1, "Cup");
$this->recipe->addIngredient("sugar", .5, "Cup");
$this->recipe->addIngredient("milk", 1.5, "cup");
$this->recipe->addIngredient("vanilla", 2, "tsp");
$this->recipe->addIngredient("flour", 2, "cup");
$this->recipe->addIngredient("baking powder", 1, "tbsp");
$this->recipe->addInstruction("Separate eggs. Whip egg whites until stiff peaks form. Set asside.");
$this->recipe->addInstruction("Melt butter, and combine with sugar. Add egg yokes and mix well.");
$this->recipe->addInstruction("Add milk and vanilla and mix well.");
$this->recipe->addInstruction("Add flour and sugar until just combined. ");
$this->recipe->addInstruction("Fold in egg whites.");
$this->recipe->addInstruction("Follow instructions on waffle maker or add .5 cup of batter to waffle iron and cook for 4 minutes.");
$this->recipe->setYield("10 waffles");
$this->recipe->setSource("Alena Holligan");
$this->recipe->addTag("breakfast, quick bread");
}
/** @test */
function hasTitle()
{
$this->assertEquals(
"Belgian Waffles",
$this->recipe->getTitle()
);
}
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I want to be able to run tests
on a fully implemented recipe.
0:00
I could setup a full recipe for
each test, but there is an easier way.
0:05
Fixtures allow you to
setup all the code and
0:10
the data that you'll need for your tests
and show that across multiple tests.
0:13
We can use the setUp.
0:18
Much like a constructor,
our setUp function will allow us to add
0:22
code that needs to happen
when a test is run.
0:26
You'll notice the return type
of void because test case is
0:30
an abstract class that defines
the framework for this function.
0:34
We're required to use
this same return type.
0:39
You can see details by visiting
the repository on GitHub,
0:43
or viewing the downloaded code,
in abstract class and setup.
0:48
Let's go back to our editor.
0:57
We'll create a new test file for
a test that will use a full recipe.
1:00
We'll call this FullRecipeTest.
1:09
We'll use PHPUnit\Framework\TestCase and
1:18
then we'll add a class,
1:23
named FullRecipeTest
that extends TestCase.
1:26
Now we can add a protected recipe and
1:37
a protected function of setUp
with a return type of void.
1:42
I'm going to copy and paste a full recipe.
1:53
You can get this recipe in the notes
associated with this video.
1:56
Now we can use that setUp to write a test.
2:00
We'll use function hasTitle[).
2:05
Now we can use
$this->asserEquals("Belgian Waffles",
2:11
$this->recipe->getTitle()).
2:21
Let's run our test again.
2:27
Now we have a little more test coverage,
great.
2:32
Let's go back and
add a few more tests for this recipe.
2:35
Test, function and
2:41
we'll check if it hasAlIIngredients.
2:43
And this time we're going
to this assertCount.
2:53
We're expecting 7 ingredients when
2:57
we call $this->recipe->getIngredients.
3:02
We'll add another for hasAllInstructions.
3:17
$this->assertCount and
this time, we'll look for
3:25
6, $this->recipe->getInstructions.
3:31
We'll add another test.
3:39
HasYield, $this->assertEquals("10
waffles",
3:42
$this->recipe->getYield()).
3:51
And another test, hasSource, and we'll
4:01
check $this->assertEquals("Alena
4:08
Holligan", $this->recipe->getSource())
4:15
And one more we're going to test, hasTags.
4:29
We'll assertCount(2,
4:40
$this->recipe->getTags()), we
4:44
can also add $this->assertEquals,
4:50
and we'll add an array for
4:56
breakfast and quick bread.
5:00
And we'll check $this->recipe->getTags.
5:07
Now let's try running our tests again.
5:16
Great, we have 75% code coverage.
5:20
Let's take a look in the browser.
5:24
Here, we can scroll down and
see which methods are not tested yet,
5:26
_toString, addIngredient.
5:31
If we go into addIngredient,
5:36
we see that the two lines that are not
tested are throwing an exception.
5:38
Let's go write some tests for
checking exceptions.
5:44
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up