• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

10up / wp_mock / 28018311463

23 Jun 2026 10:02AM UTC coverage: 59.201% (-6.9%) from 66.142%
28018311463

Pull #269

github

web-flow
Merge 53328419b into 446ea7083
Pull Request #269: Compatibility with PHPUnit 9, 10, 11, 12, 13 and PHP 7.4-8.4

12 of 12 new or added lines in 3 files covered. (100.0%)

34 existing lines in 3 files now uncovered.

415 of 701 relevant lines covered (59.2%)

2.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/php/WP_Mock/DeprecatedMethodListener.php
1
<?php
2

3
namespace WP_Mock;
4

5
/**
6
 * Internal handler for deprecated method calls.
7
 *
8
 * Flags usage of deprecated WP_Mock methods by emitting an {@see E_USER_DEPRECATED} notice,
9
 * which PHPUnit captures and attributes to the running test natively across all supported
10
 * versions:
11
 *  - PHPUnit 9.x: surfaces (and, with `convertDeprecationsToExceptions="true"`, fails the test).
12
 *  - PHPUnit 10+: reported per test; fails the suite when `failOnDeprecation="true"`.
13
 *
14
 * To flag a method as deprecated, call the following from within the deprecated method's logic:
15
 *
16
 *     \WP_Mock::getDeprecatedMethodListener()->logDeprecatedCall(__METHOD__, func_get_args());
17
 */
18
class DeprecatedMethodListener
19
{
20
    /** @var array<array{string, array<mixed>}> array of logged deprecated method calls with their arguments, if any */
21
    protected $deprecatedCalls = [];
22

23
    /** @var string */
24
    protected $testName = 'test';
25

26
    /**
27
     * Sets the test name in context.
28
     *
29
     * @param string $testName
30
     * @return $this
31
     */
32
    public function setTestName(string $testName): DeprecatedMethodListener
33
    {
34
        $this->testName = $testName;
2✔
35

36
        return $this;
2✔
37
    }
38

39
    /**
40
     * Logs a deprecated method call and emits a deprecation notice.
41
     *
42
     * The call is recorded (for inspection via {@see DeprecatedMethodListener::reset()} consumers)
43
     * and an {@see E_USER_DEPRECATED} notice is triggered immediately so PHPUnit reports it against
44
     * the running test.
45
     *
46
     * @param string $method
47
     * @param array<mixed> $args
48
     * @return $this
49
     */
50
    public function logDeprecatedCall(string $method, array $args = []): DeprecatedMethodListener
51
    {
52
        $this->deprecatedCalls[] = [$method, $args];
4✔
53

54
        trigger_error($this->buildMessage($method, $args), E_USER_DEPRECATED);
4✔
55

56
        return $this;
4✔
57
    }
58

59
    /**
60
     * Resets tracking of deprecated method calls.
61
     *
62
     * @return $this
63
     */
64
    public function reset(): DeprecatedMethodListener
65
    {
66
        $this->deprecatedCalls = [];
15✔
67

68
        return $this;
15✔
69
    }
70

71
    /**
72
     * Builds the deprecation message for a single deprecated method call.
73
     *
74
     * @param string $method
75
     * @param array<mixed> $args
76
     * @return string
77
     */
78
    protected function buildMessage(string $method, array $args): string
79
    {
80
        $message = sprintf('Deprecated WP_Mock call inside %s: %s', $this->testName, $method);
4✔
81

82
        if (! empty($args)) {
4✔
83
            $message .= ' '.json_encode(array_map([$this, 'toScalar'], $args));
4✔
84
        }
85

86
        return $message;
4✔
87
    }
88

89
    /**
90
     * Transforms a value for use in a JSON string.
91
     *
92
     * @param mixed $value
93
     * @return string|bool|null|float|int
94
     */
95
    protected function toScalar($value)
96
    {
97
        if ($value === null) {
14✔
98
            return null;
1✔
99
        } elseif (is_scalar($value)) {
13✔
100
            return $value;
9✔
101
        } elseif (is_object($value)) {
4✔
102
            return '<'.get_class($value).':'.spl_object_hash($value).'>';
2✔
103
        } elseif (is_array($value)) {
2✔
104
            if (is_callable($value)) {
1✔
105
                /** @phpstan-ignore-next-line */
UNCOV
106
                return '['.implode(',', array_map(array($this, 'toScalar'), $value)).']';
×
107
            } else {
108
                return 'Array(['.count($value).'] ...)';
1✔
109
            }
110
        } elseif (is_resource($value)) {
1✔
111
            return 'Resource';
1✔
112
        }
113

114
        return 'Unknown Value';
×
115
    }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc