1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage;
12
13use SebastianBergmann\CodeCoverage\Driver\Xdebug;
14
15/**
16 * Abstract base class for test case classes.
17 *
18 * @since Class available since Release 1.0.0
19 */
20abstract class TestCase extends \PHPUnit_Framework_TestCase
21{
22    protected static $TEST_TMP_PATH;
23
24    public static function setUpBeforeClass()
25    {
26        self::$TEST_TMP_PATH = TEST_FILES_PATH . 'tmp';
27    }
28
29    protected function getXdebugDataForBankAccount()
30    {
31        return [
32            [
33                TEST_FILES_PATH . 'BankAccount.php' => [
34                    8  => 1,
35                    9  => -2,
36                    13 => -1,
37                    14 => -1,
38                    15 => -1,
39                    16 => -1,
40                    18 => -1,
41                    22 => -1,
42                    24 => -1,
43                    25 => -2,
44                    29 => -1,
45                    31 => -1,
46                    32 => -2
47                ]
48            ],
49            [
50                TEST_FILES_PATH . 'BankAccount.php' => [
51                    8  => 1,
52                    13 => 1,
53                    16 => 1,
54                    29 => 1,
55                ]
56            ],
57            [
58                TEST_FILES_PATH . 'BankAccount.php' => [
59                    8  => 1,
60                    13 => 1,
61                    16 => 1,
62                    22 => 1,
63                ]
64            ],
65            [
66                TEST_FILES_PATH . 'BankAccount.php' => [
67                    8  => 1,
68                    13 => 1,
69                    14 => 1,
70                    15 => 1,
71                    18 => 1,
72                    22 => 1,
73                    24 => 1,
74                    29 => 1,
75                    31 => 1,
76                ]
77            ]
78        ];
79    }
80
81    protected function getCoverageForBankAccount()
82    {
83        $data = $this->getXdebugDataForBankAccount();
84        require_once TEST_FILES_PATH . '/BankAccountTest.php';
85
86        $stub = $this->createMock(Xdebug::class);
87
88        $stub->expects($this->any())
89            ->method('stop')
90            ->will($this->onConsecutiveCalls(
91                $data[0],
92                $data[1],
93                $data[2],
94                $data[3]
95            ));
96
97        $filter = new Filter;
98        $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
99
100        $coverage = new CodeCoverage($stub, $filter);
101
102        $coverage->start(
103            new \BankAccountTest('testBalanceIsInitiallyZero'),
104            true
105        );
106
107        $coverage->stop(
108            true,
109            [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
110        );
111
112        $coverage->start(
113            new \BankAccountTest('testBalanceCannotBecomeNegative')
114        );
115
116        $coverage->stop(
117            true,
118            [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
119        );
120
121        $coverage->start(
122            new \BankAccountTest('testBalanceCannotBecomeNegative2')
123        );
124
125        $coverage->stop(
126            true,
127            [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
128        );
129
130        $coverage->start(
131            new \BankAccountTest('testDepositWithdrawMoney')
132        );
133
134        $coverage->stop(
135            true,
136            [
137                TEST_FILES_PATH . 'BankAccount.php' => array_merge(
138                    range(6, 9),
139                    range(20, 25),
140                    range(27, 32)
141                )
142            ]
143        );
144
145        return $coverage;
146    }
147
148    protected function getCoverageForBankAccountForFirstTwoTests()
149    {
150        $data = $this->getXdebugDataForBankAccount();
151
152        $stub = $this->createMock(Xdebug::class);
153
154        $stub->expects($this->any())
155            ->method('stop')
156            ->will($this->onConsecutiveCalls(
157                $data[0],
158                $data[1]
159            ));
160
161        $filter = new Filter;
162        $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
163
164        $coverage = new CodeCoverage($stub, $filter);
165
166        $coverage->start(
167            new \BankAccountTest('testBalanceIsInitiallyZero'),
168            true
169        );
170
171        $coverage->stop(
172            true,
173            [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
174        );
175
176        $coverage->start(
177            new \BankAccountTest('testBalanceCannotBecomeNegative')
178        );
179
180        $coverage->stop(
181            true,
182            [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
183        );
184
185        return $coverage;
186    }
187
188    protected function getCoverageForBankAccountForLastTwoTests()
189    {
190        $data = $this->getXdebugDataForBankAccount();
191
192        $stub = $this->createMock(Xdebug::class);
193
194        $stub->expects($this->any())
195            ->method('stop')
196            ->will($this->onConsecutiveCalls(
197                $data[2],
198                $data[3]
199            ));
200
201        $filter = new Filter;
202        $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
203
204        $coverage = new CodeCoverage($stub, $filter);
205
206        $coverage->start(
207            new \BankAccountTest('testBalanceCannotBecomeNegative2')
208        );
209
210        $coverage->stop(
211            true,
212            [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
213        );
214
215        $coverage->start(
216            new \BankAccountTest('testDepositWithdrawMoney')
217        );
218
219        $coverage->stop(
220            true,
221            [
222                TEST_FILES_PATH . 'BankAccount.php' => array_merge(
223                    range(6, 9),
224                    range(20, 25),
225                    range(27, 32)
226                )
227            ]
228        );
229
230        return $coverage;
231    }
232
233    protected function getExpectedDataArrayForBankAccount()
234    {
235        return [
236            TEST_FILES_PATH . 'BankAccount.php' => [
237                8 => [
238                    0 => 'BankAccountTest::testBalanceIsInitiallyZero',
239                    1 => 'BankAccountTest::testDepositWithdrawMoney'
240                ],
241                9  => null,
242                13 => [],
243                14 => [],
244                15 => [],
245                16 => [],
246                18 => [],
247                22 => [
248                    0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
249                    1 => 'BankAccountTest::testDepositWithdrawMoney'
250                ],
251                24 => [
252                    0 => 'BankAccountTest::testDepositWithdrawMoney',
253                ],
254                25 => null,
255                29 => [
256                    0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
257                    1 => 'BankAccountTest::testDepositWithdrawMoney'
258                ],
259                31 => [
260                    0 => 'BankAccountTest::testDepositWithdrawMoney'
261                ],
262                32 => null
263            ]
264        ];
265    }
266
267    protected function getCoverageForFileWithIgnoredLines()
268    {
269        $filter = new Filter;
270        $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_ignore.php');
271
272        $coverage = new CodeCoverage(
273            $this->setUpXdebugStubForFileWithIgnoredLines(),
274            $filter
275        );
276
277        $coverage->start('FileWithIgnoredLines', true);
278        $coverage->stop();
279
280        return $coverage;
281    }
282
283    protected function setUpXdebugStubForFileWithIgnoredLines()
284    {
285        $stub = $this->createMock(Xdebug::class);
286
287        $stub->expects($this->any())
288            ->method('stop')
289            ->will($this->returnValue(
290                [
291                    TEST_FILES_PATH . 'source_with_ignore.php' => [
292                        2 => 1,
293                        4 => -1,
294                        6 => -1,
295                        7 => 1
296                    ]
297                ]
298            ));
299
300        return $stub;
301    }
302
303    protected function getCoverageForClassWithAnonymousFunction()
304    {
305        $filter = new Filter;
306        $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php');
307
308        $coverage = new CodeCoverage(
309            $this->setUpXdebugStubForClassWithAnonymousFunction(),
310            $filter
311        );
312
313        $coverage->start('ClassWithAnonymousFunction', true);
314        $coverage->stop();
315
316        return $coverage;
317    }
318
319    protected function setUpXdebugStubForClassWithAnonymousFunction()
320    {
321        $stub = $this->createMock(Xdebug::class);
322
323        $stub->expects($this->any())
324            ->method('stop')
325            ->will($this->returnValue(
326                [
327                    TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [
328                        7  => 1,
329                        9  => 1,
330                        10 => -1,
331                        11 => 1,
332                        12 => 1,
333                        13 => 1,
334                        14 => 1,
335                        17 => 1,
336                        18 => 1
337                    ]
338                ]
339            ));
340
341        return $stub;
342    }
343}
344