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

Mock Mock Mock - Mocking-Frameworks

Mock Mock Mock - Mocking-Frameworks

Mocks in unit tests. You can't live with them. You can't live without them. You need them. But as you plan on how to test your code, you also should ask yourself which Mocking-Library do you want to use. New project, legacy code or code that is hard to test: depending on the project, some libraries can be really helpful to build up your tests and save time. This talk is about common Mocking-Libraries and their (dis)advantages.
The talk of those slides were part of PHPUGKA Oct. 22nd. 2015.

Claudio Zizza

October 22, 2015
Tweet

More Decks by Claudio Zizza

Other Decks in Technology

Transcript

  1. Mock Mock Mock
    Mock Mock Mock
    Mocking-Frameworks
    Mocking-Frameworks
    Claudio Zizza
    @SenseException

    View Slide

  2. Test your code!
    (Duh!)

    View Slide

  3. Tests & Dependencies

    View Slide

  4. Dependencies
    I only want to test/run this
    small part :-(

    View Slide

  5. Test Doubles
    Mostly used in tests:

    Dummies

    Stubs

    Mocks
    (We do only Mocks for now)

    View Slide

  6. PHPUnit
    $mockFw = new PHPUnit_Framework_MockObject_Generator();
    $math = $mockFw->getMock(MathInterface::class);
    $math->expects(new
    PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce())
    ->method('sum')
    ->with(1, 1)
    ->willReturn(2);

    View Slide

  7. Prophecy
    $mockFw = new Prophet();
    $prophecy = $mockFw->prophesize(MathInterface::class);
    $prophecy->sum(1, 1)
    ->willReturn(2)
    ->shouldBeCalledTimes(1);
    $math = $prophecy->reveal();

    View Slide

  8. Mockery
    $math = Mockery::mock(MathInterface::class);
    $math->shouldReceive('sum')
    ->once()
    ->with(1, 1)
    ->andReturn(2);

    View Slide

  9. vfsStream
    baseDir = vfsStream::setup('dir');
    mkdir(vfsStream::url('dir') . '/' . 'myDir');
    Mooooock?

    View Slide

  10. bovigo/callmap
    math = NewInstance::of(MathInterface::class)
    ->mapCalls(['sum' => 2]);
    // ...
    verify($math, 'sum')->wasCalledOnce();

    View Slide

  11. Phake
    $math = Phake::mock(MathInterface::class);
    Phake::when($math)->sum(1, 1)->thenReturn(2);
    // ...
    Phake::verify($math, Phake::times(1))->sum(1, 1);

    View Slide

  12. Examples
    Let's mock mock mock:
    https://github.com/SenseException/mock-mock-mock

    View Slide

  13. Thank you
    Thank you
    Claudio Zizza
    @SenseException

    View Slide