{"id":15306896,"url":"https://github.com/kenjis/phpunit-helper","last_synced_at":"2025-04-15T00:54:43.153Z","repository":{"id":46685717,"uuid":"334854238","full_name":"kenjis/phpunit-helper","owner":"kenjis","description":"Helpers for PHPUnit. Easy mock creation and easy private property/method testing.","archived":false,"fork":false,"pushed_at":"2021-09-30T04:12:03.000Z","size":97,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"1.x","last_synced_at":"2025-03-28T12:51:11.992Z","etag":null,"topics":["hacktoberfest","phpunit","phpunit-mock","phpunit-util"],"latest_commit_sha":null,"homepage":"","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/kenjis.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}},"created_at":"2021-02-01T06:35:37.000Z","updated_at":"2022-03-17T07:40:55.000Z","dependencies_parsed_at":"2022-08-21T14:11:01.935Z","dependency_job_id":null,"html_url":"https://github.com/kenjis/phpunit-helper","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fphpunit-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fphpunit-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fphpunit-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fphpunit-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kenjis","download_url":"https://codeload.github.com/kenjis/phpunit-helper/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248813803,"owners_count":21165634,"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":["hacktoberfest","phpunit","phpunit-mock","phpunit-util"],"created_at":"2024-10-01T08:08:15.294Z","updated_at":"2025-04-15T00:54:43.135Z","avatar_url":"https://github.com/kenjis.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHPUnit Helper\n\nProvides helper traits for PHPUnit.\n\n- [TestDouble](#testdouble) ... Easy mock creation\n- [ReflectionHelper](#reflectionhelper) ... Easy private property/method testing\n- [DebugHelper](#debughelper) ... Helper function `dd()` and `d()`\n\n## Table of Contents\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [`TestDouble`](#testdouble)\n    - [`$this-\u003egetDouble()`](#this-getdouble)\n    - [`$this-\u003everifyInvoked()`](#this-verifyinvoked)\n    - [`$this-\u003everifyInvokedOnce()`](#this-verifyinvokedonce)\n    - [`$this-\u003everifyInvokedMultipleTimes()`](#this-verifyinvokedmultipletimes)\n    - [`$this-\u003everifyNeverInvoked()`](#this-verifyneverinvoked)\n  - [`ReflectionHelper`](#reflectionhelper)\n    - [`$this-\u003egetPrivateProperty()`](#this-getprivateproperty)\n    - [`$this-\u003esetPrivateProperty()`](#this-setprivateproperty)\n    - [`$this-\u003egetPrivateMethodInvoker()`](#this-getprivatemethodinvoker)\n  - [`DebugHelper`](#debughelper)\n- [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Requirements\n\n- PHP 7.3 or later\n\n## Installation\n\nRun:\n\n```sh-session\n$ composer require --dev kenjis/phpunit-helper\n```\n\n## Usage\n\n### `TestDouble`\n\nThis trait provides helper methods to create mock objects and to verify invocations.\n\nImport the `Kenjis\\PhpUnitHelper\\TestDouble` trait into your test class:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace Foo\\Bar\\Test\\Unit;\n\nuse Kenjis\\PhpUnitHelper\\TestDouble;\nuse PHPUnit\\Framework;\n\nfinal class BazTest extends Framework\\TestCase\n{\n    use TestDouble;\n}\n```\n\n#### `$this-\u003egetDouble()`\n\n| param               | type        | description                                            |\n|---------------------|-------------|--------------------------------------------------------|\n|`$classname`         | string      | class name                                             |\n|`$params`            | array       | [method_name =\u003e return_value]                          |\n|                     |             | [[method_name =\u003e [return_value1, return_value2 [, ...]]] |\n|`$constructor_params`| false/array | false: disable constructor / array: constructor params |\n\n`returns` (object) PHPUnit mock object.\n\nGets PHPUnit mock object.\n\n```php\n$email = $this-\u003egetMockBuilder(CI_Email::class)\n    -\u003edisableOriginalConstructor()\n    -\u003eonlyMethods(['send'])\n    -\u003egetMock();\n$email-\u003emethod('send')\n    -\u003ewillReturn(true);\n```\n\nYou could write code above like below:\n\n```php\n$email = $this-\u003egetDouble(CI_Email::class, ['send' =\u003e true]);\n```\n\nYou can set Closure as the return value of a mocked method.\n\n```php\n$ret = function () {\n    throw new RuntimeException('Cannot send email!');\n};\n$mock = $this-\u003egetDouble(CI_Email::class, ['send' =\u003e $ret]);\n```\n\nYou can also set the mock itself as the return value of a mocked method with using `$this-\u003ereturnSelf()`.\n\n```php\n$mock = $this-\u003egetDouble(CI_Email::class, [\n    'to'      =\u003e $this-\u003ereturnSelf(),\n    'subject' =\u003e $this-\u003ereturnSelf(),\n    'send'    =\u003e true,\n]);\n```\n\nYou can create mocks with consecutive calls.\n\n```php\n$mock = $this-\u003egetMockBuilder(CI_Email::class)\n    -\u003edisableOriginalConstructor()\n    -\u003eonlyMethods(['method'])\n    -\u003egetMock();\n$mock-\u003eexpects($this-\u003eany())-\u003emethod('method')\n    -\u003ewill($this-\u003eonConsecutiveCalls('GET', 'POST' ,'DELETE'));\n```\n\nYou could write code above like below:\n\n```php\n$mock = $this-\u003egetDouble(\n    CI_Input::class,\n    [\n        ['method' =\u003e ['GET', 'POST' ,'DELETE']],\n    ]\n);\n```\n\n#### `$this-\u003everifyInvoked()`\n\n| param   | type   | description         |\n|---------|--------|---------------------|\n|`$mock`  | object | PHPUnit mock object |\n|`$method`| string | method name         |\n|`$params`| array  | arguments           |\n\nVerifies a method was invoked at least once.\n\n```php\n$loader-\u003eexpects($this-\u003eatLeastOnce())\n    -\u003emethod('view')\n    -\u003ewith(\n        'shopConfirm', $this-\u003eanything(), true\n    );\n```\n\nYou could write code above like below:\n\n```php\n$this-\u003everifyInvoked(\n    $loader,\n    'view',\n    [\n        'shopConfirm', $this-\u003eanything(), true\n    ]\n);\n```\n\n#### `$this-\u003everifyInvokedOnce()`\n\n| param   | type   | description         |\n|---------|--------|---------------------|\n|`$mock`  | object | PHPUnit mock object |\n|`$method`| string | method name         |\n|`$params`| array  | arguments           |\n\nVerifies that method was invoked only once.\n\n```php\n$loader-\u003eexpects($this-\u003eonce())\n    -\u003emethod('view')\n    -\u003ewith(\n        'shopConfirm', $this-\u003eanything(), true\n    );\n```\n\nYou could write code above like below:\n\n```php\n$this-\u003everifyInvokedOnce(\n    $loader,\n    'view',\n    [\n        'shopConfirm', $this-\u003eanything(), true\n    ]\n);\n```\n\n#### `$this-\u003everifyInvokedMultipleTimes()`\n\n| param   | type   | description         |\n|---------|--------|---------------------|\n|`$mock`  | object | PHPUnit mock object |\n|`$method`| string | method name         |\n|`$times` | int    | times               |\n|`$params`| array  | arguments           |\n\nVerifies that method was called exactly $times times.\n\n```php\n$loader-\u003eexpects($this-\u003eexactly(2))\n    -\u003emethod('view')\n    -\u003ewithConsecutive(\n        ['shopConfirm', $this-\u003eanything(), true],\n        ['shopTmplCheckout', $this-\u003eanything()]\n    );\n```\n\nYou could write code above like below:\n\n```php\n$this-\u003everifyInvokedMultipleTimes(\n    $loader,\n    'view',\n    2,\n    [\n        ['shopConfirm', $this-\u003eanything(), true],\n        ['shopTmplCheckout', $this-\u003eanything()]\n    ]\n);\n```\n\n#### `$this-\u003everifyNeverInvoked()`\n\n| param   | type   | description         |\n|---------|--------|---------------------|\n|`$mock`  | object | PHPUnit mock object |\n|`$method`| string | method name         |\n|`$params`| array  | arguments           |\n\nVerifies that method was not called.\n\n```php\n$loader-\u003eexpects($this-\u003enever())\n    -\u003emethod('view')\n    -\u003ewith(\n        'shopConfirm', $this-\u003eanything(), true\n    );\n```\n\nYou could write code above like below:\n\n```php\n$this-\u003everifyNeverInvoked(\n    $loader,\n    'view',\n    [\n        'shopConfirm', $this-\u003eanything(), true\n    ]\n);\n```\n\n### `ReflectionHelper`\n\nThis trait provides helper methods to access private or protected properties and methods.\n\nBut generally it is not recommended testing non-public properties or methods, so think twice before you use the methods in this trait.\n\nImport the `Kenjis\\PhpUnitHelper\\ReflectionHelper` trait into your test class:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace Foo\\Bar\\Test\\Unit;\n\nuse Kenjis\\PhpUnitHelper\\ReflectionHelper;\nuse PHPUnit\\Framework;\n\nfinal class BazTest extends Framework\\TestCase\n{\n    use ReflectionHelper;\n}\n```\n\n#### `$this-\u003egetPrivateProperty()`\n\n| param     | type          | description         |\n|-----------|---------------|---------------------|\n|`$obj`     | object/string | object / class name |\n|`$property`| string        | property name       |\n\n`returns` (mixed) property value.\n\nGets private or protected property value.\n\n~~~php\n$obj = new SomeClass();\n$private_propery = $this-\u003egetPrivateProperty(\n\t$obj,\n\t'privatePropery'\n);\n~~~\n\n#### `$this-\u003esetPrivateProperty()`\n\n| param     | type          | description         |\n|-----------|---------------|---------------------|\n|`$obj`     | object/string | object / class name |\n|`$property`| string        | property name       |\n|`$value`   | mixed         | value               |\n\nSets private or protected property value.\n\n~~~php\n$obj = new SomeClass();\n$this-\u003esetPrivateProperty(\n\t$obj,\n\t'privatePropery',\n\t'new value'\n);\n~~~\n\n#### `$this-\u003egetPrivateMethodInvoker()`\n\n| param   | type          | description         |\n|---------|---------------|---------------------|\n|`$obj`   | object/string | object / class name |\n|`$method`| string        | method name         |\n\n`returns` (closure) method invoker.\n\nGets private or protected method invoker.\n\n~~~php\n$obj = new SomeClass();\n$method = $this-\u003egetPrivateMethodInvoker(\n\t$obj, 'privateMethod'\n);\n$this-\u003eassertEquals(\n\t'return value of the privateMethod() method', $method()\n);\n~~~\n\n### `DebugHelper`\n\nThis trait provides helper functions, `dd()` and `d()` to dump variables.\n\nImport the `Kenjis\\PhpUnitHelper\\DebugHelper` trait into your test class:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace Foo\\Bar\\Test\\Unit;\n\nuse Kenjis\\PhpUnitHelper\\DebugHelper;\nuse PHPUnit\\Framework;\n\nfinal class BazTest extends Framework\\TestCase\n{\n    use DebugHelper;\n}\n```\n\n## License\n\nThis package is licensed using the MIT License.\n\nPlease have a look at [`LICENSE`](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenjis%2Fphpunit-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkenjis%2Fphpunit-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenjis%2Fphpunit-helper/lists"}