-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest-case-trait.php
More file actions
130 lines (115 loc) · 3.48 KB
/
test-case-trait.php
File metadata and controls
130 lines (115 loc) · 3.48 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
<?php
/**
* Test Case Traits for the Plugin's test suites.
*
* @package KnowTheCode\GitContributing\Tests\PHP
* @since 1.0.0
* @link https://github.com/KnowTheCode/git-contributing
* @license GNU-2.0+
*/
namespace KnowTheCode\GitContributing\Tests\PHP;
/**
* Test_Case_Trait
*
* @package KnowTheCode\GitContributing\Tests\Unit
*/
trait Test_Case_Trait {
/**
* The path to test suite's root directory.
*
* @var string
*/
protected $test_root_dir;
/**
* Load the original functions into memory before we start.
*
* Then in our tests, we monkey patch via Brain Monkey, which redefines the original function.
* At tear down, the original function is restored in Brain Monkey, by calling Patchwork\restoreAll().
*
* @since 1.0.0
*
* @param array $files Array of files to load into memory.
*
* @return void
*/
protected function load_original_functions( array $files ) {
foreach ( $files as $file ) {
require_once $this->test_root_dir . $file;
}
}
/**
* 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.
*
* @since 1.0.0
*
* @param string $html HTML to strip.
*
* @return string
*/
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 );
}
/**
* Get reflective access to the private method.
*
* @since 1.0.0
*
* @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 );
$method->setAccessible( true );
return $method;
}
/**
* Get reflective access to the private property.
*
* @since 1.0.0
*
* @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 );
$property->setAccessible( true );
return $property;
}
/**
* Set the value of a property or private property.
*
* @since 1.0.0
*
* @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 );
$property->setValue( $instance, $value );
$property->setAccessible( false );
return $property;
}
}