File: AbstractRulesetTestCase.php

package info (click to toggle)
php-codesniffer 3.11.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,772 kB
  • sloc: php: 84,771; pascal: 10,061; xml: 6,832; javascript: 2,096; sh: 11; makefile: 4
file content (115 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
 * Test case with helper methods for tests for the Ruleset class.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2024 PHPCSStandards and contributors
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Tests\Core\Ruleset;

use PHPUnit\Framework\TestCase;

abstract class AbstractRulesetTestCase extends TestCase
{

    /**
     * The fully qualified name of the PHPCS runtime exception class.
     *
     * @var string
     */
    const RUNTIME_EXCEPTION = 'PHP_CodeSniffer\Exceptions\RuntimeException';


    /**
     * Asserts that an object has a specified property in a PHPUnit cross-version compatible manner.
     *
     * @param string $propertyName The name of the property.
     * @param object $object       The object on which to check whether the property exists.
     * @param string $message      Optional failure message to display.
     *
     * @return void
     */
    protected function assertXObjectHasProperty($propertyName, $object, $message='')
    {
        if (method_exists($this, 'assertObjectHasProperty') === true) {
            $this->assertObjectHasProperty($propertyName, $object, $message);
        } else {
            // PHPUnit < 9.6.11.
            $this->assertObjectHasAttribute($propertyName, $object, $message);
        }

    }//end assertXObjectHasProperty()


    /**
     * Asserts that an object does not have a specified property
     * in a PHPUnit cross-version compatible manner.
     *
     * @param string $propertyName The name of the property.
     * @param object $object       The object on which to check whether the property exists.
     * @param string $message      Optional failure message to display.
     *
     * @return void
     */
    protected function assertXObjectNotHasProperty($propertyName, $object, $message='')
    {
        if (method_exists($this, 'assertObjectNotHasProperty') === true) {
            $this->assertObjectNotHasProperty($propertyName, $object, $message);
        } else {
            // PHPUnit < 9.6.11.
            $this->assertObjectNotHasAttribute($propertyName, $object, $message);
        }

    }//end assertXObjectNotHasProperty()


    /**
     * Helper method to tell PHPUnit to expect a PHPCS RuntimeException with a certain message
     * in a PHPUnit cross-version compatible manner.
     *
     * @param string $message The expected exception message.
     *
     * @return void
     */
    protected function expectRuntimeExceptionMessage($message)
    {
        if (method_exists($this, 'expectException') === true) {
            // PHPUnit 5+.
            $this->expectException(self::RUNTIME_EXCEPTION);
            $this->expectExceptionMessage($message);
        } else {
            // PHPUnit 4.
            $this->setExpectedException(self::RUNTIME_EXCEPTION, $message);
        }

    }//end expectRuntimeExceptionMessage()


    /**
     * Helper method to tell PHPUnit to expect a PHPCS RuntimeException which matches a regex patten
     * in a PHPUnit cross-version compatible manner.
     *
     * @param string $regex The regex which should match.
     *
     * @return void
     */
    protected function expectRuntimeExceptionRegex($regex)
    {
        if (method_exists($this, 'expectExceptionMessageMatches') === true) {
            $this->expectException(self::RUNTIME_EXCEPTION);
            $this->expectExceptionMessageMatches($regex);
        } else if (method_exists($this, 'expectExceptionMessageRegExp') === true) {
            // PHPUnit < 8.4.0.
            $this->expectException(self::RUNTIME_EXCEPTION);
            $this->expectExceptionMessageRegExp($regex);
        } else {
            // PHPUnit < 5.2.0.
            $this->setExpectedExceptionRegExp(self::RUNTIME_EXCEPTION, $regex);
        }

    }//end expectRuntimeExceptionRegex()


}//end class