-
-
Save havvg/8c40af35252fc4da4f6a to your computer and use it in GitHub Desktop.
PHPUnit 3.7.8 same object with SplObjectStorage
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 | |
class DemoTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testDemo() | |
{ | |
$entry = $this->getMock('stdClass'); | |
$list = $this->getMock('stdClass'); | |
$listEntry = $this->getMock('stdClass'); | |
$listEntryData = $this->getMock('stdClass'); | |
$storage = $this->getMock('SplObjectStorage'); | |
$storage | |
->expects($this->at(0)) | |
->method('contains') | |
->with($this->identicalTo($entry)) | |
->will($this->returnValue(true)) | |
; | |
$storage | |
->expects($this->at(1)) | |
->method('offsetGet') | |
->with($this->identicalTo($entry)) | |
->will($this->returnValue($list)) | |
; | |
$storage | |
->expects($this->at(2)) | |
->method('detach') | |
->with($this->identicalTo($entry)) | |
; | |
$this->expectIterator($storage, array($listEntry), false, 3); | |
// offsetGet within foreach | |
$storage | |
->expects($this->any()) | |
->method('offsetGet') | |
->with($this->identicalTo($listEntry)) | |
->will($this->returnValue($listEntryData)) | |
; | |
// Executing code | |
if (!$storage->contains($entry)) { | |
throw InvalidArgumentException(); | |
} | |
$data = $storage[$entry]; | |
$storage->detach($entry); | |
foreach ($storage as $eachList) { | |
$listData = $storage[$eachList]; | |
} | |
} | |
/** | |
* @link https://gist.github.com/2852498 | |
*/ | |
public function expectIterator($mock, array $content, $withKey = false, $counter = 0) | |
{ | |
$mock | |
->expects($this->at($counter)) | |
->method('rewind') | |
; | |
foreach ($content as $key => $value) { | |
$mock | |
->expects($this->at(++$counter)) | |
->method('valid') | |
->will($this->returnValue(true)) | |
; | |
$mock | |
->expects($this->at(++$counter)) | |
->method('current') | |
->will($this->returnValue($value)) | |
; | |
if ($withKey) { | |
$mock | |
->expects($this->at(++$counter)) | |
->method('key') | |
->will($this->returnValue($key)) | |
; | |
} | |
$mock | |
->expects($this->at(++$counter)) | |
->method('next') | |
; | |
} | |
$mock | |
->expects($this->at(++$counter)) | |
->method('valid') | |
->will($this->returnValue(false)) | |
; | |
return ++$counter; | |
} | |
} |
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
/tmp phpunit DemoTest.php /tmp | |
PHPUnit 3.7.8 by Sebastian Bergmann. | |
F | |
Time: 0 seconds, Memory: 6.50Mb | |
There was 1 failure: | |
1) DemoTest::testDemo | |
Expectation failed for method name is equal to <string:offsetGet> when invoked zero or more times | |
Parameter 0 for invocation SplObjectStorage::offsetGet(Mock_stdClass_73daadc9 Object (...)) does not match expected value. | |
Failed asserting that two variables reference the same object. | |
/private/tmp/DemoTest.php:47 | |
FAILURES! | |
Tests: 1, Assertions: 0, Failures: 1. |
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
/tmp phpunit DemoTest.php /tmp | |
PHPUnit 3.7.8 by Sebastian Bergmann. | |
F | |
Time: 0 seconds, Memory: 6.50Mb | |
There was 1 failure: | |
1) DemoTest::testDemo | |
Expectation failed for method name is equal to <string:offsetGet> when invoked zero or more times | |
Parameter 0 for invocation SplObjectStorage::offsetGet(Mock_stdClass_353e7bf8 Object (...)) does not match expected value. | |
Failed asserting that two objects are equal. | |
--- Expected | |
+++ Actual | |
@@ @@ | |
Mock_stdClass_353e7bf8 Object ( | |
- '__phpunit_id' => 'Mock_stdClass_353e7bf8#2' | |
+ '__phpunit_id' => 'Mock_stdClass_353e7bf8#0' | |
) | |
/private/tmp/DemoTest.php:45 | |
FAILURES! | |
Tests: 1, Assertions: 0, Failures: 1. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment