, (*1)
pt_mock
This class works with PHP >= 5.3.6.
In order to run the tests, phpunit is required., (*2)
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., (*3)
Source in [https://github.com/gramos74/pt_mock], (*4)
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'., (*5)
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 :, (*6)
$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:, (*7)
$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., (*8)
$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., (*9)
$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 !, (*10)
License
Copyright (c) 2011 Released under the MIT license (see MIT-LICENSE)
Gabriel Ramos gabi@gabiramos.com, (*11)