Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created March 21, 2011 13:52
Show Gist options
  • Save mathiasverraes/879470 to your computer and use it in GitHub Desktop.
Save mathiasverraes/879470 to your computer and use it in GitHub Desktop.
Interface Discovery with PHPUnit's Mock objects
<?php
class BankAccount
{
private $twitter;
public function __construct(Twitter $twitter)
{
$this->twitter = $twitter;
}
public function deposit($amount){
}
}
<?php
class BankAccount
{
// ...
public function deposit($amount)
{
$this->twitter->tweet("Yay, someone deposited $amount");
}
}
<?php
class BankAccountTest extends PHPUnit_Framework_TestCase
{
public function testSendEmailWhenReceivingMoney()
{
$twitter = $this->getMock('Twitter');
$account = new BankAccount($twitter);
$account->deposit(10);
}
}
<?php
class BankAccountTest extends PHPUnit_Framework_TestCase
{
public function testSendEmailWhenReceivingMoney()
{
$twitter = $this->getMock('Twitter');
$twitter->expects($this->once())
->method('tweet');
$account = new BankAccount($twitter);
$account->deposit(10);
}
}
<?php
interface Twitter
{
function tweet($message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment