Created
May 19, 2009 11:43
-
-
Save fangel/114057 to your computer and use it in GitHub Desktop.
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 | |
class ClassUnderTest { | |
private $table; | |
public function __construct( $table ) { | |
$this->table = $table; | |
} | |
public function lookup($key) { | |
$entry = $this->table->lookup($key); | |
if( ! $entry ) throw new Exception('Unknown key'); | |
return $entry; | |
} | |
} | |
class TableClass { | |
public function lookup( $key ) { | |
// Stub.. | |
} | |
} | |
class ExampleStubTest extends PHPUnit_Framework_TestCase { | |
private $correct_stub; | |
public function setUp() { | |
$this->correct_stub = $this->getMock('TableClass'); | |
$this->correct_stub->expects($this->any()) | |
->method('lookup') | |
->with( 'foo' ) | |
->will( $this->returnValue('bar') ); | |
} | |
public function testSuccessfullLookup() { | |
$testclass = new ClassUnderTest( $this->correct_stub ); | |
$this->assertEquals('bar', $testclass->lookup('foo')); | |
} | |
public function testFailingLookup() { | |
$stub_with_missing_entry = $this->getMock('TableClass'); | |
$stub_with_missing_entry->expects($this->any()) | |
->method('lookup') | |
->with( 'foo' ) | |
->will( $this->returnValue( NULL )); | |
$testclass = new ClassUnderTest( $stub_with_missing_entry ); | |
$this->setExpectedException('Exception'); | |
$testclass->lookup('bar'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment