Created
March 15, 2016 13:51
-
-
Save axiac/467735db1db9992e5780 to your computer and use it in GitHub Desktop.
Sample test case for sebastianbergmann/phpunit-mock-objects issue #260
This file contains 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 | |
/** | |
* Sample test case for https://github.com/sebastianbergmann/phpunit-mock-objects/issues/260 | |
*/ | |
interface Dependency | |
{ | |
/** | |
* @param string $type | |
* @param array $extra | |
* @return array | |
*/ | |
public function get($type, array $extra); | |
} | |
class TestedClass | |
{ | |
/** @var Dependency $dep */ | |
private $dep; | |
/** | |
* Constructor | |
* | |
* @param Dependency $dep | |
*/ | |
public function __construct(Dependency $dep) | |
{ | |
$this->dep = $dep; | |
} | |
/** | |
* @return array | |
*/ | |
public function doSomething() | |
{ | |
$first = $this->dep->get('a', array('foo' => 'bar')); | |
$second = $this->dep->get('b', array('baz')); | |
return array_merge($first, $second); | |
} | |
} | |
/** | |
* Class Issue260Test - Sample test case for https://github.com/sebastianbergmann/phpunit-mock-objects/issues/260 | |
*/ | |
final class Issue260Test extends PHPUnit_Framework_TestCase | |
{ | |
/** @var Dependency|PHPUnit_Framework_MockObject_MockObject $stub */ | |
private $stub; | |
public function setUp() | |
{ | |
// Call the parent class first | |
parent::setUp(); | |
// Prepare the stub; set up the calls that are common to all tests | |
$this->stub = $this->getMock('Dependency'); | |
$this->stub | |
->expects(self::any()) | |
->method('get') | |
->with(self::identicalTo('a'), self::anything()) | |
->willReturn(array(1, 2, 3)) | |
; | |
} | |
public function testSomething() | |
{ | |
// Prepare the stub to respond to the request issued by TestedClass::doSomething() | |
$this->stub | |
->expects(self::any()) | |
->method('get') | |
->with(self::identicalTo('b'), self::anything()) | |
->willReturn(array(4, 5)) | |
; | |
$object = new TestedClass($this->stub); | |
$result = $object->doSomething(); | |
self::assertSame(array(1, 2, 3, 4, 5), $result); | |
} | |
} | |
// This is the end of file; no closing PHP tag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment