draw / tester-bundle
Installs: 45 055
Dependents: 7
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- ext-dom: *
- draw/core: ^0.16
- draw/tester: ^0.16
- symfony/browser-kit: ^6.4.0
- symfony/deprecation-contracts: ^3.5.0
- symfony/framework-bundle: ^6.4.0
- symfony/http-foundation: ^6.4.0
- symfony/validator: ^6.4.0
Requires (Dev)
- draw/profiling: ^0.16
- phpunit/phpunit: ^11.3
- symfony/messenger: ^6.4.0
- symfony/yaml: ^6.4.0
- dev-master / 0.17.x-dev
- 0.16.0
- 0.15.0
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.47
- 0.10.46
- 0.10.45
- 0.10.44
- 0.10.43
- 0.10.42
- 0.10.41
- 0.10.40
- 0.10.39
- 0.10.38
- 0.10.37
- 0.10.36
- 0.10.35
- 0.10.34
- 0.10.33
- 0.10.32
- 0.10.31
- 0.10.30
- 0.10.29
- 0.10.28
- 0.10.27
- 0.10.26
- 0.10.25
- 0.10.24
- 0.10.23
- 0.10.22
- 0.10.21
- 0.10.20
- 0.10.18
- 0.10.17
- 0.10.16
- 0.10.15
- 0.10.14
- 0.10.13
- 0.10.11
- 0.10.10
- 0.10.9
- 0.10.8
- 0.10.7
- 0.10.6
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.92
- 0.9.91
- 0.9.90
- 0.9.89
- 0.9.88
- 0.9.87
- 0.9.86
- 0.9.85
- 0.9.84
- 0.9.83
- 0.9.82
- 0.9.81
- 0.9.80
- 0.9.79
- 0.9.78
- 0.9.77
- 0.9.76
- 0.9.75
- 0.9.74
- 0.9.73
- 0.9.72
- 0.9.71
- 0.9.70
- 0.9.69
- 0.9.68
- 0.9.67
- 0.9.66
- 0.9.65
- 0.9.64
- 0.9.63
- 0.9.62
- 0.9.61
- 0.9.60
- 0.9.59
- 0.9.58
- 0.9.57
- 0.9.56
- 0.9.55
- 0.9.54
- 0.9.53
- 0.9.52
- 0.9.51
- 0.9.50
- 0.9.49
- 0.9.48
- 0.9.47
- 0.9.46
- 0.9.45
- 0.9.44
- 0.9.43
- 0.9.42
- 0.9.41
- 0.9.40
- 0.9.39
- 0.9.38
- 0.9.37
- 0.9.36
- 0.9.35
- 0.9.34
- 0.9.33
- 0.9.32
- 0.9.31
- 0.9.30
- 0.9.29
- 0.9.28
- 0.9.27
- 0.9.26
- 0.9.25
- 0.9.24
- 0.9.23
- 0.9.22
- 0.9.21
- 0.9.20
- 0.9.19
- 0.9.18
- 0.9.17
- 0.9.16
- 0.9.15
- 0.9.14
- 0.9.13
- 0.9.12
- 0.9.11
- 0.9.10
- 0.9.9
- 0.9.8
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.9
- 0.8.8
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.59
- 0.7.58
- 0.7.57
- 0.7.56
- 0.7.54
- 0.7.53
- v0.7.52
- v0.7.51
- v0.7.50
- v0.7.49
- v0.7.48
- v0.7.47
- v0.7.46
- v0.7.45
- v0.7.44
- v0.7.43
- v0.7.42
- v0.7.41
- v0.7.40
- v0.7.39
- v0.7.38
- v0.7.37
- v0.7.36
- v0.7.35
- v0.7.34
- v0.7.33
- v0.7.32
- v0.7.31
- v0.7.30
- v0.7.29
- v0.7.28
- v0.7.27
- v0.7.26
- v0.7.25
- v0.7.24
- v0.7.23
- v0.7.22
- 0.7.21
- v0.7.20
- v0.7.19
- v0.7.18
- v0.7.17
- v0.7.16
- v0.7.15
- v0.7.14
- v0.7.13
- v0.7.12
- v0.7.11
- v0.7.10
- v0.7.9
- v0.7.8
- v0.7.7
- v0.7.6
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.0
- v0.5.23
- 0.5.22
- v0.5.21
- v0.5.20
- v0.5.19
- v0.5.18
- v0.5.17
- v0.5.16
- v0.5.15
- v0.5.14
- v0.5.13
- v0.5.12
- v0.5.11
- v0.5.10
- v0.5.9
- v0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- dev-packages-git-actions
This package is auto-updated.
Last update: 2024-12-05 18:24:13 UTC
README
This bundle integrate the Draw Tester Component.
It also provides test helpers to make it easier to test your Symfony application.
Kernel Testing
When configuring your kernel you may want to test that everything is hooked up correctly.
There is the list of service, event dispatcher, command etc.
There is some TestCase/Trait to help you do that (work in progress).
Event Dispatcher
Relying on the debug:event-dispatcher
command we can dump the list of event listeners and validated it against the expected list.
<?php namespace App\Tests; use Draw\Bundle\TesterBundle\EventDispatcher\EventDispatcherTesterTrait; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class AppKernelTest extends KernelTestCase { use EventDispatcherTesterTrait; public function testEventDispatcherConfiguration(): void { $this->assertEventDispatcherConfiguration( __DIR__.'/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml', 'event_dispatcher' // This is the default value, same as the debug:event-dispatcher command ); } }
The first time you run this test it will fail and dump the current configuration in the event_dispatcher.xml
file.
Commit this file, next time your rune this test you will be able to validate that the configuration is still valid.
If you change the listener in your code or change your dependencies you can run the test again and see the diff.
This will allow you to see if some external listeners changed at the same time.
PHPUnit Extension
This bundle also provide a PHPUnit extension to make it easier to test your Symfony application.
KernelShutdown
Sometimes you need to use the kernel/container in a tearDownAfterClass method.
namespace App\Tests; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class MyTest extends KernelTestCase { use KernelShutdownTrait; public static function tearDownAfterClass(): void { static::getContainer()->get('my_service')->doSomething(); } }
Since symfony shutdown the kernel in the tearDown method, this will boot a new kernel cause a kernel to be up.
Adding the KernelShutdownExtension will make sure the kernel is shutdown after the test.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <!-- It must be after any extension that could also boot a kernel --> <bootstrap class="Draw\Bundle\TesterBundle\PHPUnit\Extension\KernelShutdown\KernelShutdownExtension"/> </extensions> </phpunit>
SetUpAutowire addon
The draw/tester component provide a way to autowire property in your test.
This bundle provide some custom Autowire attribute that can use in the context of a Symfony test cases.
Make sure to register is in your phpunit configuration file. as explained in the draw/tester
documentation.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\SetUpAutowireExtension"/> </extensions> </phpunit>
Here is an example of attribute you can use in your test case:
namespace App\Tests; use App\AServiceInterface; use App\Entity\User; use App\MyService; use App\MyOtherService; use Draw\Bundle\TesterBundle\Messenger\TransportTester; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireEntity; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireLoggerTester; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireParameter; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireServiceMock; use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireTransportTester; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireMock; use Monolog\Handler\TestHandler; use PHPUnit\Framework\MockObject\MockObject; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class MyTest extends KernelTestCase implements AutowiredInterface { // From d #[AutowireMock] private AServiceInterface&MockObject $aService // Will hook MyService from the test container. Your test need to extend KernelTestCase. // // The AutowireMockProperty will replace the aService property of $myService. // By defaults, it will use the same property name in the current test case but you can specify a different one using the second parameter. #[AutowireService] #[AutowireMockProperty('aService')] private MyService $myService; // Will hook the parameter from the container using ParameterBagInterface::resolveValue #[AutowireParameter('%my_parameter%')] private string $parameter; // Will hook the transport tester from the container. #[AutowireTransportTester('async')] private TransportTester $transportTester; // Rely on the 'monolog.handler.testing' service to be available in the container. #[AutowireLoggerTester] private TestHandler $loggerTester; #[AutowireEntity(['email' => 'test@example.com'])] private User $user; // Will create a mock object of MyOtherService and call container->set(MyOtherService::class, $mockObject) // You can also set the service id to use in the container as the first parameter of the attribute. #[AutowireServiceMock] private MyOtherService&MockObject $myOtherService; }
If you extend from a WebTestCase
you can also use the AutowireClient
attribute to get a client.
By using the AutowireClient
in conjunction with the AutowireService
you are use that the client is
created before the other service preventing the exception:
Booting the kernel before calling "Symfony\Bundle\FrameworkBundle\Test\WebTestCase::createClient" is not supported, the kernel should only be booted once
namespace App\Tests; use App\MyService;use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireClient;use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface;use Symfony\Bundle\FrameworkBundle\KernelBrowser;use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class MyTest extends WebTestCase implements AutowiredInterface { #[AutowireClient] private KernelBrowser $client; public function testSomething(): void { $this->client->request('GET', '/my-route'); static::assertResponseIsSuccessful(); } }
This is the same client as the one you get from the WebTestCase
, you can use it the same way.
Note that the AutowireClient
attribute have an options
and server
parameters like you would do when calling the createClient
method.
DoctrineTransaction
Base on dama/doctrine-test-bundle
this extension will start a transaction before each test class and rollback it after the test.
By using test class instead of test method, like the original bundle does, it will ease dependencies managements.
Make sure to configure this extension first in your phpunit configuration file.
<phpunit bootstrap="vendor/autoload.php"> <extensions> <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\DoctrineTransaction\DoctrineTransactionExtension"/> </extensions> </phpunit>
Also if one of your test should not have transaction you can use the NoTransaction
attribute on the test class.
namespace App\Tests; use Draw\Bundle\TesterBundle\PHPUnit\Extension\DoctrineTransaction\NoTransaction; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; #[NoTransaction] class MyTest extends KernelTestCase { public function testSomething(): void { /*...*/ } }