/
GroupFallbackReplacerTest.php
178 lines (154 loc) · 5.2 KB
/
GroupFallbackReplacerTest.php
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
<?php
namespace Test\Unit\TRegx\CleanRegex\Replace\Map;
use PHPUnit\Framework\TestCase;
use Test\Feature\TRegx\CleanRegex\Replace\by\group\CustomException;
use TRegx\CleanRegex\Exception\CleanRegex\NonexistentGroupException;
use TRegx\CleanRegex\Exception\CleanRegex\Messages\Group\ReplacementWithUnmatchedGroupMessage;
use TRegx\CleanRegex\Internal\InternalPattern;
use TRegx\CleanRegex\Internal\Match\Base\ApiBase;
use TRegx\CleanRegex\Internal\Match\UserData;
use TRegx\CleanRegex\Internal\SubjectableImpl;
use Test\Utils\ComputedMapper;
use TRegx\CleanRegex\Replace\GroupMapper\GroupMapper;
use TRegx\CleanRegex\Replace\GroupMapper\IdentityMapper;
use Test\Utils\NoReplacementMapper;
use TRegx\CleanRegex\Replace\GroupMapper\DictionaryMapper;
use TRegx\CleanRegex\Replace\Map\GroupFallbackReplacer;
use TRegx\CleanRegex\Replace\NonReplaced\ConstantResultStrategy;
use TRegx\CleanRegex\Replace\NonReplaced\DefaultStrategy;
use TRegx\CleanRegex\Replace\NonReplaced\ThrowStrategy;
class GroupFallbackReplacerTest extends TestCase
{
/**
* @test
* @happyPath
* @dataProvider strategies
* @param GroupMapper $mapper
* @param $expected
*/
public function shouldReplace_usingStrategy(GroupMapper $mapper, $expected)
{
// given
$mapReplacer = $this->create('\[(\w+)\]', '[two], [three], [four]');
// when
$result = $mapReplacer->replaceOrFallback(1, $mapper, new DefaultStrategy());
// then
$this->assertEquals($expected, $result);
}
function strategies(): array
{
return [
'computed' => [
new ComputedMapper('strlen'),
'3, 5, 4'
],
'identity' => [
new IdentityMapper(),
'two, three, four'
],
'ignoring' => [
new NoReplacementMapper(),
'[two], [three], [four]'
],
'map' => [
new DictionaryMapper(['two' => 'dwa', 'three' => 'trzy', 'four' => 'cztery']),
'dwa, trzy, cztery'
]
];
}
/**
* @test
*/
public function shouldReplace_emptyString()
{
// given
$fallbackReplacer = $this->create('\[(\w*)\]', '[two] [] [four]');
// when
$result = $fallbackReplacer->replaceOrFallback(1,
new ComputedMapper('strlen'),
new DefaultStrategy());
// then
$this->assertEquals('3 0 4', $result);
}
/**
* @test
*/
public function shouldFallback_toStrategy_unmatchedGroup()
{
// given
$fallbackReplacer = $this->create('\[(\w+)?\]', '[two] [] [four]');
// when
$result = $fallbackReplacer->replaceOrFallback(1, new NoReplacementMapper(), new ConstantResultStrategy('fallback'));
// then
$this->assertEquals('[two] fallback [four]', $result);
}
/**
* @test
*/
public function shouldFallback_toDefault()
{
// given
$fallbackReplacer = $this->create('\[(\w+)\]', '');
// when
$result = $fallbackReplacer->replaceOrFallback(1, new NoReplacementMapper(), new DefaultStrategy());
// then
$this->assertEquals('Subject not matched', $result);
}
/**
* @test
*/
public function shouldThrow_forInvalidGroup()
{
// given
$fallbackReplacer = $this->create('', '');
// then
$this->expectException(NonexistentGroupException::class);
$this->expectExceptionMessage("Nonexistent group: '1'");
// when
$fallbackReplacer->replaceOrFallback(1, new NoReplacementMapper(), new DefaultStrategy());
}
/**
* @test
*/
public function shouldThrow_forUnmatchedGroup_last()
{
// given
$fallbackReplacer = $this->create('word:(\d)?', 'word:');
// then
$this->expectException(CustomException::class);
$this->expectExceptionMessage("Expected to replace with group '1', but the group was not matched");
// when
$fallbackReplacer->replaceOrFallback(
1,
new NoReplacementMapper(),
new ThrowStrategy(CustomException::class, new ReplacementWithUnmatchedGroupMessage(1))
);
}
/**
* @test
*/
public function shouldThrow_forUnmatchedGroup_middle()
{
// given
$fallbackReplacer = $this->create('foo:(\d)?:(bar)', 'foo::bar');
// then
$this->expectException(CustomException::class);
$this->expectExceptionMessage("Expected to replace with group '1', but the group was not matched");
// when
$fallbackReplacer->replaceOrFallback(
1,
new NoReplacementMapper(),
new ThrowStrategy(CustomException::class, new ReplacementWithUnmatchedGroupMessage(1))
);
}
public function create($pattern, $subject): GroupFallbackReplacer
{
return new GroupFallbackReplacer(
new InternalPattern($pattern),
new SubjectableImpl($subject),
-1,
new ConstantResultStrategy('Subject not matched'),
new ApiBase(new InternalPattern($pattern), $subject, new UserData())
);
}
}