CampaignTest::getControlBucket()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Tests\Unit\Entity\BannerSelection;
6
7
use DateTime;
8
use PHPUnit\Framework\Attributes\CoversClass;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PHPUnit\Framework\TestCase;
10
use WMDE\BannerServer\Entity\BannerSelection\Banner;
11
use WMDE\BannerServer\Entity\BannerSelection\Bucket;
12
use WMDE\BannerServer\Entity\BannerSelection\Campaign;
13
use WMDE\BannerServer\Tests\Utils\FakeRandomIntegerGenerator;
14
15
#[CoversClass( Campaign::class )]
16
class CampaignTest extends TestCase {
17
18
	private function getControlBucket(): Bucket {
19
		return new Bucket(
20
			'C18_WMDE_Test_ctrl',
21
			new Banner( 'C18_WMDE_Test_ctrl_main' ),
22
			new Banner( 'C18_WMDE_Test_ctrl_secondary' )
23
		);
24
	}
25
26
	private function getVariantBucket(): Bucket {
27
		return new Bucket(
28
			'C18_WMDE_Test_var',
29
			new Banner( 'C18_WMDE_Test_var_main' ),
30
			new Banner( 'C18_WMDE_Test_var_secondary' )
31
		);
32
	}
33
34
	public function test_given_time_out_of_date_range_then_campaign_is_not_active(): void {
35
		$campaign = new Campaign(
36
			'C18_WMDE_Test',
37
			new DateTime( '2018-10-01 14:00:00' ),
38
			new DateTime( '2018-10-31 14:00:00' ),
39
			1,
40
			'default',
41
			new FakeRandomIntegerGenerator( 1 ),
42
			null,
43
			null,
44
			$this->getControlBucket(),
45
			$this->getVariantBucket()
46
		);
47
		$this->assertTrue(
48
			$campaign->isInActiveDateRange( new DateTime( '2018-10-01 14:00:00' ) ),
49
			'date given is exactly the start date of the campaign'
50
		);
51
		$this->assertTrue(
52
			$campaign->isInActiveDateRange( new DateTime( '2018-10-20 14:00:00' ) ),
53
			'date given is between the start and the end of the campaign'
54
		);
55
		$this->assertTrue(
56
			$campaign->isInActiveDateRange( new DateTime( '2018-10-31 14:00:00' ) ),
57
			'date given is exactly the end date of the campaign'
58
		);
59
	}
60
61
	public function test_given_time_in_the_date_range_then_campaign_is_active(): void {
62
		$campaign = new Campaign(
63
			'C18_WMDE_Test',
64
			new DateTime( '2018-10-01 14:00:00' ),
65
			new DateTime( '2018-10-31 14:00:00' ),
66
			1,
67
			'default',
68
			new FakeRandomIntegerGenerator( 1 ),
69
			null,
70
			null,
71
			$this->getControlBucket(),
72
			$this->getVariantBucket()
73
		);
74
		$this->assertFalse(
75
			$campaign->isInActiveDateRange( new DateTime( '2018-09-22 14:00:00' ) ),
76
			'date given is before the start of the campaign'
77
		);
78
		$this->assertFalse(
79
			$campaign->isInActiveDateRange( new DateTime( '2018-10-01 13:59:59' ) ),
80
			'date given is before the start of the campaign'
81
		);
82
		$this->assertFalse(
83
			$campaign->isInActiveDateRange( new DateTime( '2018-10-31 14:00:01' ) ),
84
			'date given is after the end of the campaign'
85
		);
86
	}
87
88
	public function test_given_valid_bucket_id_then_returns_bucket(): void {
89
		$campaign = new Campaign(
90
			'C18_WMDE_Test',
91
			new DateTime( '2018-10-01 14:00:00' ),
92
			new DateTime( '2018-10-31 14:00:00' ),
93
			1,
94
			'default',
95
			new FakeRandomIntegerGenerator( 1 ),
96
			null,
97
			null,
98
			$this->getControlBucket(),
99
			$this->getVariantBucket()
100
		);
101
		$this->assertEquals(
102
			'C18_WMDE_Test_var',
103
			$campaign->selectBucket( 'C18_WMDE_Test_var' )->getIdentifier()
104
		);
105
	}
106
107
	public function test_given_invalid_bucket_id_then_returns_random_bucket(): void {
108
		$campaign = new Campaign(
109
			'C18_WMDE_Test',
110
			new DateTime( '2018-10-01 14:00:00' ),
111
			new DateTime( '2018-10-31 14:00:00' ),
112
			1,
113
			'default',
114
			new FakeRandomIntegerGenerator( 1 ),
115
			null,
116
			null,
117
			$this->getControlBucket(),
118
			$this->getVariantBucket()
119
		);
120
		$this->assertEquals(
121
			'C18_WMDE_Test_var',
122
			$campaign->selectBucket( 'C18_WMDE_Test_var_666' )->getIdentifier()
123
		);
124
	}
125
126
	public function test_given_no_bucket_id_then_returns_random_bucket(): void {
127
		$campaign = new Campaign(
128
			'C18_WMDE_Test',
129
			new DateTime( '2018-10-01 14:00:00' ),
130
			new DateTime( '2018-10-31 14:00:00' ),
131
			1,
132
			'default',
133
			new FakeRandomIntegerGenerator( 1 ),
134
			null,
135
			null,
136
			$this->getControlBucket(),
137
			$this->getVariantBucket()
138
		);
139
		$this->assertEquals(
140
			'C18_WMDE_Test_var',
141
			$campaign->selectBucket( null )->getIdentifier()
142
		);
143
	}
144
145
	public function test_getters_return_correct_values(): void {
146
		$identifier = 'C18_WMDE_Test';
147
		$displayPercentage = 12;
148
		$endDate = new DateTime( '2018-10-31 14:00:00' );
149
		$campaign = new Campaign(
150
			$identifier,
151
			new DateTime( '2018-10-01 14:00:00' ),
152
			$endDate,
153
			$displayPercentage,
154
			'default',
155
			new FakeRandomIntegerGenerator( 1 ),
156
			null,
157
			null,
158
			$this->getControlBucket(),
159
			$this->getVariantBucket()
160
		);
161
		$this->assertEquals( $identifier, $campaign->getIdentifier() );
162
		$this->assertEquals( $displayPercentage, $campaign->getDisplayPercentage() );
163
		$this->assertEquals( $endDate, $campaign->getEnd() );
164
	}
165
166
	public function test_given_no_mindisplaywidth_and_no_maxdisplaywidth_then_width_is_always_in_range(): void {
167
		$campaign = new Campaign(
168
			'C18_WMDE_Test',
169
			new DateTime( '2018-10-01 14:00:00' ),
170
			new DateTime( '2018-10-31 14:00:00' ),
171
			1,
172
			'default',
173
			new FakeRandomIntegerGenerator( 1 ),
174
			null,
175
			null,
176
			$this->getControlBucket(),
177
			$this->getVariantBucket()
178
		);
179
180
		$this->assertTrue( $campaign->isInDisplayRange( 0 ) );
181
		$this->assertTrue( $campaign->isInDisplayRange( 999999 ) );
182
	}
183
184
	public function test_given_desktop_campaign_then_width_is_limited(): void {
185
		$campaign = new Campaign(
186
			'C18_WMDE_Test',
187
			new DateTime( '2018-10-01 14:00:00' ),
188
			new DateTime( '2018-10-31 14:00:00' ),
189
			1,
190
			'default',
191
			new FakeRandomIntegerGenerator( 1 ),
192
			1025,
193
			null,
194
			$this->getControlBucket(),
195
			$this->getVariantBucket()
196
		);
197
198
		$this->assertFalse( $campaign->isInDisplayRange( 1024 ) );
199
		$this->assertTrue( $campaign->isInDisplayRange( 1025 ) );
200
		$this->assertTrue( $campaign->isInDisplayRange( 999999 ) );
201
	}
202
203
	public function test_given_mobile_campaign_then_width_is_limited(): void {
204
		$campaign = new Campaign(
205
			'C18_WMDE_Test',
206
			new DateTime( '2018-10-01 14:00:00' ),
207
			new DateTime( '2018-10-31 14:00:00' ),
208
			1,
209
			'default',
210
			new FakeRandomIntegerGenerator( 1 ),
211
			null,
212
			1024,
213
			$this->getControlBucket(),
214
			$this->getVariantBucket()
215
		);
216
217
		$this->assertTrue( $campaign->isInDisplayRange( 0 ) );
218
		$this->assertTrue( $campaign->isInDisplayRange( 1023 ) );
219
		$this->assertFalse( $campaign->isInDisplayRange( 1024 ) );
220
	}
221
}
222