Completed
Push — fix/warning-if-plugins-use-pac... ( 301620...43d699 )
by Marin
800:31 queued 793:58
created

Test_Jetpack_JITM   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 33.9 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 20
loc 59
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 3 1
A test_jitm_disabled_by_filter() 10 10 1
A test_jitm_enabled_by_default() 10 10 1
A mock_filters() 0 18 3
A clear_mock_filters() 0 4 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;
4
5
use Automattic\Jetpack\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
		$builder = new MockBuilder();
14
		$builder->setNamespace( __NAMESPACE__ )
15
			->setName( 'add_action' )
16
			->setFunction( function() {} );
17
		$builder->build()->enable();
18
	}
19
20
	public function tearDown() {
21
		Mock::disableAll();
22
	}
23
24 View Code Duplication
	public function test_jitm_disabled_by_filter() {
25
		$this->mock_filters( array(
26
			array( 'jetpack_just_in_time_msgs', false, false ),
27
		) );
28
29
		$jitm = new JITM();
30
		$this->assertFalse( $jitm->register() );
31
32
		$this->clear_mock_filters();
33
	}
34
35 View Code Duplication
	public function test_jitm_enabled_by_default() {
36
		$this->mock_filters( array(
37
			array( 'jetpack_just_in_time_msgs', false, true ),
38
		) );
39
40
		$jitm = new JITM();
41
		$this->assertTrue( $jitm->register() );
42
43
		$this->clear_mock_filters();
44
	}
45
46
	protected function mock_filters( $filters ) {
47
		$this->mocked_filters = $filters;
48
		$builder = new MockBuilder();
49
		$builder->setNamespace( __NAMESPACE__ )
50
			->setName( 'apply_filters' )
51
			->setFunction(
52
				function() {
53
					$current_args = func_get_args();
54
					foreach ( $this->mocked_filters as $filter ) {
55
						if ( array_slice( $filter, 0, -1 ) === $current_args ) {
56
							return array_pop( $filter );
57
						}
58
					}
59
				}
60
			);
61
		$this->apply_filters_mock = $builder->build();
62
		$this->apply_filters_mock->enable();
63
	}
64
65
	protected function clear_mock_filters() {
66
		$this->apply_filters_mock->disable();
67
		unset( $this->mocked_filters );
68
	}
69
}
70