forked from mautic/mautic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DetermineWinnerSubscriberTest.php
110 lines (87 loc) · 3.4 KB
/
DetermineWinnerSubscriberTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace Mautic\AssetBundle\Tests\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Mautic\AssetBundle\Entity\DownloadRepository;
use Mautic\AssetBundle\EventListener\DetermineWinnerSubscriber;
use Mautic\CoreBundle\Event\DetermineWinnerEvent;
use Mautic\PageBundle\Entity\Page;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Contracts\Translation\TranslatorInterface;
class DetermineWinnerSubscriberTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject|EntityManagerInterface
*/
private \PHPUnit\Framework\MockObject\MockObject $em;
/**
* @var MockObject|TranslatorInterface
*/
private \PHPUnit\Framework\MockObject\MockObject $translator;
private \Mautic\AssetBundle\EventListener\DetermineWinnerSubscriber $subscriber;
protected function setUp(): void
{
parent::setUp();
$this->em = $this->createMock(EntityManagerInterface::class);
$this->translator = $this->createMock(TranslatorInterface::class);
$this->subscriber = new DetermineWinnerSubscriber($this->em, $this->translator);
}
public function testOnDetermineDownloadRateWinner(): void
{
$parentMock = $this->createMock(Page::class);
$childMock = $this->createMock(Page::class);
$children = [2 => $childMock];
$repoMock = $this->createMock(DownloadRepository::class);
$parameters = ['parent' => $parentMock, 'children' => $children];
$event = new DetermineWinnerEvent($parameters);
$startDate = new \DateTime();
$transDownloads = 'downloads';
$transHits = 'hits';
$counts = [
1 => [
'count' => 20,
'id' => 1,
'name' => 'Test 5',
'total' => 100,
],
2 => [
'count' => 25,
'id' => 2,
'name' => 'Test 6',
'total' => 150,
],
];
$this->translator->method('trans')
->willReturnOnConsecutiveCalls($transDownloads, $transHits);
$this->em->expects($this->once())
->method('getRepository')
->willReturn($repoMock);
$parentMock->expects($this->any())
->method('isPublished')
->willReturn(true);
$childMock->expects($this->any())
->method('isPublished')
->willReturn(true);
$parentMock->expects($this->any())
->method('getId')
->willReturn(1);
$childMock->expects($this->any())
->method('getId')
->willReturn(2);
$parentMock->expects($this->once())
->method('getVariantStartDate')
->willReturn($startDate);
$repoMock->expects($this->once())
->method('getDownloadCountsByPage')
->with([1, 2], $startDate)
->willReturn($counts);
$this->subscriber->onDetermineDownloadRateWinner($event);
$expectedData = [
$transDownloads => [$counts[1]['count'], $counts[2]['count']],
$transHits => [$counts[1]['total'], $counts[2]['total']],
];
$abTestResults = $event->getAbTestResults();
$this->assertEquals($abTestResults['winners'], [1]);
$this->assertEquals($abTestResults['support']['data'], $expectedData);
}
}