SluggerFilterTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 5
b 0
f 0
dl 0
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A providedValueIsFilteredAsExpected() 0 8 2
A provideValuesToFilterWithCasing() 0 8 1
A provideValuesToFilter() 0 5 1
A setUp() 0 4 1
A internalSluggerKeepsCasing() 0 4 1
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