-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlHighlightTest.php
165 lines (150 loc) · 5.4 KB
/
UrlHighlightTest.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
<?php
declare(strict_types=1);
namespace VStelmakh\UrlHighlight\Tests;
use VStelmakh\UrlHighlight\Encoder\EncoderInterface;
use VStelmakh\UrlHighlight\Encoder\HtmlSpecialcharsEncoder;
use VStelmakh\UrlHighlight\Highlighter\HighlighterInterface;
use VStelmakh\UrlHighlight\UrlHighlight;
use PHPUnit\Framework\TestCase;
use VStelmakh\UrlHighlight\Validator\ValidatorInterface;
class UrlHighlightTest extends TestCase
{
/**
* @dataProvider isUrlDataProvider
*
* @param string $string
* @param bool $expected
*/
public function testIsUrl(string $string, bool $expected): void
{
$urlHighlight = new UrlHighlight();
$actual = $urlHighlight->isUrl($string);
self::assertEquals($expected, $actual, 'Expected ' . ($expected ? '"true"' : '"false"') . ' for: ' . $string);
}
/**
* @return mixed[]
*/
public function isUrlDataProvider(): array
{
return [
['http://example.com', true],
['example.com', true],
['not url', false],
];
}
/**
* @dataProvider getUrlsDataProvider
*
* @param string $string
* @param array<string, string[]> $expected
*/
public function testGetUrls(string $string, array $expected): void
{
$urlHighlight = new UrlHighlight();
$actual = $urlHighlight->getUrls($string);
self::assertEquals($expected, $actual, 'Input: ' . $string);
}
/**
* @return mixed[]
*/
public function getUrlsDataProvider(): array
{
return [
[
'Example text before http://example.com/app.php/some/path/index.html and after. Open filename.txt at 3:00pm. For more info see google.com.',
['http://example.com/app.php/some/path/index.html', 'google.com'],
],
[
'<a href="mailto:[email protected]">Example</a>',
['mailto:[email protected]'],
],
[
'not url',
[],
],
];
}
/**
* @dataProvider highlightUrlsDataProvider
*
* @param EncoderInterface|null $encoder
* @param string $string
* @param string $expected
*/
public function testHighlightUrls(?EncoderInterface $encoder, string $string, string $expected): void
{
$urlHighlight = new UrlHighlight(null, null, $encoder);
$actual = $urlHighlight->highlightUrls($string);
self::assertEquals($expected, $actual);
}
/**
* @return mixed[]
*/
public function highlightUrlsDataProvider(): array
{
return [
[
null,
'Example text before http://example.com and after.',
'Example text before <a href="http://example.com">http://example.com</a> and after.',
],
[
null,
'With html <p>http://example.com</p>',
'With html <p><a href="http://example.com">http://example.com</a></p>',
],
[
null,
'Example text before example.com and after.',
'Example text before <a href="http://example.com">example.com</a> and after.',
],
[
null,
'With html <p>example.com</p>',
'With html <p><a href="http://example.com">example.com</a></p>',
],
[
null,
'With html <p>http://example.com and links <a href="http://example.com">http://example.com</a></p>',
'With html <p><a href="http://example.com">http://example.com</a> and links <a href="http://example.com">http://example.com</a></p>',
],
[
null,
'With email [email protected].',
'With email <a href="mailto:[email protected]">[email protected]</a>.',
],
[
null,
'<a href="http://example.com?q=query">example.com?q=query</a>',
'<a href="<a href="http://example.com?q=query">example.com?q=query</a>">http://example.com?q=query">example.com?q=query</a></a>;',
],
[
new HtmlSpecialcharsEncoder(),
'<a href="http://example.com?q=query">example.com?q=query</a>',
'<a href="<a href="http://example.com?q=query">http://example.com?q=query</a>"><a href="http://example.com?q=query">example.com?q=query</a></a>',
],
];
}
public function testDependencies(): void
{
$string = 'http://example.com';
$validator = $this->createMock(ValidatorInterface::class);
$validator
->expects(self::atLeastOnce())
->method('isValidMatch')
->willReturn(true);
$highlighter = $this->createMock(HighlighterInterface::class);
$highlighter
->expects(self::once())
->method('highlight');
$encoder = $this->createMock(EncoderInterface::class);
$encoder
->expects(self::atLeastOnce())
->method('decode')
->willReturn($string);
$urlHighlight = new UrlHighlight($validator, $highlighter, $encoder);
$urlHighlight->isUrl($string);
$urlHighlight->getUrls($string);
$urlHighlight->highlightUrls($string);
}
}