Issues (3627)

WebhookBundle/Tests/Model/WebhookModelTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\WebhookBundle\Tests\Model;
13
14
use Doctrine\ORM\EntityManager;
15
use JMS\Serializer\SerializerInterface;
16
use Mautic\CoreBundle\Helper\CoreParametersHelper;
17
use Mautic\CoreBundle\Helper\UserHelper;
18
use Mautic\WebhookBundle\Entity\Event;
19
use Mautic\WebhookBundle\Entity\Webhook;
20
use Mautic\WebhookBundle\Entity\WebhookQueue;
21
use Mautic\WebhookBundle\Entity\WebhookQueueRepository;
22
use Mautic\WebhookBundle\Entity\WebhookRepository;
23
use Mautic\WebhookBundle\Http\Client;
24
use Mautic\WebhookBundle\Model\WebhookModel;
25
use PHPUnit\Framework\MockObject\MockObject;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
29
class WebhookModelTest extends \PHPUnit\Framework\TestCase
30
{
31
    /**
32
     * @var MockObject|CoreParametersHelper
33
     */
34
    private $parametersHelperMock;
35
36
    /**
37
     * @var MockObject|SerializerInterface
38
     */
39
    private $serializerMock;
40
41
    /**
42
     * @var MockObject|EntityManager
43
     */
44
    private $entityManagerMock;
45
46
    /**
47
     * @var MockObject|WebhookRepository
48
     */
49
    private $webhookRepository;
50
51
    /**
52
     * @var MockObject|UserHelper
53
     */
54
    private $userHelper;
55
56
    /**
57
     * @var MockObject|EventDispatcherInterface
58
     */
59
    private $eventDispatcherMock;
60
61
    /**
62
     * @var WebhookModel
63
     */
64
    private $model;
65
66
    private $httpClientMock;
67
68
    protected function setUp(): void
69
    {
70
        $this->parametersHelperMock  = $this->createMock(CoreParametersHelper::class);
71
        $this->serializerMock        = $this->createMock(SerializerInterface::class);
72
        $this->entityManagerMock     = $this->createMock(EntityManager::class);
73
        $this->userHelper            = $this->createMock(UserHelper::class);
74
        $this->webhookRepository     = $this->createMock(WebhookRepository::class);
75
        $this->httpClientMock        = $this->createMock(Client::class);
76
        $this->entityManagerMock     = $this->createMock(EntityManager::class);
77
        $this->eventDispatcherMock   = $this->createMock(EventDispatcher::class);
78
        $this->model                 = $this->initModel();
79
    }
80
81
    public function testSaveEntity(): void
82
    {
83
        $entity = new Webhook();
84
85
        // The secret hash is null at first.
86
        $this->assertNull($entity->getSecret());
87
88
        $this->entityManagerMock->expects($this->once())
89
            ->method('getRepository')
90
            ->with(Webhook::class)
91
            ->willReturn($this->webhookRepository);
92
93
        $this->webhookRepository->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Mautic\WebhookBundle\Entity\WebhookRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        $this->webhookRepository->/** @scrutinizer ignore-call */ 
94
                                  expects($this->once())
Loading history...
94
            ->method('saveEntity')
95
            ->with($this->callback(function (Webhook $entity) {
96
                // The secret hash is not empty on save.
97
                $this->assertNotEmpty($entity->getSecret());
98
99
                return true;
100
            }));
101
102
        $this->model->saveEntity($entity);
103
    }
104
105
    public function testGetEventsOrderbyDirWhenSetInWebhook()
106
    {
107
        $webhook = (new Webhook())->setEventsOrderbyDir('DESC');
108
        $this->assertEquals('DESC', $this->model->getEventsOrderbyDir($webhook));
109
    }
110
111
    public function testGetEventsOrderbyDirWhenNotSetInWebhook()
112
    {
113
        $this->parametersHelperMock->method('get')->willReturn('DESC');
114
        $this->assertEquals('DESC', $this->initModel()->getEventsOrderbyDir());
115
    }
116
117
    public function testGetEventsOrderbyDirWhenWebhookNotProvided()
118
    {
119
        $this->parametersHelperMock->method('get')->willReturn('DESC');
120
        $this->assertEquals('DESC', $this->initModel()->getEventsOrderbyDir());
121
    }
122
123
    public function testGetWebhookPayloadForPayloadInWebhook()
124
    {
125
        $payload = ['the' => 'payload'];
126
        $webhook = new Webhook();
127
        $webhook->setPayload($payload);
128
129
        $this->assertEquals($payload, $this->model->getWebhookPayload($webhook));
130
    }
131
132
    public function testGetWebhookPayloadForQueueLoadedFromDatabase()
133
    {
134
        $queueMock = $this->createMock(WebhookQueue::class);
135
        $webhook   = new Webhook();
136
        $event     = new Event();
137
        $event->setEventType('leads');
138
        $queueMock->method('getPayload')->willReturn('{"the": "payload"}');
139
        $queueMock->method('getEvent')->willReturn($event);
140
        $queueMock->method('getDateAdded')->willReturn(new \DateTime('2018-04-10T15:04:57+00:00'));
141
        $queueMock->method('getId')->willReturn(12);
142
143
        $queueRepositoryMock = $this->createMock(WebhookQueueRepository::class);
144
145
        $this->parametersHelperMock->expects($this->at(4))
146
            ->method('get')
147
            ->with('queue_mode')
148
            ->willReturn(WebhookModel::COMMAND_PROCESS);
149
150
        $this->entityManagerMock->expects($this->at(0))
151
            ->method('getRepository')
152
            ->with(WebhookQueue::class)
153
            ->willReturn($queueRepositoryMock);
154
155
        $this->entityManagerMock->expects($this->once())
156
            ->method('detach')
157
            ->with($queueMock);
158
159
        $queueRepositoryMock->expects($this->once())
160
            ->method('getEntities')
161
            ->willReturn([[$queueMock]]);
162
163
        $expectedPayload = [
164
            'leads' => [
165
                [
166
                    'the'       => 'payload',
167
                    'timestamp' => '2018-04-10T15:04:57+00:00',
168
                ],
169
            ],
170
        ];
171
172
        $this->assertEquals($expectedPayload, $this->initModel()->getWebhookPayload($webhook));
173
    }
174
175
    public function testGetWebhookPayloadForQueueInWebhook()
176
    {
177
        $queue   = new WebhookQueue();
178
        $webhook = new Webhook();
179
        $event   = new Event();
180
        $event->setEventType('leads');
181
        $queue->setPayload('{"the": "payload"}');
182
        $queue->setEvent($event);
183
        $queue->setDateAdded(new \DateTime('2018-04-10T15:04:57+00:00'));
184
185
        $this->parametersHelperMock->expects($this->at(4))
186
            ->method('get')
187
            ->with('queue_mode')
188
            ->willReturn(WebhookModel::IMMEDIATE_PROCESS);
189
190
        $expectedPayload = [
191
            'leads' => [
192
                [
193
                    'the'       => 'payload',
194
                    'timestamp' => '2018-04-10T15:04:57+00:00',
195
                ],
196
            ],
197
        ];
198
199
        $this->assertEquals($expectedPayload, $this->initModel()->getWebhookPayload($webhook, $queue));
200
    }
201
202
    private function initModel()
203
    {
204
        $model = new WebhookModel(
205
            $this->parametersHelperMock,
206
            $this->serializerMock,
207
            $this->httpClientMock,
208
            $this->eventDispatcherMock
209
        );
210
211
        $model->setEntityManager($this->entityManagerMock);
212
        $model->setUserHelper($this->userHelper);
213
        $model->setDispatcher($this->eventDispatcherMock);
214
215
        return $model;
216
    }
217
}
218