Completed
Push — staging ( 14487f...0bfb1c )
by
unknown
47s queued 34s
created

PageSubscriberTest::getPageSubscriber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 0
dl 0
loc 39
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\PageBundle\Tests\EventListener;
13
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\EntityRepository;
16
use Mautic\CoreBundle\Helper\IpLookupHelper;
17
use Mautic\CoreBundle\Model\AuditLogModel;
18
use Mautic\CoreBundle\Templating\Helper\AssetsHelper;
19
use Mautic\CoreBundle\Translation\Translator;
20
use Mautic\LeadBundle\Entity\Lead;
21
use Mautic\PageBundle\Entity\Hit;
22
use Mautic\PageBundle\Event\PageBuilderEvent;
23
use Mautic\PageBundle\EventListener\PageSubscriber;
24
use Mautic\PageBundle\Model\PageModel;
25
use Mautic\QueueBundle\Event\QueueConsumerEvent;
26
use Mautic\QueueBundle\Queue\QueueConsumerResults;
27
use Mautic\QueueBundle\QueueEvents;
28
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
29
use Symfony\Component\EventDispatcher\EventDispatcher;
30
use Symfony\Component\HttpFoundation\Request;
31
32
class PageSubscriberTest extends WebTestCase
33
{
34
    public function testGetTokens_WhenCalled_ReturnsValidTokens()
35
    {
36
        $translator = $this->getMockBuilder(Translator::class)->disableOriginalConstructor()
37
            ->getMock();
38
39
        $pageBuilderEvent = new PageBuilderEvent($translator);
40
        $pageBuilderEvent->addToken('{token_test}', 'TOKEN VALUE');
41
        $tokens = $pageBuilderEvent->getTokens();
42
        $this->assertArrayHasKey('{token_test}', $tokens);
43
        $this->assertEquals($tokens['{token_test}'], 'TOKEN VALUE');
44
    }
45
46
    public function testOnPageHit_WhenCalled_AcknowledgesHit()
47
    {
48
        $dispatcher = new EventDispatcher();
49
        $subscriber = $this->getPageSubscriber();
50
51
        $dispatcher->addSubscriber($subscriber);
52
53
        $payload = $this->getNonEmptyPayload();
54
        $event   = new QueueConsumerEvent($payload);
55
56
        $dispatcher->dispatch(QueueEvents::PAGE_HIT, $event);
57
58
        $this->assertEquals($event->getResult(), QueueConsumerResults::ACKNOWLEDGE);
59
    }
60
61
    public function testOnPageHit_WhenCalled_RejectsBadHit()
62
    {
63
        $dispatcher = new EventDispatcher();
64
        $subscriber = $this->getPageSubscriber();
65
66
        $dispatcher->addSubscriber($subscriber);
67
68
        $payload = $this->getEmptyPayload();
69
        $event   = new QueueConsumerEvent($payload);
70
71
        $dispatcher->dispatch(QueueEvents::PAGE_HIT, $event);
72
73
        $this->assertEquals($event->getResult(), QueueConsumerResults::REJECT);
74
    }
75
76
    /**
77
     * Get page subscriber with mocked dependencies.
78
     *
79
     * @return PageSubscriber
80
     */
81
    protected function getPageSubscriber()
82
    {
83
        $assetsHelperMock   = $this->createMock(AssetsHelper::class);
84
        $ipLookupHelperMock = $this->createMock(IpLookupHelper::class);
85
        $auditLogModelMock  = $this->createMock(AuditLogModel::class);
86
        $pageModelMock      = $this->createMock(PageModel::class);
87
        $entityManagerMock  = $this->createMock(EntityManager::class);
88
        $hitRepository      = $this->createMock(EntityRepository::class);
89
        $pageRepository     = $this->createMock(EntityRepository::class);
90
        $leadRepository     = $this->createMock(EntityRepository::class);
91
        $hitMock            = $this->createMock(Hit::class);
92
        $leadMock           = $this->createMock(Lead::class);
93
94
        $hitRepository->expects($this->any())
95
            ->method('find')
96
            ->will($this->returnValue($hitMock));
97
98
        $leadRepository->expects($this->any())
99
            ->method('find')
100
            ->will($this->returnValue($leadMock));
101
102
        $entityManagerMock->expects($this->any())
103
            ->method('getRepository')
104
            ->will($this->returnValueMap([
105
                ['MauticPageBundle:Hit', $hitRepository],
106
                ['MauticPageBundle:Page', $pageRepository],
107
                ['MauticLeadBundle:Lead', $leadRepository],
108
            ]));
109
110
        $pageSubscriber = new PageSubscriber(
111
            $assetsHelperMock,
112
            $ipLookupHelperMock,
113
            $auditLogModelMock,
114
            $pageModelMock
115
        );
116
117
        $pageSubscriber->setEntityManager($entityManagerMock);
118
119
        return $pageSubscriber;
120
    }
121
122
    /**
123
     * Get non empty payload, having a Request and non-null entity IDs.
124
     *
125
     * @return array
126
     */
127
    protected function getNonEmptyPayload()
128
    {
129
        $requestMock = $this->createMock(Request::class);
130
131
        return [
132
            'request' => $requestMock,
133
            'isNew'   => true,
134
            'hitId'   => 123,
135
            'pageId'  => 456,
136
            'leadId'  => 789,
137
        ];
138
    }
139
140
    /**
141
     * Get empty payload with all null entity IDs.
142
     *
143
     * @return array
144
     */
145
    protected function getEmptyPayload()
146
    {
147
        return array_fill_keys(['request', 'isNew', 'hitId', 'pageId', 'leadId'], null);
148
    }
149
}
150