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!
17.
Full Mock Example: the Sequel
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
}
}
7 Comments
There are couple of problems:
First: you don't set to which enclosure Dinosaur belongs:
add
setEnclosure($enclosure)to Dinosaur EntitySecond: there may be thrown NotABuffetException later with integration tests because you draw Dinosaur diet in EnclosureBuilderService and different species may be in same enclosure. I solved it like this
`
private function addDinosaurs(int $numberOfDinosaurs, Enclosure $enclosure): void
{
}
`
Hey manhee
Thanks for your feedback, You are actually right on the second case, the test may fail because the implementation is wrong, it randomly selects which diet the dinosaur will have. I'll talk about this problem internally and come up with a solution
About first case. I don't think it's needed to set the inverse side of the relationship but either is wrong to set it up. So, it's up to your use case.
Cheers!
Yep, nice catch on the randomness flaw! As Diego mentioned, the setEnclosure() call (which is actually the owning side - Diego has that part slightly off) is called already when we set the inverses side - the addDinosaur() method has that logic internally - it’s just a nice thing the make:entity command gives you. But let me know if I’m missing a detail!
Cheers!
I don't understand 2 things:
- why is the exception not thrown? I tried with 1000 dinosaurs, it always pass. Although if I'm right, the enclosure and dinosaurs are a real objects, so the exception should be thrown, no?
- how could we rewrite the test so that it fail because of this randomness error (I mean, how could the test fail before we add the correction manhee?
Hey Francois,
The test should fail at some point, if you check the implementation of
EnclosureBuilderService::addDinosaurs()it's possible to add dinosaurs with different diets and trigger the exception.I don't think that's a good idea because you're working with random data, unless you install a library to manipulate the built-in
array_rand()function and mock it's return value, then your test won't be reliable. The solution is to fix the production code and leave the test as it isCheers!
Hey Diego, thanks a lot
No really, it doesn't fail. Did you try?
And, about having the test testing this specific potential error, I think it could be tested independantly and we have a hole in the coverage. But:
Unfortunately it's not important for me anymore, sorry, I'm on something else now, so I won't be able to come back on this deep enough..
thanks!
Hey, to be honest I didn't run the code but by analyzing it you can see the diet it's chosen randomly every time you call
addDinosaur(), so, if you call that method many times it should fail eventually. Anyways, that's a flaw in the implementation and my recomendation is, instead of adding a test for it, just fix the bug and keep the current testCheers!
"Houston: no signs of life"
Start the conversation!