Chapters
48 Chapters
|
5:05:05
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
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!
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.
This tutorial is built with Symfony 4.3, but will work well on Symfony 4.4 or 5.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // v1.8.0
"doctrine/doctrine-bundle": "^1.6.10", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.6.3
"intervention/image": "^2.4", // 2.4.2
"league/flysystem-bundle": "^1.0", // 1.1.0
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.1
"sensio/framework-extra-bundle": "^5.3", // v5.3.1
"symfony/console": "4.3.*", // v4.3.2
"symfony/dotenv": "4.3.*", // v4.3.2
"symfony/flex": "^1.9", // v1.21.6
"symfony/framework-bundle": "4.3.*", // v4.3.2
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/property-access": "4.3.*", // v4.3.2
"symfony/property-info": "4.3.*", // v4.3.2
"symfony/serializer": "4.3.*", // v4.3.2
"symfony/validator": "4.3.*", // v4.3.2
"symfony/webpack-encore-bundle": "^1.5", // v1.6.2
"symfony/yaml": "4.3.*" // v4.3.2
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.7", // v1.0.7
"symfony/debug-bundle": "4.3.*", // v4.3.2
"symfony/maker-bundle": "^1.0", // v1.12.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/stopwatch": "4.3.*", // v4.3.2
"symfony/twig-bundle": "4.3.*", // v4.3.2
"symfony/var-dumper": "4.3.*", // v4.3.2
"symfony/web-profiler-bundle": "4.3.*" // v4.3.2
}
}
21 Comments
If you don't have .env.test. The first time you run "php bin/phpunit" it will be created. Remember to open that file and add all of you credentials and secrets, otherwise the program/app/website will not work.
Hey Dilyan,
Thank you for this tip. Though, I'm not sure it works the exact way you described. Most probably the ".env.test" is created for you when you requires PHPUnit bridge with Composer (comes from the recipe), i.e. when executing "composer require phpunit".
Cheers!
Hey Ryan,
Thank for this nice tutorial.
I have error when I launch the test : FAILURES! Tests: 1, Assertions: 1, Failures: 1.
Like Jérôme, I have a big message of HTML code into terminal. Access denied for user root. How can I change this default user ?
Hey Stéphane,
First of all, you need to know the correct DB credentials. If those credentials are just defaults, like not secret ones - set them on DATABASE_URL env var in ".env" or ".env.test" respectively and commit them out. If your credentials are secret and you don't want to commit them - create ".env.local" or ".env.test.local" and set the them on DATABASE_URL there.
Cheers!
Hi, when I try
dd($client->getResponses()->getContent()), I get an error: <blockquote>Doctrine\DBAL\Exception\DriverException:\nAn exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away\n</blockquote> I don't understand why as I'm just following the course and I didn't find a solution to fix the problem. Maybe I'm doing something wrong..? Can you help me please?
Hey Ajie62!
Hmm. that is a wild error! That usually happens if, for example, you have some long-running connection to the database... and the database eventually times out your connection. But in a functional test... that's very odd. Does it happen *every* time you run the test? And how long does it take for this error to show up - just a few seconds?
Cheers!
Hey, thanks for the answer. It happens every time I run the test and takes approximatively 3 seconds (more or less). I think it's weird because I wrote exactly what you wrote.
Hmm, yea, it's VERY odd. 3 seconds is not long enough for the database connection to "go away". Wait... let me think. Question:
1) Does
dd($client->getResponses()->getContent())actually cause the error? If you removed this line, do you not see the error?2) Do you have any stack trace that goes with that error.
Something is fishy... :)
Yes, the
ddpart causes the error. When I remove it, the problem disappears. There's a stack trace, but it's too big to show here. This stack trace includes the error message I mentioned earlier. Basically, it's a Symfony error page HTML that appears in the terminal.Hmm, probably you need to tweak a bit your MySql config. I found this by googling your error
> The MySQL server has gone away (error 2006) has two main causes and solutions: Server timed out and closed the connection. To fix, check that wait_timeout mysql variable in your my.cnf configuration file is large enough. ... set max_allowed_packet = 128M , then restart your MySQL server: sudo /etc/init.d/mysql restart.
Could you give it a try to those possible fixes and let us know if it worked?
Cheers!
Already 3am here, I tried a lot of things, I set max_allowed_packet, added wait_timeout in my.cnf.. The last ten lines of the MySQL config on my Macbook Air are:
I don't know what to do anymore.....
Hey Ajie62!
Can you take a screenshot of the error and stack trace in the terminal? Here's the part I'm not sure about yet: when you make a request, (A) does the error happen on that request... and then you print the error with dump() or (B) does the error NOT happen on the request, but when you dump the successful response, that causes something weird to happen and the database dies.
I think it's probably the first, but I don't know for sure. If it is the first, I'd be interested in finding out which code during the request caused the issue to happen. Another way to "sort of" check all of this would be to comment out the
$messageBus->dispatch()in the controller to see if that makes any difference.I hope you're not losing too much sleep over this ;). These issues are the worst!
Cheers!
Hey, so I made the screenshots, but I don't know how I can give them to you. Please let me know! Thanks!
Hey Jerome,
It's pretty easy! You can upload to any image hosting you want (like Imgur service for example) and then just put links to the images in a new comment.
Cheers!
Ok, thank you victor! Does this work: https://imgur.com/a/SrD8HJR There are 2 pictures. If you need more, just tell me.
Hey Ajie62!
Thank you! I'm pretty sure that the database is "going away" inside the request itself - e.g. the POST request to the
app_imagepost_createroute. So, let's do some debugging :). With weird things like this, I usually first try to eliminate as many lines of code as possible to see if we can identify one line that might be causing the problem. So, for example, in your controller, I would delete one line of code then try the test. If you get the same result, delete another line of code. Do this until the database stops "going away" or until you have a completely blank controller (well, you'll always need to return a response - so you could put a dummyreturn $this->json(['success' => true]);at the bottom.Let me know what you find out!
Cheers!
Ok, I just did what you suggested. I replaced the
returnwith the one you gave me, then deleted each line one by one. Finally, I think I know where the problem comes from... When I remove$entityManager->persist($imagePost);and$entityManager->flush();, the error disappears. When I put it back, the problem is here again. SO, there's a big chance it comes from the entityManager OR when I persist the $imagePost. But... Even though I know where the problem comes from, I don't know the origin... Why does it cause the request to fail?[EDIT] : After I dumped the
$entityManager, I found out something interesting:<blockquote>
-params: array:11 [
</blockquote>
The URL parameter is incorrect. So I just wanted to let you know that first of all, I created a .env.local file, and the original .env file does still have the default values. The URL param of the entity manager is equal to the default value in .env, not my .env.local, and I can't manage to change this URL... Any idea?
Hey Ajie62!
Brilliant debugging! I think I know the cause now... though I'm still *quite* confused about why you're exactly getting a "MySQL has gone away"... I would expect something more like "Unable to connect" in this situation.
What's going on? By chance, I just talked about it on the API Platform security tutorial a few days ago: https://symfonycasts.com/sc...
So, yes, you're exactly right: your .env is being read and your .env.local is being completely ignored. This is a little "feature" that I like and hate at the same time, and argued against originally when it was added (though it IS useful half the time).
Let me know if this makes the difference :).
Cheers!
I tried the solution you gave me, but no success. I added the env variable DATABASE_URL in .env.test, but it looks like it's not being overriden. BUT, I managed to make it work (no problem anymore) by using the (basic) .env, which I didn't really want to do. SO, when I try to use
.env.local,.env.testor.env.test.local, nothing works. When I use.env, it's okay... It's weird that I can't use another .env file...I had same problem and I found that phpunit does not reads the .env files, instead it looks phpunit.xml in symfony's root dir. First copy
phpunit.xml.dist to phpunit.xml. after that in section <php> .. </php> add these lines with yout values
`
`
That works for me, I am using symfony 4.4.7
Hey Ajie62!
Nice job debugging! And yea... that is very weird :/. For reference, here is the line that skips the .env.local file in the test environment: https://github.com/symfony/symfony/blob/6dd9d24e5062544aac1367ee0de2801e860116ff/src/Symfony/Component/Dotenv/Dotenv.php#L94-L97 (or rather, this is the section that LOADS .env.local in all environments except for the
$testEnvs. And here are the lines that should ALWAYS load.env.testand.env.test.localhttps://github.com/symfony/symfony/blob/6dd9d24e5062544aac1367ee0de2801e860116ff/src/Symfony/Component/Dotenv/Dotenv.php#L103-L109 - AND, values from later files override values in earlier files.Anyways, if you want to nerd out and do some debugging to figure out what was going wrong there, awesome. If not, then I'm glad we at least got it working :D.
Cheers!
"Houston: no signs of life"
Start the conversation!