sebastianbergmann/phpunit — Issue #427: Incorrect behaviour of PHPUnit_Framework_Testcase::expects in some cases
Author: dkop
Created: 2011-11-29 15:05:18+00:00
State: open

I think, PHPUnit_Framework_Testcase::expects should work like PHPUnit_Framework_Testcase::staticExpects when used for method, who defined statically in mocked class.

Please check following code:

<?php
class ClassWithStaticMethods {

public static function iAmStaticMethod() {
return 100500;
}

}

class AATest extends PHPUnit_Framework_TestCase {

/**
* @var ClassWithStaticMethods|PHPUnit_Framework_MockObject_MockObject
*/
private $mockedClass;

public function setUp() {
$this->mockedClass = $this->getMock('ClassWithStaticMethods');
}

public function testNonMockedMethodReturnsCorrectValue() {
$ObjectWithStaticMethods = new ClassWithStaticMethods();
$this->assertEquals(100500, $ObjectWithStaticMethods->iAmStaticMethod());
}

public function testMockedMethodReturnsCorrectValueWhenCalledDynamically() {
$this->mockedClass
->expects($this->once())
->method('iAmStaticMethod')
->will($this->returnValue(100499));

$this->assertEquals(100499, $this->mockedClass->iAmStaticMethod());
}

public function testStaticallyMockedMethodReturnsCorrectValueWhenCalledDynamically() {
$this->mockedClass
->staticExpects($this->once())
->method('iAmStaticMethod')
->will($this->returnValue(100501));

$this->assertEquals(100501, $this->mockedClass->iAmStaticMethod());
}

}

Expected test result:

PHPUnit 3.6.3 by Sebastian Bergmann.

...

Time: 0 seconds, Memory: 8.00Mb

OK (3 tests, 5 assertions)

Actual test result:

PHPUnit 3.6.3 by Sebastian Bergmann.

.F.

Time: 0 seconds, Memory: 8.25Mb

There was 1 failure:

1) AATest::testMockedMethodReturnsCorrectValueWhenCalledDynamically
Failed asserting that null matches expected 100499.

{pathtotests}/AATest.php:33
/usr/local/bin/phpunit:44

FAILURES!
Tests: 3, Assertions: 4, Failures: 1.