Completed
Push — feature/jetpack-packages ( 5255bf...199f42 )
by Marin
16:20 queued 07:19
created

Test_Jetpack_JITM   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 31.58 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 3 1
A test_jitm_disabled_by_filter() 9 9 1
A test_jitm_enabled_by_default() 9 9 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\JITM;
4
5
use Automattic\Jetpack\JITM\Manager as 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
		$this->assertFalse( Jetpack_JITM::init() );
30
31
		$this->clear_mock_filters();
32
	}
33
34 View Code Duplication
	public function test_jitm_enabled_by_default() {
35
		$this->mock_filters( array(
36
			array( 'jetpack_just_in_time_msgs', false, true ),
37
		) );
38
39
		$this->assertTrue( ! ! Jetpack_JITM::init() );
40
41
		$this->clear_mock_filters();
42
	}
43
44
	protected function mock_filters( $filters ) {
45
		$this->mocked_filters = $filters;
46
		$builder = new MockBuilder();
47
		$builder->setNamespace( __NAMESPACE__ )
48
			->setName( 'apply_filters' )
49
			->setFunction(
50
				function() {
51
					$current_args = func_get_args();
52
					foreach ( $this->mocked_filters as $filter ) {
53
						if ( array_slice( $filter, 0, -1 ) === $current_args ) {
54
							return array_pop( $filter );
55
						}
56
					}
57
				}
58
			);
59
		$this->apply_filters_mock = $builder->build();
60
		$this->apply_filters_mock->enable();
61
	}
62
63
	protected function clear_mock_filters() {
64
		$this->apply_filters_mock->disable();
65
		unset( $this->mocked_filters );
66
	}
67
}
68