Completed
Push — add/implement_pre_connection_j... ( 8bfffe...e2c027 )
by
unknown
08:35 queued 01:13
created

Test_Jetpack_JITM   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 225
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 50
loc 225
rs 10
c 0
b 0
f 0
wmc 25
lcom 2
cbo 1

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A tearDown() 0 6 1
A test_jitm_disabled_by_filter() 16 16 1
A test_jitm_enabled_by_default() 16 16 1
A test_prepare_jitms_enqueues_assets() 0 20 1
A mock_site_url() 9 9 1
A mock_filters() 0 17 3
A clear_mock_filters() 0 4 1
A mock_add_get_current_screen() 9 9 1
A mock_add_action() 0 20 3
A mock_do_action() 0 23 4
A mock_wp_enqueue_script() 0 15 2
A get_enqueued_script() 0 4 2
A clear_added_actions() 0 4 1
A clear_enqueued_scripts() 0 4 1
A mock_empty_function() 0 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  // phpcs:disable
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\JITMS\JITM;
6
use phpmock\functions\FunctionProvider;
7
use phpmock\Mock;
8
use phpmock\MockBuilder;
9
use PHPUnit\Framework\TestCase;
10
11
class Test_Jetpack_JITM extends TestCase {
12
	public function setUp() {
13
		$this->mock_add_get_current_screen();
14
		$this->mock_add_action();
15
		$this->mock_do_action();
16
		$this->mock_wp_enqueue_script();
17
18
		// input/output of these functions doesn't matter right now, they just need to exist
19
		$this->mock_empty_function( 'wp_register_style' );
20
		$this->mock_empty_function( 'plugins_url' );
21
		$this->mock_empty_function( 'wp_style_add_data' );
22
		$this->mock_empty_function( 'wp_enqueue_style' );
23
		$this->mock_empty_function( 'wp_localize_script' );
24
		$this->mock_empty_function( 'esc_url_raw' );
25
		$this->mock_empty_function( 'rest_url' );
26
		$this->mock_empty_function( 'esc_html__' );
27
	}
28
29
	public function tearDown() {
30
		Mock::disableAll();
31
		\Mockery::close();
32
		$this->clear_added_actions();
33
		$this->clear_enqueued_scripts();
34
	}
35
36 View Code Duplication
	public function test_jitm_disabled_by_filter() {
37
		$this->mock_filters( array(
38
			array( 'jetpack_just_in_time_msgs', false, false ),
39
		), "Automattic\Jetpack\JITMS" );
40
41
		// Used for Status->is_development_mode().
42
		$this->mock_filters( array(
43
			array( 'jetpack_just_in_time_msgs', false, false ),
44
		), "Automattic\Jetpack" );
45
		$this->mock_site_url();
46
47
		$jitm = new JITM();
48
		$this->assertFalse( $jitm->register() );
49
50
		$this->clear_mock_filters();
51
	}
52
53 View Code Duplication
	public function test_jitm_enabled_by_default() {
54
		$this->mock_filters( array(
55
			array( 'jetpack_just_in_time_msgs', false, true ),
56
		), "Automattic\Jetpack\JITMS" );
57
58
		// Used for Status->is_development_mode().
59
		$this->mock_filters( array(
60
			array( 'jetpack_just_in_time_msgs', false, true ),
61
		), "Automattic\Jetpack" );
62
		$this->mock_site_url();
63
64
		$jitm = new JITM();
65
		$this->assertTrue( $jitm->register() );
66
67
		$this->clear_mock_filters();
68
	}
69
70
	/**
71
	 * This is an example of a test which uses Mockery to tests a class static method.
72
	 *
73
	 * It requires the runInSeparateProcess tag so that the class isn't already autoloaded.
74
	 *
75
	 * @runInSeparateProcess
76
	 */
77
	public function test_prepare_jitms_enqueues_assets() {
78
		$mockAssets = \Mockery::mock('alias:Automattic\Jetpack\Assets');
79
80
		// mock the static method and return a dummy value
81
		$mockAssets
82
			->shouldReceive('get_file_url_for_environment')
83
			->andReturn('the_file_url');
84
85
		$jitm = new JITM();
86
		$screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object
87
		$jitm->prepare_jitms( $screen );
88
89
		// this should enqueue a jetpack-jitm-new script
90
		do_action( 'admin_enqueue_scripts' );
91
92
		// assert our script was enqueued with the right value
93
		$script = $this->get_enqueued_script( 'jetpack-jitm-new' );
94
95
		$this->assertEquals( 'the_file_url', $script['src'] );
96
	}
97
98
	/*
99
	public function test_prepare_jitms_does_not_show_on_some_screens() {
100
		$jitm = new JITM();
101
		$screen = new \stdClass();
102
		$screen->id = 'jetpack_page_stats';
103
		$jitm->prepare_jitms( $screen );
104
	}
105
	*/
106
107 View Code Duplication
	protected function mock_site_url() {
108
		$builder = new MockBuilder();
109
		$builder->setNamespace( "Automattic\Jetpack" )
110
			->setName( 'site_url' )
111
			->setFunction( function() {
112
				return "unit-test";
113
			} );
114
		$builder->build()->enable();
115
	}
116
117
	protected function mock_filters( $filters, $namespace ) {
118
		$this->mocked_filters = $filters;
119
		$builder = new MockBuilder();
120
		$builder->setNamespace( $namespace )
121
			->setName( 'apply_filters' )
122
			->setFunction(
123
				function( ...$current_args ) {
124
					foreach ( $this->mocked_filters as $filter ) {
125
						if ( array_slice( $filter, 0, -1 ) === $current_args ) {
126
							return array_pop( $filter );
127
						}
128
					}
129
				}
130
			);
131
		$this->apply_filters_mock = $builder->build();
132
		$this->apply_filters_mock->enable();
133
	}
134
135
	protected function clear_mock_filters() {
136
		$this->apply_filters_mock->disable();
137
		unset( $this->mocked_filters );
138
	}
139
140 View Code Duplication
	protected function mock_add_get_current_screen() {
141
		$builder = new MockBuilder();
142
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
143
			->setName( 'get_current_screen' )
144
			->setFunction( function() {
145
				return new \stdClass;
146
			} );
147
		$builder->build()->enable();
148
	}
149
150
	protected function mock_add_action() {
151
		$builder = new MockBuilder();
152
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
153
			->setName( 'add_action' )
154
			->setFunction( function( $name, $callable ) {
155
				global $actions;
156
157
				if ( is_null( $actions ) ) {
158
					$actions = array();
159
				}
160
161
				// don't worry about precedence for now
162
				if ( ! isset( $actions[$name] ) ) {
163
					$actions[$name] = array();
164
				}
165
166
				$actions[$name][] = $callable;
167
			} );
168
		$builder->build()->enable();
169
	}
170
171
	protected function mock_do_action() {
172
		$builder = new MockBuilder();
173
		$builder->setNamespace( __NAMESPACE__ )
174
			->setName( 'do_action' )
175
			->setFunction( function( ...$args ) {
176
				global $actions;
177
				$name = array_shift( $args );
178
179
				if ( is_null( $actions ) ) {
180
					$actions = array();
181
				}
182
183
				// don't worry about precedence for now
184
				if ( ! isset( $actions[$name] ) ) {
185
					$actions[$name] = array();
186
				}
187
188
				foreach( $actions[$name] as $callable ) {
189
					call_user_func_array( $callable, $args );
190
				}
191
			} );
192
		$builder->build()->enable();
193
	}
194
195
	protected function mock_wp_enqueue_script() {
196
		$builder = new MockBuilder();
197
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
198
			->setName( 'wp_enqueue_script' )
199
			->setFunction( function( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
200
				global $wp_scripts;
201
202
				if ( is_null( $wp_scripts ) ) {
203
					$wp_scripts = array();
204
				}
205
206
				$wp_scripts[$handle] = compact( 'src', 'deps', 'ver', 'in_footer' );
207
			} );
208
		$builder->build()->enable();
209
	}
210
211
	protected function get_enqueued_script( $handle ) {
212
		global $wp_scripts;
213
		return isset( $wp_scripts[$handle] ) ? $wp_scripts[$handle] : null;
214
	}
215
216
	protected function clear_added_actions() {
217
		global $actions;
218
		$actions = array();
219
	}
220
221
	protected function clear_enqueued_scripts() {
222
		global $wp_scripts;
223
		$wp_scripts = array();
224
	}
225
226
	protected function mock_empty_function( $name ) {
227
		$builder = new MockBuilder();
228
		$builder->setNamespace( "Automattic\Jetpack\JITMS" )
229
			->setName( $name )
230
			->setFunction( function() use ( $name ) {
231
				// echo "Called $name with " . print_r( func_get_args(),1 ) . "\n";
232
			} );
233
		$builder->build()->enable();
234
	}
235
}
236