Completed
Push — add/oauth-connection ( 084714...305e42 )
by
unknown
14:09 queued 06:55
created

ManagerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 201
Duplicated Lines 16.42 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 1
dl 33
loc 201
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A tearDown() 0 5 1
A test_is_active_when_connected() 0 11 1
A test_is_active_when_not_connected() 0 7 1
A test_api_url_defaults() 0 12 1
A test_api_url_uses_constants_and_filters() 0 49 1
A test_is_user_connected_with_default_user_id_logged_out() 0 5 1
A test_is_user_connected_with_false_user_id_logged_out() 0 5 1
A test_is_user_connected_with_user_id_logged_out_not_connected() 0 8 1
A test_is_user_connected_with_default_user_id_logged_in() 12 12 1
A test_is_user_connected_with_user_id_logged_in() 12 12 1
A mock_function() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Automattic\Jetpack\Connection;
4
5
use phpmock\Mock;
6
use phpmock\MockBuilder;
7
use PHPUnit\Framework\TestCase;
8
9
use Automattic\Jetpack\Constants;
10
11
class ManagerTest extends TestCase {
12
13
	protected $arguments_stack = [];
14
15
	public function setUp() {
16
		$this->manager = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
17
		                      ->setMethods( [ 'get_access_token' ] )
18
		                      ->getMock();
19
20
		$builder = new MockBuilder();
21
		$builder->setNamespace( __NAMESPACE__ )
22
				->setName( 'apply_filters' )
23
				->setFunction(
24
					function( $filter_name, $return_value ) {
25
						return $return_value;
26
					}
27
				);
28
29
		$this->apply_filters = $builder->build();
30
	}
31
32
	public function tearDown() {
33
		unset( $this->manager );
34
		Constants::clear_constants();
35
		Mock::disableAll();
36
	}
37
38
	/**
39
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
40
	 */
41
	public function test_is_active_when_connected() {
42
		$access_token = (object) [
43
			'secret'           => 'abcd1234',
44
			'external_user_id' => 1,
45
		];
46
		$this->manager->expects( $this->once() )
47
		              ->method( 'get_access_token' )
48
		              ->will( $this->returnValue( $access_token ) );
49
50
		$this->assertTrue( $this->manager->is_active() );
51
	}
52
53
	/**
54
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
55
	 */
56
	public function test_is_active_when_not_connected() {
57
		$this->manager->expects( $this->once() )
58
		              ->method( 'get_access_token' )
59
		              ->will( $this->returnValue( false ) );
60
61
		$this->assertFalse( $this->manager->is_active() );
62
	}
63
64
	public function test_api_url_defaults() {
65
		$this->apply_filters->enable();
66
67
		$this->assertEquals(
68
			'https://jetpack.wordpress.com/jetpack.something/1/',
69
			$this->manager->api_url( 'something' )
70
		);
71
		$this->assertEquals(
72
			'https://jetpack.wordpress.com/jetpack.another_thing/1/',
73
			$this->manager->api_url( 'another_thing/' )
74
		);
75
	}
76
77
	/**
78
	 * Testing the ability of the api_url method to follow set constants and filters.
79
	 *
80
	 * @covers Automattic\Jetpack\Connection\Manager::api_url
81
	 */
82
	public function test_api_url_uses_constants_and_filters() {
83
		$this->apply_filters->enable();
84
85
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/base.' );
86
		$this->assertEquals(
87
			'https://example.com/api/base.something/1/',
88
			$this->manager->api_url( 'something' )
89
		);
90
91
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/another.' );
92
		Constants::set_constant( 'JETPACK__API_VERSION', '99' );
93
		$this->assertEquals(
94
			'https://example.com/api/another.something/99/',
95
			$this->manager->api_url( 'something' )
96
		);
97
98
		$this->apply_filters->disable();
99
100
		// Getting a new special mock just for this occasion.
101
		$builder = new MockBuilder();
102
		$builder->setNamespace( __NAMESPACE__ )
103
				->setName( 'apply_filters' )
104
				->setFunction(
105
					function( $filter_name, $return_value ) {
106
						$this->arguments_stack[ $filter_name ] [] = func_get_args();
107
						return 'completely overwrite';
108
					}
109
				);
110
111
		$builder->build()->enable();
112
113
		$this->assertEquals(
114
			'completely overwrite',
115
			$this->manager->api_url( 'something' )
116
		);
117
118
		// The jetpack_api_url argument stack should not be empty, making sure the filter was
119
		// called with a proper name and arguments.
120
		$call_arguments = array_pop( $this->arguments_stack['jetpack_api_url'] );
121
		$this->assertEquals( 'something', $call_arguments[2] );
122
		$this->assertEquals(
123
			Constants::get_constant( 'JETPACK__API_BASE' ),
124
			$call_arguments[3]
125
		);
126
		$this->assertEquals(
127
			'/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/',
128
			$call_arguments[4]
129
		);
130
	}
131
132
	/**
133
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
134
	 */
135
	public function test_is_user_connected_with_default_user_id_logged_out() {
136
		$this->mock_function( 'get_current_user_id', 0 );
137
138
		$this->assertFalse( $this->manager->is_user_connected() );
139
	}
140
141
	/**
142
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
143
	 */
144
	public function test_is_user_connected_with_false_user_id_logged_out() {
145
		$this->mock_function( 'get_current_user_id', 0 );
146
147
		$this->assertFalse( $this->manager->is_user_connected( false ) );
148
	}
149
150
	/**
151
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
152
	 */
153
	public function test_is_user_connected_with_user_id_logged_out_not_connected() {
154
		$this->mock_function( 'absint', 1 );
155
		$this->manager->expects( $this->once() )
156
		              ->method( 'get_access_token' )
157
		              ->will( $this->returnValue( false ) );
158
159
		$this->assertFalse( $this->manager->is_user_connected( 1 ) );
160
	}
161
162
163
	/**
164
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
165
	 */
166 View Code Duplication
	public function test_is_user_connected_with_default_user_id_logged_in() {
167
		$this->mock_function( 'get_current_user_id', 1 );
168
		$access_token = (object) [
169
			'secret'           => 'abcd1234',
170
			'external_user_id' => 1,
171
		];
172
		$this->manager->expects( $this->once() )
173
		              ->method( 'get_access_token' )
174
		              ->will( $this->returnValue( $access_token ) );
175
176
		$this->assertTrue( $this->manager->is_user_connected() );
177
	}
178
179
	/**
180
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
181
	 */
182 View Code Duplication
	public function test_is_user_connected_with_user_id_logged_in() {
183
		$this->mock_function( 'absint', 1 );
184
		$access_token = (object) [
185
			'secret'           => 'abcd1234',
186
			'external_user_id' => 1,
187
		];
188
		$this->manager->expects( $this->once() )
189
		              ->method( 'get_access_token' )
190
		              ->will( $this->returnValue( $access_token ) );
191
192
		$this->assertTrue( $this->manager->is_user_connected( 1 ) );
193
	}
194
195
	/**
196
	 * Mock a global function and make it return a certain value.
197
	 *
198
	 * @param string $function_name Name of the function.
199
	 * @param mixed  $return_value  Return value of the function.
200
	 * @return phpmock\Mock The mock object.
201
	 */
202 View Code Duplication
	protected function mock_function( $function_name, $return_value = null ) {
203
		$builder = new MockBuilder();
204
		$builder->setNamespace( __NAMESPACE__ )
205
			->setName( $function_name )
206
			->setFunction( function() use ( &$return_value ) {
207
				return $return_value;
208
			} );
209
		return $builder->build()->enable();
210
	}
211
}
212