-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestCaseTrait.php
More file actions
154 lines (127 loc) · 4.47 KB
/
Copy pathTestCaseTrait.php
File metadata and controls
154 lines (127 loc) · 4.47 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
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace WPMedia\PHPUnit;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
trait TestCaseTrait {
protected static function stubPolyfills() {
require_once __DIR__ . '/Fixtures/polyfills.php';
}
/**
* Gets the test data, if it exists, for this test class.
*
* @param string $dir Directory of the test class.
* @param string $filename Test data filename without the .php extension.
*
* @return array array of test data.
*/
protected function getTestData( $dir, $filename ) {
if ( empty( $dir ) || empty( $filename ) ) {
return [];
}
$dir = str_replace( [ 'Integration', 'Unit' ], 'Fixtures', $dir );
$dir = rtrim( $dir, '\\/' );
$testdata = "$dir/{$filename}.php";
return is_readable( $testdata )
? require $testdata
: [];
}
/**
* Get reflective access to the private/protected method.
*
* @param string $method_name Method name for which to gain access.
* @param string $class_name Name of the target class.
*
* @return ReflectionMethod
* @throws ReflectionException Throws an exception if method does not exist.
*/
protected function get_reflective_method( $method_name, $class_name ) {
$class = new ReflectionClass( $class_name );
$method = $class->getMethod( $method_name );
self::set_reflector_accessible( $method, true );
return $method;
}
/**
* Get reflective access to the private/protected property.
*
* @param string $property Property name for which to gain access.
* @param string|mixed $class Class name or instance.
*
* @return ReflectionProperty|string
* @throws ReflectionException Throws an exception if property does not exist.
*/
protected function get_reflective_property( $property, $class ) {
$class = new ReflectionClass( $class );
$property = $class->getProperty( $property );
self::set_reflector_accessible( $property, true );
return $property;
}
/**
* Set the value of a property or private property.
*
* @param mixed $value The value to set for the property.
* @param string $property Property name for which to gain access.
* @param mixed $instance Instance of the target object.
*
* @return ReflectionProperty|string
* @throws ReflectionException Throws an exception if property does not exist.
*/
protected function set_reflective_property( $value, $property, $instance ) {
$property = $this->get_reflective_property( $property, $instance );
// A static property has no target object: passing anything but null is deprecated as of PHP 8.3.
if ( $property->isStatic() ) {
$property->setValue( null, $value );
} else {
$property->setValue( $instance, $value );
}
self::set_reflector_accessible( $property, false );
return $property;
}
protected function getNonPublicPropertyValue( $property, $class, $instance = null ) {
$property = $this->get_reflective_property( $property, $class );
if ( is_null( $instance ) || $property->isStatic() ) {
return $property->getValue();
}
return $property->getValue( $instance );
}
/**
* Toggles accessibility on a reflected method or property.
*
* ReflectionMethod::setAccessible() and ReflectionProperty::setAccessible() have no effect since PHP 8.1, where
* reflection grants access to non-public members by default, and are deprecated as of PHP 8.5. They are still
* required on PHP 7.4 and 8.0.
*
* @param ReflectionMethod|ReflectionProperty $reflector Reflected method or property.
* @param bool $accessible Whether to make the member accessible.
*
* @return void
*/
private static function set_reflector_accessible( $reflector, $accessible ) {
if ( PHP_VERSION_ID >= 80100 ) {
return;
}
$reflector->setAccessible( $accessible );
}
/**
* Format the HTML by stripping out the whitespace between the HTML tags and then putting each tag on a separate
* line.
*
* Why? We can then compare the actual vs. expected HTML patterns without worrying about tabs, new lines, and extra
* spaces.
*
* @param string $html HTML to strip.
*
* @return string stripped HTML.
*/
protected function format_the_html( $html ) {
$html = trim( $html );
// Strip whitespace between the tags.
$html = preg_replace( '/(\>)\s*(\<)/m', '$1$2', $html );
// Strip whitespace at the end of a tag.
$html = preg_replace( '/(\>)\s*/m', '$1$2', $html );
// Strip whitespace at the start of a tag.
$html = preg_replace( '/\s*(\<)/m', '$1$2', $html );
return str_replace( '>', ">\n", $html );
}
}