{"id":20408336,"url":"https://github.com/phpspec/phpspec-mocks","last_synced_at":"2025-04-12T15:22:10.600Z","repository":{"id":1496841,"uuid":"1749282","full_name":"phpspec/phpspec-mocks","owner":"phpspec","description":"PHPSpec Doubles (mocks and stubs) adapters","archived":false,"fork":false,"pushed_at":"2011-06-11T20:15:43.000Z","size":124,"stargazers_count":7,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T09:51:16.495Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.phpspec.net","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phpspec.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-05-14T22:43:36.000Z","updated_at":"2021-08-04T23:16:24.000Z","dependencies_parsed_at":"2022-08-16T13:25:20.631Z","dependency_job_id":null,"html_url":"https://github.com/phpspec/phpspec-mocks","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/phpspec%2Fphpspec-mocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpspec%2Fphpspec-mocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpspec%2Fphpspec-mocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpspec%2Fphpspec-mocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpspec","download_url":"https://codeload.github.com/phpspec/phpspec-mocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586495,"owners_count":21129049,"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":[],"created_at":"2024-11-15T05:30:13.845Z","updated_at":"2025-04-12T15:22:10.561Z","avatar_url":"https://github.com/phpspec.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PHPSpec Mocks\n=============\n\n**PHPSpec Mocks** is a lightweight mocking framework designed to be used in the\nPHP BDD Framework **PHPSpec**.\n\nExamples\n--------\n\n### 1. Stubbing a method\n\nWrite your spec, replacing the dependency with a stub. Use the 'stub' method to\nadd a stubbed method and 'andReturn' to specify the value you want to be\nreturned: \n\n    \u003c?php\n    \n    class HelloWorldSpec extends \\PHPSpec\\Context\n    {\n        function itGreetsAccordingToTheSetGreeterStrategy()\n        {\n            $greeter = mock('Greeter');\n            $greeter-\u003estub('greet')-\u003eandReturn('Hello, World!');\n            \n            $helloWorld = new HelloWorld($greeter);\n            $this-\u003espec($helloWorld-\u003ehello())-\u003eshould-\u003eequal('Hello, World!');\n        }\n    }\n    \nYou can now write your class:\n\n    \u003c?php\n    class HelloWorld\n    {\n        private $greeter;\n        public function __construct(Greeter $message)\n        {\n            $this-\u003egreeter = $message;\n        }\n        \n        public function hello()\n        {\n            return $this-\u003egreeter-\u003egreet();\n        }\n    }\n\n### 2. Partial stubbing\n\nYou can specify that you want a method to be stubbed only when certain arguments are\npassed:\n\n    \u003c?php\n    class HelloWorldSpec extends \\PHPSpec\\Context\n    {\n        function itGreetsAccordingToTheSetGreeterStrategy()\n        {\n            $greeter = mock('Greeter');\n            $greeter-\u003estub('greet')\n                    -\u003eshouldReceive('Chuck')\n                    -\u003eandReturn('Hello, Chuck!');\n        \n            $helloWorld = new HelloWorld($greeter);\n            $this-\u003espec($helloWorld-\u003ehello('Chuck'))\n                  -\u003eshould-\u003eequal('Hello, Chuck!');\n    }\n\n### 3. Stubbing a property\n\nIt is possible to stub a property in the same manner:\n\n    \u003c?php\n    class HelloWorldSpec extends \\PHPSpec\\Context\n    {\n        function itKnowsWhoItIsGreeting() // the class who knew too much?\n        {\n            $greeter = mock('Greeter');\n            $greeter-\u003estub('who')-\u003eandReturn('Chuck');\n            \n            $helloWorld = new HelloWorld($greeter);\n            $this-\u003espec($helloWorld-\u003egreetingWho())-\u003eshould-\u003eequal('Chuck');\n        }\n    }\n    \n    \u003c?php\n    class HelloWorld\n    {\n        ...\n        public function greetingWho()\n        {\n            return $this-\u003egreeter-\u003ewho;\n        }\n    }\n\n### 4. Shortcut for creating a double\n\nIt is possible to create a double and stubbing methods/properties all at once.\nSo the method in the previous example could be replaced by\n\n    \u003c?php\n    ...\n    function itKnowsWhoItIsGreeting() // the class who knew too much?\n    {\n        $greeter = mock('Greeter', array('who' =\u003e 'Chuck');\n\n        $helloWorld = new HelloWorld($greeter);\n        $this-\u003espec($helloWorld-\u003egreetingWho())-\u003eshould-\u003eequal('Chuck');\n    }\n\n### 5. Empty doubles\n\nIf your interface is type agnostic you can even create a mock without\ngiving it a name\n\n    \u003c?php\n    ...\n    function itGreetsAccordingToTheSetGreeterStrategy()\n    {\n        $greeter = mock(); // \u003c-- no Greeter here\n        $greeter-\u003estub('greet')-\u003eandReturn('Hello, World!');\n\n        $helloWorld = new HelloWorld($greeter);\n        $this-\u003espec($helloWorld-\u003ehello())-\u003eshould-\u003eequal('Hello, World!');\n    }\n    \n    ...\n    // HelloWorld constructor:\n    public function __construct($greeter) // \u003c-- no type hinting\n\n### 6. Counters\n\nIt is also possible to specify mocks that expect a method/property to be\naccessed a specified number of times:\n\n    \u003c?php\n    ...\n    // property will be accessed exactly 1 time\n    $greeter-\u003estub('who')-\u003eandReturn('Chuck')-\u003eexactly(1);\n    \n    // property will never be accessed\n    $greeter-\u003estub('who')-\u003enever();\n    \n    // property will be accessed any number of times\n    $greeter-\u003estub('who')-\u003eandReturn('Chuck');","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpspec%2Fphpspec-mocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpspec%2Fphpspec-mocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpspec%2Fphpspec-mocks/lists"}