|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\BannerServer\Tests\Unit\UseCase\BannerSelection; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
|
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\ImpressionThreshold; |
|
10
|
|
|
use WMDE\BannerServer\Entity\Visitor; |
|
11
|
|
|
use WMDE\BannerServer\Tests\Fixtures\CampaignFixture; |
|
12
|
|
|
use WMDE\BannerServer\Tests\Fixtures\VisitorFixture; |
|
13
|
|
|
use WMDE\BannerServer\Tests\Utils\FakeRandomIntegerGenerator; |
|
14
|
|
|
use WMDE\BannerServer\UseCase\BannerSelection\BannerSelectionUseCase; |
|
15
|
|
|
use WMDE\BannerServer\Utils\SystemRandomIntegerGenerator; |
|
16
|
|
|
|
|
17
|
|
|
#[CoversClass( BannerSelectionUseCase::class )] |
|
18
|
|
|
class BannerSelectionUseCaseTest extends TestCase { |
|
19
|
|
|
|
|
20
|
|
|
public function test_given_max_percentage_then_limit_is_not_applied(): void { |
|
21
|
|
|
$useCase = new BannerSelectionUseCase( |
|
22
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection( 100 ), |
|
23
|
|
|
new ImpressionThreshold( 10 ), |
|
24
|
|
|
new SystemRandomIntegerGenerator() |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
$bannerSelectionData = $useCase->selectBanner( VisitorFixture::getFirstTimeVisitor() ); |
|
28
|
|
|
$this->assertEquals( 'test', $bannerSelectionData->getVisitorData()->getBucketIdentifier() ); |
|
29
|
|
|
$this->assertSame( 1, $bannerSelectionData->getVisitorData()->getTotalImpressionCount() ); |
|
30
|
|
|
$this->assertEquals( 'TestBanner', $bannerSelectionData->getBannerIdentifier() ); |
|
31
|
|
|
$this->assertEquals( CampaignFixture::getTestCampaignEndDate(), $bannerSelectionData->getCampaignEnd() ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function test_given_one_percent_ratio_then_limit_is_not_applied_for_one_percent_rng(): void { |
|
35
|
|
|
$useCase = new BannerSelectionUseCase( |
|
36
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection( 1 ), |
|
37
|
|
|
new ImpressionThreshold( 10 ), |
|
38
|
|
|
new FakeRandomIntegerGenerator( 1 ) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$bannerSelectionData = $useCase->selectBanner( VisitorFixture::getFirstTimeVisitor() ); |
|
42
|
|
|
$this->assertEquals( 'test', $bannerSelectionData->getVisitorData()->getBucketIdentifier() ); |
|
43
|
|
|
$this->assertSame( 1, $bannerSelectionData->getVisitorData()->getTotalImpressionCount() ); |
|
44
|
|
|
$this->assertEquals( 'TestBanner', $bannerSelectionData->getBannerIdentifier() ); |
|
45
|
|
|
$this->assertEquals( CampaignFixture::getTestCampaignEndDate(), $bannerSelectionData->getCampaignEnd() ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function test_given_one_percent_ratio_then_limit_is_applied_for_two_percent_rng(): void { |
|
49
|
|
|
$useCase = new BannerSelectionUseCase( |
|
50
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection( 1 ), |
|
51
|
|
|
new ImpressionThreshold( 10 ), |
|
52
|
|
|
new FakeRandomIntegerGenerator( 2 ) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$bannerSelectionData = $useCase->selectBanner( VisitorFixture::getFirstTimeVisitor() ); |
|
56
|
|
|
$this->assertFalse( $bannerSelectionData->displayBanner() ); |
|
57
|
|
|
$this->assertNull( $bannerSelectionData->getVisitorData()->getBucketIdentifier() ); |
|
58
|
|
|
$this->assertSame( 0, $bannerSelectionData->getVisitorData()->getTotalImpressionCount() ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function test_given_visitor_with_category_of_campaign_then_no_banner_is_shown(): void { |
|
62
|
|
|
$useCase = new BannerSelectionUseCase( |
|
63
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection( 100 ), |
|
64
|
|
|
new ImpressionThreshold( 10 ), |
|
65
|
|
|
new SystemRandomIntegerGenerator() |
|
66
|
|
|
); |
|
67
|
|
|
$visitor = new Visitor( 0, |
|
68
|
|
|
null, |
|
69
|
|
|
500, |
|
70
|
|
|
CampaignFixture::TEST_CATEGORY, |
|
71
|
|
|
'another-irrelevant-category' |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$bannerSelectionData = $useCase->selectBanner( $visitor ); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertFalse( $bannerSelectionData->displayBanner(), 'No banner should be selected' ); |
|
77
|
|
|
$this->assertNull( $bannerSelectionData->getVisitorData()->getBucketIdentifier() ); |
|
78
|
|
|
$this->assertSame( 0, $bannerSelectionData->getVisitorData()->getTotalImpressionCount() ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function test_when_banner_is_returned_then_view_count_is_incremented(): void { |
|
82
|
|
|
$useCase = new BannerSelectionUseCase( |
|
83
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection( 100 ), |
|
84
|
|
|
new ImpressionThreshold( 10 ), |
|
85
|
|
|
new SystemRandomIntegerGenerator() |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$bannerSelectionData = $useCase->selectBanner( VisitorFixture::getTestVisitor() ); |
|
89
|
|
|
$this->assertEquals( |
|
90
|
|
|
VisitorFixture::VISITOR_TEST_IMPRESSION_COUNT + 1, |
|
91
|
|
|
$bannerSelectionData->getVisitorData()->getTotalImpressionCount() |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function test_when_no_banner_is_returned_then_view_count_is_unchanged(): void { |
|
96
|
|
|
$useCase = new BannerSelectionUseCase( |
|
97
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection( 1 ), |
|
98
|
|
|
new ImpressionThreshold( 10 ), |
|
99
|
|
|
new FakeRandomIntegerGenerator( 2 ) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$bannerSelectionData = $useCase->selectBanner( VisitorFixture::getTestVisitor() ); |
|
103
|
|
|
$this->assertEquals( |
|
104
|
|
|
VisitorFixture::VISITOR_TEST_IMPRESSION_COUNT, |
|
105
|
|
|
$bannerSelectionData->getVisitorData()->getTotalImpressionCount() |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function test_given_display_width_range_filters_banners(): void { |
|
110
|
|
|
$smallMobileMaxWidth = 400; |
|
111
|
|
|
|
|
112
|
|
|
$useCase = new BannerSelectionUseCase( |
|
113
|
|
|
CampaignFixture::getFixedViewportWidthCampaignCollection( $smallMobileMaxWidth ), |
|
114
|
|
|
new ImpressionThreshold( 10 ), |
|
115
|
|
|
new SystemRandomIntegerGenerator() |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
$bannerSelectionData = $useCase->selectBanner( VisitorFixture::getTestVisitor() ); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertFalse( $bannerSelectionData->displayBanner(), |
|
121
|
|
|
'No banner should be selected, because campaign was for mobile, visitor was desktop width.' ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function test_given_display_width_range_with_multiple_banners_selects_correct_banner(): void { |
|
125
|
|
|
$desktopMinWidth = 600; |
|
126
|
|
|
$desktopIdentifier = 'C20_WMDE_Test_01'; |
|
127
|
|
|
$mobileMaxWidth = 600; |
|
128
|
|
|
$mobileIdentifier = 'C20_WMDE_Test_Mobile_01'; |
|
129
|
|
|
|
|
130
|
|
|
$useCase = new BannerSelectionUseCase( |
|
131
|
|
|
CampaignFixture::getMixedFixedViewportWidthCampaignCollection( $desktopMinWidth, $mobileMaxWidth, $desktopIdentifier, $mobileIdentifier ), |
|
132
|
|
|
new ImpressionThreshold( 10 ), |
|
133
|
|
|
new SystemRandomIntegerGenerator() |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$desktopSelectionData = $useCase->selectBanner( VisitorFixture::getTestVisitor( 1000 ) ); |
|
137
|
|
|
$mobileSelectionData = $useCase->selectBanner( VisitorFixture::getTestVisitor( 320 ) ); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertEquals( $desktopIdentifier, $desktopSelectionData->getBannerIdentifier() ); |
|
140
|
|
|
$this->assertEquals( $mobileIdentifier, $mobileSelectionData->getBannerIdentifier() ); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths