Completed
Push — master ( e460d4...f97631 )
by Der Mundschenk
13:51 queued 40s
created

TestCase::setStaticValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
/**
3
 *  This file is part of mundschenk-at/check-wp-requirements.
4
 *
5
 *  Copyright 2017-2018 Peter Putzer.
6
 *
7
 *  This program is free software; you can redistribute it and/or
8
 *  modify it under the terms of the GNU General Public License
9
 *  as published by the Free Software Foundation; either version 2
10
 *  of the License, or ( at your option ) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 *
21
 *  @package mundschenk-at/check-wp-requirements/tests
22
 *  @license http://www.gnu.org/licenses/gpl-2.0.html
23
 */
24
25
namespace Mundschenk\Check_WordPress_Requirements\Tests;
26
27
use Brain\Monkey;
28
29
/**
30
 * Abstract base class for our unit tests.
31
 */
32
abstract class TestCase extends \PHPUnit\Framework\TestCase {
33
34
	/**
35
	 * Set up Brain Monkey.
36
	 */
37
	protected function setUp() {
38
		parent::setUp();
39
		Monkey\setUp();
40
	}
41
42
	/**
43
	 * Tear down Brain Monkey.
44
	 */
45
	protected function tearDown() {
46
		Monkey\tearDown();
47
		parent::tearDown();
48
	}
49
50
	/**
51
	 * Call protected/private method of a class.
52
	 *
53
	 * @param object $object      Instantiated object that we will run method on.
54
	 * @param string $method_name Method name to call.
55
	 * @param array  $parameters  Array of parameters to pass into method.
56
	 * @param string $classname   Optional. The class to use for accessing private properties.
57
	 *
58
	 * @return mixed Method return.
59
	 */
60
	protected function invokeMethod( $object, $method_name, array $parameters = [], $classname = '' ) {
61
		if ( empty( $classname ) ) {
62
			$classname = get_class( $object );
63
		}
64
65
		$reflection = new \ReflectionClass( $classname );
66
		$method     = $reflection->getMethod( $method_name );
67
		$method->setAccessible( true );
68
69
		return $method->invokeArgs( $object, $parameters );
70
	}
71
72
	/**
73
	 * Call protected/private method of a class.
74
	 *
75
	 * @param string $classname   A class that we will run the method on.
76
	 * @param string $method_name Method name to call.
77
	 * @param array  $parameters  Array of parameters to pass into method.
78
	 *
79
	 * @return mixed Method return.
80
	 */
81
	protected function invokeStaticMethod( $classname, $method_name, array $parameters = [] ) {
82
		$reflection = new \ReflectionClass( $classname );
83
		$method     = $reflection->getMethod( $method_name );
84
		$method->setAccessible( true );
85
86
		return $method->invokeArgs( null, $parameters );
87
	}
88
89
	/**
90
	 * Sets the value of a private/protected property of a class.
91
	 *
92
	 * @param string     $classname     A class whose property we will access.
93
	 * @param string     $property_name Property to set.
94
	 * @param mixed|null $value         The new value.
95
	 */
96 View Code Duplication
	protected function setStaticValue( $classname, $property_name, $value ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
		$reflection = new \ReflectionClass( $classname );
98
		$property   = $reflection->getProperty( $property_name );
99
		$property->setAccessible( true );
100
		$property->setValue( $value );
101
	}
102
103
	/**
104
	 * Sets the value of a private/protected property of a class.
105
	 *
106
	 * @param object     $object        Instantiated object that we will run method on.
107
	 * @param string     $property_name Property to set.
108
	 * @param mixed|null $value         The new value.
109
	 * @param string     $classname     Optional. The class to use for accessing private properties.
110
	 */
111
	protected function setValue( $object, $property_name, $value, $classname = '' ) {
112
		if ( empty( $classname ) ) {
113
			$classname = get_class( $object );
114
		}
115
116
		$reflection = new \ReflectionClass( $classname );
117
		$property   = $reflection->getProperty( $property_name );
118
		$property->setAccessible( true );
119
		$property->setValue( $object, $value );
120
	}
121
122
	/**
123
	 * Retrieves the value of a private/protected property of a class.
124
	 *
125
	 * @param string $classname     A class whose property we will access.
126
	 * @param string $property_name Property to set.
127
	 *
128
	 * @return mixed
129
	 */
130 View Code Duplication
	protected function getStaticValue( $classname, $property_name ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
		$reflection = new \ReflectionClass( $classname );
132
		$property   = $reflection->getProperty( $property_name );
133
		$property->setAccessible( true );
134
135
		return $property->getValue();
136
	}
137
138
	/**
139
	 * Retrieves the value of a private/protected property of a class.
140
	 *
141
	 * @param object $object        Instantiated object that we will run method on.
142
	 * @param string $property_name Property to set.
143
	 * @param string $classname     Optional. The class to use for accessing private properties.
144
	 *
145
	 * @return mixed
146
	 */
147 View Code Duplication
	protected function getValue( $object, $property_name, $classname = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
		if ( empty( $classname ) ) {
149
			$classname = get_class( $object );
150
		}
151
152
		$reflection = new \ReflectionClass( $classname );
153
		$property   = $reflection->getProperty( $property_name );
154
		$property->setAccessible( true );
155
156
		return $property->getValue( $object );
157
	}
158
159
	/**
160
	 * Reports an error identified by $message if $attribute in $object does not have the $key.
161
	 *
162
	 * @param string $key       The array key.
163
	 * @param string $attribute The attribute name.
164
	 * @param object $object    The object.
165
	 * @param string $message   Optional. Default ''.
166
	 */
167 View Code Duplication
	protected function assertAttributeArrayHasKey( $key, $attribute, $object, $message = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
		$ref  = new \ReflectionClass( get_class( $object ) );
169
		$prop = $ref->getProperty( $attribute );
170
		$prop->setAccessible( true );
171
172
		return $this->assertArrayHasKey( $key, $prop->getValue( $object ), $message );
173
	}
174
175
	/**
176
	 * Reports an error identified by $message if $attribute in $object does have the $key.
177
	 *
178
	 * @param string $key       The array key.
179
	 * @param string $attribute The attribute name.
180
	 * @param object $object    The object.
181
	 * @param string $message   Optional. Default ''.
182
	 */
183 View Code Duplication
	protected function assertAttributeArrayNotHasKey( $key, $attribute, $object, $message = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
		$ref  = new \ReflectionClass( get_class( $object ) );
185
		$prop = $ref->getProperty( $attribute );
186
		$prop->setAccessible( true );
187
188
		return $this->assertArrayNotHasKey( $key, $prop->getValue( $object ), $message );
189
	}
190
}
191