2017 © Pedro Peláez
 

library pt_mock

Easy mock for php testing

image

trilopin/pt_mock

Easy mock for php testing

  • Saturday, March 7, 2015
  • by trilopin
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4,306 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

Build Status Build status Scrutinizer Code Quality Code Coverage, (*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)

The Versions

07/03 2015

dev-master

9999999-dev

Easy mock for php testing

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gabi Ramos

05/01 2015

dev-log-psr3

dev-log-psr3

Easy mock for php testing

  Sources   Download

MIT

The Requires

  • php >=5.3.6

 

The Development Requires

by Gabi Ramos