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!
23.
Functional Tests
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
}
}
23 Comments
Hello guys,
in case you are using Symfony 3.3 and PHPUnit 7 or later (the version that will be installed if you follow this tutorial at this moment), you may end in a limbo of compatibility with liip. Make sure you are using the same version of PHPUnit that this tutorial is using, the version 6.3. Then you won't have any problem installing liip.
Cheers
Hey fahani ! Thanks again for reporting this issue, we've updated the script and added a note to the video. You rock! 👏
Hey fahani!
Thanks for the pointer on this! We'll look into adding a note so other people don't hit this :).
Cheers!
It seems there's an error with test-created "fixtures"/objects (like $user) when I run $this->manager-remove($user); but only when I run the following twice (prior to $this->manager->remove() being called in tearDown() - where the acctual error is triggered):
the error follows (once remove() is called):
<blockquote>1) App\Tests\Service\SecurityTest::verifyAccountTwiceError
Doctrine\ORM\ORMInvalidArgumentException: Detached entity 790 cannot be removed
</blockquote>
It's fine if I don't try to run request() a second time, but when I try $this->manager->remove($user) it throws this error. Very strange!
My research led me to this related SO question:
https://stackoverflow.com/questions/38073802/symfony-detached-entity-cannot-be-removed-in-test-suite-w-phpunit
The manager is retrieved like this (in setUp()):
Otherwise, I'm starting to like creating tests, they are the first tests I've built for a project, but there's definnitely some challenges.
If anyone has some ideas, I would love to know what's going on.
Hey Cameron,
You definitely should not call remove() method twice for the same object. If the object was already removed - Doctrine will throw an error saying that it cannot delete it (that makes sense, it was deleted before). So, just make sure you call that remove() only once for each entity you want to remove in the system.
> I'm starting to like creating tests, they are the first tests I've built for a project, but there's definnitely some challenges.
Congrats with your first steps! Writing tests will definitely worth it later when you will need to upgrade, add new or change existent features :)
Cheers!
Hi Victor, thanks for the response.
Remove is not being called twice, it's: $this->client->request(...) that is being called twice - but it's the $manager->remove() that triggers the error. Calling $this->client->request(...) twice is the only time I get the error, when I use it once per method callin remove() (later on) does not trigger this problem).
Is there some type of issue with making 2 "http" calls in one test method? Or maybe there's something I've overlooked?
Hey Cameron,
Hm, could you show the code, please? Well, sometimes you have to run $manager->refresh($entity) if you did any modifications to the object and want to have a fresh data in it. Try to add that refresh($entity) call before the logic that checks if the entity should be deleted or no, this should help I think.
I hope this helps!
Cheers!
Hi there,
I'm struggling with the installation of the liip/functional-test-bundle, composer doesn't seems to want to install anymore this package on the symfony version you deliver with this course; I just downloaded your course, made a composer install and followed the 22 previous chapter religiously, without installing anything fancy.
Tell me if this works well for you, but maybe a fresh version of the code on symfony 4 or 5 would be fast to do for you, and changes barely anything in the course :)
Because overall, "testing with a bite" is excellent : the subject is one of the most important in coding, the dino-project is totally fun, the chapters are perfectly designed, the girl voice-acting here is lovely, everything is fine, so I would love to continue over chapter 23 !
Hey Julien R.!
Nice to chat with you and sorry for the troubles! Let's see if we can work it out :).
Hmm, yes, I think I see the issue. The latest version of the bundle doesn't support the Symfony version I'm using. You can fix that (for the purpose of this tutorial) by forcing an older version. We actually have a note about this in the tutorial - have you tried this command?
Let me know :). If you DID and you got an error, then I definitely want to know about it!
❤️ I'll pass that feedback to the team. And sorry again for the issue :).
Cheers!
Hello Ryan !
Thank you for taking time chatting with me :)
OK I spend an hour making tests in every possible ways.
To be sure, I Tried with my actual following-the-tutorial-folder, and a brand new install of your code-phpunit.zip --> start folder, and with various PHP versions.
First of all, on the fresh install, I noticed that the PHP version matters.
A composer install with some versions of PHP :
7.2.31 OK
7.3.19 OK
7.4.7 NOK
`D:\Developpement\dinopark_fresh
λ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Your requirements could not be resolved to an installable set of packages.
Problem 1
As I don't want to test the liip bundle with every PHP versions, so I will continue with PHP 7.3.19 and my composer version is 1.8.6.
Then we can factorise again, as my tests with a fresh install from start folder AND my tests from my personal chapter 22 folder make the same results. The good new in this is that you will be able to reproduce my bugs very easily.
composer require --dev liip/functional-test-bundle:^1.9, it never works :`D:\Developpement\dinopark
λ composer require --dev liip/functional-test-bundle:^1.9
[InvalidArgumentException]
Could not find package liip/functional-test-bundle in a version matching 1.9`
`
D:\Developpement\Dinopark (master)
λ composer require --dev liip/functional-test-bundle
Using version ^4.2 for liip/functional-test-bundle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
Installation failed, reverting ./composer.json to its original content.
`
This is a shortened version of the bug, as the real one is very long.
So if I don't fail any important step, we have :
Let me know if you need screenshots for the error messages, but I think it's now really easy for you to test this out ;)
<blockquote>❤️ I'll pass that feedback to the team. And sorry again for the issue :).</blockquote>
No problem, in fact my message was more about "Keep this course for the future of symfonyCasts because it is so cool" more than "there is a bug in this tutorial" you know :)
I would add also that since symfony 3, PHPUnit and PHPUnit bridge evolved, and the way to test your app changed a bit (for example, I think now it's something like php bin/phpunit ? Or this is the old version...), so a lot of reason to refresh your awesome dinopark !
Cheers !
Hey Julien R.!
Sorry for the slow reply and thanks for the detailed reply and taking so much time to test thing - I really appreciate that :).
Let's unpack some things :)
1) About PHP 7.4 not working
This is a known issue - it's due to some dependencies not being compatible with PHP 7.4 and upgrading them would require major version upgrades. To help with this, we recently launched a feature where we advertise this fact when you download the course code:
<img src="https://i.imgur.com/A1vDSgy.png">
But perhaps this isn't obvious enough? Or is hard to understand? Perhaps it should exactly say "This tutorial does not work in PHP 7.4 and higher"? Let me know - this is a relatively new feature, and this was our first attempt at sharing this information. And yes, ideally the tutorial would work with the latest version of PHP, and we upgrade dependencies to make this happen whenever that's feasible. But when it's not, we want to make it obvious. The fact that the tutorial doesn't work with PHP 7.4 definitely motivates us to upgrade it ;).
2) About
composer require --dev liip/functional-test-bundle:^1.9not workingHmm. What if you surround the argument with quotes?
I'm asking because I just tried this command on the "start" code and it DID work. I'm wondering if your shell is treating "^" as a special character, and so it's looking exactly for a version called 1.9 (when I run the command, it actually installs 1.10.0). Let me know!
I really appreciate this! It's something we work on and think about a lot and we're hoping to add some additional testing to our tutorials this year to know when there are php version problems of library install problems.
💯:)
Cheers!
Update : some how I managed to do the login with :
$form = $crawler->filter('html > body > div > form')->form();
The problem now is I don't know how to log out, the session stays active between two tests.
Hi everybody, I had a hard time making a basic functional test work, I need to login in the first place. I use symfony 4.4, I find the different tutorials on internet confusing, many ways to do thing..., here is my code (symfony CssSelector is installed):
` public function testLoginToApp()
I just want to login and test if I can access certain routes.
However the nodelist is empty, it cannot find the button# to post data. What did I do wrong?
Hey Benoit L.
The only difference between your first attempt to submit the form and the later one is that you forgot to call
->form()after filtering the response e.g.$crawler->filter('#loginForm')->form();by the way, check out this post https://symfony.com/blog/new-in-symfony-5-1-simpler-login-in-tests
for your subsequent tests it would be better to just "simulate" the login and perform the action under test
Cheers!
Hi thank you for your tip, unfortunately I am not in symfony 5 and the app is already in production, I wouldn't date making that change. I will visit the logout page prior to do anything else, or empty a cache. I think it would be interesting to put that in tearDown function
I think I understood why I couldn't get the text of login page, because with the static::createClient() with the credential , you are already logged in, no need to post credential via submit !
Cool! I'm glad to hear you could find what's going on here. Thanks for sharing it.
Cheers!
I've been trying to install liip's bundle for more than an hour now. I'm giving up this course for tonight but can anyone please provide a version number that should install?
`
composer/ca-bundle 1.2.7 Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.
doctrine/annotations 1.10.1 Docblock Annotations Parser
doctrine/cache 1.10.0 PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and ...
doctrine/collections 1.6.4 PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common 2.12.0 PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflec...
doctrine/data-fixtures 1.4.2 Data Fixtures for all Doctrine Object Managers
doctrine/dbal v2.10.1 Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/doctrine-bundle 1.10.3 Symfony DoctrineBundle
doctrine/doctrine-cache-bundle 1.3.5 Symfony Bundle for Doctrine Cache
doctrine/event-manager 1.1.0 The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector 1.3.1 Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator 1.3.0 A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer 1.2.0 PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm v2.7.2 Object-Relational-Mapper for PHP
doctrine/persistence 1.3.7 The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/reflection 1.2.1 The Doctrine Reflection project is a simple library used by the various Doctrine projects which adds some additional functionality on top of...
fig/link-util 1.1.0 Common utility implementations for HTTP links
incenteev/composer-parameter-handler v2.1.4 Composer script handling your ignored parameter file
jdorn/sql-formatter v1.2.17 a PHP SQL highlighting library
monolog/monolog 1.25.3 Sends your logs to files, sockets, inboxes, databases and various web services
myclabs/deep-copy 1.9.5 Create deep copies (clones) of your objects
ocramius/package-versions 1.4.2 Composer plugin that provides efficient querying for installed package versions (no runtime IO)
paragonie/random_compat v9.99.99 PHP 5.x polyfill for random_bytes() and randomint() from PHP 7 phar-io/manifest 1.0.1 Component for reading phar.io manifest information from a PHP Archive (PHAR) phar-io/version 1.0.1 Library for handling version information and constraints phpdocumentor/reflection-common 2.0.0 Common reflection classes used by phpdocumentor to reflect the code structure phpdocumentor/reflection-docblock 5.1.0 With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a Doc... phpdocumentor/type-resolver 1.1.0 A PSR-5 based resolver of Class names, Types and Structural Element Names phpspec/prophecy v1.10.3 Highly opinionated mocking framework for PHP 5.3+ phpunit/php-code-coverage 5.3.2 Library that provides collection, processing, and rendering functionality for PHP code coverage information. phpunit/php-file-iterator 1.4.5 FilterIterator implementation that filters files based on a list of suffixes. phpunit/php-text-template 1.2.1 Simple template engine. phpunit/php-timer 1.0.9 Utility class for timing phpunit/php-token-stream 2.0.2 Wrapper around PHP's tokenizer extension. phpunit/phpunit 6.3.0 The PHP Unit Testing framework. phpunit/phpunit-mock-objects 4.0.4 Mock Object library for PHPUnit psr/cache 1.0.1 Common interface for caching libraries psr/container 1.0.0 Common Container Interface (PHP FIG PSR-11) psr/link 1.0.0 Common interfaces for HTTP links psr/log 1.1.3 Common interface for logging libraries psr/simple-cache 1.0.1 Common interfaces for simple caching sebastian/code-unit-reverse-lookup 1.0.1 Looks up which function or method a line of code belongs to sebastian/comparator 2.1.3 Provides the functionality to compare PHP values for equality sebastian/diff 2.0.1 Diff implementation sebastian/environment 3.1.0 Provides functionality to handle HHVM/PHP environments sebastian/exporter 3.1.2 Provides the functionality to export PHP variables for visualization sebastian/global-state 2.0.0 Snapshotting of global state sebastian/object-enumerator 3.0.3 Traverses array structures and object graphs to enumerate all referenced objects sebastian/object-reflector 1.1.1 Allows reflection of object attributes, including inherited and non-public ones sebastian/recursion-context 3.0.0 Provides functionality to recursively process PHP variables sebastian/resource-operations 1.0.0 Provides a list of PHP built-in functions that operate on resources sebastian/version 2.0.1 Library that helps with managing the version number of Git-hosted PHP projects sensio/distribution-bundle v5.0.25 Base bundle for Symfony Distributions sensio/framework-extra-bundle v3.0.29 This bundle provides a way to configure your controllers with annotations sensio/generator-bundle v3.1.7 This bundle generates code for you sensiolabs/security-checker v5.0.3 A security checker for your composer.lock swiftmailer/swiftmailer v5.4.12 Swiftmailer, free feature-rich PHP mailer symfony/monolog-bundle v3.2.0 Symfony MonologBundle symfony/polyfill-apcu v1.15.0 Symfony polyfill backporting apcu* functions to lower PHP versions
symfony/polyfill-ctype v1.15.0 Symfony polyfill for ctype functions
symfony/polyfill-intl-icu v1.15.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring v1.15.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56 v1.15.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70 v1.15.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util v1.15.0 Symfony utilities for portability of PHP codes
symfony/swiftmailer-bundle v2.6.7 Symfony SwiftmailerBundle
symfony/symfony v3.3.18 The Symfony PHP framework
theseer/tokenizer 1.1.3 A small library for converting tokenized PHP source code into XML and potentially other formats
twig/twig v2.12.5 Twig, the flexible, fast, and secure template language for PHP
webmozart/assert 1.8.0 Assertions to validate method input/output with nice error messages.
`
Wow I never look to the content below the videos, just saw the *tip*. Answer is: liip/functional-test-bundle:^1.9
Hey julien_bonnier
Yeah sometimes it can be useful :))))
We are glad that you solved your issue! Feel free to ask any questions!
Cheers!!!
if when clicks buttons it is called functional test, then what is end to end test?
Hey Lijana Z.
Good question, an end-to-end test is when you test the entire flow of a feature in your system, in other words, you test that all your components communicates as expected, in this kind of tests you don't mock anything, if you have to store something in the DB, you configure a test DB, if you have to hit an external resource like an API, first you have to find if they offer a sandbox endpoint, if not, then you would have to create a test account so you can use those credentials in your tests
Cheers!
Hi, so in functgional test we still mock some classes?
Usually you don't, but if you don't care about *that* service (maybe it's just a dependency that you are not exercicing in that test), then you can mock it out.
"Houston: no signs of life"
Start the conversation!