Created
June 2, 2011 15:26
-
-
Save benjaminhawkeslewis/1004638 to your computer and use it in GitHub Desktop.
Trying to set expectations for a cloned object in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
phpunit --debug test.php | |
PHPUnit 3.5.13 by Sebastian Bergmann. | |
Starting test 'Cloner_Test::testFunctionThatClones'. | |
F | |
Time: 1 second, Memory: 6.50Mb | |
There was 1 failure: | |
1) Cloner_Test::testFunctionThatClones | |
Expectation failed for method name is equal to <string:__clone> when invoked 1 time(s). | |
Method was expected to be called 1 times, actually called 0 times. | |
FAILURES! | |
Tests: 1, Assertions: 1, Failures: 1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load PHPUnit out of our include path (e.g. from a PEAR install) | |
include 'PHPUnit/Autoload.php'; | |
class Clonable | |
{ | |
public function functionOnClonable() | |
{ | |
} | |
} | |
// This is our system under test | |
class Cloner | |
{ | |
public function functionThatClones(Clonable $clonable) | |
{ | |
$clone = clone $clonable; | |
$clone->functionOnClonable(); | |
} | |
} | |
class Cloner_Test extends PHPUnit_Framework_TestCase | |
{ | |
public function testFunctionThatClones() | |
{ | |
$cloner = new Cloner(); // system under test | |
$mockClonable = $this->getMockBuilder('Clonable') | |
->getMock(); | |
$mockClone = $this->getMockBuilder('Clonable') | |
->getMock(); | |
// Try to return $mockClone when functionThatClones | |
// clones our mock Clonable | |
$mockClonable->expects($this->once()) | |
->method('__clone') | |
->will($this->returnValue($mockClone)); | |
$mockClone->expects($this->once()) | |
->method('functionOnClonable'); | |
$cloner->functionThatClones($mockClonable); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment