1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Validation; |
5
|
|
|
|
6
|
|
|
use Cocur\Slugify\SlugifyInterface; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
9
|
|
|
use Shlinkio\Shlink\Common\Validation\SluggerFilter; |
10
|
|
|
|
11
|
|
|
class SluggerFilterTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** @var SluggerFilter */ |
14
|
|
|
private $filter; |
15
|
|
|
/** @var ObjectProphecy */ |
16
|
|
|
private $slugger; |
17
|
|
|
|
18
|
|
|
public function setUp(): void |
19
|
|
|
{ |
20
|
|
|
$this->slugger = $this->prophesize(SlugifyInterface::class); |
21
|
|
|
$this->filter = new SluggerFilter($this->slugger->reveal()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @test |
26
|
|
|
* @dataProvider provideValuesToFilter |
27
|
|
|
*/ |
28
|
|
|
public function providedValueIsFilteredAsExpected($providedValue, $expectedValue): void |
29
|
|
|
{ |
30
|
|
|
$slugify = $this->slugger->slugify($providedValue)->willReturn('slug'); |
31
|
|
|
|
32
|
|
|
$result = $this->filter->filter($providedValue); |
33
|
|
|
|
34
|
|
|
$this->assertEquals($expectedValue, $result); |
35
|
|
|
$slugify->shouldHaveBeenCalledTimes($expectedValue !== null ? 1 : 0); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function provideValuesToFilter(): iterable |
39
|
|
|
{ |
40
|
|
|
yield 'null' => [null, null]; |
41
|
|
|
yield 'empty string' => ['', null]; |
42
|
|
|
yield 'not empty string' => ['foo', 'slug']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @test */ |
46
|
|
|
public function internalSluggerKeepsCasing(): void |
47
|
|
|
{ |
48
|
|
|
$filter = new SluggerFilter(); |
49
|
|
|
$this->assertEquals('FoO-baR', $filter->filter('FoO baR')); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|