hermeslin / mockery-overload-properties
mock instance's properties via Mockery's overload keyword
Requires
- php: >=5.6.0
- mockery/mockery: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.10
- phpunit/phpunit: ^6.5
- squizlabs/php_codesniffer: ^3.2
This package is not auto-updated.
Last update: 2024-04-28 02:32:34 UTC
README
mock instance's properties via Mockery's overload keyword
installation
use composer to install mockery-overload-properties
$ composer require --dev hermeslin/mockery-overload-properties
test legacy sample code
under the example
folder, you can see the sample code taht we want to test:
- User.php
- Vip.php
and you'll find the hard dependencies in Vip.php
.
hard dependencies are not a big deal, you can use Mockery's overload
keyword to mock User
instance easily, but it difficult to mock User
instance properties when User
set the properties on its __construct
phase.
test case
under the tests
folder, there are two test cases here:
- LegacyCodeFailTest.php
- LegacyCodeSuccessTest.php
see tests\LegacyCodeSuccessTest.php
will show you how to use mockery-overload-properties
to mock User
/** * @test */ public function notVipUserBonusShouldCorrect() { $properties = [ 'id' => 2, 'isVip' => false, 'rank' => 99 ]; $user = mop::mock('\User', $properties); // bounus should be 149 $bunus = 100 * 0.5 + 99; $vip = new Vip; $this->assertEquals($bunus, $vip->bonus($userId = 2)); }
when mock instance whith properties, you still can use Expectation Declarations
from Mockery
to test you code.
/** * @test */ public function notVipUserBonusShouldCorrect() { $properties = [ 'id' => 2, 'isVip' => false, 'rank' => 99 ]; $user = mop::mock('\User', $properties); // Expectation $user->shouldReceive('name_of_method'); ->with($arg1, $arg2, ...); ->andReturn($value); // etc... }