trilopin/pt_mock
Composer 安装命令:
composer require trilopin/pt_mock
包简介
Easy mock for php testing
README 文档
README
pt_mock
This class works with PHP >= 5.3.6. In order to run the tests, phpunit is required.
This class provides a way to mock objects when testing. Makes it simple to simulate method calls returning data based on parameters. Can raise exceptions instead of return data.
Source in [https://github.com/gramos74/pt_mock]
Examples
We have a method class that receives a object as parameter. Inside this method we will call the object method 'method_mock' with parameter 'a' and we expect that it returns 'b'.
class class_a { function my_method(class_b) { return class_b->method_mock('a'); } } $class_b = new \Pt\Mock('Class B'); $class_b->expects('method_mock')->with('a')->returns('b'); $class_a = new class_a(); echo $class_a->my_method($class_b); // ----> 'b'
To check that all expectations have been accomplished :
$class_b->verify(); // for a instance of pt_mock
\Pt\Mock::verifyAll(); // for all mocks instantiated
If you want to test that the method is called two times:
$class_b->expects('method_mock')->with('a')->times(2)->returns('b');
Sometimes you don't want to test if a method is called, you only want that if a method is called the mock object returns a value based on parameters.
$class_b->stubs('method_mock')->with('a')->returns('b'); echo $class_b->method_mock('a') ---> 'b' echo $class_b->method_mock('a') ---> 'b' echo $class_b->method_mock('a') ---> 'b' echo $class_b->method_mock('a') ---> 'b'
And sometimes you want to raise a exception instead of to return data.
$class_b->stubs('method_mock')->with('a')->raises(new Exception());
echo $class_b->method_mock('a') ---> raises a exception
Credits
Thanks a lot to Mathias Biilmann [http://mathias-biilmann.net/] by the original idea. Was a awesome experience work together !
License
Copyright (c) 2011 Released under the MIT license (see MIT-LICENSE) Gabriel Ramos gabi@gabiramos.com
统计信息
- 总下载量: 8.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-05