1 | <?php |
||
18 | abstract class BaseTestCase extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | private $expectException = false; |
||
21 | |||
22 | /** |
||
23 | * Wraps the default #runTest() method to provide an exception hook. |
||
24 | * |
||
25 | * @return mixed|null |
||
26 | * |
||
27 | * @throws \Exception |
||
28 | */ |
||
29 | 626 | protected function runTest() |
|
30 | { |
||
31 | try { |
||
32 | 626 | $result = parent::runTest(); |
|
33 | 626 | } catch (\Exception $ex) { |
|
34 | 61 | $this->exceptionHook($ex); |
|
35 | |||
36 | 61 | return; |
|
37 | } |
||
38 | |||
39 | // @codeCoverageIgnoreStart |
||
40 | if ($this->expectException) { |
||
41 | $this->fail('An exception was expected but none has been thrown.'); |
||
42 | } |
||
43 | // @codeCoverageIgnoreEnd |
||
44 | |||
45 | 565 | return $result; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Sets the flag indicating that an exception is expected. |
||
50 | */ |
||
51 | 61 | protected function expectException() |
|
55 | |||
56 | /** |
||
57 | * @codeCoverageIgnore (shouldn't happen in a green test suite) |
||
58 | * |
||
59 | * Hook called when an unexpected exception is thrown. |
||
60 | * |
||
61 | * Override this hook to make custom assertions on exceptions. |
||
62 | * |
||
63 | * @param \Exception $ex |
||
64 | * |
||
65 | * @throws \Exception |
||
66 | */ |
||
67 | protected function exceptionHook(\Exception $ex) |
||
71 | |||
72 | /** |
||
73 | * Returns the JSON-decoded content of a file. |
||
74 | * |
||
75 | * @param string $file |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | 93 | protected function loadJsonFromFile($file) |
|
80 | { |
||
81 | 93 | return Utils::loadJsonFromFile($file); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Returns a JSON-decoded schema from tests/Data/schemas. |
||
86 | * |
||
87 | * @param string $name Name of the file without the extension |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | 93 | protected function loadSchema($name) |
|
92 | { |
||
93 | 93 | $schemaDir = realpath(__DIR__.'/../../tests/Data/schemas'); |
|
94 | |||
95 | 93 | return $this->loadJsonFromFile("{$schemaDir}/{$name}.json"); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Asserts the validation results equal the expected one and make |
||
100 | * a full report otherwise. |
||
101 | * |
||
102 | * @param string $file |
||
103 | * @param string $title |
||
104 | * @param mixed $instance |
||
105 | * @param \stdClass $schema |
||
106 | * @param bool $isInstanceValid |
||
107 | * @param array $expectedErrors |
||
108 | * @param array $actualErrors |
||
109 | */ |
||
110 | 487 | protected function assertValidationResult( |
|
135 | |||
136 | /** |
||
137 | * Returns a mock object, bypassing original constructor. |
||
138 | * |
||
139 | * @param string $class |
||
140 | * |
||
141 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
142 | */ |
||
143 | 83 | protected function mock($class) |
|
149 | |||
150 | /** |
||
151 | * Returns URI to local file system. |
||
152 | * |
||
153 | * @param string $path |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 307 | protected function getLocalUri($path) |
|
167 | |||
168 | /** |
||
169 | * @codeCoverageIgnore (cannot cover code whose behaviour depends on PHP version) |
||
170 | * |
||
171 | * @param mixed $variable |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | private function dump($variable) |
||
193 | |||
194 | 153 | private function assertHasError(array $errors, array $reportParameters) |
|
202 | |||
203 | 334 | private function assertErrorsAreEqual(array $actual, array $expected, array $reportParameters) |
|
221 | |||
222 | 334 | private function getFailureReportMask() |
|
242 | } |
||
243 |