Skip to content

Instantly share code, notes, and snippets.

@fangel
Created May 19, 2009 11:43
Show Gist options
  • Save fangel/114057 to your computer and use it in GitHub Desktop.
Save fangel/114057 to your computer and use it in GitHub Desktop.
<?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