{"id":15014483,"url":"https://github.com/jakzal/phpunit-injector","last_synced_at":"2025-10-25T23:38:00.873Z","repository":{"id":30018039,"uuid":"123633060","full_name":"jakzal/phpunit-injector","owner":"jakzal","description":"Injects services from a PSR-11 dependency injection container to PHPUnit test cases","archived":false,"fork":false,"pushed_at":"2024-10-07T09:41:04.000Z","size":252,"stargazers_count":63,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"2.5","last_synced_at":"2025-04-09T18:19:25.106Z","etag":null,"topics":["annotations","inject-services","injector","php","phpunit","phpunit-listener","psr-11","symfony","test","tests"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jakzal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["jakzal"]}},"created_at":"2018-03-02T21:32:03.000Z","updated_at":"2024-10-16T15:59:00.000Z","dependencies_parsed_at":"2024-06-04T11:58:28.617Z","dependency_job_id":null,"html_url":"https://github.com/jakzal/phpunit-injector","commit_stats":{"total_commits":135,"total_committers":3,"mean_commits":45.0,"dds":"0.029629629629629672","last_synced_commit":"6be4d503cfff15cdfecc78d23e4dd1ca570b4a2d"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-injector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-injector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-injector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-injector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jakzal","download_url":"https://codeload.github.com/jakzal/phpunit-injector/tar.gz/refs/heads/2.5","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085325,"owners_count":21045139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotations","inject-services","injector","php","phpunit","phpunit-listener","psr-11","symfony","test","tests"],"created_at":"2024-09-24T19:45:41.106Z","updated_at":"2025-10-25T23:38:00.798Z","avatar_url":"https://github.com/jakzal.png","language":"PHP","funding_links":["https://github.com/sponsors/jakzal"],"categories":[],"sub_categories":[],"readme":"# PHPUnit Injector\n\n[![Build](https://github.com/jakzal/phpunit-injector/actions/workflows/build.yml/badge.svg)](https://github.com/jakzal/phpunit-injector/actions/workflows/build.yml)\n\nProvides a PHPUnit listener to inject services from a PSR-11 dependency injection container to PHPUnit test cases.\n\nServices are injected to test cases that implement `Zalas\\Injector\\PHPUnit\\TestCase\\ServiceContainerTestCase`\nto any property tagged with `@inject`.\n\n[Symfony DependencyInjection component](https://github.com/symfony/dependency-injection) integration is also provided.\n\n## Installation\n\n### Composer\n\n```bash\ncomposer require --dev zalas/phpunit-injector\n```\n\n### Phar\n\nThe extension is also distributed as a PHAR, which can be downloaded from the most recent\n[Github Release](https://github.com/jakzal/phpunit-injector/releases).\n\nPut the extension in your PHPUnit extensions directory.\nRemember to instruct PHPUnit to load extensions in your `phpunit.xml`:\n\n```xml\n\u003cphpunit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"https://schema.phpunit.de/8.0/phpunit.xsd\"\n         extensionsDirectory=\"tools/phpunit.d\"\n\u003e\n\u003c/phpunit\u003e\n```\n\n## Configuration\n\nEnable the service injector listener\nin the [PHPUnit configuration file](https://phpunit.de/manual/current/en/appendixes.configuration.html):\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cphpunit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"https://schema.phpunit.de/8.0/phpunit.xsd\"\u003e\n\n    \u003c!-- ... --\u003e\n\n    \u003clisteners\u003e\n        \u003clistener class=\"Zalas\\Injector\\PHPUnit\\TestListener\\ServiceInjectorListener\" /\u003e\n    \u003c/listeners\u003e\n\u003c/phpunit\u003e\n```\n\n## Usage\n\nTo inject services using any PSR-11 service container, implement the `Zalas\\Injector\\PHPUnit\\TestCase\\ServiceContainerTestCase`\nand tag selected properties with `@inject`:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Psr\\Container\\ContainerInterface;\nuse Psr\\Log\\LoggerInterface;\nuse Symfony\\Component\\Serializer\\SerializerInterface;\nuse Zalas\\Injector\\PHPUnit\\TestCase\\ServiceContainerTestCase;\n\nclass ServiceInjectorTest extends TestCase implements ServiceContainerTestCase\n{\n    /**\n     * @inject\n     */\n    private SerializerInterface $serializer;\n\n    /**\n     * @inject logger\n     */\n    private LoggerInterface $logger;\n\n    public function testThatServicesAreInjected()\n    {\n        $this-\u003eassertInstanceOf(SerializerInterface::class, $this-\u003eserializer, 'The service is injectd by its type');\n        $this-\u003eassertInstanceOf(LoggerInterface::class, $this-\u003elogger, 'The service is injected by its id');\n    }\n\n    public function createServiceContainer(): ContainerInterface\n    {\n        // create a service container here\n    }\n}\n```\n\nThe service is found by its type, or an id if it's given in the `@inject` tag.\n\nThe `createServiceContainer` method would be usually provided by a base test case or a trait.\nIn case of Symfony, such a trait is provided by this package (see the next section).\n\n### Symfony Test Container (Symfony \u003e= 4.1)\n\nThe `Zalas\\Injector\\PHPUnit\\Symfony\\TestCase\\SymfonyTestContainer` trait provides\naccess to the test container ([introduced in Symfony 4.1](https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing)).\nIncluding the trait in a test case implementing the `ServiceContainerTestCase` will make that services are injected\ninto annotated properties:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Psr\\Log\\LoggerInterface;\nuse Symfony\\Component\\Serializer\\SerializerInterface;\nuse Zalas\\Injector\\PHPUnit\\Symfony\\TestCase\\SymfonyTestContainer;\nuse Zalas\\Injector\\PHPUnit\\TestCase\\ServiceContainerTestCase;\n\nclass ServiceInjectorTest extends TestCase implements ServiceContainerTestCase\n{\n    use SymfonyTestContainer;\n\n    /**\n     * @inject\n     */\n    private SerializerInterface $serializer;\n\n    /**\n     * @inject logger\n     */\n    private LoggerInterface $logger;\n\n    public function testThatServicesAreInjected()\n    {\n        $this-\u003eassertInstanceOf(SerializerInterface::class, $this-\u003eserializer, 'The service is injectd by its type');\n        $this-\u003eassertInstanceOf(LoggerInterface::class, $this-\u003elogger, 'The service is injected by its id');\n    }\n}\n```\n\nNote that `test` needs to be set to `true` in your test environment configuration for the framework bundle:\n\n```yaml\nframework:\n    test: true\n```\n\nEven though services are automatically made private by Symfony, the test container makes them available in your tests.\nNote that this only happens for private services that are actually used in your app (so are injected into\na public service, i.e. a controller). If a service is not injected anywhere, it's removed by the container compiler.\n\nThe kernel used to bootstrap the container is created in a similar way to the `KernelTestCase` known from the FrameworkBundle.\nSimilar environment variables are supported:\n\n * `KERNEL_CLASS` *required* - kernel class to instantiate to create the service container\n * `APP_ENV` default: test - kernel environment\n * `APP_DEBUG` default: false - kernel debug flag\n\nThese could for example be configured in `phpunit.xml`, or via [global variables](https://github.com/jakzal/phpunit-globals).\n\n### Symfony Container (Symfony 3.4 \u0026 4.0)\n\nThe `Zalas\\Injector\\PHPUnit\\Symfony\\TestCase\\SymfonyContainer` trait gives access to the full Symfony Container\nand can be used with any Symfony version.\nOpposed to the Test Container approach for Symfony 4.1, this version provides access to each service even if it's\nnot used by your application anywhere and would normally be removed by the compiler.\nThis should be treated as a limitation rather than a feature.\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Psr\\Log\\LoggerInterface;\nuse Symfony\\Component\\Serializer\\SerializerInterface;\nuse Zalas\\Injector\\PHPUnit\\Symfony\\TestCase\\SymfonyContainer;\nuse Zalas\\Injector\\PHPUnit\\TestCase\\ServiceContainerTestCase;\n\nclass ServiceInjectorTest extends TestCase implements ServiceContainerTestCase\n{\n    use SymfonyContainer;\n\n    /**\n     * @inject\n     */\n    private SerializerInterface $serializer;\n\n    /**\n     * @inject logger\n     */\n    private LoggerInterface $logger;\n\n    public function testThatServicesAreInjected()\n    {\n        $this-\u003eassertInstanceOf(SerializerInterface::class, $this-\u003eserializer, 'The service is injectd by its type');\n        $this-\u003eassertInstanceOf(LoggerInterface::class, $this-\u003elogger, 'The service is injected by its id');\n    }\n}\n```\n\nSince the test container is not available until Symfony 4.1,\nyou'll also have to register the `Zalas\\Injector\\PHPUnit\\Symfony\\Compiler\\ExposeServicesForTestsPass` compiler pass:\n\n```php\nuse Zalas\\Injector\\PHPUnit\\Symfony\\Compiler\\ExposeServicesForTestsPass;\n\nclass Kernel extends BaseKernel\n{\n    // ...\n\n    protected function build(ContainerBuilder $container)\n    {\n        if ('test' === $this-\u003egetEnvironment()) {\n            $container-\u003eaddCompilerPass(new ExposeServicesForTestsPass());\n        }\n    }\n}\n```\n\nThe compiler pass makes sure that even private services are available to be used in tests.\n\n## Contributing\n\nPlease read the [Contributing guide](CONTRIBUTING.md) to learn about contributing to this project.\nPlease note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md).\nBy participating in this project you agree to abide by its terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakzal%2Fphpunit-injector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjakzal%2Fphpunit-injector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakzal%2Fphpunit-injector/lists"}