Chapters
37 Chapters
|
3:43:31
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^3.0", // v3.1.2
"doctrine/annotations": "^2.0", // 2.0.1
"doctrine/doctrine-bundle": "^2.8", // 2.8.3
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.14", // 2.14.1
"nelmio/cors-bundle": "^2.2", // 2.2.0
"nesbot/carbon": "^2.64", // 2.66.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
"phpstan/phpdoc-parser": "^1.15", // 1.16.1
"symfony/asset": "6.2.*", // v6.2.5
"symfony/console": "6.2.*", // v6.2.5
"symfony/dotenv": "6.2.*", // v6.2.5
"symfony/expression-language": "6.2.*", // v6.2.5
"symfony/flex": "^2", // v2.2.4
"symfony/framework-bundle": "6.2.*", // v6.2.5
"symfony/property-access": "6.2.*", // v6.2.5
"symfony/property-info": "6.2.*", // v6.2.5
"symfony/runtime": "6.2.*", // v6.2.5
"symfony/security-bundle": "6.2.*", // v6.2.6
"symfony/serializer": "6.2.*", // v6.2.5
"symfony/twig-bundle": "6.2.*", // v6.2.5
"symfony/ux-react": "^2.6", // v2.7.1
"symfony/ux-vue": "^2.7", // v2.7.1
"symfony/validator": "6.2.*", // v6.2.5
"symfony/webpack-encore-bundle": "^1.16", // v1.16.1
"symfony/yaml": "6.2.*" // v6.2.5
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
"mtdowling/jmespath.php": "^2.6", // 2.6.1
"phpunit/phpunit": "^9.5", // 9.6.3
"symfony/browser-kit": "6.2.*", // v6.2.5
"symfony/css-selector": "6.2.*", // v6.2.5
"symfony/debug-bundle": "6.2.*", // v6.2.5
"symfony/maker-bundle": "^1.48", // v1.48.0
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/phpunit-bridge": "^6.2", // v6.2.5
"symfony/stopwatch": "6.2.*", // v6.2.5
"symfony/web-profiler-bundle": "6.2.*", // v6.2.5
"zenstruck/browser": "^1.2", // v1.2.0
"zenstruck/foundry": "^1.26" // v1.28.0
}
}
24 Comments
Hello,
I tried to do the API Part 3 directly but on 2nd chapter I got an error with tests running I couldn't handled. So I went back on tutorials to this one. I started to do it and the first time I ran phpunit, Bang, same error again. Here it is :
Can't find real help on google. The talk about a directory "test" in config/packages but I don't have one.
Any tip ?
Kind Regard,
Eric
Hey @Eric-J
Sorry for my late reply. It seems your tests are not running in the "test" environment. I'd need to look at your test code. What happens if you boot the kernel manually at the beginning of the test?
self::bootKernel();Hi @MolloKhan
The same happens to me
I'm using docker with symfony 7.0
.env.test
If i used
symfony console doctrine:database:create --env=testcreate the databaseTest code:
The config framework.yaml:
Work for me :
Hey Billy,
Thanks for sharing your solution with others!
Cheers!
Hello,
I got the same issue and eventually fixed it by running
export APP_ENV=testbefore running phpunit.Hope this helps
Hey Stoakes,
Thanks for sharing your solution with others! Yeah, you can export it with
exportcommand, or fo simplicity you can just run it asAPP_ENV=test symfony php bin/phpunit, i.e. add that ENV in front of the command.Or it should be enough to put that env var on the
phpunit.xml.distconfig file to do not forget to export that next time.Cheers!
Hey @JuanLuisGarciaBorrego
It's hard to tell what's wrong here. It could be due to Docker or Symfony 7, but let's try out a few things:
1) Try extending from
WebTestCasein your test class2) Re-install your vendors
3) Downgrade to Symfony 6.4
If I throw a custom Api Platform exception, It should return 400. But it returns a symfony error 500.
---- github/api-platform/core/issues/3239
After some digging, I found that api-platform only handles the exception if it's a route managed by the framework, in my case I was throwing the exception from a custom controller.
So, try adding either a $request->attributes->set( '_api_respond', true); at the beginning of your method (after injecting the Request), or a defaults={"_api_respond": true} in your @Route annotation (or defaults: ['_api_respond' => true] in your #[Route] attribute)
hey @Jay
Thanks for digging, I hope it will be helpful, however does it relates to current chapter or maybe some other?
Cheers
I'm not sure. It's defiantly related to the test system.
I just hope @weaverryan will mention this some where in the course.
so it's already mentioned here in comments, but if you will find a chapter where we can apply it then it will be a better win, until it will be mentioned here in comments ;)
Cheers!
Just in case anyone else stumbles over this: I had to name my local test env file
env.test.local(instead ofenv.local.test).You're right - I totally messed that up! Fixed the script at least in https://github.com/SymfonyCasts/api-platform3/commit/02f7a9ab05f872aa1d741335c8918f1f693bc4ef
Thanks!
Is there any reason to use
zenstruck/browserinstead ofsymfony/panther?(I've never used both of them but my first thought would have been to choose Panther to stay in the Symfony ecosystem)
Yo @Seb33300!
Fair question! I just REALLY like the user experience from anything built by
zenstruck. The fluid browser interface, for me, is more friendly than the native Symfony way of doing things. Also, the lead dev behind thezenstruckstuff is on the Symfony core team - so you're not straying too far from the ecosystem ;).Cheers!
is there any setup to IDE quick start buttons? My project runs with symfony cli, and i am not realy able to setup phpstorm to prefix phpunit with sysmfony.
Hey @Peter-P
I'm afraid I don't fully understand your problem. If you need PHPUnit autocompletion, you can install the "PHPUnit enhancement" plugin, and you can even make PhpStorm to run your tests (setting it up depends on your local environment)
Cheers!
Hey @MolloKhan
thanks for the quick reply. My question is there a way to use ide build in run test feature ctrl+shift-f10 on windows? I can setup phpstorm to run tests, but as i use symfony cli as a web server, phpunit cannot find out the port for database connection.
i hope it clarify my question.
thanks
Yes, you can configure PhpStorm to run your tests just like that, but the config depends entirely on your local environment. In my case, I use WSL Ubuntu under the hood, so I had to configure PhpStorm to work with WSL by setting up a WSL interpreter. I recommend you to take a look at JetBrains docs, they have very good docs and cover almost all of the cases
Cheers!
Hey, I've experienced some strange behaviors with my Database Tests. Next to my
tests/FunctionalI've created some Integration tests intests/Integrationand created a newDatabaseTestCasethat sole purpose is touse ResetDatabaseand a convenience functiongetEntityManager(). MyApiTestCase(and so all myFunctionalTests) extends fromDatabaseTestCase.Now, all my API tests uses ZenstruckFoundry to set up my test case, it works without any problem. When I try the same with an Integration test to test a common Service (that only extends
DatabaseTestCase) that tests myBudgetCategoryService::createBudgetCategoriesForEvent(Event $event), a method that creates new entities, adds them to the$eventand also persists them (no cascade persist), I getBut the event was created with
$event = EventFactory::createOne([...])->object();When I persist everything manually beforehand, it looks like it works, but in the end, I get lots of duplicate keys.
It seems like there are 2 different entity managers, but I am not sure and don't know why
Hey @Sebastian-K!
Sorry fro the slow reply! And yikes, this indeed sounds like a mystery/mess. Because of how odd this is, my first instinct is to look for some sort of misconfiguration. Do you have
use Factoriesat the top of your test class (or base class)? I had forgotten that once (actually, maybe in this tutorial) and thing worked fine for along time... until they didn't. I caught and fixed that in the code before recording episode 3 actually.Anyway, let's start by checking that part.
Cheers!
Yeah, that solved my problem. Thanks a lot!!! I've now added
use Factoriesto myDatabaseTestCasebut I don't understand why my 10 Api Tests works without a problem, they also use Zenstruck FactoriesI wondered about that exact thing. The system is complicated - so a bit of a mystery :)
"Houston: no signs of life"
Start the conversation!