Completed
Push — add/stats-package ( 873a22...c3aabb )
by
unknown
230:03 queued 222:25
created

tests/php/class-test-terms-of-service.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Tests the TOS package.
4
 *
5
 * @package automattic/jetpack-terms-of-service
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use PHPUnit\Framework\TestCase;
11
use phpmock\Mock;
12
use phpmock\MockBuilder;
13
14
/**
15
 * Class Test_Terms_Of_Service
16
 *
17
 * @package Automattic\Jetpack
18
 */
19
class Test_Terms_Of_Service extends TestCase {
20
21
	/**
22
	 * Test setup.
23
	 */
24
	public function setUp() {
25
		$this->terms_of_service = $this->createPartialMock(
26
			__NAMESPACE__ . '\\Terms_Of_Service',
27
			array( 'get_raw_has_agreed', 'is_development_mode', 'set_agree', 'is_active', 'set_reject' )
28
		);
29
	}
30
31
	/**
32
	 * Test teardown.
33
	 */
34
	public function tearDown() {
35
		Mock::disableAll();
36
	}
37
38
	/**
39
	 * Tests the agree function.
40
	 *
41
	 * @covers Automattic\Jetpack\Terms_Of_Service->agree
42
	 */
43
	public function test_agree() {
44
		$this->mock_function( 'do_action', null, 'jetpack_agreed_to_terms_of_service' );
45
		$this->terms_of_service->expects( $this->once() )->method( 'set_agree' );
46
47
		$this->terms_of_service->agree();
48
	}
49
50
	/**
51
	 * Tests the revoke function.
52
	 *
53
	 * @covers Automattic\Jetpack\Terms_Of_Service->revoke
54
	 */
55
	public function test_revoke() {
56
		$this->mock_function( 'do_action', null, 'jetpack_reject_terms_of_service' );
57
		$this->terms_of_service->expects( $this->once() )->method( 'set_reject' );
58
59
		$this->terms_of_service->reject();
60
	}
61
62
	/**
63
	 * Tests if has_agreed returns correctly if TOS not agreed to.
64
	 *
65
	 * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed
66
	 */
67
	public function test_returns_false_if_not_agreed() {
68
		$this->terms_of_service->expects( $this->once() )->method( 'get_raw_has_agreed' )->willReturn( false );
69
		$this->assertFalse( $this->terms_of_service->has_agreed() );
70
	}
71
72
	/**
73
	 * Tests if has_agreed returns corrected if agreed but in dev mode.
74
	 *
75
	 * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed
76
	 */
77
	public function test_returns_false_if_has_agreed_but_is_development_mode() {
78
		// is_development_mode.
79
		$this->terms_of_service->method( 'get_raw_has_agreed' )->willReturn( true );
80
		$this->terms_of_service->expects( $this->once() )->method( 'is_development_mode' )->willReturn( true );
81
		$this->assertFalse( $this->terms_of_service->has_agreed() );
82
	}
83
84
	/**
85
	 * Tests has_agreed if active but not agreed.
86
	 *
87
	 * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed
88
	 */
89
	public function test_returns_true_if_active_even_if_not_agreed() {
90
		$this->terms_of_service->expects( $this->once() )->method( 'get_raw_has_agreed' )->willReturn( false );
91
		$this->terms_of_service->expects( $this->once() )->method( 'is_development_mode' )->willReturn( false );
92
93
		// Jetpack is active.
94
		$this->terms_of_service->expects( $this->once() )->method( 'is_active' )->willReturn( true );
95
96
		$this->assertTrue( $this->terms_of_service->has_agreed() );
97
	}
98
99
	/**
100
	 * Mock a global function and make it return a certain value.
101
	 *
102
	 * @param string $function_name Name of the function.
103
	 * @param mixed  $return_value  Return value of the function.
104
	 * @param string $called_with Value called with.
0 ignored issues
show
Should the type for parameter $called_with not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
105
	 *
106
	 * @return phpmock\Mock The mock object.
107
	 */
108 View Code Duplication
	protected function mock_function( $function_name, $return_value = null, $called_with = null ) {
109
		$builder = new MockBuilder();
110
		$builder->setNamespace( __NAMESPACE__ )
111
				->setName( $function_name )
112
				->setFunction(
113
					function( $value ) use ( &$return_value, $called_with ) {
114
						if ( $called_with ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $called_with of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
115
							$this->assertEquals( $value, $called_with );
116
						}
117
						return $return_value;
118
					}
119
				);
120
		return $builder->build()->enable();
121
	}
122
}
123