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