Passed
Push — master ( 5e5d56...4cec36 )
by Tim
54s queued 10s
created

CampaignConfigurationLoaderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_given_broken_bucket_campaign_configuration_then_errors_are_caught() 0 6 1
A test_given_campaigns_are_loaded_then_loaded_campaign_data_is_correct() 0 19 1
A test_given_missing_campaign_data_then_errors_are_caught() 0 6 1
A test_given_invalid_campaign_file_then_empty_campaign_configuration_is_returned() 0 6 1
A test_given_broken_banner_campaign_configuration_then_errors_are_caught() 0 6 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Tests\Unit\Utils;
6
7
use Symfony\Component\Yaml\Exception\ParseException;
8
use WMDE\BannerServer\Utils\CampaignConfigurationLoader;
9
10
/**
11
 * @covers \WMDE\BannerServer\Utils\CampaignConfigurationLoader
12
 * Class ActiveBannerSelectionDataTest
13
 */
14
class CampaignConfigurationLoaderTest extends \PHPUnit\Framework\TestCase {
15
16
	const TEST_VALID_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/campaigns.yml';
17
	const TEST_BROKEN_BUCKET_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_bucket_campaign.yml';
18
	const TEST_BROKEN_BANNER_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_banner_campaign.yml';
19
	const TEST_BROKEN_DATA_CAMPAIGN_CONFIGURATION_FILE = 'tests/Fixtures/campaigns/broken_data_campaign.yml';
20
21
	public function test_given_campaigns_are_loaded_then_loaded_campaign_data_is_correct() {
22
		$loader = new CampaignConfigurationLoader( self::TEST_VALID_CAMPAIGN_CONFIGURATION_FILE );
23
		$collection = $loader->getCampaignCollection();
24
25
		$campaign = $collection->getCampaign( new \DateTime( '2018-12-12' ) );
26
		$this->assertNotNull( $campaign );
27
		$this->assertEquals( 'B18WPDE_01_180131', $campaign->getIdentifier() );
28
		$this->assertEquals( '2019-01-01 14:00:00', $campaign->getEnd()->format( 'Y-m-d H:i:s' ) );
29
		$this->assertEquals( 10, $campaign->getDisplayPercentage() );
30
31
		$bucketA = $campaign->selectBucket( 'B18WPDE_01_180131_ctrl' );
32
		$bucketB = $campaign->selectBucket( 'B18WPDE_01_180131_var' );
33
		$this->assertEquals( $bucketA->getIdentifier(), 'B18WPDE_01_180131_ctrl' );
34
		$this->assertEquals( $bucketB->getIdentifier(), 'B18WPDE_01_180131_var' );
35
36
		$this->assertEquals( 'B18WPDE_01_180131_fulltop_ctrl', $bucketA->getBanner( 0 ) );
37
		$this->assertEquals( 'B18WPDE_01_180131_top_ctrl2', $bucketA->getBanner( 1 ) );
38
		$this->assertEquals( 'B18WPDE_02_180511_top_ctrl_last', $bucketA->getBanner( 5 ) );
39
		$this->assertEquals( 'B18WPDE_02_180511_top_ctrl_last', $bucketA->getBanner( 10 ) );
40
	}
41
42
	public function test_given_broken_bucket_campaign_configuration_then_errors_are_caught() {
43
		$loader = new CampaignConfigurationLoader(
44
			self::TEST_BROKEN_BUCKET_CAMPAIGN_CONFIGURATION_FILE
45
		);
46
		$this->expectExceptionMessage( 'A configured bucket has no name.' );
47
		$loader->getCampaignCollection();
48
	}
49
50
	public function test_given_broken_banner_campaign_configuration_then_errors_are_caught() {
51
		$loader = new CampaignConfigurationLoader(
52
			self::TEST_BROKEN_BANNER_CAMPAIGN_CONFIGURATION_FILE
53
		);
54
		$this->expectExceptionMessage( 'A configured bucket has no associated banners.' );
55
		$loader->getCampaignCollection();
56
	}
57
58
	public function test_given_missing_campaign_data_then_errors_are_caught() {
59
		$loader = new CampaignConfigurationLoader(
60
			self::TEST_BROKEN_DATA_CAMPAIGN_CONFIGURATION_FILE
61
		);
62
		$this->expectExceptionMessage( 'Campaign data is incomplete.' );
63
		$loader->getCampaignCollection();
64
	}
65
66
	public function test_given_invalid_campaign_file_then_empty_campaign_configuration_is_returned() {
67
		$loader = new CampaignConfigurationLoader(
68
			'SOME_INVALID_PATH/' . self::TEST_VALID_CAMPAIGN_CONFIGURATION_FILE
69
		);
70
		$this->expectException( ParseException::class );
71
		$loader->getCampaignCollection();
72
	}
73
}
74