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

10up / wp_mock / 28777471084

06 Jul 2026 08:13AM UTC coverage: 70.94% (+4.8%) from 66.142%
28777471084

Pull #269

github

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

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

1 existing line in 1 file now uncovered.

498 of 702 relevant lines covered (70.94%)

3.59 hits per line

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

92.0
/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 optional test-name context for messages; empty unless {@see setTestName()} is called */
22
    protected $testName = '';
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
        $context = $this->testName !== '' ? sprintf(' inside %s', $this->testName) : '';
4✔
79

80
        $message = sprintf('Deprecated WP_Mock call%s: %s', $context, $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