Automated tests Flashcards
Which external testing framework does symfony integrate?
PHPUnit
Which different type of tests exist?
Unit tests
Integration tests
Application / functional tests
What needs to be installed before starting with automated tests?
symfony/test-pack
In which directory should testclasses be located?
/tests
Which file is used to configure PHPUnit?
phpunit.xml.dist
How should Testfiles be stored according to best practice?
In the same structure like the actual classes, but under
/tests
What do unit tests do?
Ensure that individual units of source code behave as intended
How can tests be run?
php bin/phpunit
php bin/phpunit tests/Form
php bin/phpunit tests/Form/UserTypeTest.php
What do integration tests do?
Test larger parts of the application compared to a unit test (e.g. a combination of services)
Which predefined symfony class might be helpful for integration?
KernelTestCase
Why is the KernelTestCase helpful for integration tests?
It reboots the kernel and provides the DI-container for every testcase
How can you integrate a different database connection for testcases instead using the one for dev environment?
By overriding the DATABASE_URL via the .env.test
What is the order of .env overriding?
.env: containing env vars with application defaults;
.env.test: overriding/setting specific test values or vars;
.env.test.local: overriding settings specific for this machine.
How can a concrete service in the container be exchanged by a mocked one?
Use KernelTestCase and:
$newsRepository = $this->createMock(NewsRepositoryInterface::class); $container->set(NewsRepositoryInterface::class, $newsRepository);
How can a test database be created?
php bin/console --env=test doctrine:database:create
How is dummy database-data being created?
with doctrine fixtures:
doctrine/doctrine-fixtures-bundle
What do application tests do?
Check the integration of all the different layers of the application
Where are application-tests typcally stored?
tests/Controller/
Which base-class does symfony provide for application tests?
WebTestCase
What additional functinalities does the WebTestCase provide?
BrowserKitAssertions,
DomCrawlerAssertions,
ClientAssertions
How can you make the client for WebTestCases follow / unfollow redirects that happen during a process?
$client->followRedirects();
$client->followRedirects(false);
How can you test parts of the application that require login for being used inside an application test?
Before using the client to send the request, execute:
$client->loginUser($testUser);
`
The test user can come from e.g. Doctrine-Fixture only for test environment
Which method is being used to send an AJAX-request within an application test?
$client = static::createClient(); $client->xmlHttpRequest('POST', '/submit', ['name' => 'Fabien']);
How do you set custom headers when sending a request within an application test?
For all requests
$client = static::createClient([], [ 'HTTP_HOST' => 'en.example.com', 'HTTP_USER_AGENT' => 'MySuperBrowser/1.0', ]);
or for specific requests
$client->request('GET', '/', [], [], [ 'HTTP_HOST' => 'en.example.com', 'HTTP_USER_AGENT' => 'MySuperBrowser/1.0', ]);