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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package phpunit-injector

PHPUnit Injector

Build

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:

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

PHP Build Version
Package Version
Requires php Version ~8.1.0 || ~8.2.0 || ~8.3.0
phpunit/phpunit Version ^9.0
psr/container Version ^1.0 || ^2.0
zalas/injector Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zalas/phpunit-injector contains the following files

Loading the files please wait ....