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