Passed
Push — test ( d838cf...cef4a5 )
by Tom
03:29
created

TestCase::setExpectedExceptionRegExp()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 9
nop 3
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
use BadMethodCallException;
8
use PHPUnit\Framework\Exception;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase as PhpunitTestCase;
11
12
/**
13
 * UnitTestCase
14
 *
15
 * Base test case for pipelines test-suite, basically a shim
16
 * for older/newer phpunit versions.
17
 *
18
 * @method assertDirectoryNotExists - deprecated in phpunit 9
19
 * @method assertDirectoryNotExist
20
 * @method assertFileNotExists - deprecated in phpunit 9
21
 * @method assertFileNotExist
22
 * @method assertRegExp - deprecated in phpunit 9
23
 *
24
 * @see TestCase::assertMatchesRegularExpression
25
 *
26
 * @coversNothing
27
 */
28
class TestCase extends PhpunitTestCase
29
{
30
    /**
31
     * @var null|string
32
     */
33
    private $expectedException;
34
35
    /**
36
     * @var null|string
37
     */
38
    private $expectedExceptionMessage;
39
40
    /**
41
     * @param $actual
42
     * @param string $message
43
     */
44
    public static function assertIsArray($actual, $message = ''): void
45
    {
46
        if (is_callable('parent::' . __FUNCTION__)) {
47
            parent::assertIsArray($actual, $message);
48
49
            return;
50
        }
51
52
        self::assertInternalType('array', $actual, $message);
0 ignored issues
show
Bug introduced by
The method assertInternalType() does not exist on Ktomk\Pipelines\TestCase. Did you maybe mean assertIsIterable()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        self::/** @scrutinizer ignore-call */ 
53
              assertInternalType('array', $actual, $message);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
    }
54
55
    public static function assertIsBool($actual, $message = ''): void
56
    {
57
        if (is_callable('parent::' . __FUNCTION__)) {
58
            parent::assertIsBool($actual, $message);
59
60
            return;
61
        }
62
63
        self::assertInternalType('bool', $actual, $message);
64
    }
65
66
    public static function assertIsCallable($actual, $message = ''): void
67
    {
68
        if (is_callable('parent::' . __FUNCTION__)) {
69
            parent::assertIsCallable($actual, $message);
70
71
            return;
72
        }
73
74
        self::assertInternalType('callable', $actual, $message);
75
    }
76
77
    public static function assertIsInt($actual, $message = ''): void
78
    {
79
        if (is_callable('parent::' . __FUNCTION__)) {
80
            parent::assertIsInt($actual, $message);
81
82
            return;
83
        }
84
85
        self::assertInternalType('integer', $actual, $message);
86
    }
87
88
    public static function assertIsString($actual, $message = ''): void
89
    {
90
        if (is_callable('parent::' . __FUNCTION__)) {
91
            parent::assertIsString($actual, $message);
92
93
            return;
94
        }
95
96
        self::assertInternalType('string', $actual, $message);
97
    }
98
99
    /**
100
     * Asserts that a string matches a given regular expression.
101
     *
102
     * @param string $pattern
103
     * @param string $string
104
     * @param string $message
105
     */
106
    public static function assertMatchesRegularExpression($pattern, $string, $message = ''): void
107
    {
108
        if (is_callable('parent::' . __FUNCTION__)) {
109
            parent::assertMatchesRegularExpression($pattern, $string, $message);
110
111
            return;
112
        }
113
114
        self::assertRegExp($pattern, $string, $message);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertRegExp() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/4086 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
        /** @scrutinizer ignore-deprecated */ self::assertRegExp($pattern, $string, $message);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
115
    }
116
117
    /**
118
     * assertContains() with string haystacks >>> assertStringContainsString
119
     *
120
     * assertStringContainsString was added in Phpunit 8 to lighten up usage
121
     * of assertContains power-factory
122
     *
123
     * @param string $needle
124
     * @param string $haystack
125
     * @param string $message
126
     */
127
    public static function assertStringContainsString($needle, $haystack, $message = ''): void
128
    {
129
        if (is_callable('parent::' . __FUNCTION__)) {
130
            parent::assertStringContainsString($needle, $haystack, $message);
131
132
            return;
133
        }
134
135
        self::assertContains($needle, $haystack, $message);
0 ignored issues
show
Bug introduced by
$haystack of type string is incompatible with the type iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertContains(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        self::assertContains($needle, /** @scrutinizer ignore-type */ $haystack, $message);
Loading history...
136
    }
137
138
    /**
139
     * assertContains() with string haystacks >>> assertStringContainsString
140
     *
141
     * assertStringContainsString was added in Phpunit 8 to lighten up usage
142
     * of assertContains power-factory
143
     *
144
     * @param string $needle
145
     * @param string $haystack
146
     * @param string $message
147
     *
148
     * @return void
149
     */
150
    public static function assertStringNotContainsString($needle, $haystack, $message = ''): void
151
    {
152
        if (is_callable('parent::' . __FUNCTION__)) {
153
            parent::assertStringNotContainsString($needle, $haystack, $message);
154
155
            return;
156
        }
157
158
        self::assertNotContains($needle, $haystack, $message);
0 ignored issues
show
Bug introduced by
$haystack of type string is incompatible with the type iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertNotContains(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        self::assertNotContains($needle, /** @scrutinizer ignore-type */ $haystack, $message);
Loading history...
159
    }
160
161
    /**
162
     * Backwards compatible assertions (as far as in use)
163
     *
164
     * @param string $name
165
     * @param array $arguments
166
     */
167
    public function __call($name, array $arguments)
168
    {
169
        switch ($name) {
170
            // file and directory assertions came w/ PHPUnit 5.7
171
            case 'assertDirectoryExists':
172
                list($directory, $message) = $arguments + array(1 => '');
173
                $this->shimAssertDirectoryExists($directory, $message);
174
175
                return;
176
            case 'assertDirectoryNotExists':
177
            case 'assertDirectoryNotExist':
178
                list($directory, $message) = $arguments + array(1 => '');
179
                $this->shimAssertDirectoryNotExists($directory, $message);
180
181
                return;
182
            case 'assertFileExists':
183
                list($filename, $message) = $arguments + array(1 => '');
184
                $this->shimAssertFileExists($filename, $message);
185
186
                return;
187
            case 'assertFileNotExists':
188
            case 'assertFileNotExist':
189
                list($filename, $message) = $arguments + array(1 => '');
190
                $this->shimAssertFileNotExists($filename, $message);
191
192
                return;
193
        }
194
195
        throw new \BadMethodCallException(
196
            sprintf('Testcase %s::%s(%d)', get_class($this), $name, count($arguments))
197
        );
198
    }
199
200
    /* setUp / tearDown phpunit compat overrides */
201
202
    /**
203
     * @deprecated phpunit compat shim, use doSetUp() downstream
204
     * @see doSetUp
205
     */
206
    protected function setUp(): void
207
    {
208
        $this->doSetUp();
209
    }
210
211
    /**
212
     * phpunit compat shim
213
     *
214
     * @see setUp
215
     */
216
    protected function doSetUp()
217
    {
218
        $this->expectedException = null;
219
        $this->expectedExceptionMessage = null;
220
    }
221
222
    /**
223
     * @deprecated phpunit compat shim, use doTearDown() downstream
224
     * @see doTearDown
225
     */
226
    protected function tearDown(): void
227
    {
228
        $this->doTearDown();
229
    }
230
231
    /**
232
     * phpunit compat shim
233
     *
234
     * @see tearDown
235
     */
236
    protected function doTearDown()
237
    {
238
        parent::tearDown();
239
    }
240
241
    /* exceptions */
242
243
    /**
244
     * @param string $exception
245
     *
246
     * @return void
247
     */
248
    public function expectException($exception): void
249
    {
250
        $this->expectedException = $exception;
251
252
        if (is_callable('parent::' . __FUNCTION__)) {
253
            parent::expectException($exception);
254
255
            return;
256
        }
257
258
        $this->setExpectedException($exception);
259
    }
260
261
    /**
262
     * @param string $message
263
     *
264
     * @throws Exception
265
     *
266
     * @return void
267
     */
268
    public function expectExceptionMessage($message): void
269
    {
270
        if (is_callable('parent::' . __FUNCTION__)) {
271
            parent::expectExceptionMessage($message);
272
273
            return;
274
        }
275
276
        if (null === $this->expectedException) {
277
            throw new \BadMethodCallException('Hmm this is message without class *gg* - reflection?');
278
        }
279
280
        $this->expectedExceptionMessage = $message;
281
        $this->setExpectedException($this->expectedException, $message);
282
    }
283
284
    /**
285
     * @param int|string $code
286
     *
287
     * @throws Exception
288
     *
289
     * @return void
290
     */
291
    public function expectExceptionCode($code): void
292
    {
293
        if (is_callable('parent::' . __FUNCTION__)) {
294
            parent::expectExceptionCode($code);
295
296
            return;
297
        }
298
299
        if (null === $this->expectedException) {
300
            throw new BadMethodCallException('No exception expected');
301
        }
302
303
        $this->setExpectedException($this->expectedException, $this->expectedExceptionMessage, $code);
304
    }
305
306
    /**
307
     * Phpunit 8 deprecated (first silently) expectExceptionMessageRegExp(),
308
     * will be gone in Phpunit 9.
309
     *
310
     * @param string $messageRegExp
311
     *
312
     * @throws Exception
313
     *
314
     * @return void
315
     */
316
    public function expectExceptionMessageMatches($messageRegExp): void
317
    {
318
        if (is_callable('parent::' . __FUNCTION__)) {
319
            parent::expectExceptionMessageMatches($messageRegExp);
320
321
            return;
322
        }
323
324
        $this->expectExceptionMessageRegExp($messageRegExp);
0 ignored issues
show
Deprecated Code introduced by
The function Ktomk\Pipelines\TestCase...xceptionMessageRegExp() has been deprecated: In phpunit 8. Use expectExceptionMessageMatches() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

324
        /** @scrutinizer ignore-deprecated */ $this->expectExceptionMessageRegExp($messageRegExp);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
325
    }
326
327
    /**
328
     * @param string $messageRegExp
329
     *
330
     * @deprecated In phpunit 8. Use expectExceptionMessageMatches() instead
331
     * @see expectExceptionMessageMatches
332
     *
333
     * @return void
334
     */
335
    public function expectExceptionMessageRegExp($messageRegExp): void
336
    {
337
        if (is_callable('parent::' . __FUNCTION__)) {
338
            parent::expectExceptionMessageRegExp($messageRegExp);
0 ignored issues
show
Bug introduced by
The method expectExceptionMessageRegExp() does not exist on PHPUnit\Framework\TestCase. Did you maybe mean expectExceptionMessage()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

338
            parent::/** @scrutinizer ignore-call */ 
339
                    expectExceptionMessageRegExp($messageRegExp);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
339
340
            return;
341
        }
342
343
        if (null === $this->expectedException) {
344
            throw new \BadMethodCallException('Hmm this is message-regex without class *gg* - reflection?');
345
        }
346
347
        $this->setExpectedExceptionRegExp($this->expectedException, $messageRegExp);
348
    }
349
350
    /**
351
     * @param string $class
352
     * @param null|string $message
353
     * @param null|int|string $code
354
     *
355
     * @return void
356
     */
357
    public function setExpectedException($class, $message = null, $code = null)
358
    {
359
        if (is_callable('parent::' . __FUNCTION__)) {
360
            parent::setExpectedException($class, $message, $code);
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not exist on PHPUnit\Framework\TestCase. It seems like you code against a sub-type of PHPUnit\Framework\TestCase such as Ktomk\Pipelines\TestCase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

360
            parent::/** @scrutinizer ignore-call */ 
361
                    setExpectedException($class, $message, $code);
Loading history...
361
362
            return;
363
        }
364
365
        if (is_callable('parent::expectException')) {
366
            $this->expectException($class);
367
        }
368
369
        if (null !== $message && is_callable('parent::expectExceptionMessage')) {
370
            $this->expectExceptionMessage($message);
371
        }
372
373
        if (null !== $code && is_callable('parent::expectExceptionCode')) {
374
            $this->expectExceptionCode($code);
375
        }
376
    }
377
378
    /**
379
     * @param string $class
380
     * @param string $messageRegExp
381
     * @param null|int|string $code
382
     *
383
     * @return void
384
     */
385
    public function setExpectedExceptionRegExp($class, $messageRegExp = '', $code = null)
386
    {
387
        if (is_callable('parent::' . __FUNCTION__)) {
388
            parent::setExpectedExceptionRegExp($class, $messageRegExp, $code);
0 ignored issues
show
Bug introduced by
The method setExpectedExceptionRegExp() does not exist on PHPUnit\Framework\TestCase. It seems like you code against a sub-type of PHPUnit\Framework\TestCase such as Ktomk\Pipelines\TestCase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

388
            parent::/** @scrutinizer ignore-call */ 
389
                    setExpectedExceptionRegExp($class, $messageRegExp, $code);
Loading history...
389
390
            return;
391
        }
392
393
        if (is_callable('parent::expectException')) {
394
            $this->expectException($class);
395
        }
396
397
        if (is_callable('parent::expectExceptionMessageMatches')) {
398
            $this->expectExceptionMessageMatches($messageRegExp);
399
        }
400
401
        if (null !== $code && is_callable('parent::expectExceptionCode')) {
402
            $this->expectExceptionCode($code);
403
        }
404
    }
405
406
    /* mocks */
407
408
    /**
409
     * Returns a test double for the specified class.
410
     *
411
     * @param string $originalClassName
412
     *
413
     * @throws Exception
414
     * @throws \InvalidArgumentException
415
     *
416
     * @return MockObject
417
     */
418
    protected function createMock($originalClassName): MockObject
419
    {
420
        if (is_callable('parent::' . __FUNCTION__)) {
421
            return parent::createMock($originalClassName);
422
        }
423
424
        return $this->getMockBuilder($originalClassName)
425
            ->disableOriginalConstructor()
426
            ->disableOriginalClone()
427
            ->disableArgumentCloning()
428
            # phpunit ^4 does not have the method:
429
            # ->disallowMockingUnknownTypes()
430
            ->getMock();
431
    }
432
433
    /**
434
     * Returns a configured test double for the specified class.
435
     *
436
     * @param string $originalClassName
437
     * @param array $configuration
438
     *
439
     * @throws Exception
440
     * @throws \InvalidArgumentException
441
     *
442
     * @return MockObject
443
     */
444
    protected function createConfiguredMock($originalClassName, array $configuration): MockObject
445
    {
446
        if (is_callable('parent::' . __FUNCTION__)) {
447
            return parent::createConfiguredMock($originalClassName, $configuration);
448
        }
449
450
        $o = $this->createMock($originalClassName);
451
452
        foreach ($configuration as $method => $return) {
453
            $o->method($method)->willReturn($return);
454
        }
455
456
        return $o;
457
    }
458
459
    /**
460
     * Returns a partial test double for the specified class.
461
     *
462
     * @param string $originalClassName
463
     * @param string[] $methods
464
     *
465
     * @throws Exception
466
     * @throws \InvalidArgumentException
467
     *
468
     * @return MockObject
469
     */
470
    protected function createPartialMock($originalClassName, array $methods): MockObject
471
    {
472
        if (is_callable('parent::' . __FUNCTION__)) {
473
            return parent::createPartialMock($originalClassName, $methods);
474
        }
475
476
        return $this->getMockBuilder($originalClassName)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

476
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder($originalClassName)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
477
            ->disableOriginalConstructor()
478
            ->disableOriginalClone()
479
            ->disableArgumentCloning()
480
            # phpunit ^4 does not have the method:
481
            # ->disallowMockingUnknownTypes()
482
            ->setMethods(empty($methods) ? null : $methods)
483
            ->getMock();
484
    }
485
486
    /**
487
     * Asserts that a directory exists.
488
     *
489
     * @param string $directory
490
     * @param string $message
491
     *
492
     * @return void
493
     */
494
    private function shimAssertDirectoryExists($directory, $message = '')
495
    {
496
        if (!\is_string($directory)) {
0 ignored issues
show
introduced by
The condition is_string($directory) is always true.
Loading history...
497
            throw new \InvalidArgumentException('$directory is not a string');
498
        }
499
500
        /** @noinspection PhpUnitTestsInspection */
501
        self::assertTrue(\is_dir($directory), $message);
502
    }
503
504
    /**
505
     * Asserts that a directory exists.
506
     *
507
     * @param string $directory
508
     * @param string $message
509
     *
510
     * @return void
511
     */
512
    private function shimAssertDirectoryNotExists($directory, $message = '')
513
    {
514
        if (!\is_string($directory)) {
0 ignored issues
show
introduced by
The condition is_string($directory) is always true.
Loading history...
515
            throw new \InvalidArgumentException('$directory is not a string');
516
        }
517
518
        /** @noinspection PhpUnitTestsInspection */
519
        self::assertFalse(\is_dir($directory), $message);
520
    }
521
522
    /**
523
     * Asserts that a file exists.
524
     *
525
     * @param string $filename
526
     * @param string $message
527
     *
528
     * @return void
529
     */
530
    private function shimAssertFileExists($filename, $message = '')
531
    {
532
        if (!\is_string($filename)) {
0 ignored issues
show
introduced by
The condition is_string($filename) is always true.
Loading history...
533
            throw new \InvalidArgumentException('$filename is not a string');
534
        }
535
536
        /** @noinspection PhpUnitTestsInspection */
537
        self::assertTrue(\file_exists($filename), $message);
538
    }
539
540
    /**
541
     * Asserts that a file exists.
542
     *
543
     * @param string $filename
544
     * @param string $message
545
     *
546
     * @return void
547
     */
548
    private function shimAssertFileNotExists($filename, $message = '')
549
    {
550
        if (!\is_string($filename)) {
0 ignored issues
show
introduced by
The condition is_string($filename) is always true.
Loading history...
551
            throw new \InvalidArgumentException('$filename is not a string');
552
        }
553
554
        /** @noinspection PhpUnitTestsInspection */
555
        self::assertFalse(\file_exists($filename), $message);
556
    }
557
}
558