13.
Using a Test Database
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
22 Comments
Hello,
I have a weird error when adding the test database.
When creating a programmer in the ProgrammerControllerTest using the dev env., the programmer is created in test db not the base db, why ?
Hey mehdi
That's because the tests have a special setup, when you call
self::bootKernel()on any test method, the kernel by default will choose "test" environment, you can change it by passing as first argument an array with the key "APP_ENV", or by setting up an environment variableCheers!
How do you create an `app_test.php` in Symfony 4 for testing purposes?
Hey Vladimir Z.
In Symfony4 you only have one "app.php" file, and in reality, it's not called "app.php" anymore, instead it's called "index.php". So, you don't have to create another file for your testing environment, you only have to set the
APP_ENVglobal variable to "test". So, if you are using PHPUnit, you will have to declare that variable in your phpunit.xml:Cheers!
MolloKhan if my PHPUnit test cases use Guzzle to issue requests to API endpoints, will it automatically recognize these Guzzle requests as coming from the PHPUnit tests?
Good question. I'm not totally sure about it, you can give it a try. If it doesn't work let me know, and probably you may want to use the "BrowserKit" from Symfony meanwhile we find a proper solution to this situation
https://symfony.com/doc/cur...
Cheers!
MolloKhan I see that the Symfony Client has an 'enviroment' parameter that can be used (haven't tested it yet). I was wondering whether Guzzle has something like that. MolloKhan do you have an idea here?
Actually, there is nothing wrong using Symfony's client instead of Guzzle in your tests, it's easier to use and it does not require any setup
Another Symfony 4 question!
In the documentation it says to add the following to phpunit.xml.dist:
<phpunit>
<php>
<env name="DATABASE_URL" value="mysql://USERNAME:PASSWORD@127.0.0.1/DB_NAME"/>
</php>
</phpunit>
However when I try to run
bin/console doctrine:database:create --env=test
I get an error saying that the database already exists - but it seems to be trying to recreate the dev database with settings from .env
Any idea what I am doing wrong here?
Hey Shaun T.
That config only works when working on PHPUnit, you need to specify your test database in your test doctrine.yaml file.
Cheers!
Thanks MolloKhan :)
Shaun T. I have come across a problem related to this.
In my TokenControllerTest I can create a user in the test database, but in TokenController findOneBy is querying the Development database!
Do you know how I can resolve this please?
Hmm, how are you executing that controller's action? You have to assure that your kernel is booting on test environment, like "PHPUnit KernelCase class" does when you run
self::bootKernel();Thanks MolloKhan, you were right, it was the Kernel :)
Hey guys, I'm using Symfony 4, could you give me some guidance on how I can setup phpunit to use a test Database using this version?
Hey Shaun,
Yes, we can! Actually, there's Symfony Demo project that's on Symfony 4 now and has PHPUnit configured, you can look at the code here: https://github.com/symfony/...
Cheers!
Thanks victor :)
I have had a look at the Symfony demo and it seems to have it's own client rather than using Guzzle.
How can the setUpBeforeClass() method be modified so that it uses the Symfony client rather than Guzzle?
public static function setUpBeforeClass()
{
self::$staticClient = new Client([
'base_url' => 'http://localhost:8000',
'defaults' => [
'exceptions' => false
]
]);
self::$history = new History();
self::$staticClient->getEmitter()
->attach(self::$history);
self::bootKernel();
}
Hey Shaun,
If you want to use Symfony's client instead of Guzzle, just do as Symfony Demo does, it uses exactly Symfony client :) i.e. you just need to extend WebTestCase and then with static::createClient(); you will get the client.
Cheers!
Setting the 'TEST_BASE_URL' value in phpunit.xml.dist doesn't seem to be working. getenv('TEST_BASE_URL') returns an empty response.
Hey I just had a similar problem although the response was not empty Guzzle was kind of ignoring the "/app_test.php" part of the base url. The problem was that in my setup (Symfony 3, Guzzle ^6.2) Guzzle overrides the path if the request uses an absolute path like "/api/...". Using "api/..." instead solved the problem for me.
Since it worked in the video I suppose this might be an issue with the later versions.
You're absolutely correct about the absolute paths (and I *do* think this was something that was added in some more recent versions of Guzzle). To get around it, we use a middleware that adds the app_test.php even when the URL starts with a slash. It's an annoying little thing to need to add, but it works pretty well. Here's the Guzzle 6 version for those who are curious: https://gist.github.com/wea...
Cheers!
This just saved my day. :D Thank You
"Houston: no signs of life"
Start the conversation!