{"id":15103805,"url":"https://github.com/silasyudi/inject-mocks","last_synced_at":"2026-01-19T21:33:42.943Z","repository":{"id":56899628,"uuid":"478294186","full_name":"silasyudi/inject-mocks","owner":"silasyudi","description":"Automatic injection of mocks into test subjects via @InjectMocks and @Mock annotations, to speed up unit testing with PHPUnit.","archived":false,"fork":false,"pushed_at":"2024-06-20T14:30:20.000Z","size":64,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-16T15:17:58.441Z","etag":null,"topics":["annotations","inject-mocks","mocks","php","phpunit","testing-tools","tests"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/silasyudi/inject-mocks","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/silasyudi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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}},"created_at":"2022-04-05T20:43:04.000Z","updated_at":"2024-06-20T14:29:12.000Z","dependencies_parsed_at":"2024-06-21T06:27:33.929Z","dependency_job_id":"1484e2cc-6d6b-4a7e-aec5-cb555cb22a87","html_url":"https://github.com/silasyudi/inject-mocks","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"686da2418119396e2b6981cb687f017df83cc06f"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/silasyudi/inject-mocks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silasyudi%2Finject-mocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silasyudi%2Finject-mocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silasyudi%2Finject-mocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silasyudi%2Finject-mocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/silasyudi","download_url":"https://codeload.github.com/silasyudi/inject-mocks/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silasyudi%2Finject-mocks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28585518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-19T20:45:59.482Z","status":"ssl_error","status_checked_at":"2026-01-19T20:45:41.500Z","response_time":67,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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-mocks","mocks","php","phpunit","testing-tools","tests"],"created_at":"2024-09-25T19:42:23.925Z","updated_at":"2026-01-19T21:33:42.927Z","avatar_url":"https://github.com/silasyudi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Inject-Mocks\n\n[![Tests](https://github.com/silasyudi/inject-mocks/actions/workflows/tests.yml/badge.svg)](https://github.com/silasyudi/inject-mocks/actions/workflows/tests.yml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/b89bc606334c7edec92e/maintainability)](https://codeclimate.com/github/silasyudi/inject-mocks/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/b89bc606334c7edec92e/test_coverage)](https://codeclimate.com/github/silasyudi/inject-mocks/test_coverage)\n\nAutomatic injection of mocks into test subjects via #InjectMocks and #Mock annotations, to speed up unit testing with\nPHPUnit.\n\n## Summary\n\n- [Language / Idioma](#language--idioma)\n- [Instalation](#instalation)\n- [Requirements](#requirements)\n- [Features](#features)\n- [Usage](#usage)\n\n## Language / Idioma\n\nLeia a versão em português :brazil: [aqui](README_PT_BR.md).\n\n## Instalation\n\nTo install in the development environment:\n\n```sh\ncomposer require --dev silasyudi/inject-mocks\n```\n\n## Requirements\n\n- PHP 8.3+\n- Composer 2\n\n## Features\n\nUsing #InjectMocks and #Mock annotations in test classes you can automatically inject mocks into test subjects.\n\nIn a typical scenario, we would do it like this:\n\n### Example without #InjectMocks/#Mock:\n\n```php\nclass SomeTest extends \\PHPUnit\\Framework\\TestCase\n{\n    public void testSomething() \n    {\n        $someDependency = $this-\u003ecreateMock(Dependency::class);    \n        $anotherDependency = $this-\u003ecreateMock(AnotherDependency::class);\n        ...\n        $subject = new Service($someDependency, $anotherDependency, ...);\n        ...    \n    }\n    \n    ...\n```\n\nThis approach brings the difficulty of maintenance, because if the test subject is changed, either by adding, decreasing\nor replacing the dependencies, you will have to change it in each test.\n\nWith the #InjectMocks/#Mock annotations, we abstract these test subject changes. Example:\n\n### Example with #InjectMocks/#Mock:\n\n```php\nuse SilasYudi\\InjectMocks\\InjectMocks;\nuse SilasYudi\\InjectMocks\\Mock;\nuse SilasYudi\\InjectMocks\\MockInjector;\n\nclass SomeTest extends \\PHPUnit\\Framework\\TestCase\n{\n    #[Mock]\n    private Dependency $someDependency;\n    #[Mock]\n    private AnotherDependency $anotherDependency;\n    \n    ...\n    \n    #[InjectMocks]\n    \n    public function setUp() : void \n    {\n        MockInjector::inject($this);\n    }\n    \n    public void testSomething()\n    {\n        // $this-\u003esubject e as dependências já estão instanciadas.\n    }\n    \n    ...\n```\n\n## Usage\n\nAs in the example in the previous topic, the #InjectMocks attribute must be placed on the property of the test subject\nthat you want to test, and the #Mock attribute must be placed on the properties corresponding to the dependencies that\nyou want to mock or inject.\n\nAfter that, run the injector service with the sentence `MockInjector::inject($this)`. This execution can be declared in\neach test or in `setUp`.\n\nAfter executing the injector, the `service` annotated with #InjectMocks will be a real instance available in the scope\nof the test class, and each dependency annotated with #Mock will be an instance of MockObject, injected into the test\nsubject via the constructor, and will also be available in the scope of the test class.\n\n### Details\n\n#### 1. Scope of Attributes\n\n- Both #InjectMocks and #Mock MUST be placed over a TYPED property in a TestCase class;\n- Properties that receive #InjectMocks and #Mock attributes MUST be an object;\n- You MUST use only one #InjectMocks attribute per TestCase. When using more than one on the same scope, this library\n  will use only the first one, and ignore the others;\n- You MUST use a #Mock attribute for each test subject dependency you want to mock;\n- Using attributes on untyped properties or on primitive types will cause a `MockInjectException` exception.\n- When using #Mock on more than one object of the same type in the same test class, this library will match each one\n  via property names, which MUST be identical to the test subject class.\n\n#### 2. Behaviors\n\n#InjectMocks and #Mock work independently and alone, or together. Details about each one:\n\n##### 2.1. #InjectMocks\n\nIt will create a real instance through the constructor, and if there are parameters in the constructor, the following\nvalue will be used in each parameter, in this order:\n\n- A `mock` created from the #[Mock] attribute, if one exists;\n- The `default` value if it is an optional parameter;\n- `null` if it is typed as `null`;\n- Will create a `mock` if it is not a primitive type. In this case, this `mock` will not be injected in the TestCase\n  scope;\n- Finally, if the previous options are not satisfied, it will throw `MockInjectException` exception.\n\nObs.: You can use #Mock attribute on all, some or none of the test subject's dependencies.\n\n##### 2.2. #Mock\n\nWill create a `mock` injected into the TestCase scope, without using the constructor. This creation behavior is\nidentical to `TestCase::createMock()`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilasyudi%2Finject-mocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsilasyudi%2Finject-mocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilasyudi%2Finject-mocks/lists"}