File: SmsboxOptionsTest.php

package info (click to toggle)
symfony 7.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,804 kB
  • sloc: php: 1,506,509; xml: 6,816; javascript: 1,043; sh: 586; makefile: 241; pascal: 70
file content (190 lines) | stat: -rw-r--r-- 7,047 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?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\Smsbox\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Charset;
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Day;
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Mode;
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Strategy;
use Symfony\Component\Notifier\Bridge\Smsbox\Enum\Udh;
use Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;

class SmsboxOptionsTest extends TestCase
{
    public function testSmsboxOptions()
    {
        $smsboxOptions = (new SmsboxOptions())
            ->mode(Mode::Expert)
            ->sender('SENDER')
            ->strategy(Strategy::Marketing)
            ->charset(Charset::Utf8)
            ->udh(Udh::DisabledConcat)
            ->maxParts(2)
            ->validity(100)
            ->destIso('FR');

        self::assertSame([
            'mode' => 'Expert',
            'sender' => 'SENDER',
            'strategy' => 4,
            'charset' => 'utf-8',
            'udh' => 0,
            'max_parts' => 2,
            'validity' => 100,
            'dest_iso' => 'FR',
        ], $smsboxOptions->toArray());
    }

    public function testSmsboxOptionsInvalidDestIso()
    {
        if (!class_exists(Countries::class)) {
            $this->markTestSkipped('The "symfony/intl" component is required to run this test.');
        }

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The country code "X1" is not valid.');

        (new SmsboxOptions())
            ->mode(Mode::Expert)
            ->sender('SENDER')
            ->strategy(Strategy::Marketing)
            ->destIso('X1');
    }

    public function testDateIsCalledWithDateTime()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Either Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::dateTime() or Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::date() and Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::hour() must be called, but not both.');

        (new SmsboxOptions())
            ->dateTime(new \DateTimeImmutable('+1 day'))
            ->date('01/01/2021');
    }

    public function testDateInWrongFormat()
    {
        $this->expectException(\DateMalformedStringException::class);
        $this->expectExceptionMessage('The date must be in DD/MM/YYYY format.');

        (new SmsboxOptions())
            ->date('01/2021');
    }

    public function testHourIsCalledWithDateTime()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Either Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::dateTime() or Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::date() and Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::hour() must be called, but not both.');

        (new SmsboxOptions())
            ->dateTime(new \DateTimeImmutable('+1 day'))
            ->hour('12:00');
    }

    public function testHourInWrongFormat()
    {
        $this->expectException(\DateMalformedStringException::class);
        $this->expectExceptionMessage('Hour must be in HH:MM format.');

        (new SmsboxOptions())
            ->hour('12:00:00');
    }

    public function testDateTimeIsCalledWithDate()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Either Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::dateTime() or Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::date() and Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::hour() must be called, but not both.');

        (new SmsboxOptions())
            ->date('01/01/2021')
            ->dateTime(new \DateTimeImmutable('+1 day'));
    }

    public function testDateTimeIsCalledWithHour()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Either Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::dateTime() or Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::date() and Symfony\Component\Notifier\Bridge\Smsbox\SmsboxOptions::hour() must be called, but not both.');

        (new SmsboxOptions())
            ->hour('12:00')
            ->dateTime(new \DateTimeImmutable('+1 day'));
    }

    public function testDateTimeIsInPast()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The given DateTime must be greater to the current date.');

        (new SmsboxOptions())
            ->dateTime(new \DateTimeImmutable('-1 day'));
    }

    #[\PHPUnit\Framework\Attributes\TestWith([0])]
    #[\PHPUnit\Framework\Attributes\TestWith([9])]
    public function testMaxPartIsInvalid(int $maxPart)
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage(\sprintf('The "max_parts" option must be an integer between 1 and 8, got "%d".', $maxPart));

        (new SmsboxOptions())
            ->maxParts($maxPart);
    }

    #[\PHPUnit\Framework\Attributes\TestWith([4])]
    #[\PHPUnit\Framework\Attributes\TestWith([1441])]
    public function testValidityIsInvalid(int $validity)
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage(\sprintf('The "validity" option must be an integer between 5 and 1440, got "%d".', $validity));

        (new SmsboxOptions())
            ->validity($validity);
    }

    public function testDayMinIsAfterMax()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The minimum day must be before the maximum day or the same.');

        (new SmsboxOptions())
            ->daysMinMax(Day::Sunday, Day::Friday);
    }

    public function testHourIsNegative()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The minimum hour must be greater than 0 and lower than the maximum hour.');

        (new SmsboxOptions())
            ->hoursMinMax(-1, 12);
    }

    public function testMinHourIsAfterMax()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The minimum hour must be greater than 0 and lower than the maximum hour.');

        (new SmsboxOptions())
            ->hoursMinMax(12, 11);
    }

    public function testMaxHourIsOutOfBounds()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The maximum hour must be lower or equal to 23.');

        (new SmsboxOptions())
            ->hoursMinMax(0, 24);
    }
}