File: KazInfoTehTransportTest.php

package info (click to toggle)
symfony 6.4.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,776 kB
  • sloc: php: 1,443,643; xml: 6,601; sh: 605; javascript: 597; makefile: 188; pascal: 71
file content (83 lines) | stat: -rw-r--r-- 3,705 bytes parent folder | download | duplicates (2)
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
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Notifier\Bridge\KazInfoTeh\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransport;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
 * @author Egor Taranov <dev@taranovegor.com>
 */
final class KazInfoTehTransportTest extends TransportTestCase
{
    public static function createTransport(?HttpClientInterface $client = null): TransportInterface
    {
        return (new KazInfoTehTransport('username', 'password', 'sender', $client ?? new MockHttpClient()))->setHost('test.host');
    }

    public static function toStringProvider(): iterable
    {
        yield ['kaz-info-teh://test.host?sender=sender', self::createTransport()];
    }

    public static function supportedMessagesProvider(): iterable
    {
        yield [new SmsMessage('77000000000', 'KazInfoTeh!')];
    }

    public static function unsupportedMessagesProvider(): iterable
    {
        yield [new SmsMessage('420000000000', 'KazInfoTeh!')];

        yield [new DummyMessage()];
    }

    public function createClient(int $statusCode, string $content): HttpClientInterface
    {
        return new MockHttpClient(new MockResponse($content, ['http_code' => $statusCode]));
    }

    public static function responseProvider(): iterable
    {
        $responses = [
            ['status' => 200, 'content' => '<?xml version="1.0" encoding="utf-8" ?><acceptreport><statuscode>1</statuscode><statusmessage>Status code is not valid</statusmessage></acceptreport>', 'error_message' => 'Unable to send the SMS: "Status code is not valid".'],
            ['status' => 200, 'content' => '{"message": Response not in XML format}', 'error_message' => 'Unable to send the SMS: "Couldn\'t read response".'],
            ['status' => 200, 'content' => '<?xml version="1.0" encoding="utf-8" ?><acceptreport><errorcode>1</errorcode><errormessage>Error code is not valid</errormessage></acceptreport>', 'error_message' => 'Unable to send the SMS: "Error code is not valid".'],
            ['status' => 500, 'content' => '<?xml version="1.0" encoding="utf-8" ?><acceptreport><errorcode>1</errorcode><errormessage>Something went wrong</errormessage></acceptreport>', 'error_message' => 'Unable to send the SMS: "Something went wrong".'],
            ['status' => 500, 'content' => '', 'error_message' => 'Unable to send the SMS: "Couldn\'t read response".'],
        ];

        foreach ($responses as $response) {
            yield [$response['status'], $response['content'], $response['error_message']];
        }
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('responseProvider')]
    public function testThrowExceptionWhenMessageWasNotSent(int $statusCode, string $content, string $errorMessage)
    {
        $client = $this->createClient($statusCode, $content);
        $transport = $this->createTransport($client);
        $message = new SmsMessage('77000000000', 'Hello, bug!');

        $this->expectException(TransportException::class);
        $this->expectExceptionMessage($errorMessage);

        $transport->send($message);
    }
}