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

Test Proxies #132

Closed
sebastianbergmann opened this issue Jul 10, 2013 · 1 comment
Closed

Test Proxies #132

sebastianbergmann opened this issue Jul 10, 2013 · 1 comment
Assignees
Milestone

Comments

@sebastianbergmann
Copy link
Owner

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

nickhen commented Sep 11, 2016

Sorry for the stupid question, but I am a newbie to phpunit. Are phpunit Proxies considered to be Dummy, Fake, Stub, Mock, or Spy objects???? Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants