PHP code example of draw / tester-bundle

1. Go to this page and download the library: Download draw/tester-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

draw / tester-bundle example snippets




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
        );
    }
}

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();
    }
}

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' => '[email protected]'])]
   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;
}

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();
    }
}


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
    {
        /*...*/
    }
}
xml
<phpunit bootstrap="vendor/autoload.php">
    <extensions>
        <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\SetUpAutowireExtension"/>
    </extensions>
</phpunit>
xml
<phpunit bootstrap="vendor/autoload.php">
    <extensions>
        <bootstrap class="Draw\Component\Tester\PHPUnit\Extension\DoctrineTransaction\DoctrineTransactionExtension"/>
    </extensions>
</phpunit>