Chapters
23 Chapters
|
1:59:49
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.4.0, <7.3.0
Subscribe to download the code!Compatible PHP versions: >=5.4.0, <7.3.0
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
16.
When *I* do Something: Handling the Current User
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 uses a very old version of Symfony. The fundamentals of Behat are still valid, but integration with Symfony will be different.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.4.0, <7.3.0",
"symfony/symfony": "^2.7", // v2.7.4
"twig/twig": "^1.22", // v1.22.1
"sensio/framework-extra-bundle": "^3.0", // v3.0.16
"doctrine/doctrine-bundle": "^1.5", // v1.5.1
"doctrine/orm": "^2.5", // v2.5.1
"doctrine/doctrine-fixtures-bundle": "^2.2", // v2.2.1
"behat/symfony2-extension": "^2.0" // v2.0.0
},
"require-dev": {
"behat/mink-extension": "^2.0", // v2.0.1
"behat/mink-goutte-driver": "^1.1", // v1.1.0
"behat/mink-selenium2-driver": "^1.2", // v1.2.0
"phpunit/phpunit": "^4.8" // 4.8.18
}
}
14 Comments
there Thank you Ryan for another outstanding post.
I have noticed that saving the authenticated user in `$currentUser` may become an issue if the test involves clicking on links.
The currentUser would become unmanaged after requesting another page as the kernel is being shut down every time and the entity managers are clear()ed... This is since the following commit in Doctrine Bundle.
https://github.com/doctrine...
This issue is being discussed here:
https://github.com/doctrine...
Not sure if you have a current way of getting around this, but just thought some people might run into the same issue.
Thank you for your outstanding posts!
Hey Anthony R.!
Hmmm, interesting. What driver are you using? Goutte? Selenium? Or the Symfony driver? My first thought is that, unless you're using the Symfony driver, this should not happen. The reason is that, if you use something like Goutte or Selenium that makes real HTTP requests, the kernel inside your *Context files is booted, and remains booted throughout the entire test suite. So the shutdown should never happen. But if you're using the Symfony driver, then yea, it will shutdown and reboot the kernel between each request - you would indeed likely be in this boat!
I do have one idea to get around it: store the
$currentUserIdinstead. Then, add agetCurrentUser()method that always uses that value to query for a fresh object from the EntityManager. That should guarantee it's always fresh :).Cheers!
Ryan! This is exactly right, I was using Symfony driver and I have now switched to Goutte driver - thank you! You are a legend! Greetings to you and the team from Australia.
Yo Anthony R.!
Woohoo! Greetings back to Australia from us (Michigan USA, Mexico and Ukraine) :D
Cheers!
Hey guys! I've been following along with tests against my current project, and I've run into a snag. When I manipulate the database in my FeatureContext.php file, I am affecting the database defined in my config_test.yml. But when I execute the tests, I'm hitting the database defined in config.yml (there is no database definition in config_dev.yml). I assume that's because behat is running in the test environment and the tests are hitting my built in server that's running in the dev environment.
I'd like to test against a database that is emptied and configured specifically for each test (as you recommend), but I'd like to do that without blowing out my dev database. Is there a way to do that, or is that simply a bad idea?
Thanks!
Hey Dan_M
Working in isolation with a dedicated Database is the *way* to do your testing. I believe you have something misconfigured.
Are you using the Symfony2 extension?
Can you show me your behat.yml file?
Cheers!
Yes, I am using the Symfony2 extension.
My behat.yml is pretty basic:
For what it's worth, I'm using the following:
behat 3.4.1
symfony 3.3.10
Aha! I get it. I installed behat/mink and behat/mink-browserkit-driver and updated my behat.yml to the following:
Now behat runs against the server in the test environment without my having to start the server.
Thanks!
Hmmm...with this behat.yml, my @javascript annotations still want to run on the built-in server (dev environment) instead of using the symfony2 extension server in the test mode. What am I missing?
Yo Dan_M!
Ok, let's talk about a few possibilities :). First, using the symfony2 "session" is fine - this is how you solved that pesky "different databases" problem - nice work! A different solution to that problem (and one you will probably need to do) is to add a new front controller - e.g.
app_test.phpthat is identical toapp_dev.phpexcept that it boots the AppKernel() in the test environment. Then, you should be able to update your base_url tohttp://localhost:8000/app_test.php.Next issue: why does adding
@javascriptnot cause the selenium session to be used? I'm not sure... but try changing the annotation to@mink:selenium2- the @javascript annotation is "sort of" short for this. We can see if that makes any difference. I've actually not run into this issue yet... so it must be something minor...Cheers!
I set up app_test.php, started the selenium driver, started the server with "console --env=test server:run", then ran behat against a scenario with @javascript, and everything went according to plan.
When I use either @javascript or @mink:selenium2 without first staring the server, the browser opens but I get a site failed to connect error. (I'm on Windows 10, by the way.)
All in all, that's OK. I have a way to start the server in test mode and run javascript tests against it.
Thanks!
Hey Dan_M
I'm glad we could figure it out!
One more thing, I think you don't have to start your server in test mode in order to run your tests, try starting it as normal and try again. If I'm wrong, let me know :)
Have a nice day
Hey MolloKhan
You're right, of course. When the selenium driver runs, it goes to the url given as base_url in the config. Because that's http://localhost:8000/app_test.php, it hits the test database.
Thanks!
Besides Ryan's comment, don't forget that you need to have Selenium server running whenever you want to use @javascript annotation
"Houston: no signs of life"
Start the conversation!