jelito / devstack
此包的最新版本(v1.2.0)没有可用的许可信息。
v1.2.0
2015-08-11 17:51 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 16:25:52 UTC
README
验证
异常来自命名空间 \Jelito\DevStack\Mocker\Exception
- StaticMethodException - 尝试模拟静态方法
- PrivateMethodException - 尝试模拟私有方法
- ProtectedMethodException - 尝试模拟受保护的方法
- NonExistentMethodException - 尝试模拟未知方法
- FinalMethodException - 尝试模拟最终方法
- UnknownVerifyMethodException - 尝试验证未模拟的方法
- UndeclaredMethodInvocationException - 尝试调用未模拟的方法
返回值
返回值
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $mocker->mockMethod('sum')->willReturn(666); // create mocked object $mockedObject = $mocker->createMock(); // test it $this->assertEquals(666, $mockedObject->sum(1, 2));
抛出异常
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $expectedException = new Exception('test'); $mocker->mockMethod('sum')->willThrow($expectedException); // create mocked object $mockedObject = $mocker->createMock(); // test it try { $mockedObject->sum(1, 2); } catch (Exception $actualException) { $this->assertSame($expectedException, $actualException); }
返回自身 - 返回 $this
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $mocker->mockMethod('sum')->willReturnSelf(); // create mocked object $mockedObject = $mocker->createMock(); // test it $this->assertSame($mockedObject, $mockedObject->sum(1));
返回参数 - 返回传递给函数的参数
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $mocker->mockMethod('sum')->willReturnArgument(1); // create mocked object $mockedObject = $mocker->createMock(); // test it $this->assertEquals(1, $mockedObject->sum(1)); $this->assertEquals(2, $mockedObject->sum(2)); $this->assertEquals(3, $mockedObject->sum(3));
调用回调
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $mocker->mockMethod('sum')->willCallback(function ($a, $b) { return $a * $b; }); // create mocked object $mockedObject = $mocker->createMock(); // test it $this->assertEquals(2, $mockedObject->sum(1, 2)); $this->assertEquals(12, $mockedObject->sum(3, 4)); $this->assertEquals(30, $mockedObject->sum(5, 6));
发布资产
调用次数
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $mocker->mockMethod('sum')->willReturn(666); // create mocked object $mockedObject = $mocker->createMock(); // test it $mocker->verifyMethod('sum')->calledNever(); # method sum() was never called $mockedObject->sum(1, 2); # called first time $mocker->verifyMethod('sum')->calledOnce(); $mockedObject->sum(3, 4); # called second time $mocker->verifyMethod('sum')->calledExactly(2);
调用期望的参数
// create mocker, $this is instance of PHPUnit_Framework_TestCase $mocker = new Mocker('MockBuilderTestClass', $this); // mock method $mocker->mockMethod('sum')->willReturn(666); // create mocked object $mockedObject = $mocker->createMock(); // call it $mockedObject->sum(1, 2); $mockedObject->sum(3, 4); $mockedObject->sum(5, 6); // test it $mocker->verifyMethod('sum')->invocationNo(1)->expectedParams(1, 2); # first call with params 1 and 2 $mocker->verifyMethod('sum')->invocationNo(2)->expectedParams(3, 4); # second call with params 3 and 4 $mocker->verifyMethod('sum')->invocationNo(3)->expectedParams(5, 6); # third call with params 5 and 6 $mocker->verifyMethod('sum')->invocationNo(-1)->expectedParams(5, 6); # last call with params 5 and 6 $mocker->verifyMethod('sum')->invocationNo(-2)->expectedParams(3, 4); # second call from end with params 3 and 4 $mocker->verifyMethod('sum')->invocationNo(-3)->expectedParams(1, 2); # third call from end with params 1 and 2