1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Common\Validation; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
10
|
|
|
use Shlinkio\Shlink\Common\Validation\SluggerFilter; |
11
|
|
|
use Symfony\Component\String\Slugger\SluggerInterface; |
12
|
|
|
|
13
|
|
|
use function Symfony\Component\String\u; |
14
|
|
|
|
15
|
|
|
class SluggerFilterTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
use ProphecyTrait; |
18
|
|
|
|
19
|
|
|
private SluggerFilter $filter; |
20
|
|
|
private ObjectProphecy $slugger; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->slugger = $this->prophesize(SluggerInterface::class); |
25
|
|
|
$this->filter = new SluggerFilter($this->slugger->reveal()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
* @dataProvider provideValuesToFilter |
31
|
|
|
*/ |
32
|
|
|
public function providedValueIsFilteredAsExpected(?string $providedValue, ?string $expectedValue): void |
33
|
|
|
{ |
34
|
|
|
$slugify = $this->slugger->slug($providedValue)->willReturn(u('slug')); |
35
|
|
|
|
36
|
|
|
$result = $this->filter->filter($providedValue); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($expectedValue, $result); |
39
|
|
|
$slugify->shouldHaveBeenCalledTimes($expectedValue !== null ? 1 : 0); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function provideValuesToFilter(): iterable |
43
|
|
|
{ |
44
|
|
|
yield 'null' => [null, null]; |
45
|
|
|
yield 'empty string' => ['', 'slug']; |
46
|
|
|
yield 'not empty string' => ['foo', 'slug']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @test |
51
|
|
|
* @dataProvider provideValuesToFilterWithCasing |
52
|
|
|
*/ |
53
|
|
|
public function internalSluggerKeepsCasing(string $providedValue, string $expectedValue): void |
54
|
|
|
{ |
55
|
|
|
$filter = new SluggerFilter(); |
56
|
|
|
$this->assertEquals($expectedValue, $filter->filter($providedValue)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function provideValuesToFilterWithCasing(): iterable |
60
|
|
|
{ |
61
|
|
|
yield ['FoO baR', 'FoO-baR']; |
62
|
|
|
yield [' FoO/bar', 'FoO-bar']; |
63
|
|
|
yield [' FoO/bar ', 'FoO-bar']; |
64
|
|
|
yield ['foobar ', 'foobar']; |
65
|
|
|
yield ['fo ob ar ', 'fo-ob-ar']; |
66
|
|
|
yield ['/', '']; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|