Issues (21)

Branch: main

Unit/Utils/CampaignConfigurationLoaderTest.php (1 issue)

Labels
Severity
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Tests\Unit\Utils;
6
7
use Closure;
8
use DateTime;
9
use PHPUnit\Framework\Attributes\CoversClass;
0 ignored issues
show
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...
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\Yaml\Exception\ParseException;
12
use WMDE\BannerServer\Entity\BannerSelection\Campaign;
13
use WMDE\BannerServer\Utils\CampaignConfigurationLoader;
14
15
#[CoversClass( CampaignConfigurationLoader::class )]
16
class CampaignConfigurationLoaderTest extends TestCase {
17
18
	private const TEST_VALID_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/campaigns.yml';
19
	private const TEST_BROKEN_BUCKET_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_bucket_campaign.yml';
20
	private const TEST_BROKEN_BANNER_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_banner_campaign.yml';
21
	private const TEST_BROKEN_DATA_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_data_campaign.yml';
22
	private const TEST_BROKEN_DISPLAYWIDTH_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_displayWidth_campaign.yml';
23
24
	public function test_given_campaigns_are_loaded_then_loaded_campaign_data_is_correct(): void {
25
		$loader = new CampaignConfigurationLoader( self::TEST_VALID_CAMPAIGN_CONFIGURATION_FILE );
26
		$collection = $loader->getCampaignCollection();
27
28
		$campaign = $collection->getCampaign( new DateTime( '2018-12-12' ) );
29
		$categorizedCampaign = $collection->getCampaign( new DateTime( '2020-11-12' ) );
30
		$this->assertNotNull( $campaign );
31
		$this->assertEquals( 'B18WPDE_01_180131', $campaign->getIdentifier() );
32
		$this->assertEquals( '2019-01-01 14:00:00', $campaign->getEnd()->format( 'Y-m-d H:i:s' ) );
33
		$this->assertEquals( 10, $campaign->getDisplayPercentage() );
34
		$this->assertEquals( 'default', $campaign->getCategory() );
35
		$this->assertEquals( 'fundraising_2020', $categorizedCampaign->getCategory() );
36
37
		$bucketA = $campaign->selectBucket( 'B18WPDE_01_180131_ctrl' );
38
		$bucketB = $campaign->selectBucket( 'B18WPDE_01_180131_var' );
39
		$this->assertEquals( 'B18WPDE_01_180131_ctrl', $bucketA->getIdentifier() );
40
		$this->assertEquals( 'B18WPDE_01_180131_var', $bucketB->getIdentifier() );
41
42
		$this->assertEquals( 'B18WPDE_01_180131_fulltop_ctrl', $bucketA->getBanner( 0 ) );
43
		$this->assertEquals( 'B18WPDE_01_180131_top_ctrl2', $bucketA->getBanner( 1 ) );
44
		$this->assertEquals( 'B18WPDE_02_180511_top_ctrl_last', $bucketA->getBanner( 5 ) );
45
		$this->assertEquals( 'B18WPDE_02_180511_top_ctrl_last', $bucketA->getBanner( 10 ) );
46
	}
47
48
	public function test_given_broken_bucket_campaign_configuration_then_errors_are_caught(): void {
49
		$loader = new CampaignConfigurationLoader(
50
			self::TEST_BROKEN_BUCKET_CAMPAIGN_CONFIGURATION_FILE
51
		);
52
		$this->expectExceptionMessage( 'A configured bucket has no name.' );
53
		$loader->getCampaignCollection();
54
	}
55
56
	public function test_given_broken_banner_campaign_configuration_then_errors_are_caught(): void {
57
		$loader = new CampaignConfigurationLoader(
58
			self::TEST_BROKEN_BANNER_CAMPAIGN_CONFIGURATION_FILE
59
		);
60
		$this->expectExceptionMessage( 'A configured bucket has no associated banners.' );
61
		$loader->getCampaignCollection();
62
	}
63
64
	public function test_given_missing_campaign_data_then_errors_are_caught(): void {
65
		$loader = new CampaignConfigurationLoader(
66
			self::TEST_BROKEN_DATA_CAMPAIGN_CONFIGURATION_FILE
67
		);
68
		$this->expectExceptionMessage( 'Campaign data is incomplete.' );
69
		$loader->getCampaignCollection();
70
	}
71
72
	public function test_given_display_widths_max_is_larger_than_min_or_undefined(): void {
73
		$loader = new CampaignConfigurationLoader(
74
			self::TEST_BROKEN_DISPLAYWIDTH_CAMPAIGN_CONFIGURATION_FILE
75
		);
76
		$this->expectExceptionMessage( 'Campaign data display width values are invalid (if defined, max must be higher than min)' );
77
		$loader->getCampaignCollection();
78
	}
79
80
	public function test_given_invalid_campaign_file_then_empty_campaign_configuration_is_returned(): void {
81
		$loader = new CampaignConfigurationLoader(
82
			'SOME_INVALID_PATH/' . self::TEST_VALID_CAMPAIGN_CONFIGURATION_FILE
83
		);
84
		$this->expectException( ParseException::class );
85
		$loader->getCampaignCollection();
86
	}
87
88
	public function test_given_empty_display_limits_they_are_set_to_null(): void {
89
		$loader = new CampaignConfigurationLoader( self::TEST_VALID_CAMPAIGN_CONFIGURATION_FILE );
90
		$collection = $loader->getCampaignCollection();
91
		// Using $campaign->isInDisplayRange instead of private property access wouldn't test reliably for null values,
92
		// adding getters for min/max width would break domain encapsulation, so we're cheating here in the test
93
		$readPrivateProperty = Closure::bind( static function ( Campaign $campaign, string $propertyName ) {
94
			return $campaign->$propertyName;
95
		}, null, Campaign::class );
96
97
		$campaign = $collection->getCampaign( new DateTime( '2020-11-26' ) );
98
99
		$this->assertNull( $readPrivateProperty( $campaign, 'minDisplayWidth' ) );
100
		$this->assertNull( $readPrivateProperty( $campaign, 'maxDisplayWidth' ) );
101
	}
102
}
103