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
86
87
88
89
90
91
92
93
94
<?php

declare(strict_types=1);

namespace Mautic\WebhookBundle\Tests\Entity;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\WebhookBundle\Entity\Event;
use Mautic\WebhookBundle\Entity\Webhook;
use Mautic\WebhookBundle\Entity\WebhookQueue;
use PHPUnit\Framework\Assert;

class WebhookQueueFunctionalTest extends MauticMysqlTestCase
{
    public function testPayloadBackwardCompatible(): void
    {
        $webhookQueue = $this->createWebhookQueue();

        $payload  = 'BC payload';
        $property = new \ReflectionProperty(WebhookQueue::class, 'payload');
        $property->setAccessible(true);
        $property->setValue($webhookQueue, $payload);

        Assert::assertSame($payload, $webhookQueue->getPayload());

        $this->em->flush();

        $payloadDbValues = $this->fetchPayloadDbValues($webhookQueue);
        Assert::assertSame($payload, $payloadDbValues['payload']);
        Assert::assertNull($payloadDbValues['payload_compressed']);

        $this->em->clear();
        $webhookQueue = $this->em->getRepository(WebhookQueue::class)
            ->find($webhookQueue->getId());

        Assert::assertSame($payload, $webhookQueue->getPayload());
    }

    public function testPayloadCompressed(): void
    {
        $webhookQueue = $this->createWebhookQueue();

        $payload  = 'Compressed payload';
        $webhookQueue->setPayload($payload);

        Assert::assertSame($payload, $webhookQueue->getPayload());

        $this->em->flush();

        $payloadDbValues = $this->fetchPayloadDbValues($webhookQueue);
        Assert::assertNull($payloadDbValues['payload']);
        Assert::assertSame($payload, gzuncompress($payloadDbValues['payload_compressed']));

        $this->em->clear();
        $webhookQueue = $this->em->getRepository(WebhookQueue::class)
            ->find($webhookQueue->getId());

        Assert::assertSame($payload, $webhookQueue->getPayload());
    }

    private function createWebhookQueue(): WebhookQueue
    {
        $webhook = new Webhook();
        $webhook->setName('Test');
        $webhook->setWebhookUrl('http://domain.tld');
        $webhook->setSecret('secret');
        $this->em->persist($webhook);

        $even = new Event();
        $even->setWebhook($webhook);
        $even->setEventType('Type');
        $this->em->persist($even);

        $webhookQueue = new WebhookQueue();
        $webhookQueue->setWebhook($webhook);
        $webhookQueue->setEvent($even);
        $this->em->persist($webhookQueue);

        return $webhookQueue;
    }

    /**
     * @return mixed[]
     */
    private function fetchPayloadDbValues(WebhookQueue $webhookQueue): array
    {
        $prefix = static::getContainer()->getParameter('mautic.db_table_prefix');
        $query  = sprintf('SELECT payload, payload_compressed FROM %swebhook_queue WHERE id = ?', $prefix);

        return $this->connection->executeQuery($query, [$webhookQueue->getId()])
            ->fetchAssociative();
    }
}