I’m new to Unit-Testing and want to test if the frontend-login of my FLOW Application is working. Assuming this is more a functional test, I started to think about the requirements of testing such a case. Creating a new user in the database, log in, check response, log out, delete user, etc.
Unfortunately the Testing-Section of the documentation is not written (https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/Testing.html).
Can someone help me out with the more FLOW-related stuff? I’m pretty sure, there are some predefined classes and tools inside FLOW wich makes things easier.
Common questions so far…
- Different database for functional testing?
- How to make a request?
- How to check the response?
Any kind of help welcome (youtube-links, doc-links). I’m not shy to read and watch.
Thanks in advance.
Hey! Glad you want to take a look into testing your application. Unfortunately, testing is (IMO) one of the least documented and/or polished things in the whole framework, but here are a couple of things you can do to get into the topic:
- Always extend the
UnitTestCase classes for your test case classes, they come with convenience helper methods for Flow
Testing context, flow will by default work with an SQLite in-memory backend for persistence (as specified in Flow Testing Configuration Settings.yaml)
- You can make requests in Testing via the Browser client
FunctionalTestCase this is already instanciated as
- The response you receive from the browser is a normal Flow Http Response which you can inspect for status code, return body and headers.
- Also, for convenience on testing expectations on the result body or working with HTML forms, the browser provides a Symfony
->getCrawler(), which allows you to skim through the result HTML via X-Path (see https://symfony.com/doc/current/components/dom_crawler.html)
- to run your functional tests, execute
bin/phpunit -c Build/BuildEssentials/PhpUnit/FunctionalTests.xml from your flow root directory (you can also filter to only execute the test suite you want by specifying it as additional parameter)
For working tests and some examples on how to test your application, you can for example take a look at the functional
Mvc\ActionControllerTest and for the authentication related stuff, take a look at the functional
Security\AuthenticationTest inside the Flow Tests folder.
Hope this helps for a start. If you have further questions, just ask!
Thank you very much for your short introduction. You helped a lot to get started! There are some questions but they’re more or less bound to the IDE (PhpStorm) or the Docker-Setup I use.
One thing wich I couldn’t figure out is the context which my tests are running.
Any Unit-Test I’ve run so far, was working well. But when I run my Functional-Tests I’ve got the this message:
The context “Development/Beach/Instance” is not allowed. Only “Testing” context or one of its subcontexts “Testing/*” is allowed.
I understand the meaning of this, as there is need for a bootstrap of the whole framework. But how could I setup my tests to run in a Testing-Context? This also happens for the tests in the Flow-Framework…
Yes. Its a combination of IDE and Beach. In my IDE I can’t set the environment variable for the php (that one for the unit test). It would be possible on the CLI …
Actually I’m thinking about, having a different subdomain for testing with a Testing-Context.
There is a new feature-request for the beach development environment. Thanks, @robert.