1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
/** |
3
|
|
|
* Tests the AB Test package. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-abtest |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack; |
9
|
|
|
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Test_Abtest |
14
|
|
|
* |
15
|
|
|
* @package Automattic\Jetpack |
16
|
|
|
*/ |
17
|
|
|
class Test_Abtest extends TestCase { |
18
|
|
|
/** |
19
|
|
|
* Test setup. |
20
|
|
|
* |
21
|
|
|
* @before |
22
|
|
|
*/ |
23
|
|
|
public function set_up() { |
24
|
|
|
$this->abtest = $this->getMockBuilder( 'Automattic\\Jetpack\\Abtest' ) |
25
|
|
|
->setMethods( array( 'request_variation' ) ) |
26
|
|
|
->getMock(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tests with no test name provided. |
31
|
|
|
* |
32
|
|
|
* @covers Automattic\Jetpack\Abtest::get_variation |
33
|
|
|
*/ |
34
|
|
|
public function test_with_no_test_name_provided() { |
35
|
|
|
$result = $this->abtest->get_variation( null ); |
36
|
|
|
$this->assertNull( $result ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Tests when incorrect test name is provided. |
41
|
|
|
* |
42
|
|
|
* @covers Automattic\Jetpack\Abtest::get_variation |
43
|
|
|
*/ |
44
|
|
|
public function test_with_incorrect_test_name_provided() { |
45
|
|
|
$result = $this->abtest->get_variation( 'example-test' ); |
46
|
|
|
$this->assertNull( $result ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Tests when a test is inactive or does not exist. |
51
|
|
|
* |
52
|
|
|
* @covers Automattic\Jetpack\Abtest::get_variation |
53
|
|
|
*/ |
54
|
|
|
public function test_when_test_inactive_or_does_not_exist() { |
55
|
|
|
$this->abtest->expects( $this->once() ) |
56
|
|
|
->method( 'request_variation' ) |
57
|
|
|
->willReturn( |
58
|
|
|
array( |
59
|
|
|
'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
60
|
|
|
array( |
61
|
|
|
'code' => 'incorrect_test_name', |
62
|
|
|
'message' => 'This A/B test does not exist or is currently inactive.', |
63
|
|
|
) |
64
|
|
|
), |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$result = $this->abtest->get_variation( 'example_test' ); |
69
|
|
|
$this->assertNull( $result ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Tests an error or malformed response. |
74
|
|
|
* |
75
|
|
|
* @covers Automattic\Jetpack\Abtest::get_variation |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function test_when_error_or_malformed_response() { |
78
|
|
|
$this->abtest->expects( $this->once() ) |
79
|
|
|
->method( 'request_variation' ) |
80
|
|
|
->willReturn( |
81
|
|
|
array( |
82
|
|
|
'status' => 500, |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$result = $this->abtest->get_variation( 'some_test' ); |
87
|
|
|
$this->assertNull( $result ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Tests when the response is in an unexpected format. |
92
|
|
|
* |
93
|
|
|
* @covers Automattic\Jetpack\Abtest::get_variation |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function test_when_response_in_unexpected_format() { |
96
|
|
|
$this->abtest->expects( $this->once() ) |
97
|
|
|
->method( 'request_variation' ) |
98
|
|
|
->willReturn( |
99
|
|
|
array( |
100
|
|
|
'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
101
|
|
|
array( |
102
|
|
|
'foo' => 'bar', |
103
|
|
|
) |
104
|
|
|
), |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$result = $this->abtest->get_variation( 'some_test' ); |
109
|
|
|
$this->assertNull( $result ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test with an active test. |
114
|
|
|
* |
115
|
|
|
* @covers Automattic\Jetpack\Abtest::get_variation |
116
|
|
|
*/ |
117
|
|
|
public function test_with_valid_active_test() { |
118
|
|
|
$variation = 'original'; |
119
|
|
|
$this->abtest->expects( $this->once() ) |
120
|
|
|
->method( 'request_variation' ) |
121
|
|
|
->willReturn( |
122
|
|
|
array( |
123
|
|
|
'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
124
|
|
|
array( |
125
|
|
|
'variation' => $variation, |
126
|
|
|
) |
127
|
|
|
), |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$result = $this->abtest->get_variation( 'some_test' ); |
132
|
|
|
$this->assertEquals( $variation, $result ); |
133
|
|
|
|
134
|
|
|
// Try again to verify we're caching the value instead of requesting it with `request_variation()` again. |
135
|
|
|
$result = $this->abtest->get_variation( 'some_test' ); |
136
|
|
|
$this->assertEquals( $variation, $result ); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|