File size: 2,973 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace Mautic\MessengerBundle\Tests\MessageHandler;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadRepository;
use Mautic\MessengerBundle\Message\PageHitNotification;
use Mautic\MessengerBundle\MessageHandler\PageHitNotificationHandler;
use Mautic\PageBundle\Entity\Hit;
use Mautic\PageBundle\Entity\HitRepository;
use Mautic\PageBundle\Entity\Page;
use Mautic\PageBundle\Entity\PageRepository;
use Mautic\PageBundle\Entity\Redirect;
use Mautic\PageBundle\Entity\RedirectRepository;
use Mautic\PageBundle\Model\PageModel;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;

class PageHitNotificationHandlerTest extends TestCase
{
    public function testInvoke(): void
    {
        [$hitId, $pageId, $leadId, $redirectId]                 = [random_int(1, 1000), random_int(1, 1000), random_int(1, 1000), random_int(1, 1000)];

        $redirectObject = new Redirect();
        $redirectObject->setRedirectId((string) $redirectId);

        [$hitObject, $pageObject, $leadObject] = [
            (new Hit())->setCode(7),
            (new Page())->setAlias('james_bond'),
            (new Lead())->setId($leadId),
        ];

        $hitRepoMock = $this->createMock(HitRepository::class);
        $hitRepoMock
            ->expects($this->once())
            ->method('find')
            ->with($hitId)
            ->willReturn($hitObject);

        $pageRepoMock = $this->createMock(PageRepository::class);
        $pageRepoMock->expects($this->once())
            ->method('find')
            ->with($pageId)
            ->willReturn($pageObject);

        $redirectRepoMock = $this->createMock(RedirectRepository::class);
        $redirectRepoMock
            ->expects($this->never())
            ->method('find')
            ->with($redirectId)
            ->willReturn($redirectObject);

        $leadRepoMock = $this->createMock(LeadRepository::class);
        $leadRepoMock
            ->expects($this->once())
            ->method('find')
            ->with($leadId)
            ->willReturn($leadObject);

        $request = new Request();
        $request->query->set('testMe', 'I am here');

        /** @var MockObject|PageModel $pageModelMock */
        $pageModelMock = $this->createMock(PageModel::class);
        $pageModelMock
            ->expects($this->exactly(1))
            ->method('processPageHit')
            ->with($hitObject, $pageObject, $request, $leadObject, false, false);

        $message = new PageHitNotification($hitId, $request, false, false, $pageId, $leadId);

        /** @var MockObject|LoggerInterface $loggerMock */
        $loggerMock = $this->createMock(LoggerInterface::class);

        $handler = new PageHitNotificationHandler(
            $pageRepoMock, $hitRepoMock, $leadRepoMock, $loggerMock, $redirectRepoMock, $pageModelMock
        );

        $handler->__invoke($message);
    }
}