Completed
Push — update/phpunit-php-8-packages ( 8ca541 )
by
unknown
91:32 queued 83:34
created

Test_Constants::tear_down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tests the Contacts package.
4
 *
5
 * @package automattic/jetpack-constants
6
 */
7
8
use Automattic\Jetpack\Constants;
9
use Brain\Monkey;
10
use Brain\Monkey\Functions;
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * Class Test_Constants
15
 */
16
class Test_Constants extends TestCase {
17
	/**
18
	 * Sets up the test.
19
	 *
20
	 * @before
21
	 */
22
	public function set_up() {
23
		if ( ! defined( 'JETPACK__VERSION' ) ) {
24
			define( 'JETPACK__VERSION', '7.5' );
25
		}
26
		Monkey\setUp();
27
	}
28
29
	/**
30
	 * Tears down the test.
31
	 *
32
	 * @after
33
	 */
34
	public function tear_down() {
35
		Monkey\tearDown();
36
		Constants::$set_constants = array();
37
	}
38
39
	/**
40
	 * Tests when a constant is defined via class.
41
	 *
42
	 * @covers Automattic\Jetpack\Constants::is_defined
43
	 */
44
	public function test_jetpack_constants_is_defined_when_constant_set_via_class() {
45
		Constants::set_constant( 'TEST', 'hello' );
46
		$this->assertTrue( Constants::is_defined( 'TEST' ) );
47
	}
48
49
	/**
50
	 * Tests when a constant is not set.
51
	 *
52
	 * @covers Automattic\Jetpack\Constants::is_defined
53
	 */
54
	public function test_jetpack_constants_is_defined_false_when_constant_not_set() {
55
		$this->assertFalse( Constants::is_defined( 'UNDEFINED' ) );
56
	}
57
58
	/**
59
	 * Tests when a constant is defined as true.
60
	 *
61
	 * @covers Automattic\Jetpack\Constants::is_defined
62
	 */
63
	public function test_jetpack_constants_is_defined_true_when_set_with_define() {
64
		$this->assertTrue( Constants::is_defined( 'JETPACK__VERSION' ) );
65
	}
66
67
	/**
68
	 * Tests when a constant is defined as null.
69
	 *
70
	 * @covers Automattic\Jetpack\Constants::is_defined
71
	 */
72
	public function test_jetpack_constants_is_defined_when_constant_set_to_null() {
73
		Constants::set_constant( 'TEST', null );
74
		$this->assertTrue( Constants::is_defined( 'TEST' ) );
75
	}
76
77
	/**
78
	 * Tests when a constant defaults to a constant.
79
	 *
80
	 * @covers Automattic\Jetpack\Constants::get_constant
81
	 */
82
	public function test_jetpack_constants_default_to_constant() {
83
		Functions\expect( 'apply_filters' )->never();
84
85
		$actual_output = Constants::get_constant( 'JETPACK__VERSION' );
86
87
		$this->assertEquals( JETPACK__VERSION, $actual_output );
88
	}
89
90
	/**
91
	 * Tests that null is returned when a constant is not set.
92
	 *
93
	 * @covers Automattic\Jetpack\Constants::get_constant
94
	 */
95
	public function test_jetpack_constants_get_constant_null_when_not_set() {
96
		$test_constant_name = 'UNDEFINED';
97
98
		Functions\expect( 'apply_filters' )->once()->with(
99
			'jetpack_constant_default_value',
100
			null,
101
			$test_constant_name
102
		)->andReturn( null );
103
104
		$actual_output = Constants::get_constant( $test_constant_name );
105
106
		$this->assertNull( $actual_output );
107
	}
108
109
	/**
110
	 * Tests that the class can override an existing constant.
111
	 *
112
	 * @covers Automattic\Jetpack\Constants::get_constant
113
	 */
114
	public function test_jetpack_constants_can_override_previously_defined_constant() {
115
		Functions\expect( 'apply_filters' )->never();
116
117
		$test_version = '1.0.0';
118
		Constants::set_constant( 'JETPACK__VERSION', $test_version );
119
120
		$this->assertEquals( Constants::get_constant( 'JETPACK__VERSION' ), $test_version );
121
	}
122
123
	/**
124
	 * Tests that an override to null returns null.
125
	 *
126
	 * @covers Automattic\Jetpack\Constants::get_constant
127
	 */
128
	public function test_jetpack_constants_override_to_null_gets_null() {
129
		Functions\expect( 'apply_filters' )->never();
130
131
		Constants::set_constant( 'JETPACK__VERSION', null );
132
133
		$this->assertNull( Constants::get_constant( 'JETPACK__VERSION' ) );
134
	}
135
136
	/**
137
	 * Tests that constant will use the filter value.
138
	 *
139
	 * @covers Automattic\Jetpack\Constants::get_constant
140
	 */
141
	public function test_jetpack_constants_get_constant_use_filter_value() {
142
		$test_constant_name  = 'TEST_CONSTANT';
143
		$test_constant_value = 'test value';
144
145
		Functions\expect( 'apply_filters' )->once()->with(
146
			'jetpack_constant_default_value',
147
			null,
148
			$test_constant_name
149
		)->andReturn( $test_constant_value );
150
151
		$actual_output = Constants::get_constant( $test_constant_name );
152
153
		$this->assertEquals( $test_constant_value, $actual_output );
154
	}
155
156
	/**
157
	 * Tests that a constant can add to an array.
158
	 *
159
	 * @covers Automattic\Jetpack\Constants::set_constant
160
	 */
161
	public function test_jetpack_constants_set_constants_adds_to_set_constants_array() {
162
		$key = 'TEST';
163
		Constants::set_constant( $key, '1' );
164
		$this->assertArrayHasKey( $key, Constants::$set_constants );
165
		$this->assertSame( '1', Constants::$set_constants[ $key ] );
166
	}
167
168
	/**
169
	 * Tests that all constants can be cleared.
170
	 *
171
	 * @covers Automattic\Jetpack\Constants::clear_constants
172
	 */
173
	public function test_jetpack_constants_can_clear_all_constants() {
174
		Constants::set_constant( 'JETPACK__VERSION', '1.0.0' );
175
		Constants::clear_constants();
176
		$this->assertEmpty( Constants::$set_constants );
177
	}
178
179
	/**
180
	 * Tests that a single constant can be cleared.
181
	 *
182
	 * @covers Automattic\Jetpack\Constants::clear_single_constant
183
	 */
184
	public function test_jetpack_constants_can_clear_single_constant() {
185
		Constants::set_constant( 'FIRST', '1' );
186
		Constants::set_constant( 'SECOND', '2' );
187
188
		$this->assertCount( 2, Constants::$set_constants );
189
190
		Constants::clear_single_constant( 'FIRST' );
191
192
		$this->assertCount( 1, Constants::$set_constants );
193
		$this->assertContains( 'SECOND', array_keys( Constants::$set_constants ) );
194
	}
195
196
	/**
197
	 * Tests that a single constant can be cleared with null.
198
	 *
199
	 * @covers Automattic\Jetpack\Constants::clear_single_constant
200
	 */
201
	public function test_jetpack_constants_can_clear_single_constant_when_null() {
202
		Constants::set_constant( 'TEST', null );
203
		$this->assertCount( 1, Constants::$set_constants );
204
205
		Constants::clear_single_constant( 'TEST' );
206
207
		$this->assertEmpty( Constants::$set_constants );
208
	}
209
210
	/**
211
	 * Tests is_true.
212
	 *
213
	 * @covers Automattic\Jetpack\Constants::is_true
214
	 */
215
	public function test_jetpack_constants_is_true_method() {
216
		$this->assertFalse( Constants::is_true( 'FOO' ), 'unset constant returns true' );
217
		Constants::set_constant( 'FOO', false );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
218
219
		$this->assertFalse( Constants::is_true( 'FOO' ), 'false constant returns true' );
220
		Constants::set_constant( 'FOO', true );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
221
222
		$this->assertTrue( Constants::is_true( 'FOO' ), 'true constant returns false' );
223
	}
224
}
225