{"id":16831599,"url":"https://github.com/jasonmccreary/test-double","last_synced_at":"2025-04-11T04:33:17.262Z","repository":{"id":56996858,"uuid":"109888375","full_name":"jasonmccreary/test-double","owner":"jasonmccreary","description":"A simple helper method to make using Mockery easier.","archived":false,"fork":false,"pushed_at":"2017-11-14T16:00:32.000Z","size":6,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T02:40:17.696Z","etag":null,"topics":["mockery","phpunit"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasonmccreary.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":"2017-11-07T20:41:00.000Z","updated_at":"2022-05-21T17:59:19.000Z","dependencies_parsed_at":"2022-08-21T14:10:57.891Z","dependency_job_id":null,"html_url":"https://github.com/jasonmccreary/test-double","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonmccreary%2Ftest-double","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonmccreary%2Ftest-double/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonmccreary%2Ftest-double/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonmccreary%2Ftest-double/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasonmccreary","download_url":"https://codeload.github.com/jasonmccreary/test-double/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345202,"owners_count":21088231,"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":["mockery","phpunit"],"created_at":"2024-10-13T11:44:35.582Z","updated_at":"2025-04-11T04:33:17.200Z","avatar_url":"https://github.com/jasonmccreary.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Test Double\nTired of remembering the difference between mocks, partials, and spies in Mockery? I am, which is why I created `double()` - a simple helper method to make using Mockery easier.\n\nWhen writing tests I don't want to think about the differences between _fakes_, _mocks_, and _spies_. I just to create a generic _test double_ and focus on writing my test. This generalization is common in other testing frameworks such as [RSpec](https://relishapp.com/rspec/rspec-mocks/docs/basics/test-doubles), [td.js](https://github.com/testdouble/testdouble.js), and more.\n\n## Installation\nTo install the latest version of the `double()` helper, run the command:\n\n```sh\ncomposer require --dev jasonmccreary/test-double\n```\n\n## Usage\nAnytime you need to create a _test double_ simply call `double()`\n\nBy default, `double()` returns an object that will allow you to stub methods as well as verify method calls.\n\n```php\n\u003c?php\n$td = double();\n\n$td-\u003eshouldReceive('someMethod')-\u003eandReturn(5);\n\n$td-\u003esomeMethod();       // returns 5\n$td-\u003eunstubbedMethod();  // returns null, does not throw an exception\n\n$td-\u003eanotherMethod();\n$td-\u003eshouldHaveReceived('anotherMethod');\n```\n\nIn Mockery, this _test double_ is equivalent to `Mockery::mock()-\u003eshouldIgnoreMissing()` or, in recent versions, `Mockery::spy()`.\n\nYou can also pass `double()` a reference to a class or interface. This will create a test object that extends the class or implements the interface. This allows the double to pass any type hints or type checking in your implementation.\n\n```php\n\u003c?php\n$td = double(Str::class);\n\n$td-\u003eshouldReceive('length')-\u003eandReturn(5);\n\n$td-\u003elength();           // 5\n$td-\u003esubstr(1, 3);       // null\n\n$td instanceof Str;      // true\n\n$td-\u003eshouldHaveReceived('substr')-\u003ewith(1, 3);\n```\n\nFinally, `double()` accepts a second argument of _passthru_. By default, _passthru_ is `false`. When set to `true`, the test object will pass any method calls through to the underlying object.\n\nIn Mockery, this is equivalent to `Mockery::mock(Number::class)-\u003eshouldDeferMissing()`.\n\n```php\n\u003c?php\nclass Number\n{\n    public function one()\n    {\n        return 1;\n    }\n\n    public function random()\n    {\n        return 5;\n    }\n}\n\n$td = double(Number::class, true);\n\n$td-\u003eshouldReceive('random')-\u003eandReturn(21);\n\n$td-\u003erandom();            // 21\n$td-\u003eone();               // 1\n\n$td instanceof Number;    // true\n\n$td-\u003eshouldHaveReceived('one');\n```\n\nNote: _passthru_ can only be used when creating a test double with a class reference as that is the only time an underlying implementation exists.\n\nIn the end, `double()` is an opinionated way to create test objects for your underlying code. If it does not meet your needs, you can always create a `Mockery::mock()` directly. However, doing so is likely a smell you're testing your implementation in a way that does not reflect real world behavior. Remember, `double()` returns an object which implements the `MockeryInterface`. So it can be treated as any other `Mockery::mock()` object.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonmccreary%2Ftest-double","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasonmccreary%2Ftest-double","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonmccreary%2Ftest-double/lists"}