Skip to content

Instantly share code, notes, and snippets.

@mlively
Created March 5, 2011 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlively/856582 to your computer and use it in GitHub Desktop.
Save mlively/856582 to your computer and use it in GitHub Desktop.
MyBestPokerGameTest.php
<?php
class MagicClass
{
public function __call($method, $args)
{
return '__call';
}
}
?>
<?php
class MagicClassTest extends PHPUnit_Framework_TestCase
{
public function testMagicCall()
{
$mock = Phake::mock('MagicClass');
$mock->magicCall();
Phake::verify($mock)->magicCall();
}
}
?>
<?php
class MagicClassTest extends PHPUnit_Framework_TestCase
{
public function testMagicCall()
{
$mock = Phake::mock('MagicClass');
$mock->magicCall();
Phake::verifyCallMethodWith('magicCall')->isCalledOn($mock);
}
}
?>
<?php
class 52CardDeckMatcher implements Phake_Matchers_IArgumentMatcher
{
public function matches($argument)
{
return ($argument instanceof CardCollection
&& $argument->getNumberOfCards() == 52);
}
public function __toString()
{
return '<object:CardCollection with 52 cards>';
}
}
class MyBestPokerGameTest extends PHPUnit_Framework_TestCase
{
public function testDealCards()
{
$dealer = Phake::mock('MyPokerDealer');
$players = Phake::mock('PlayerCollection');
$cardGame = new MyPokerGame($dealer, $players);
Phake::verify($dealer)->deal(new 52CardDeckMatcher(), $players);
}
}
?>
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testPHPUnitMock()
{
$mock = Phake::mock('PhakeTest_MockedClass');
$mock->fooWithArgument('foo');
$mock->fooWithArgument('bar');
Phake::inOrder(
Phake::verify($mock)->fooWithArgument('foo'),
Phake::verify($mock)->fooWithArgument('bar')
);
}
}
?>
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testPHPUnitMock()
{
$mock = Phake::mock('PhakeTest_MockedClass');
$mock->fooWithArgument('foo');
$mock->fooWithArgument('bar');
Phake::verify($mock)->fooWithArgument('foo');
Phake::verify($mock)->fooWithArgument('bar');
}
}
?>
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testPHPUnitMock()
{
$mock = Phake::mock('PhakeTest_MockedClass');
$mock->fooWithArgument('foo');
$mock->fooWithArgument('foo');
Phake::verify($mock, Phake::times(2))->fooWithArgument('foo');
}
}
?>
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testPHPUnitMock()
{
$mock = $this->getMock('PhakeTest_MockedClass');
$mock->expects($this->once())->method('fooWithArgument')
->with('foo');
$mock->expects($this->once())->method('fooWithArgument')
->with('bar');
$mock->fooWithArgument('foo');
$mock->fooWithArgument('bar');
}
}
?>
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testPHPUnitMock()
{
$mock = $this->getMock('PhakeTest_MockedClass');
//NOTICE this is now at() instead of once()
$mock->expects($this->at(0))->method('fooWithArgument')
->with('foo');
//NOTICE this is now at() instead of once()
$mock->expects($this->at(1))->method('fooWithArgument')
->with('bar');
$mock->fooWithArgument('foo');
$mock->fooWithArgument('bar');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment