forked from Respect/StringFormatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatterBuilderTest.php
More file actions
198 lines (154 loc) · 5.93 KB
/
FormatterBuilderTest.php
File metadata and controls
198 lines (154 loc) · 5.93 KB
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
191
192
193
194
195
196
197
198
<?php
/*
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-License-Identifier: ISC
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\StringFormatter\Test\Integration;
use ArgumentCountError;
use Error;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use Respect\StringFormatter\FormatterBuilder;
use Respect\StringFormatter\InvalidFormatterException;
use Respect\StringFormatter\MaskFormatter;
use Respect\StringFormatter\PatternFormatter;
use Respect\StringFormatter\PlaceholderFormatter;
use function sprintf;
#[CoversClass(FormatterBuilder::class)]
final class FormatterBuilderTest extends TestCase
{
#[Test]
public function itShouldFormatWithSingleFormatter(): void
{
$input = '1234123412341234';
$range = '1-3,8-12';
$maskFormatter = new MaskFormatter($range);
$expected = $maskFormatter->format($input);
$builder = new FormatterBuilder();
$actual = $builder->mask($range)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldFormatWithMultipleFormatters(): void
{
$input = '1234123412341234';
$range = '1-3,8-12';
$pattern = '#### #### #### ####';
$maskFormatter = new MaskFormatter($range);
$patternFormatter = new PatternFormatter($pattern);
$expected = $patternFormatter->format($maskFormatter->format($input));
$builder = new FormatterBuilder();
$actual = $builder->mask($range)->pattern($pattern)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldThrowExceptionWhenFormattingWithoutFormatters(): void
{
$builder = new FormatterBuilder();
$this->expectException(InvalidFormatterException::class);
$this->expectExceptionMessage('No formatters have been added to the builder');
$builder->format('test');
}
#[Test]
public function itShouldAllowCallingSameFormatterMultipleTimes(): void
{
$input = '1234567890';
$firstRange = '1-3';
$secondRange = '5-7';
$firstMaskFormatter = new MaskFormatter($firstRange);
$secondMaskFormatter = new MaskFormatter($secondRange);
$expected = $secondMaskFormatter->format($firstMaskFormatter->format($input));
$builder = new FormatterBuilder();
$builder = $builder->mask($firstRange)->mask($secondRange);
$actual = $builder->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldCreateMaskFormatterUsingStaticFactory(): void
{
$input = '1234567890';
$range = '1-3';
$maskFormatter = new MaskFormatter($range);
$expected = $maskFormatter->format($input);
$actual = FormatterBuilder::mask($range)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldCreatePatternFormatterUsingStaticFactory(): void
{
$input = '1234567890';
$pattern = '###-###-####';
$patternFormatter = new PatternFormatter($pattern);
$expected = $patternFormatter->format($input);
$actual = FormatterBuilder::pattern($pattern)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldCreatePlaceholderFormatterUsingStaticFactory(): void
{
$input = 'Hello, {{name}}!';
$parameters = ['name' => 'World'];
$placeholderFormatter = new PlaceholderFormatter($parameters);
$expected = $placeholderFormatter->format($input);
$actual = FormatterBuilder::placeholder($parameters)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldUsePlaceholderFormatter(): void
{
$input = 'Hello, {{name}}! Your balance is {{amount}}.';
$parameters = [
'name' => 'John',
'amount' => 100.5,
];
$expected = (new PlaceholderFormatter($parameters))->format($input);
$builder = new FormatterBuilder();
$actual = $builder->placeholder($parameters)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldBuildFormatterWithMultipleArguments(): void
{
$input = '1234567890';
$range = '1-3,7-9';
$replacement = 'X';
$maskFormatter = new MaskFormatter($range, $replacement);
$expected = $maskFormatter->format($input);
$builder = new FormatterBuilder();
$actual = $builder->mask($range, $replacement)->format($input);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldThrowExceptionWhenFormatterIsNotInstantiable(): void
{
$builder = new FormatterBuilder();
$this->expectException(Error::class);
$this->expectExceptionMessage('Cannot instantiate interface Respect\StringFormatter\Formatter');
$builder->__call('', []);
}
#[Test]
public function itShouldThrowExceptionWhenFormatterArgumentIsMissing(): void
{
$builder = new FormatterBuilder();
$this->expectException(ArgumentCountError::class);
$this->expectExceptionMessage(sprintf(
'Too few arguments to function %s::__construct(), 0 passed and exactly 1 expected',
PatternFormatter::class,
));
/** @phpstan-ignore arguments.count */
$builder->pattern();
}
#[Test]
public function itShouldThrowExceptionWhenFormatterDoesNotExist(): void
{
$builder = new FormatterBuilder();
$this->expectException(ReflectionException::class);
$this->expectExceptionMessage('Class "Respect\StringFormatter\NonexistentFormatter" does not exist');
/** @phpstan-ignore method.notFound */
$builder->nonexistent();
}
}