Completed
Push — add/signature-error-reporting ( 95a087...072d13 )
by
unknown
18:31 queued 09:22
created

Test_Constants   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 132
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 2
A tearDown() 0 4 1
A test_jetpack_constants_is_defined_when_constant_set_via_class() 0 4 1
A test_jetpack_constants_is_defined_false_when_constant_not_set() 0 3 1
A test_jetpack_constants_is_defined_true_when_set_with_define() 0 3 1
A test_jetpack_constants_is_defined_when_constant_set_to_null() 0 4 1
A test_jetpack_constants_default_to_constant() 0 3 1
A test_jetpack_constants_get_constant_null_when_not_set() 0 3 1
A test_jetpack_constants_can_override_previously_defined_constant() 0 5 1
A test_jetpack_constants_override_to_null_gets_null() 0 4 1
A test_jetpack_constants_set_constants_adds_to_set_constants_array() 0 6 1
A test_jetpack_constants_can_clear_all_constants() 0 5 1
A test_jetpack_constants_can_clear_single_constant() 0 11 1
A test_jetpack_constants_can_clear_single_constant_when_null() 0 8 1
A test_jetpack_constants_is_true_method() 0 9 1
1
<?php
2
3
use Automattic\Jetpack\Constants;
4
use PHPUnit\Framework\TestCase;
5
6
class Test_Constants extends TestCase {
7
	public function setUp() {
8
		if ( ! defined( 'JETPACK__VERSION' ) ) {
9
			define( 'JETPACK__VERSION', '7.5' );
10
		}
11
	}
12
13
	public function tearDown() {
14
		parent::tearDown();
15
		Constants::$set_constants = array();
16
	}
17
18
	/**
19
	 * @covers Automattic\Jetpack\Constants::is_defined
20
	 */
21
	function test_jetpack_constants_is_defined_when_constant_set_via_class() {
22
		Constants::set_constant( 'TEST', 'hello' );
23
		$this->assertTrue( Constants::is_defined( 'TEST' ) );
24
	}
25
26
	/**
27
	 * @covers Automattic\Jetpack\Constants::is_defined
28
	 */
29
	function test_jetpack_constants_is_defined_false_when_constant_not_set() {
30
		$this->assertFalse( Constants::is_defined( 'UNDEFINED' ) );
31
	}
32
33
	/**
34
	 * @covers Automattic\Jetpack\Constants::is_defined
35
	 */
36
	function test_jetpack_constants_is_defined_true_when_set_with_define() {
37
		$this->assertTrue( Constants::is_defined( 'JETPACK__VERSION' ) );
38
	}
39
40
	/**
41
	 * @covers Automattic\Jetpack\Constants::is_defined
42
	 */
43
	function test_jetpack_constants_is_defined_when_constant_set_to_null() {
44
		Constants::set_constant( 'TEST', null );
45
		$this->assertTrue( Constants::is_defined( 'TEST' ) );
46
	}
47
48
	/**
49
	 * @covers Automattic\Jetpack\Constants::get_constant
50
	 */
51
	function test_jetpack_constants_default_to_constant() {
52
		$this->assertEquals( Constants::get_constant( 'JETPACK__VERSION' ), JETPACK__VERSION );
53
	}
54
55
	/**
56
	 * @covers Automattic\Jetpack\Constants::get_constant
57
	 */
58
	function test_jetpack_constants_get_constant_null_when_not_set() {
59
		$this->assertNull( Constants::get_constant( 'UNDEFINED' ) );
60
	}
61
62
	/**
63
	 * @covers Automattic\Jetpack\Constants::get_constant
64
	 */
65
	function test_jetpack_constants_can_override_previously_defined_constant() {
66
		$test_version = '1.0.0';
67
		Constants::set_constant( 'JETPACK__VERSION', $test_version );
68
		$this->assertEquals( Constants::get_constant( 'JETPACK__VERSION' ), $test_version );
69
	}
70
71
	/**
72
	 * @covers Automattic\Jetpack\Constants::get_constant
73
	 */
74
	function test_jetpack_constants_override_to_null_gets_null() {
75
		Constants::set_constant( 'JETPACK__VERSION', null );
76
		$this->assertNull( Constants::get_constant( 'JETPACK__VERSION' ) );
77
	}
78
79
	/**
80
	 * @covers Automattic\Jetpack\Constants::set_constant
81
	 */
82
	function test_jetpack_constants_set_constants_adds_to_set_constants_array() {
83
		$key = 'TEST';
84
		Constants::set_constant( $key, '1' );
85
		$this->assertArrayHasKey( $key, Constants::$set_constants );
86
		$this->assertEquals( '1', Constants::$set_constants[ $key ] );
87
	}
88
89
	/**
90
	 * @covers Automattic\Jetpack\Constants::clear_constants
91
	 */
92
	function test_jetpack_constants_can_clear_all_constants() {
93
		Constants::set_constant( 'JETPACK__VERSION', '1.0.0' );
94
		Constants::clear_constants();
95
		$this->assertEmpty( Constants::$set_constants );
96
	}
97
98
	/**
99
	 * @covers Automattic\Jetpack\Constants::clear_single_constant
100
	 */
101
	function test_jetpack_constants_can_clear_single_constant() {
102
		Constants::set_constant( 'FIRST', '1' );
103
		Constants::set_constant( 'SECOND', '2' );
104
105
		$this->assertCount( 2, Constants::$set_constants );
106
107
		Constants::clear_single_constant( 'FIRST' );
108
109
		$this->assertCount( 1, Constants::$set_constants );
110
		$this->assertContains( 'SECOND', array_keys( Constants::$set_constants ) );
111
	}
112
113
	/**
114
	 * @covers Automattic\Jetpack\Constants::clear_single_constant
115
	 */
116
	function test_jetpack_constants_can_clear_single_constant_when_null() {
117
		Constants::set_constant( 'TEST', null );
118
		$this->assertCount( 1, Constants::$set_constants );
119
120
		Constants::clear_single_constant( 'TEST' );
121
122
		$this->assertEmpty( Constants::$set_constants );
123
	}
124
125
	/**
126
	 * @covers Automattic\Jetpack\Constants::is_true
127
	 */
128
	function test_jetpack_constants_is_true_method() {
129
		$this->assertFalse( Constants::is_true( 'FOO' ), 'unset constant returns true' );
130
		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...
131
132
		$this->assertFalse( Constants::is_true( 'FOO' ), 'false constant returns true' );
133
		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...
134
135
		$this->assertTrue( Constants::is_true( 'FOO' ), 'true constant returns false');
136
	}
137
}
138