Chapters
29 Chapters
|
2:54:09
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.0, <7.4
Subscribe to download the code!Compatible PHP versions: ^7.0, <7.4
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
10.
Testing Exceptions
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
While the fundamentals of PHPUnit haven't changed, this tutorial *is* built on an older version of Symfony and PHPUnit.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.0, <7.4",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^1.6", // 1.10.3
"doctrine/orm": "^2.5", // v2.7.2
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"sensio/distribution-bundle": "^5.0.19", // v5.0.21
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.28
"symfony/monolog-bundle": "^3.1.0", // v3.1.2
"symfony/polyfill-apcu": "^1.0", // v1.6.0
"symfony/swiftmailer-bundle": "^2.3.10", // v2.6.7
"symfony/symfony": "3.3.*", // v3.3.13
"twig/twig": "^1.0||^2.0" // v2.4.4
},
"require-dev": {
"doctrine/data-fixtures": "^1.3", // 1.3.3
"doctrine/doctrine-fixtures-bundle": "^2.3", // v2.4.1
"liip/functional-test-bundle": "^1.8", // 1.8.0
"phpunit/phpunit": "^6.3", // 6.5.2
"sensio/generator-bundle": "^3.0", // v3.1.6
"symfony/phpunit-bridge": "^3.0" // v3.4.30
}
}
8 Comments
dinosaurs is an array of dinosaur, first() does what?
is it checking only the first dinosaur in the array?
return count($this->dinosaurs) === 0 || $this->dinosaurs->first()->isCarnivorous() === $dinosaur->isCarnivorous();
Hey Shubham,
The "dinosaurs" property is a Doctrine's ArrayCollection... but yeah, you can think about it like about an array on steroids, and the first() method returns the first value of that array or null if the array is empty, that's why we check if the array is NOT null first with "count($this->dinosaurs) === 0" check, and then we're sure that the first() will return a Dinosaur entity on which we can call that isCarnivorous() method and do not worry it will fail with "Method was called on null" error :)
Cheers!
Hi friends! For those who are using PHPUnit 8 the
@expectedExceptionannotation is deprecated.`
PHPUnit 8.0.4 by Sebastian Bergmann and contributors.
.......W.IS........ 19 / 19 (100%)
Time: 77 ms, Memory: 4.00 MB
There was 1 warning:
1) Tests\AppBundle\Entity\EnclosureTest::testItDoesNotAllowToAddNonCarnivorousDinosaursToCarnivorousEnclosure
The @expectedException, @expectedExceptionCode, @expectedExceptionMessage, and @expectedExceptionMessageRegExp annotations are deprecated. They will be removed in PHPUnit 9. Refactor your test to use expectException(), expectExceptionCode(), expectExceptionMessage(), or expectExceptionMessageRegExp() instead.
WARNINGS!
Tests: 19, Assertions: 31, Warnings: 1, Skipped: 1, Incomplete: 1.
`
Bye!
Hey Marcel D.!
Thanks for posting! You're totally right - you should now use the pure PHP way of expecting exceptions (the first we show) - but not the annotations :).
Cheers!
Hi there,
Then you should delete the section which tells us about annotations to avoid to be followed by us.
Cheers!
Hey AbelardoLG,
Unfortunately, we cannot delete anything in released tutorials, but we will add a note to warn about it. Thank you!
Cheers!
Hallo ,
why we have to make sure adding $this->expectException(NotBuffetException::class); before our last code
$Enclosure->addDinosaur(new Dinosaur('Carn Dinasour',true));
thnx ,
Hey Boran Alsaleh
Because when you add an expected exception like that, PHPUnit will wrap within a try-catch block your test method, so if it throws, it will check if the thrown exception matches to the one you specified, otherwise, the execution would explode (because you know, the code throwed an exception and nobody catched it)
I hope it helps you understanding what's going on here :)
Cheers!
"Houston: no signs of life"
Start the conversation!