1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
<?php
require_once(dirname(__FILE__) . '/../exceptions.php');
require_once(dirname(__FILE__) . '/../expectation.php');
require_once(dirname(__FILE__) . '/../test_case.php');
Mock::generate('SimpleTestCase');
Mock::generate('SimpleExpectation');
class MyTestException extends Exception {}
class HigherTestException extends MyTestException {}
class OtherTestException extends Exception {}
class TestOfExceptionExpectation extends UnitTestCase {
function testExceptionClassAsStringWillMatchExceptionsRootedOnThatClass() {
$expectation = new ExceptionExpectation('MyTestException');
$this->assertTrue($expectation->test(new MyTestException()));
$this->assertTrue($expectation->test(new HigherTestException()));
$this->assertFalse($expectation->test(new OtherTestException()));
}
function testMatchesClassAndMessageWhenExceptionExpected() {
$expectation = new ExceptionExpectation(new MyTestException('Hello'));
$this->assertTrue($expectation->test(new MyTestException('Hello')));
$this->assertFalse($expectation->test(new HigherTestException('Hello')));
$this->assertFalse($expectation->test(new OtherTestException('Hello')));
$this->assertFalse($expectation->test(new MyTestException('Goodbye')));
$this->assertFalse($expectation->test(new MyTestException()));
}
function testMessagelessExceptionMatchesOnlyOnClass() {
$expectation = new ExceptionExpectation(new MyTestException());
$this->assertTrue($expectation->test(new MyTestException()));
$this->assertFalse($expectation->test(new HigherTestException()));
}
}
class TestOfExceptionTrap extends UnitTestCase {
function testNoExceptionsInQueueMeansNoTestMessages() {
$test = new MockSimpleTestCase();
$test->expectNever('assert');
$queue = new SimpleExceptionTrap();
$this->assertFalse($queue->isExpected($test, new Exception()));
}
function testMatchingExceptionGivesTrue() {
$expectation = new MockSimpleExpectation();
$expectation->setReturnValue('test', true);
$test = new MockSimpleTestCase();
$test->setReturnValue('assert', true);
$queue = new SimpleExceptionTrap();
$queue->expectException($expectation, 'message');
$this->assertTrue($queue->isExpected($test, new Exception()));
}
function testMatchingExceptionTriggersAssertion() {
$test = new MockSimpleTestCase();
$test->expectOnce('assert', array(
'*',
new ExceptionExpectation(new Exception()),
'message'));
$queue = new SimpleExceptionTrap();
$queue->expectException(new ExceptionExpectation(new Exception()), 'message');
$queue->isExpected($test, new Exception());
}
}
class TestOfCatchingExceptions extends UnitTestCase {
function testCanCatchAnyExpectedException() {
$this->expectException();
throw new Exception();
}
function testCanMatchExceptionByClass() {
$this->expectException('MyTestException');
throw new HigherTestException();
}
function testCanMatchExceptionExactly() {
$this->expectException(new Exception('Ouch'));
throw new Exception('Ouch');
}
function testLastListedExceptionIsTheOneThatCounts() {
$this->expectException('OtherTestException');
$this->expectException('MyTestException');
throw new HigherTestException();
}
}
class Test1Exception extends Exception {}
class Test2Exception extends Exception {}
class TestOfCallingTearDownWithExceptions extends UnitTestCase {
public function setUp() {
$GLOBALS['setUp'] = true;
}
public function tearDown() {
$GLOBALS['tearDown'] = true;
}
public function test1() {
$this->assertTrue($GLOBALS['setUp']);
$this->assertNull($GLOBALS['tearDown']);
$this->expectException('Test1Exception');
throw new Test1Exception(__FUNCTION__);
}
public function test2() {
$this->assertTrue($GLOBALS['setUp']);
$this->assertTrue($GLOBALS['tearDown']);
$this->expectException('Test2Exception');
throw new Test2Exception(__FUNCTION__);
}
}
?>
|