Completed
Push — renovate/jest-monorepo ( bd2eaf...d289c3 )
by
unknown
44:07 queued 37:28
created

Test_Abtest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 20 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 22
loc 110
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A tearDown() 0 3 1
A test_with_no_test_name_provided() 0 4 1
A test_with_incorrect_test_name_provided() 0 4 1
A test_when_test_inactive_or_does_not_exist() 0 13 1
A test_when_error_or_malformed_response() 10 10 1
A test_when_response_in_unexpected_format() 12 12 1
A test_with_valid_active_test() 0 17 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\Abtest;
6
use PHPUnit\Framework\TestCase;
7
use phpmock\Mock;
8
use phpmock\MockBuilder;
9
10
class Test_Abtest extends TestCase {
11
	/**
12
	 * Test setup.
13
	 */
14
	public function setUp() {
15
		$this->abtest = $this->getMockBuilder( 'Automattic\\Jetpack\\Abtest' )
16
					 ->setMethods( [ 'request_variation' ] )
17
					 ->getMock();
18
19
		$builder = new MockBuilder();
20
		$builder->setNamespace( __NAMESPACE__ )
21
			->setName( 'is_wp_error' )
22
			->setFunction( function( $object ) {
23
				return is_a( $object, __NAMESPACE__ . '\\Error' );
24
			} );
25
		$mock = $builder->build();
26
		$mock->enable();
27
	}
28
29
	/**
30
	 * Test teardown.
31
	 */
32
	public function tearDown() {
33
		Mock::disableAll();
34
	}
35
36
	/**
37
	 * @covers Automattic\Jetpack\Abtest::get_variation
38
	 */
39
	public function test_with_no_test_name_provided() {
40
		$result = $this->abtest->get_variation( null );
41
		$this->assertNull( $result );
42
	}
43
44
	/**
45
	 * @covers Automattic\Jetpack\Abtest::get_variation
46
	 */
47
	public function test_with_incorrect_test_name_provided() {
48
		$result = $this->abtest->get_variation( 'example-test' );
49
		$this->assertNull( $result );
50
	}
51
52
	/**
53
	 * @covers Automattic\Jetpack\Abtest::get_variation
54
	 */
55
	public function test_when_test_inactive_or_does_not_exist() {
56
		$this->abtest->expects( $this->once() )
57
			 ->method( 'request_variation' )
58
			 ->willReturn( [
59
				'body' => json_encode( [
60
					'code'    => 'incorrect_test_name',
61
					'message' => 'This A/B test does not exist or is currently inactive.',
62
				] ),
63
			] );
64
65
		$result = $this->abtest->get_variation( 'example_test' );
66
		$this->assertNull( $result );
67
	}
68
69
	/**
70
	 * @covers Automattic\Jetpack\Abtest::get_variation
71
	 */
72 View Code Duplication
	public function test_when_error_or_malformed_response() {
73
		$this->abtest->expects( $this->once() )
74
			 ->method( 'request_variation' )
75
			 ->willReturn( [
76
				'status' => 500,
77
			] );
78
79
		$result = $this->abtest->get_variation( 'some_test' );
80
		$this->assertNull( $result );
81
	}
82
83
	/**
84
	 * @covers Automattic\Jetpack\Abtest::get_variation
85
	 */
86 View Code Duplication
	public function test_when_response_in_unexpected_format() {
87
		$this->abtest->expects( $this->once() )
88
			 ->method( 'request_variation' )
89
			 ->willReturn( [
90
				'body' => json_encode( [
91
					'foo' => 'bar',
92
				] ),
93
			] );
94
95
		$result = $this->abtest->get_variation( 'some_test' );
96
		$this->assertNull( $result );
97
	}
98
99
	/**
100
	 * @covers Automattic\Jetpack\Abtest::get_variation
101
	 */
102
	public function test_with_valid_active_test() {
103
		$variation = 'original';
104
		$this->abtest->expects( $this->once() )
105
			 ->method( 'request_variation' )
106
			 ->willReturn( [
107
				'body' => json_encode( [
108
					'variation' => $variation,
109
				] ),
110
			] );
111
112
		$result = $this->abtest->get_variation( 'some_test' );
113
		$this->assertEquals( $variation, $result );
114
115
		// Try again to verify we're caching the value instead of requesting it with `request_variation()` again.
116
		$result = $this->abtest->get_variation( 'some_test' );
117
		$this->assertEquals( $variation, $result );
118
	}
119
}
120
121
/**
122
 * We're declaring this class to mock Automattic\Jetpack\Error in the tests.
123
 */
124
class Error {
125
126
}
127