Completed
Push — add/sync-classmapped-package ( bf1ff0...7eeaec )
by Marin
12:55 queued 01:37
created

Test_Manager::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Constants\Manager as Constants_Manager;
4
use PHPUnit\Framework\TestCase;
5
6
class Test_Manager 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_Manager::$set_constants = array();
16
	}
17
18
	// Constants_Manager::is_defined()
19
20
	function test_jetpack_constants_is_defined_when_constant_set_via_class() {
21
		Constants_Manager::set_constant( 'TEST', 'hello' );
22
		$this->assertTrue( Constants_Manager::is_defined( 'TEST' ) );
23
	}
24
25
	function test_jetpack_constants_is_defined_false_when_constant_not_set() {
26
		$this->assertFalse( Constants_Manager::is_defined( 'UNDEFINED' ) );
27
	}
28
29
	function test_jetpack_constants_is_defined_true_when_set_with_define() {
30
		$this->assertTrue( Constants_Manager::is_defined( 'JETPACK__VERSION' ) );
31
	}
32
33
	function test_jetpack_constants_is_defined_when_constant_set_to_null() {
34
		Constants_Manager::set_constant( 'TEST', null );
35
		$this->assertTrue( Constants_Manager::is_defined( 'TEST' ) );
36
	}
37
38
	// Constants_Manager::get_constant()
39
40
	function test_jetpack_constants_default_to_constant() {
41
		$this->assertEquals( Constants_Manager::get_constant( 'JETPACK__VERSION' ), JETPACK__VERSION );
42
	}
43
44
	function test_jetpack_constants_get_constant_null_when_not_set() {
45
		$this->assertNull( Constants_Manager::get_constant( 'UNDEFINED' ) );
46
	}
47
48
	function test_jetpack_constants_can_override_previously_defined_constant() {
49
		$test_version = '1.0.0';
50
		Constants_Manager::set_constant( 'JETPACK__VERSION', $test_version );
51
		$this->assertEquals( Constants_Manager::get_constant( 'JETPACK__VERSION' ), $test_version );
52
	}
53
54
	function test_jetpack_constants_override_to_null_gets_null() {
55
		Constants_Manager::set_constant( 'JETPACK__VERSION', null );
56
		$this->assertNull( Constants_Manager::get_constant( 'JETPACK__VERSION' ) );
57
	}
58
59
	// Constants_Manager::set_constant()
60
61
	function test_jetpack_constants_set_constants_adds_to_set_constants_array() {
62
		$key = 'TEST';
63
		Constants_Manager::set_constant( $key, '1' );
64
		$this->assertArrayHasKey( $key, Constants_Manager::$set_constants );
65
		$this->assertEquals( '1', Constants_Manager::$set_constants[ $key ] );
66
	}
67
68
	// Constants_Manager::clear_constants()
69
70
	function test_jetpack_constants_can_clear_all_constants() {
71
		Constants_Manager::set_constant( 'JETPACK__VERSION', '1.0.0' );
72
		Constants_Manager::clear_constants();
73
		$this->assertEmpty( Constants_Manager::$set_constants );
74
	}
75
76
	// Constants_Manager::clear_single_constant()
77
78
	function test_jetpack_constants_can_clear_single_constant() {
79
		Constants_Manager::set_constant( 'FIRST', '1' );
80
		Constants_Manager::set_constant( 'SECOND', '2' );
81
82
		$this->assertCount( 2, Constants_Manager::$set_constants );
83
84
		Constants_Manager::clear_single_constant( 'FIRST' );
85
86
		$this->assertCount( 1, Constants_Manager::$set_constants );
87
		$this->assertContains( 'SECOND', array_keys( Constants_Manager::$set_constants ) );
88
	}
89
90
	function test_jetpack_constants_can_clear_single_constant_when_null() {
91
		Constants_Manager::set_constant( 'TEST', null );
92
		$this->assertCount( 1, Constants_Manager::$set_constants );
93
94
		Constants_Manager::clear_single_constant( 'TEST' );
95
96
		$this->assertEmpty( Constants_Manager::$set_constants );
97
	}
98
99
	// Jetpack_Constant::is_true
100
	function test_jetpack_constants_is_true_method() {
101
		$this->assertFalse( Constants_Manager::is_true( 'FOO' ), 'unset constant returns true' );
102
		Constants_Manager::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...
103
104
		$this->assertFalse( Constants_Manager::is_true( 'FOO' ), 'false constant returns true' );
105
		Constants_Manager::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...
106
107
		$this->assertTrue( Constants_Manager::is_true( 'FOO' ), 'true constant returns false');
108
	}
109
}
110