Skip to content

Instantly share code, notes, and snippets.

@MTGap
Created July 29, 2013 20: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 MTGap/6107547 to your computer and use it in GitHub Desktop.
Save MTGap/6107547 to your computer and use it in GitHub Desktop.
<?php
class Thrower {
public function test($shouldThrow) {
if ($shouldThrow) {
throw new Exception();
}
}
}
class Test {
private $thrower;
public function __construct(Thrower $thrower) {
$this->thrower = $thrower;
}
public function testCatchingException() {
try {
$this->thrower->test(true);
} catch (Exception $exception) {
return true;
}
return false;
}
}
class ThrowException extends \PHPUnit_Framework_TestCase {
public function testCatchingException() {
$thrower = $this->getMock('Thrower');
$thrower->expects($this->once())
->method('test')
->with($this->equalTo(true))
->will($this->throwException(new Exception()));
$test = new Test($thrower);
$this->assertTrue($test->testCatchingException());
}
public function testCatchingExceptionWithMap() {
$thrower = $this->getMock('Thrower');
$map = array(
array(true, $this->throwException(new Exception())),
);
$thrower->expects($this->once())
->method('test')
->will($this->returnValueMap($map));
$test = new Test($thrower);
$this->assertTrue($test->testCatchingException());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment