Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Test Proxies #132

@sebastianbergmann

Description

@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));
    }
}

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions