File size: 2,955 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
95
96
97
98
99
100
101
102
<?php

namespace Mautic\SmsBundle\Tests\Integration\Twilio;

use Mautic\SmsBundle\Helper\ContactHelper;
use Mautic\SmsBundle\Integration\Twilio\Configuration;
use Mautic\SmsBundle\Integration\Twilio\TwilioCallback;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class TwilioCallbackTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ContactHelper|\PHPUnit\Framework\MockObject\MockObject
     */
    private \PHPUnit\Framework\MockObject\MockObject $contactHelper;

    /**
     * @var Configuration|\PHPUnit\Framework\MockObject\MockObject
     */
    private \PHPUnit\Framework\MockObject\MockObject $configuration;

    protected function setUp(): void
    {
        $this->contactHelper = $this->createMock(ContactHelper::class);
        $this->configuration = $this->createMock(Configuration::class);
        $this->configuration->method('getAccountSid')
            ->willReturn('123');
    }

    public function testMissingFromThrowsBadRequestException(): void
    {
        $this->expectException(BadRequestHttpException::class);

        $request          = $this->createMock(Request::class);
        $inputBag         = new InputBag([
            'AccountSid' => '123',
            'From'       => '',
        ]);

        $request->request = $inputBag;

        $this->getCallback()->getMessage($request);
    }

    public function testMissingBodyThrowsBadRequestException(): void
    {
        $this->expectException(BadRequestHttpException::class);

        $request          = $this->createMock(Request::class);
        $inputBag         = new InputBag([
            'AccountSid' => '123',
            'From'       => '321',
            'Body'       => '',
        ]);

        $request->request = $inputBag;

        $this->getCallback()->getMessage($request);
    }

    public function testMismatchedAccountSidThrowsBadRequestException(): void
    {
        $this->expectException(BadRequestHttpException::class);

        $request          = $this->createMock(Request::class);
        $inputBag         = new InputBag([
            'AccountSid' => '321',
        ]);

        $request->request = $inputBag;

        $this->getCallback()->getMessage($request);
    }

    public function testMessageIsReturned(): void
    {
        $request      = $this->createMock(Request::class);
        $request->method('get')
            ->willReturn('Hello');

        $inputBag = new InputBag([
            'AccountSid' => '123',
            'From'       => '321',
            'Body'       => 'Hello',
        ]);

        $request->request = $inputBag;

        $this->assertEquals('Hello', $this->getCallback()->getMessage($request));
    }

    /**
     * @return TwilioCallback
     */
    private function getCallback()
    {
        return new TwilioCallback($this->contactHelper, $this->configuration);
    }
}