|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\BannerServer\Tests\Unit\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Asset\Packages; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use WMDE\BannerServer\Controller\BannerSelectionController; |
|
10
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\Banner; |
|
11
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\Bucket; |
|
12
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\Campaign; |
|
13
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\CampaignCollection; |
|
14
|
|
|
use WMDE\BannerServer\Entity\BannerSelection\ImpressionThreshold; |
|
15
|
|
|
use WMDE\BannerServer\UseCase\BannerSelection\BannerSelectionUseCase; |
|
16
|
|
|
use WMDE\BannerServer\UseCase\BannerSelection\Visitor; |
|
17
|
|
|
use WMDE\BannerServer\Utils\RandomInteger; |
|
18
|
|
|
|
|
19
|
|
|
class BannerSelectionControllerTest extends \PHPUnit\Framework\TestCase { |
|
20
|
|
|
|
|
21
|
|
|
private const VISITOR_TEST_IMPRESSION_COUNT = 5; |
|
22
|
|
|
private const VISITOR_TEST_BUCKET = 'test_bucket'; |
|
23
|
|
|
private const VISITOR_TEST_DONATION_HISTORY = false; |
|
24
|
|
|
|
|
25
|
|
|
private function getTestbucket(): Bucket { |
|
26
|
|
|
return new Bucket( |
|
27
|
|
|
'test', |
|
28
|
|
|
new Banner( 'TestMain' ) |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function getTrueRandomTestCampaign(): Campaign { |
|
33
|
|
|
return new Campaign( |
|
34
|
|
|
'C18_WMDE_Test', |
|
35
|
|
|
new \DateTime( '2000-01-01 00:00:00' ), |
|
36
|
|
|
new \DateTime( '2099-12-31 23:59:59' ), |
|
37
|
|
|
1, |
|
38
|
|
|
new RandomInteger(), |
|
39
|
|
|
$this->getTestbucket() |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function getTrueRandomTestCampaignCollection(): CampaignCollection { |
|
44
|
|
|
return new CampaignCollection( |
|
45
|
|
|
$this->getTrueRandomTestCampaign() |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function getTestRequest(): Request { |
|
50
|
|
|
return new Request( |
|
51
|
|
|
[], |
|
52
|
|
|
[], |
|
53
|
|
|
[], |
|
54
|
|
|
[ |
|
55
|
|
|
BannerSelectionController::IMPRESSION_COUNT_COOKIE => self::VISITOR_TEST_IMPRESSION_COUNT, |
|
56
|
|
|
BannerSelectionController::BUCKET_COOKIE => self::VISITOR_TEST_BUCKET, |
|
57
|
|
|
BannerSelectionController::DONATED_COOKIE => self::VISITOR_TEST_DONATION_HISTORY ] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function getTestVisitor(): Visitor { |
|
62
|
|
|
return new Visitor( |
|
63
|
|
|
self::VISITOR_TEST_IMPRESSION_COUNT, |
|
64
|
|
|
self::VISITOR_TEST_BUCKET, |
|
65
|
|
|
self::VISITOR_TEST_DONATION_HISTORY |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function getFirstTimeVisitor(): Visitor { |
|
70
|
|
|
return new Visitor( |
|
71
|
|
|
0, |
|
72
|
|
|
null, |
|
73
|
|
|
false |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getMockAssets(): Packages { |
|
78
|
|
|
$mockAssets = $this->createMock( Packages::class ); |
|
79
|
|
|
$mockAssets->method( 'getUrl' )->willReturnArgument( 0 ); |
|
|
|
|
|
|
80
|
|
|
return $mockAssets; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function test_controller_passes_cookies_to_use_case() { |
|
84
|
|
|
$mockUseCase = $this->createMock( BannerSelectionUseCase::class ); |
|
85
|
|
|
$mockUseCase->expects( $this->once() )->method( 'provideBannerRequest' )->with( $this->getTestVisitor() ); |
|
86
|
|
|
$controller = new BannerSelectionController( $mockUseCase, $this->getMockAssets() ); |
|
87
|
|
|
$controller->selectBanner( $this->getTestRequest() ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function test_given_no_cookies_passed_through_request_controller_assigns_default_values() { |
|
91
|
|
|
$mockUseCase = $this->createMock( BannerSelectionUseCase::class ); |
|
92
|
|
|
$mockUseCase->expects( $this->once() )->method( 'provideBannerRequest' )->with( $this->getFirstTimeVisitor() ); |
|
93
|
|
|
$controller = new BannerSelectionController( $mockUseCase, $this->getMockAssets() ); |
|
94
|
|
|
$controller->selectBanner( new Request() ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function test_given_no_cookies_passed_through_request_controller_creates_cookies_with_updated_default_values() { |
|
98
|
|
|
$testUseCase = new BannerSelectionUseCase( |
|
99
|
|
|
$this->getTrueRandomTestCampaignCollection(), |
|
100
|
|
|
new ImpressionThreshold( 10 ) |
|
101
|
|
|
); |
|
102
|
|
|
$controller = new BannerSelectionController( $testUseCase, $this->getMockAssets() ); |
|
103
|
|
|
$response = $controller->selectBanner( new Request() ); |
|
104
|
|
|
|
|
105
|
|
|
$cookies = $response->headers->getCookies(); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertEquals( 2, count( $cookies ) ); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals( BannerSelectionController::BUCKET_COOKIE, $cookies[0]->getName() ); |
|
110
|
|
|
$this->assertEquals( 'test', $cookies[0]->getValue() ); |
|
111
|
|
|
$this->assertEquals( |
|
112
|
|
|
( new \DateTime( '2099-12-31 23:59:59' ) )->modify( '+2 week' )->getTimestamp(), |
|
113
|
|
|
$cookies[0]->getExpiresTime() |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertEquals( BannerSelectionController::IMPRESSION_COUNT_COOKIE, $cookies[1]->getName() ); |
|
117
|
|
|
$this->assertEquals( '1', $cookies[1]->getValue() ); |
|
118
|
|
|
$this->assertEquals( |
|
119
|
|
|
( new \DateTime( '2099-12-31 23:59:59' ) )->modify( '+2 week' )->getTimestamp(), |
|
120
|
|
|
$cookies[1]->getExpiresTime() |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.