Created
July 29, 2013 20:30
-
-
Save MTGap/6107547 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 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