Repository navigation
Expand file tree
/
Copy pathMock_Action.php
More file actions
143 lines (128 loc) · 2.91 KB
/
Copy pathMock_Action.php
File metadata and controls
143 lines (128 loc) · 2.91 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* This file contains the MockAction class
*
* @package Mantle
*/
namespace Mantle\Testing;
/**
* Helper class for testing code that involves actions and filters.
*
* Typical use: $ma = new MockAction(); add_action( 'foo', [ $ma, 'action' ] );
*
* @deprecated Used as a shim for core tests. New tests should use Mantle's
* testing hook helpers {@see https://mantle.alley.com/docs/testing/hooks}.
*/
class Mock_Action {
/**
* Events log. When a callback fires, it will log an event to this array.
*
* @var array<mixed>
*/
public $events = [];
/**
* Constructor.
*/
public function __construct() {
$this->reset();
}
/**
* Reset the events.
*/
public function reset(): void {
$this->events = [];
}
/**
* Get the current or last-run filter.
*
* @return mixed|string
*/
public function current_filter() {
if ( is_callable( 'current_filter' ) ) {
return current_filter();
}
global $wp_actions;
return end( $wp_actions );
}
/**
* Action callback.
*
* @param mixed ...$args Arguments passed to the callback.
* @return mixed First argument.
*/
public function action( ...$args ) {
$this->events[] = [
'action' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
];
return $args[0];
}
/**
* Filter callback.
*
* @param mixed ...$args Arguments passed to the callback.
* @return mixed First argument.
*/
public function filter( ...$args ) {
$this->events[] = [
'filter' => __FUNCTION__,
'tag' => $this->current_filter(),
'args' => $args,
];
return $args[0];
}
/**
* Filter callback for 'all'.
*
* @param string $tag Action/filter tag.
* @param mixed ...$args Arguments passed to the callback.
*/
public function filter_all( $tag, ...$args ): void {
// This one doesn't return the result, so it's safe to use with the 'all' filter.
$this->events[] = [
'filter' => __FUNCTION__,
'tag' => $tag,
'args' => $args,
];
}
/**
* Get the events (actions/filters, tags, args) that the mock has logged.
*
* @return array Events.
*/
public function get_events() {
return $this->events;
}
/**
* Return a count of the number of times the action was called since the last
* reset.
*
* @param string $tag Optional. Action or filter tag. If absent, counts all
* events.
*/
public function get_call_count( $tag = '' ): int {
if ( ! empty( $tag ) ) {
$count = 0;
foreach ( $this->events as $event ) {
if ( $event['action'] === $tag ) {
++$count;
}
}
return $count;
}
return count( $this->events );
}
/**
* Return an array of the tags that triggered calls to this action.
*/
public function get_tags(): array {
return array_column( $this->events, 'tag' );
}
/**
* Return an array of args passed in calls to this action.
*/
public function get_args(): array {
return array_column( $this->events, 'args' );
}
}