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!
21.
Clearing the Database
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
}
}
12 Comments
Somehow for me the url key was not working, as I got the following errors:
[Doctrine\ORM\Tools\ToolsException]
Schema-Tool failed with Error 'An exception occurred in driver: SQLSTATE[HY000] [14] unable to open database file' while executing DDL: CREATE TABLE dinosaurs (id INTEGER NOT NULL, encl
osure_id INTEGER DEFAULT NULL, length INTEGER NOT NULL, genus VARCHAR(255) NOT NULL, is_carnivorous BOOLEAN NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_B1DE6E91D04FE1E5 FOREIGN KEY (enclos
ure_id) REFERENCES enclosure (id) NOT DEFERRABLE INITIALLY IMMEDIATE)
[Doctrine\DBAL\Exception\ConnectionException]
An exception occurred in driver: SQLSTATE[HY000] [14] unable to open database file
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [14] unable to open database file
[PDOException]
SQLSTATE[HY000] [14] unable to open database file
In the end I resolved it by replacing the url key by path, and removing the sqlite:// part. As in:
That did the trick for me.
Hey Ben,
Hm, I don't see any issues, it should work, actually, Symfony demo uses the same path: https://github.com/symfony/demo/blob/e45b5d59f8b253531e66b0ec8ac91dbd5559934b/.env.dist#L15 . Well, only one tip for you in case you're on Symfony 4 and use environment variables, you should not forget about resolve filter to handle the "kernel.project_dir" parameter, i.e. do this:
Anyway, I'm glad you found a working solution. And thanks for sharing it with others!
Cheers!
I use Symfony 5 with php 8.1 and having problems to use the ORMPurger.
in the moment i call $puger->purge() i got a null reference error.
has there anything changed what is is not listed?
<blockquote>Error : Call to a member function getMetadataFactory() on null</blockquote>
Hey Maik T.!
Hmm. Are you passing the entity manager into ORMPurger? If that's not passed, then it would cause this exact error.
Cheers!
What changes do I have to make incase I'm using PostGRE SQL.
Hey Akshit,
If you're using this project for learning purposes and data lose is not a problem for you - the easiest way would be to create a DB and schema with next commands:
$ bin/console doctrine:database:create
$ bin/console doctrine:schema:create
$ bin/console doctrine:fixtures:load
And that's it, you should have the valid DB schema now, you can check it with:
$ bin/console doctrine:schema:validate
If all green - great, you're ready to go :)
For production - it's more complex, you would need to manually create the migration for any changes you made in the schema, you can leverage the "bin/console make:migration" command.
I hope this helps!
Cheers!
What do you say to the argument that one doesn't need to test against the database, since it such tests you're basically just testing that the entity manager works (which you can safely assume without needing to test it). Instead, you should mock the EM and write unit tests. It's not a position I'm advocating, but I can't think of a good response to it.
Hey there!
That's a good question, there are times when you need to test the interaction with the database, for example when you're writting a custom repository method, or, when you're writing an integration test. In general, if you can mockout a service, you should, you want to narrow down your tests as much as possible and make them to run fast as much as possible too. You may find this article interesting https://martinfowler.com/ar....
Cheers!
If you get "[Doctrine\DBAL\Exception\DriverException] An exception occurred in driver: could not find driver", simply uncomment extension=pdo_sqlite in php.ini in your php installation folder (by deleting ";").
I have a question about testing APIs. Say I have a Symfony 5 app, which serves a number of endpoints. I want to test that when I hit endpoint `cats/x/fleas` I get a list of fleas for cat number x. So in my test, i:
1. Create a cat, persist it to the db, and get its ID (e.g. 5).
2. Use HTTPClient to hit `https://localhost/cats/5/fleas`.
My problem is, in step 1 I am using my test database, i.e. in my `test` environment. When I hit the endpoint with my HTTPClient, I am hitting the `dev` environment (which uses a different, `dev`, database). So it can't find the cat I created in the test database. One way to solve this is to set my local dev environment to `test`. But that's not ideal. Any ideas how to get around this? Thanks!
I've found a solution. Do not use the symfony HTTP Client package, instead extend `WebTestCase` in your test class, and use its built in client. When you create the client you can define which environment you want it to use when making requests, so I create it and ask it to use the `test` environment.
Hey @gawandulp!
Nice work figuring all of this out! Yes, the WebTestCase client is just a nicer way to do it - and you can even get the "profiler" for that request if you want to, or assert that an email was sent - cool stuff like that :).
Cheers!
"Houston: no signs of life"
Start the conversation!