-
Notifications
You must be signed in to change notification settings - Fork 4
/
In-LineTeardownTest.php
70 lines (63 loc) · 2.15 KB
/
In-LineTeardownTest.php
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
<?php
/**
* All the tests will be failing. This is to demonstrate the difficult part
* of implementing In-Line Teardown: ensure it is executed while test are
* failing.
*/
class InLineTeardownTest extends PHPUnit_Framework_TestCase
{
/**
* I keep the object on a field so that if In-Line Teardown is not
* executed, we'll see destruction messages at the end of the suite
* instead of after each test.
*/
private $fixtureToTeardown;
public function testExpectMethodForInlineTeardown()
{
$this->fixtureToTeardown = new MyClassWithDestructor();
// this doesn't *immediately* throw an exception
$this->expectTrue(false, 'First test failure message.');
unset($this->fixtureToTeardown);
}
private $expectationBooleans = array();
private $expectationErrorMessages = array();
private function expectTrue($boolean, $message = '')
{
$this->expectationBooleans[] = $boolean;
$this->expectationErrorMessages[] = $message;
}
/**
* A workaround to being able to support expect() methods
*/
public function tearDown()
{
foreach ($this->expectationBooleans as $i => $boolean) {
$this->assertTrue($boolean, $this->expectationErrorMessages[$i]);
}
}
public function testMovementOfAssertionsAfterInLineTeardown()
{
$this->fixtureToTeardown = new MyClassWithDestructor();
$result = 1 == 0; // or whatever computation over the results
// that ultimately produces a boolean
unset($this->fixtureToTeardown);
$this->assertTrue($result, 'Second test failure message.');
}
public function testFinallyLikeSolutions()
{
$this->fixtureToTeardown = new MyClassWithDestructor();
try {
$this->assertTrue(false, 'Third test failure message.');
} catch (Exception $assertionException) {
unset($this->fixtureToTeardown);
throw $assertionException;
}
}
}
class MyClassWithDestructor
{
public function __destruct()
{
echo "The instance of MyClassWithDestructor has been destroyed.\n";
}
}