Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Test Proxies #132

Closed
sebastianbergmann opened this Issue · 0 comments

1 participant

@sebastianbergmann

Background

Thanks to Gerard Meszaros, we have the following (names for) categories of test doubles:

Of these, PHPUnit has had built-in support for stubs and mocks for quite some time.

The stubs and mocks generated by PHPUnit(_MockObject) can be used in every context where an object of the original class is expected. As it should be, the code of the original class is not executed when a method is called on the stub or mock.

Motivation for Test Proxies

When writing integration tests in general and edge-to-edge tests in particular, you generally do not want to stub or mock dependencies. Sometimes, though, the ability to have expectations such as "this method has to be called with these arguments for this test to be successful" would also be useful in tests that have a larger scope than unit tests.

The idea is to have a "Test Proxy" object that provides the same API for expectations as a mock object while at the same time proxying method calls to the original class.

Usage Example

Foo.php

<?php
class Foo
{
    public function doSomething(Bar $bar)
    {
        return $bar->doSomethingElse();
    }
}

Bar.php

<?php
class Bar
{
    public function doSomethingElse()
    {
        return 'result';
    }
}

FooTest.php

<?php
class FooTest extends PHPUnit_Framework_TestCase
    public function testSomething()
    {
        $proxy = $this->getMockBuilder('Bar')
                      ->enableProxyingToOriginalMethods()
                      ->getMock();

        $proxy->expects($this->once())
              ->method('doSomethingElse');

        $foo = new Foo;
        $this->assertEquals('result', $foo->doSomething($proxy));
    }
}
@theseer theseer referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.