aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/TestCase.php
blob: 781e7aa36a09d220a4d4408e5e706a6606a3aee4 (plain) (blame)
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
<?php

declare(strict_types=1);

namespace Shaarli;

/**
 * Helper class extending \PHPUnit\Framework\TestCase.
 * Used to make Shaarli UT run on multiple versions of PHPUnit.
 */
class TestCase extends \PHPUnit\Framework\TestCase
{
    /**
     * expectExceptionMessageRegExp has been removed and replaced by expectExceptionMessageMatches in PHPUnit 9.
     */
    public function expectExceptionMessageRegExp(string $regularExpression): void
    {
        if (method_exists($this, 'expectExceptionMessageMatches')) {
            $this->expectExceptionMessageMatches($regularExpression);
        } else {
            parent::expectExceptionMessageRegExp($regularExpression);
        }
    }

    /**
     * assertContains is now used for iterable, strings should use assertStringContainsString
     */
    public function assertContainsPolyfill($expected, $actual, string $message = ''): void
    {
        if (is_string($actual) && method_exists($this, 'assertStringContainsString')) {
            static::assertStringContainsString($expected, $actual, $message);
        } else {
            static::assertContains($expected, $actual, $message);
        }
    }

    /**
     * assertNotContains is now used for iterable, strings should use assertStringNotContainsString
     */
    public function assertNotContainsPolyfill($expected, $actual, string $message = ''): void
    {
        if (is_string($actual) && method_exists($this, 'assertStringNotContainsString')) {
            static::assertStringNotContainsString($expected, $actual, $message);
        } else {
            static::assertNotContains($expected, $actual, $message);
        }
    }

    /**
     * assertFileNotExists has been renamed in assertFileDoesNotExist
     */
    public static function assertFileNotExists(string $filename, string $message = ''): void
    {
        if (method_exists(TestCase::class, 'assertFileDoesNotExist')) {
            static::assertFileDoesNotExist($filename, $message);
        } else {
            parent::assertFileNotExists($filename, $message);
        }
    }

    /**
     * assertRegExp has been renamed in assertMatchesRegularExpression
     */
    public static function assertRegExp(string $pattern, string $string, string $message = ''): void
    {
        if (method_exists(TestCase::class, 'assertMatchesRegularExpression')) {
            static::assertMatchesRegularExpression($pattern, $string, $message);
        } else {
            parent::assertRegExp($pattern, $string, $message);
        }
    }

    public function isInTestsContext(): bool
    {
        return true;
    }
}