|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\BannerServer\Tests\Unit\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
|
|
|
|
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use WMDE\BannerServer\Controller\BannerSelectionController; |
|
14
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\ImpressionThreshold; |
|
15
|
|
|
use WMDE\BannerServer\Tests\Fixtures\CampaignFixture; |
|
16
|
|
|
use WMDE\BannerServer\Tests\Fixtures\VisitorFixture; |
|
17
|
|
|
use WMDE\BannerServer\Tests\Utils\FakeRandomIntegerGenerator; |
|
18
|
|
|
use WMDE\BannerServer\UseCase\BannerSelection\BannerSelectionUseCase; |
|
19
|
|
|
|
|
20
|
|
|
#[CoversClass( BannerSelectionController::class )] |
|
21
|
|
|
class BannerSelectionControllerTest extends TestCase { |
|
22
|
|
|
|
|
23
|
|
|
private const BANNER_PATH = '/test_banners/'; |
|
24
|
|
|
|
|
25
|
|
|
public function test_given_controller_receives_cookies_then_it_passes_cookie_values_to_use_case(): void { |
|
26
|
|
|
$mockUseCase = $this->createMock( BannerSelectionUseCase::class ); |
|
27
|
|
|
$mockUseCase->expects( $this->once() )->method( 'selectBanner' )->with( |
|
28
|
|
|
VisitorFixture::getTestVisitor() |
|
29
|
|
|
); |
|
30
|
|
|
$controller = new BannerSelectionController( $mockUseCase, self::BANNER_PATH ); |
|
31
|
|
|
$controller->selectBanner( VisitorFixture::getReturningVisitorRequest() ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function test_given_no_cookies_then_it_assigns_default_values(): void { |
|
35
|
|
|
$mockUseCase = $this->createMock( BannerSelectionUseCase::class ); |
|
36
|
|
|
$mockUseCase->expects( $this->once() )->method( 'selectBanner' )->with( |
|
37
|
|
|
VisitorFixture::getFirstTimeVisitor() |
|
38
|
|
|
); |
|
39
|
|
|
$controller = new BannerSelectionController( $mockUseCase, self::BANNER_PATH ); |
|
40
|
|
|
$controller->selectBanner( new Request() ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_given_no_cookies_passed_through_request_then_it_creates_cookies_with_updated_default_values(): void { |
|
44
|
|
|
$testUseCase = new BannerSelectionUseCase( |
|
45
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection(), |
|
46
|
|
|
new ImpressionThreshold( 10 ), |
|
47
|
|
|
new FakeRandomIntegerGenerator( 100 ) |
|
48
|
|
|
); |
|
49
|
|
|
$controller = new BannerSelectionController( $testUseCase, self::BANNER_PATH ); |
|
50
|
|
|
$response = $controller->selectBanner( new Request() ); |
|
51
|
|
|
|
|
52
|
|
|
$cookies = $response->headers->getCookies(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertCount( 2, $cookies ); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals( BannerSelectionController::BUCKET_COOKIE, $cookies[0]->getName() ); |
|
57
|
|
|
$this->assertEquals( 'test', $cookies[0]->getValue() ); |
|
58
|
|
|
$this->assertEquals( |
|
59
|
|
|
( new DateTime( '2099-12-31 23:59:59' ) )->modify( '+2 week' )->getTimestamp(), |
|
60
|
|
|
$cookies[0]->getExpiresTime(), |
|
61
|
|
|
'Cookie life-time should be the campaign expiration date plus two weeks.' |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals( BannerSelectionController::IMPRESSION_COUNT_COOKIE, $cookies[1]->getName() ); |
|
65
|
|
|
$this->assertSame( '1', $cookies[1]->getValue() ); |
|
66
|
|
|
$this->assertEquals( |
|
67
|
|
|
( new DateTime( 'midnight first day of january next year' ) )->getTimestamp(), |
|
68
|
|
|
$cookies[1]->getExpiresTime(), |
|
69
|
|
|
'Impression cookie should expire after the campaign season has ended.' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function test_given_banner_is_to_be_shown_then_it_returns_redirect_response(): void { |
|
74
|
|
|
$testUseCase = new BannerSelectionUseCase( |
|
75
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection(), |
|
76
|
|
|
new ImpressionThreshold( 10 ), |
|
77
|
|
|
new FakeRandomIntegerGenerator( 100 ) |
|
78
|
|
|
); |
|
79
|
|
|
$controller = new BannerSelectionController( $testUseCase, self::BANNER_PATH ); |
|
80
|
|
|
$response = $controller->selectBanner( new Request() ); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertInstanceOf( RedirectResponse::class, $response ); |
|
83
|
|
|
$this->assertEquals( '/test_banners/TestBanner.js', $response->headers->get( 'location' ) ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function test_given_impression_limit_is_reached_then_it_returns_http_ok_response(): void { |
|
87
|
|
|
$testUseCase = new BannerSelectionUseCase( |
|
88
|
|
|
CampaignFixture::getTrueRandomTestCampaignCollection(), |
|
89
|
|
|
new ImpressionThreshold( VisitorFixture::VISITOR_TEST_IMPRESSION_COUNT ), |
|
90
|
|
|
new FakeRandomIntegerGenerator( 100 ) |
|
91
|
|
|
); |
|
92
|
|
|
$controller = new BannerSelectionController( $testUseCase, self::BANNER_PATH ); |
|
93
|
|
|
$response = $controller->selectBanner( VisitorFixture::getReturningVisitorRequest() ); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertEquals( Response::HTTP_OK, $response->getStatusCode() ); |
|
96
|
|
|
$this->assertEquals( '// Sorry, no banner for you!', $response->getContent() ); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
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