Completed
Push — update/use-verify_secrets-in-v... ( a4f77f...1f7a67 )
by
unknown
44:48 queued 38:23
created

ManagerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 22.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 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 10 1
A test_api_url_uses_constants() 0 14 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
	public function setUp() {
13
		$this->manager = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
14
		                      ->setMethods( [ 'get_access_token' ] )
15
		                      ->getMock();
16
	}
17
18
	public function tearDown() {
19
		unset( $this->manager );
20
		Constants::clear_constants();
21
		Mock::disableAll();
22
	}
23
24
	/**
25
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
26
	 */
27
	public function test_is_active_when_connected() {
28
		$access_token = (object) [
29
			'secret'           => 'abcd1234',
30
			'external_user_id' => 1,
31
		];
32
		$this->manager->expects( $this->once() )
33
		              ->method( 'get_access_token' )
34
		              ->will( $this->returnValue( $access_token ) );
35
36
		$this->assertTrue( $this->manager->is_active() );
37
	}
38
39
	/**
40
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
41
	 */
42
	public function test_is_active_when_not_connected() {
43
		$this->manager->expects( $this->once() )
44
		              ->method( 'get_access_token' )
45
		              ->will( $this->returnValue( false ) );
46
47
		$this->assertFalse( $this->manager->is_active() );
48
	}
49
50
	public function test_api_url_defaults() {
51
		$this->assertEquals(
52
			'https://jetpack.wordpress.com/jetpack.something/1/',
53
			$this->manager->api_url( 'something' )
54
		);
55
		$this->assertEquals(
56
			'https://jetpack.wordpress.com/jetpack.another_thing/1/',
57
			$this->manager->api_url( 'another_thing/' )
58
		);
59
	}
60
61
	public function test_api_url_uses_constants() {
62
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/base.' );
63
		$this->assertEquals(
64
			'https://example.com/api/base.something/1/',
65
			$this->manager->api_url( 'something' )
66
		);
67
68
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/another.' );
69
		Constants::set_constant( 'JETPACK__API_VERSION', '99' );
70
		$this->assertEquals(
71
			'https://example.com/api/another.something/99/',
72
			$this->manager->api_url( 'something' )
73
		);
74
	}
75
76
	/**
77
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
78
	 */
79
	public function test_is_user_connected_with_default_user_id_logged_out() {
80
		$this->mock_function( 'get_current_user_id', 0 );
81
82
		$this->assertFalse( $this->manager->is_user_connected() );
83
	}
84
85
	/**
86
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
87
	 */
88
	public function test_is_user_connected_with_false_user_id_logged_out() {
89
		$this->mock_function( 'get_current_user_id', 0 );
90
91
		$this->assertFalse( $this->manager->is_user_connected( false ) );
92
	}
93
94
	/**
95
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
96
	 */
97
	public function test_is_user_connected_with_user_id_logged_out_not_connected() {
98
		$this->mock_function( 'absint', 1 );
99
		$this->manager->expects( $this->once() )
100
		              ->method( 'get_access_token' )
101
		              ->will( $this->returnValue( false ) );
102
103
		$this->assertFalse( $this->manager->is_user_connected( 1 ) );
104
	}
105
106
	/**
107
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
108
	 */
109 View Code Duplication
	public function test_is_user_connected_with_default_user_id_logged_in() {
110
		$this->mock_function( 'get_current_user_id', 1 );
111
		$access_token = (object) [
112
			'secret'           => 'abcd1234',
113
			'external_user_id' => 1,
114
		];
115
		$this->manager->expects( $this->once() )
116
		              ->method( 'get_access_token' )
117
		              ->will( $this->returnValue( $access_token ) );
118
119
		$this->assertTrue( $this->manager->is_user_connected() );
120
	}
121
122
	/**
123
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
124
	 */
125 View Code Duplication
	public function test_is_user_connected_with_user_id_logged_in() {
126
		$this->mock_function( 'absint', 1 );
127
		$access_token = (object) [
128
			'secret'           => 'abcd1234',
129
			'external_user_id' => 1,
130
		];
131
		$this->manager->expects( $this->once() )
132
		              ->method( 'get_access_token' )
133
		              ->will( $this->returnValue( $access_token ) );
134
135
		$this->assertTrue( $this->manager->is_user_connected( 1 ) );
136
	}
137
138
	/**
139
	 * Mock a global function and make it return a certain value.
140
	 *
141
	 * @param string $function_name Name of the function.
142
	 * @param mixed  $return_value  Return value of the function.
143
	 * @return phpmock\Mock The mock object.
144
	 */
145 View Code Duplication
	protected function mock_function( $function_name, $return_value = null ) {
146
		$builder = new MockBuilder();
147
		$builder->setNamespace( __NAMESPACE__ )
148
			->setName( $function_name )
149
			->setFunction( function() use ( &$return_value ) {
150
				return $return_value;
151
			} );
152
		return $builder->build()->enable();
153
	}
154
}
155