Created
August 31, 2012 11:47
-
-
Save mageekguy/3551810 to your computer and use it in GitHub Desktop.
Atoum, mock and __call are in a boat !
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 | |
/* | |
And it's AWESOME ! | |
Imagine that you have a class like this : | |
*/ | |
namespace mageekguy\atoum; | |
class foo | |
{ | |
function __call($method, $arguments) | |
{ | |
} | |
} | |
/* | |
And imagine that you're using it in class, like this : | |
*/ | |
namespace mageekguy\atoum; | |
class bar | |
{ | |
protected $foo = null; | |
protected $awesome = ''; | |
function __construct(foo $foo) | |
{ | |
$this->foo = $foo; | |
} | |
function getAwesome() | |
{ | |
return $this->awesome; | |
} | |
function doSomething() | |
{ | |
$this->awesome = $this->foo->callMagicMethod(__FUNCTION__); | |
return $this; | |
} | |
} | |
/* | |
When, you can write this test with atoum, the simple, moderne and intuitive unit testing framework for PHP ≥ 5.3 (http://www.atoum.org) : | |
*/ | |
namespace mageekguy\atoum\tests\units; | |
use mageekguy\atoum; | |
class bar extends atoum\test | |
{ | |
public function testDoSomething() | |
{ | |
$this | |
->if($bar = new atoum\bar($foo = new \mock\mageekguy\atoum\foo())) | |
->then | |
->object($bar->doSomething())->isIdenticalTo($bar) | |
->mock($foo)->call('callMagicMethod')->withArguments('doSomething')->once() | |
->variable($bar->getAwesome())->isNull() | |
->if($foo->getMockController()->callMagicMethod = 'It is really AWESOME !') | |
->then | |
->object($bar->doSomething())->isIdenticalTo($bar) | |
->mock($foo)->call('callMagicMethod')->withArguments('doSomething')->exactly(2) | |
->string($bar->getAwesome())->isEqualTo('It is really AWESOME !') | |
->if($foo->getMockController()->callMagicMethod[3] = 'It is the AWESOME third call !') | |
->then | |
->object($bar->doSomething())->isIdenticalTo($bar) | |
->mock($foo)->call('callMagicMethod')->withArguments('doSomething')->exactly(3) | |
->string($bar->getAwesome())->isEqualTo('It is the AWESOME third call !') | |
->if($foo->getMockController()->callMagicMethod[4] = 'It is an AWESOME idea of @Guiled, thanks, dude !') | |
->then | |
->object($bar->doSomething())->isIdenticalTo($bar) | |
->mock($foo)->call('callMagicMethod')->withArguments('doSomething')->exactly(4) | |
->string($bar->getAwesome())->isEqualTo('It is an AWESOME idea of @Guiled, thanks, dude !') | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment