Skip to content

Commit

Permalink
[Tester\TesterBundle] Refactor PHPUnit extension and improve document…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
mpoiriert committed Aug 11, 2024
1 parent b80fb2a commit 9ce0d31
Show file tree
Hide file tree
Showing 36 changed files with 367 additions and 190 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension;
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\KernelShutdown;

use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\Finished as TestFinished;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Draw\Bundle\TesterBundle\WebTestCase as DrawWebTestCase;
use Draw\Component\Core\Reflection\ReflectionAccessor;
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;

use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;

use Draw\Component\Core\Reflection\ReflectionExtractor;
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

Expand Down

This file was deleted.

125 changes: 124 additions & 1 deletion packages/tester-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,127 @@ Commit this file, next time your rune this test you will be able to validate tha

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.
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.

```php
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.

```xml
<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](https://github.com/mpoiriert/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.

```xml
<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:

```php
namespace App\Tests;

use App\AServiceInterface;
use App\MyService;
use Draw\Bundle\TesterBundle\Messenger\TransportTester;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireParameter;
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService;
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 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;
}
```

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`

```php
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.
6 changes: 3 additions & 3 deletions packages/tester-bundle/extension.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
draw.tester_bundle.autowire_client_read_write_properties_extension:
class: Draw\Bundle\TesterBundle\PHPStan\Rules\Properties\AutowireReadWritePropertiesExtension
draw.component.tester.autowire_client_read_write_properties_extension:
class: Draw\Component\Tester\PHPStan\Rules\Properties\AutowireReadWritePropertiesExtension
arguments:
- 'Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireInterface'
- 'Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireInterface'
tags:
- phpstan.properties.readWriteExtension
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPStan\Rules\Properties;
namespace Draw\Component\Tester\PHPStan\Rules\Properties;

use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Properties\ReadWritePropertiesExtension;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Component\Tester\PHPUnit\Extension;
namespace Draw\Component\Tester\PHPUnit\Extension\CarbonReset;

use Carbon\Carbon;
use PHPUnit\Event\Test\Finished as TestFinished;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;
namespace Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;
namespace Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire;

use Draw\Component\Core\Reflection\ReflectionAccessor;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;
namespace Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire;

use Draw\Component\Core\Reflection\ReflectionAccessor;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;
namespace Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire;

interface AutowiredCompletionAwareInterface extends AutowiredInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire;

interface AutowiredInterface
{
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;
namespace Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire;

use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\Prepared as TestPrepared;
Expand Down
Loading

0 comments on commit 9ce0d31

Please sign in to comment.