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

10up / wp_mock / 28073012213

24 Jun 2026 03:26AM UTC coverage: 70.899% (+4.8%) from 66.142%
28073012213

Pull #269

github

web-flow
Merge 221736aee into 446ea7083
Pull Request #269: Compatibility with PHPUnit 10, 11, 12, 13 and PHP 8.1-8.4

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

1 existing line in 1 file now uncovered.

497 of 701 relevant lines covered (70.9%)

3.59 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: it is reported per test
10
 * and fails the suite when `failOnDeprecation="true"` is set in the PHPUnit configuration.
11
 *
12
 * To flag a method as deprecated, call the following from within the deprecated method's logic:
13
 *
14
 *     \WP_Mock::getDeprecatedMethodListener()->logDeprecatedCall(__METHOD__, func_get_args());
15
 */
16
class DeprecatedMethodListener
17
{
18
    /** @var array<array{string, array<mixed>}> array of logged deprecated method calls with their arguments, if any */
19
    protected $deprecatedCalls = [];
20

21
    /** @var string */
22
    protected $testName = 'test';
23

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

34
        return $this;
2✔
35
    }
36

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

52
        trigger_error($this->buildMessage($method, $args), E_USER_DEPRECATED);
4✔
53

54
        return $this;
4✔
55
    }
56

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

66
        return $this;
15✔
67
    }
68

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

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

84
        return $message;
4✔
85
    }
86

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

112
        return 'Unknown Value';
×
113
    }
114
}
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