-
Notifications
You must be signed in to change notification settings - Fork 25
/
JoliTypoTest.php
182 lines (139 loc) · 5.73 KB
/
JoliTypoTest.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
179
180
181
182
<?php
/*
* This file is part of JoliTypo - a project by JoliCode.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
namespace JoliTypo\Tests;
use JoliTypo\Exception\BadRuleSetException;
use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\StateBag;
use PHPUnit\Framework\TestCase;
class JoliTypoTest extends TestCase
{
public function testSimpleInstance(): void
{
$fixer = new Fixer(['Ellipsis']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$this->assertSame('Coucou…', $fixer->fix('Coucou...'));
}
public function testSimpleInstanceRulesChange(): void
{
$fixer = new Fixer(['Ellipsis']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$this->assertSame('Coucou…', $fixer->fix('Coucou...'));
$fixer->setRules(['CurlyQuote']);
$this->assertSame('I’m a pony.', $fixer->fix("I'm a pony."));
}
public function testHtmlComments(): void
{
$fixer = new Fixer(['Ellipsis']);
$this->assertSame('<p>Coucou…</p> <!-- Not Coucou... -->', $fixer->fix('<p>Coucou...</p> <!-- Not Coucou... -->'));
// This test can't be ok, DomDocument is encoding entities even in comments (╯°□°)╯︵ ┻━┻
// $this->assertSame("<p>Coucou…</p> <!-- abusé -->", $fixer->fix("<p>Coucou...</p> <!-- abusé -->"));
}
public function testBadRuleSetsWithEmptyArray(): void
{
$this->expectException(BadRuleSetException::class);
new Fixer([]);
}
public function testBadRuleSetsWithEmptyArrayAfterConstructor(): void
{
$this->expectException(BadRuleSetException::class);
$fixer = new Fixer(['Ellipsis']);
$fixer->setRules([]);
}
public function testInvalidCustomFixerInstance(): void
{
$this->expectException(BadRuleSetException::class);
new Fixer([new FakeFixer()]);
}
public function testOkFixer(): void
{
$fixer = new Fixer([new OkFixer()]);
$this->assertSame('<p>Nope !</p>', $fixer->fix('<p>Nope !</p>'));
}
public function testProtectedTags(): void
{
$fixer = new Fixer(['Ellipsis']);
$fixer->setProtectedTags(['pre', 'a']);
$fixed_content = $fixer->fix('<p>Fixed...</p> <pre>Not fixed...</pre> <p>Fixed... <a>Not Fixed...</a>.</p>');
$this->assertSame('<p>Fixed…</p> <pre>Not fixed...</pre> <p>Fixed… <a>Not Fixed...</a>.</p>', $fixed_content);
}
public function testBadClassName(): void
{
$this->expectException(BadRuleSetException::class);
new Fixer(['Ellipsis', 'Acme\\Demo\\Fixer']);
}
public function testBadLocale(): void
{
$this->expectException(\InvalidArgumentException::class);
$fixer = new Fixer(['Ellipsis']);
$fixer->setLocale(false);
}
public function testEmptyRules(): void
{
$this->expectException(BadRuleSetException::class);
new Fixer([]);
}
public function testXmlPrefixedContent(): void
{
$fixer = new Fixer(['Ellipsis']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$this->assertSame('<p>Hey épic dude…</p>', $fixer->fix('<?xml encoding="UTF-8"><body><p>Hey épic dude...</p></body>'));
$this->assertSame('<p>Hey épic dude…</p>', $fixer->fix('<?xml encoding="ISO-8859-1"><body><p>Hey épic dude...</p></body>'));
}
public function testBadEncoding(): void
{
$fixer = new Fixer(['Trademark']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$this->assertSame('Mentions Légales', $fixer->fix('Mentions Légales'));
// JoliTypo can handle double encoded UTF-8 strings, or ISO strings, but that's not a feature.
$isoString = mb_convert_encoding('Mentions Légales', 'ISO-8859-1', 'UTF-8');
$this->assertSame('Mentions Légales', $fixer->fix($isoString));
$this->assertSame('Mentions Légales', $fixer->fix('Mentions Légales'));
}
public function testEmptyContent(): void
{
$fixer = new Fixer(['Trademark']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$this->assertSame('', $fixer->fix(''));
$this->assertSame("\n ", $fixer->fix("\n "));
$this->assertSame('some content ®', $fixer->fix("\n some content (r)"));
}
public function testNonHTMLContent(): void
{
$fixer = new Fixer(['Trademark', 'SmartQuotes']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$toFix = <<<NOT_HTML
I don't think "FosUserBundle" is a good idea for a complex application.
\tThat being said, it's an awesome way to get stuffs done(c) in a snap!
NOT_HTML;
$fixed = <<<NOT_HTML
I don't think “FosUserBundle” is a good idea for a complex application.
\tThat being said, it's an awesome way to get stuffs done© in a snap!
NOT_HTML;
$this->assertSame($fixed, $fixer->fix($toFix));
$this->assertSame(html_entity_decode($fixed, \ENT_COMPAT, 'UTF-8'), $fixer->fixString($toFix));
$this->assertSame('Here is a “protip©”!', $fixer->fixString('Here is a "protip(c)"!'));
}
/** @group legacy */
public function testDeprecatedFixer(): void
{
$fixer = new Fixer(['Numeric']);
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);
$this->assertSame('3' . Fixer::NO_BREAK_SPACE . '€', $fixer->fixString('3 €'));
}
}
class FakeFixer
{
}
class OkFixer implements FixerInterface
{
public function fix(string $content, ?StateBag $stateBag = null)
{
return $content;
}
}