{"id":13669284,"url":"https://github.com/danrevah/shortify-punit","last_synced_at":"2025-07-17T02:12:33.211Z","repository":{"id":23765050,"uuid":"27139736","full_name":"danrevah/shortify-punit","owner":"danrevah","description":"🔨 PHP Mocking Framework, includes method chaining stubbing.","archived":false,"fork":false,"pushed_at":"2017-06-25T10:24:10.000Z","size":144,"stargazers_count":32,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T10:21:17.569Z","etag":null,"topics":["mock","mock-library","php","php-mocking-framework","php7"],"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/danrevah.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-25T18:25:15.000Z","updated_at":"2024-03-03T19:31:11.000Z","dependencies_parsed_at":"2022-08-21T09:20:59.726Z","dependency_job_id":null,"html_url":"https://github.com/danrevah/shortify-punit","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danrevah%2Fshortify-punit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danrevah%2Fshortify-punit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danrevah%2Fshortify-punit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danrevah%2Fshortify-punit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danrevah","download_url":"https://codeload.github.com/danrevah/shortify-punit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248767926,"owners_count":21158559,"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":["mock","mock-library","php","php-mocking-framework","php7"],"created_at":"2024-08-02T08:01:08.533Z","updated_at":"2025-04-13T19:31:39.760Z","avatar_url":"https://github.com/danrevah.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# ShortifyPunit \u0026nbsp; \n[![Build Status](https://scrutinizer-ci.com/g/danrevah/ShortifyPunit/badges/build.png?b=master)](https://scrutinizer-ci.com/g/danrevah/ShortifyPunit/build-status/master) [![Coverage Status](https://coveralls.io/repos/danrevah/ShortifyPunit/badge.svg?branch=master)](https://coveralls.io/repos/danrevah/ShortifyPunit/badge.svg?branch=master) \n![Code Quality](https://scrutinizer-ci.com/g/danrevah/ShortifyPunit/badges/quality-score.png?b=master)  [![Latest Stable Version](https://poser.pugx.org/danrevah/shortifypunit/v/stable.svg)](https://packagist.org/packages/danrevah/shortifypunit) \n\n\u003e PHP Mocking Framework, \n\u003e inspired by Mockito library for Java\n\n## Table of contents\n\n * [Installation](#installation)\n * [Mocking](#mocking-examples)\n * [Stubbing](#stubbing)\n * [Partial Mock](#partial-mock)\n * [Stubbing Method Chaining](#stubbing-method-chaining)\n * [Verifying](#verifying)\n * [Argument Matcher](#argument-matcher)\n\n## Installation\n\n\u003e PHP Version \u003e= 5.4 is required!\n\nThe following instructions outline installation using Composer. If you don't\nhave Composer, you can download it from [http://getcomposer.org/](http://getcomposer.org/)\n\n```\n$ composer require \"danrevah/shortifypunit\":\"dev-master\" \n$ php composer.phar require \"danrevah/shortifypunit\":\"dev-master\"\n```\n\n## Mocking Examples\n```php\n// Creating a new mock for SimpleClassForMocking\n$mock = ShortifyPunit::mock('SimpleClassForMocking');\n\n// Returns NULL, was not stubbed yet\n$mock-\u003efirst_method();\n```\n\nBasic mocking example, if a function wasn't stubbed the return value will always be `NULL`.\n\n## Stubbing\n```php\n// Creating a new mock for SimpleClassForMocking\n$mock = ShortifyPunit::mock('SimpleClassForMocking');\n\n// Stubbing first_method() function without arguments\nShortifyPunit::when($mock)-\u003efirst_method()-\u003ereturns(1);\necho $mock-\u003efirst_method(); // prints '1'\n\n// Stubbing first_method() function with arguments\nShortifyPunit::when($mock)-\u003efirst_method(1,2)-\u003ereturns(2);\necho $mock-\u003efirst_method(); // still prints '1'\necho $mock-\u003efirst_method(1,2); // prints '2'\n\n// Stubbing callback\nShortifyPunit::when($mock)-\u003efirst_method()-\u003ecallback(function() { echo 'Foo Bar'; });\necho $mock-\u003efirst_method(); // prints 'Foo Bar'\n\n// Stubbing throws exception\nShortifyPunit::when($mock)-\u003esecond_method()-\u003ethrows(new Exception());\n$mock-\u003esecond_method(); // throws Exception\n```\nThe `when` function is used to stubbing methods with specific parameters, following a `throws`, `returns` or a `callback` action.\n\nMethods:\n* `throws($exception)` - Throws an exception\n* `returns($response)` - Returns a $response\n* `callback(function() { /*...*/ })` - Calling a callback\n\n## Partial Mock\n\npartial mock is used when you need some of the methods to behave normally except from that one method you need to test. that can be done with partial mock, it keeps the logic unless you stub the method.\n\n```php\nclass Foo {\n  function bar() { return 'bar'; }\n}\n\n$mock = ShortifyPunit::mock('Foo');\n$partialMock = ShortifyPunit::partialMock('Foo');\n\n$mock-\u003ebar(); // returns NULL\necho $partialMock-\u003ebar(); // prints 'bar'\n\nShortifyPunit::when($partialMock)-\u003ebar()-\u003ereturns('foo'); // stubbing partialMock\necho $partialMock-\u003ebar(); // prints 'foo'\n```\n\n## Stubbing Method Chaining\n```php\n // Creating a new mock for SimpleClassForMocking\n $mock = ShortifyPunit::mock('SimpleClassForMocking');\n\n  ShortifyPunit::when($mock)-\u003efirst_method()-\u003esecond_method(1)-\u003ereturns(1);\n  ShortifyPunit::when($mock)-\u003efirst_method()-\u003esecond_method(2)-\u003ereturns(2);\n  ShortifyPunit::when($mock)-\u003efirst_method(1)-\u003esecond_method(1)-\u003ereturns(3);\n  ShortifyPunit::when($mock)-\u003efirst_method(2)-\u003esecond_method(2)-\u003ethird_method()-\u003ereturns(4);\n  \n  echo $mock-\u003efirst_method()-\u003esecond_method(1); // prints '1'\n  echo $mock-\u003efirst_method()-\u003esecond_method(2); // prints '2'\n  echo $mock-\u003efirst_method(1)-\u003esecond_method(1); // prints '3'\n  echo $mock-\u003efirst_method(2)-\u003esecond_method(2)-\u003ethird_method(); // prints '4'\n```\n`when` function is also used to stub chained methods, follows the same actions as the single function stubbing `return`, `throw` or `callback`.\n\n## Verifying\n\nOnce created, mock will remember all invocations. Then you can selectively verify some interaction you are inserted in.\n\n```php\n    $mock = ShortifyPunit::mock('SimpleClassForMocking');\n\n    ShortifyPunit::when($mock)-\u003efirst_method()-\u003ereturns(1);\n    echo $mock-\u003efirst_method(); // method called once\n\n    ShortifyPunit::verify($mock)-\u003efirst_method()-\u003eneverCalled(); // returns FALSE\n    ShortifyPunit::verify($mock)-\u003efirst_method()-\u003eatLeast(2); // returns FALSE\n    ShortifyPunit::verify($mock)-\u003efirst_method()-\u003ecalledTimes(1); // returns TRUE\n\n    echo $mock-\u003efirst_method(); // method has been called twice\n\n    ShortifyPunit::verify($mock)-\u003efirst_method()-\u003eneverCalled(); // returns FALSE\n    ShortifyPunit::verify($mock)-\u003efirst_method()-\u003eatLeast(2); // returns TRUE\n    ShortifyPunit::verify($mock)-\u003efirst_method()-\u003ecalledTimes(2); // returns TRUE\n```\n\nMethods:\n* `atLeast($times)` - Verify called at least $times\n* `atLeastOnce()` - Alias of atLeast(1)\n* `calledTimes($times)` - Verify called exactly $times\n* `neverCalled()` - Alias of calledTimes(0)\n* `lessThan($times)` - Verify called less than $times\n\n## Argument Matcher\n\nShortifyPunit allows the use of Hamcrest PHP (https://github.com/hamcrest/hamcrest-php) matcher on any argument. Hamcrest is a library of \"matching functions\" that, given a value, return true if that value\nmatches some rule.\n\nHamcrest matchers are included by default.\n\nExamples:\n\n```php\nclass Foo\n{\n  function bar($arg){}\n}\n\n$stub = ShortifyPunit::mock('Foo');\nShortifyPunit::when($stub)-\u003ebar(anything())-\u003ereturn('FooBar');\n```\n\nSome common Hamcrest matchers:\n\n- Core\n\t* `anything` - always matches, useful if you don't care what the object under test is\n- Logical\n\t* `allOf` - matches if all matchers match, short circuits (like PHP \u0026\u0026)\n\t* `anyOf` - matches if any matchers match, short circuits (like PHP ||)\n\t* `not` - matches if the wrapped matcher doesn't match and vice versa\n- Object\n\t* `equalTo` - test object equality using the == operator\n\t* `anInstanceOf` - test type\n\t* `notNullValue`, `nullValue` - test for null\n- Number\n\t* `closeTo` - test floating point values are close to a given value\n\t* `greaterThan`, `greaterThanOrEqualTo`, `lessThan`, `lessThanOrEqualTo` - test ordering\n- Text\n\t* `equalToIgnoringCase` - test string equality ignoring case\n\t* `equalToIgnoringWhiteSpace` - test string equality ignoring differences in runs of whitespace\n\t* `containsString`, `endsWith`, `startsWith` - test string matching\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanrevah%2Fshortify-punit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanrevah%2Fshortify-punit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanrevah%2Fshortify-punit/lists"}