PHP fóra: Builder | JakPsatWeb | Webtrh
Nejste přihlášen(a)
Na http://www.phpunit.de/…doubles.html#…
je postup jak v phpunit mockovat běžné metody. Zná někdo chodivý způsob
jak podobně mockovat magickou metodu __get? Zatím jsem dospěl k tomu, že
problém je s vícenásobným voláním funkce __get s různými parametry.
Uvádim příklad srovnávající běžnou a magickou metodu:
<?php
require_once 'PHPUnit/Framework.php';
class myClass
{
public function normalMethod() {
return 'normal';
}
public function normalMethod2() {
return 'normal2';
}
public function __get($name) {
switch($name){
case 'propertyA':
return 'AAA';
case 'propertyB':
return 'BBB';
}
}
}
class testedClass
{
public function testedMethodNormal(myClass $obj) {
// tady se bude nejak pracovat s metodami objektu $obj
return $obj->normalMethod() == 'fake' && $obj->normalMethod2() == 'fake2';
}
public function testedMethodMagic(myClass $obj) {
// tady se bude nejak pracovat s vlastnostmi objektu $obj
return $obj->propertyA == 'fakeA' && $obj->propertyB == 'fakeB';
}
}
class testedClassTest extends PHPUnit_Framework_TestCase
{
public function testNormal() {
$mock = $this->getMock('myClass');
$mock->expects($this->any())
->method('normalMethod')
->will($this->returnValue('fake'));
$mock->expects($this->any())
->method('normalMethod2')
->will($this->returnValue('fake2'));
$this->assertEquals('fake', $mock->normalMethod()); // OK
$this->assertEquals('fake2', $mock->normalMethod2()); // OK
$tested = new testedClass();
$this->assertTrue($tested->testedMethodNormal($mock)); // OK
}
public function testMagic() {
$mock = $this->getMock('myClass');
$mock->expects($this->any())
->method('__get')
->with($this->equalTo('propertyA'))
->will($this->returnValue('fakeA'));
$this->assertEquals('fakeA', $mock->propertyA); // OK
$mock->expects($this->any())
->method('__get')
->with($this->equalTo('propertyB'))
->will($this->returnValue('fakeB'));
$this->assertEquals('fakeB', $mock->propertyB); // fail
}
public function testMagic2() {
$mock = $this->getMock('myClass');
$mock->expects($this->any())
->method('__get')
->with($this->equalTo('propertyA'))
->will($this->returnValue('fakeA'));
$this->assertEquals('fakeA', $mock->propertyA); // OK
$mock = $this->getMock('myClass');
$mock->expects($this->any())
->method('__get')
->with($this->equalTo('propertyB'))
->will($this->returnValue('fakeB'));
$this->assertEquals('fakeB', $mock->propertyB); // OK
// tohle reseni nevyhovuje, protoze mockovany objekt se predava do
// testovane metody a je potreba mockovat vice vlastnosti
$tested = new testedClass();
$this->assertTrue($tested->testedMethodMagic($mock)); // fail
}
public function testMagic3() {
$mock = $this->getMock(
'myClass',
array('_get'),
array('propertyA', 'propertyB'),
'',
false
);
$mock->expects($this->any())
->method('__get')
->with($this->equalTo('propertyA'))
->will($this->returnValue('fakeA'));
$this->assertEquals('fakeA', $mock->propertyA); // fail
$mock->expects($this->any())
->method('__get')
->with($this->equalTo('propertyB'))
->will($this->returnValue('fakeB'));
$this->assertEquals('fakeB', $mock->propertyB); // fail
}
}
?>