BannerSelectionUseCaseTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
eloc 77
dl 0
loc 124
rs 10
c 3
b 1
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A test_given_display_width_range_filters_banners() 0 13 1
A test_when_banner_is_returned_then_view_count_is_incremented() 0 11 1
A test_given_visitor_with_category_of_campaign_then_no_banner_is_shown() 0 18 1
A test_when_no_banner_is_returned_then_view_count_is_unchanged() 0 11 1
A test_given_display_width_range_with_multiple_banners_selects_correct_banner() 0 17 1
A test_given_one_percent_ratio_then_limit_is_applied_for_two_percent_rng() 0 11 1
A test_given_max_percentage_then_limit_is_not_applied() 0 12 1
A test_given_one_percent_ratio_then_limit_is_not_applied_for_one_percent_rng() 0 12 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Tests\Unit\UseCase\BannerSelection;
6
7
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...
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