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!
27.
Testing a Form Submit
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
}
}
15 Comments
Hi, thanks for the tutorial!
What should I do if I need to mock a service used by the controller during a functional test? Doing the same as an integration test doesn't seem to work.
Thanks!
Ok, so.. my bad. I was trying to set null for the service and the "set" was ignored. Using a proper mock fixed the problem.
One other problem that I have is that I cannot get to make two requests in the same test to work, the second one looses the mock. Any hints? thanks a lot.
Hey Matias C.
Is it your custom service? if so you can create a real mock service and add it to
service_test.yamlso it can be wired to your controllerCheers!
Hi sadikoff!
I would rather not do it like that because I need to change the mock between tests. Any thoughts? Thanks!
hm can you share your tests part where your mock and where it fails? It will be easier to help with some real example =)
Cheers!
I think you should add $client->request('GET', '/'); again after you submit the form. Otherwise you'll get a redirect content page.
Hey GuitarSessions,
It depends on your needs, I see we use followRedirects() in this video because we do want to get the content of the page we redirected.
Cheers!
Hey Diego!
I'm coming back to see this tutorial as i'm currently testing my app. I'm trying to unit test all my forms. The simple forms are easy to unit test, but i'm not finding any correct way to test the forms with EntityType or CollectionType.
I've tested a lot of solutions for the EntityType, but from what i understand it looks like the EntityType depends too heavily on other Symfony process to be unit tested.
And i'm able to test the form with CollectionType without errors, but the tested data (i'm passing an ArrayCollection with correct data to the field that is CollectionType in my Form) are not retrieved by the "submit()" method of form created.
I searched a lot over the internet, but i didn't find a lot of responses about this. I'm not an expert with phpunit so i'm wondering if i'm doing something wrong or if it's not a good practice to unit test the form, and if it's better to just stick with integration or functional test.
If you could give some of your advices about that it would be really helpful :)
Thanks again for the good tutorials you're providing!
David
Hey David R.
I really love testing but I don't unit test my forms, I don't think it's needed but what I do is a functional test for them. The best way for me is creating a Behat feature file and add all the scenarios I want to test. If you are not familiar with Behat, we do have a course about it: https://symfonycasts.com/sc...
but if you don't feel like jumping on Behat right now, you can perform functional tests with PHPUnit and Symfony (just follow this link: https://symfony.com/doc/cur... )
I hope it helps you :)
Cheers!
Hey MolloKhan
Thanks a lot for your reply!
I'll stick with the classic functional tests for the form and after that i'll follow the Behat course.
Thanks again and keep up the great work on SC :)
David
You don't have to delete last char in class name, just press alt + ender and in popup, chose import class
enter*
Hey Dawid
Yeah, you are right, "alt + enter" is a shortcut for importing dependencies and more things (depending on where you pressed alt + enter)
Thanks for sharing it!
Have a nice day :)
BTW the method name is followRedirects not followsRedirect
Hey Thao L.
Nice catch! thanks for informing us. It's already fixed :)
Have a nice day
"Houston: no signs of life"
Start the conversation!