jakzal / phpunit-injector

Injects services from a PSR-11 dependency injection container to PHPUnit test cases
MIT License
63 stars 4 forks source link

Add support for the test service container from Symfony 4.1 #4

Closed jakzal closed 6 years ago

jakzal commented 6 years ago

The Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer trait provides access to the test container (introduced in Symfony 4.1).

Including the trait in a test case implementing the ServiceContainerTestCase will make that services are injected into annotated properties:

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer;
use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;

class ServiceInjectorTest extends TestCase implements ServiceContainerTestCase
{
    use SymfonyTestContainer;

    /**
     * @var SerializerInterface
     * @inject
     */
    private $serializer;

    /**
     * @var LoggerInterface
     * @inject logger
     */
    private $logger;

    public function testThatServicesAreInjected()
    {
        $this->assertInstanceOf(SerializerInterface::class, $this->serializer, 'The service is injectd by its type');
        $this->assertInstanceOf(LoggerInterface::class, $this->logger, 'The service is injected by its id');
    }
}