Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mockery

 Mockery

A short talk about Mockery, how to start using it, what are it's different parts, some gotchas and real-life examples that are bit more complicated than what fits into the official documentation.

Presented at the phpnl15 uncon.

robertbasic

January 24, 2015
Tweet

More Decks by robertbasic

Other Decks in Programming

Transcript

  1. Mockery

    @robertbasic

    https://github.com/robertbasic/

    http://robertbasic.com/

    View full-size slide

  2. Problems with unit testing

    Code that uses 3rd party services and APIs

    Databases

    Parts of our code that are not relevant to the
    code we are testing

    View full-size slide

  3. How do we fix it?

    Avoid calls to the database

    Avoid calls to 3rd party services and APIs

    Avoid calls to parts of our own code

    Mock out the moving bits

    View full-size slide

  4. What to use?

    A mocking framework

    PHPUnit's built-in mock stuff

    FBMock

    Phake

    ...

    View full-size slide

  5. What's Mockery?

    A framework to mock objects

    Easy to use with PHPUnit (and maybe other
    unit testing tools?)

    Makes mocking a lot saner than the built-in
    PHPUnit stuff

    Developed by @padraicb and lots of help from
    @davedevelopment

    View full-size slide

  6. Installing

    composer require mockery/mockery

    https://github.com/padraic/mockery

    View full-size slide

  7. Connect with PHPUnit

    Add the listener in phpunit.xml

    View full-size slide

  8. Bootstrap

    Set up autoloading and stuff

    View full-size slide

  9. A simple example

    Foo has a dependency on Bar

    Foo calls a method on Bar that returns
    something

    We don't want to call the real Bar's method,
    but a mock

    This gets more complicated in real life

    View full-size slide

  10. A simple example

    View full-size slide

  11. Expectations for method calls

    shouldReceive('methodName');

    once(), twice(), zeroOrMoreTimes(), times(n),
    never()

    If no expectation is set on the number of calls,
    it will surprise you.

    View full-size slide

  12. Expectations for method calls

    View full-size slide

  13. Argument validation

    with($param1, $param2);

    withAnyArgs();

    withNoArgs();

    with(m::type('\Foo\Bar'));

    with(m::on(closure));

    with($regex);

    View full-size slide

  14. Return values

    andReturn($value);

    andReturn($firstRun, $secondRun, $thirdRun);

    andReturnUsing(function($x) { return $x * 2 });

    andThrow(Exception);

    View full-size slide

  15. But you can also read all that in the
    documentation

    View full-size slide

  16. Some REAL examples

    View full-size slide

  17. Don't touch the database!

    View full-size slide

  18. Don't call that service!

    View full-size slide

  19. Don't call that code!

    View full-size slide

  20. Mocking hard dependencies

    You know all those “new” statements in your
    legacy code?

    http://robertbasic.com/blog/mocking-hard-
    dependencies-with-mockery

    https://github.com/robertbasic/mockery-
    hard-dependency

    View full-size slide

  21. Closure to validate arguments

    View full-size slide

  22. Regex to validate arguments

    View full-size slide

  23. Crazy bits

    Doesn't do a diff on method arguments that
    are arrays

    Reflection can go nuts with PHP classes

    __magic methods... yea, don't.

    View full-size slide

  24. Thanks!

    Questions?

    @robertbasic

    https://github.com/padraic/mockery

    View full-size slide