Download the PHP package zalas/phpunit-injector without Composer
On this page you can find all versions of the php package zalas/phpunit-injector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zalas/phpunit-injector
More information about zalas/phpunit-injector
Files in zalas/phpunit-injector
Package phpunit-injector
Short Description Injects services from a PSR-11 dependency injection container to PHPUnit test cases
License MIT
Informations about the package phpunit-injector
PHPUnit Injector
Provides a PHPUnit listener to inject services from a PSR-11 dependency injection container to PHPUnit test cases.
Services are injected to test cases that implement Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase
to any property tagged with @inject
.
Symfony DependencyInjection component integration is also provided.
Installation
Composer
Phar
The extension is also distributed as a PHAR, which can be downloaded from the most recent Github Release.
Put the extension in your PHPUnit extensions directory.
Remember to instruct PHPUnit to load extensions in your phpunit.xml
:
Configuration
Enable the service injector listener in the PHPUnit configuration file:
Usage
To inject services using any PSR-11 service container, implement the Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase
and tag selected properties with @inject
:
The service is found by its type, or an id if it's given in the @inject
tag.
The createServiceContainer
method would be usually provided by a base test case or a trait.
In case of Symfony, such a trait is provided by this package (see the next section).
Symfony Test Container (Symfony >= 4.1)
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:
Note that test
needs to be set to true
in your test environment configuration for the framework bundle:
Even though services are automatically made private by Symfony, the test container makes them available in your tests. Note that this only happens for private services that are actually used in your app (so are injected into a public service, i.e. a controller). If a service is not injected anywhere, it's removed by the container compiler.
The kernel used to bootstrap the container is created in a similar way to the KernelTestCase
known from the FrameworkBundle.
Similar environment variables are supported:
KERNEL_CLASS
required - kernel class to instantiate to create the service containerAPP_ENV
default: test - kernel environmentAPP_DEBUG
default: false - kernel debug flag
These could for example be configured in phpunit.xml
, or via global variables.
Symfony Container (Symfony 3.4 & 4.0)
The Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyContainer
trait gives access to the full Symfony Container
and can be used with any Symfony version.
Opposed to the Test Container approach for Symfony 4.1, this version provides access to each service even if it's
not used by your application anywhere and would normally be removed by the compiler.
This should be treated as a limitation rather than a feature.
Since the test container is not available until Symfony 4.1,
you'll also have to register the Zalas\Injector\PHPUnit\Symfony\Compiler\ExposeServicesForTestsPass
compiler pass:
The compiler pass makes sure that even private services are available to be used in tests.
Contributing
Please read the Contributing guide to learn about contributing to this project. Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
All versions of phpunit-injector with dependencies
phpunit/phpunit Version ^9.0
psr/container Version ^1.0 || ^2.0
zalas/injector Version ^2.0