Passed
Push — master ( 89f24e...8c4dad )
by Sylvain
08:05
created

UrlTypeTest::testSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use Ecodev\Felix\Api\Scalar\UrlType;
8
use GraphQL\Language\AST\StringValueNode;
9
use PHPUnit\Framework\TestCase;
10
11
final class UrlTypeTest extends TestCase
12
{
13
    /**
14
     * @dataProvider providerUrls
15
     */
16
    public function testSerialize(?string $input, bool $isValid): void
0 ignored issues
show
Unused Code introduced by
The parameter $isValid is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
    public function testSerialize(?string $input, /** @scrutinizer ignore-unused */ bool $isValid): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
        $type = new UrlType();
19
        $actual = $type->serialize($input);
20
        self::assertSame($input, $actual);
21
    }
22
23
    /**
24
     * @dataProvider providerUrls
25
     */
26
    public function testParseValue(?string $input, bool $isValid): void
27
    {
28
        $type = new UrlType();
29
30
        if (!$isValid) {
31
            $this->expectExceptionMessage('Query error: Not a valid Url');
32
        }
33
34
        $actual = $type->parseValue($input);
35
36
        self::assertSame($input, $actual);
37
    }
38
39
    /**
40
     * @dataProvider providerUrls
41
     */
42
    public function testParseLiteral(?string $input, bool $isValid): void
43
    {
44
        $type = new UrlType();
45
        $ast = new StringValueNode(['value' => $input]);
46
47
        if (!$isValid) {
48
            $this->expectExceptionMessage('Query error: Not a valid Url');
49
        }
50
51
        $actual = $type->parseLiteral($ast);
52
53
        self::assertSame($input, $actual);
54
    }
55
56
    public function providerUrls(): array
57
    {
58
        return [
59
            ['http://www.example.com', true],
60
            ['https://www.example.com', true],
61
            ['http://example.com', true],
62
            ['http://www.example.com/path', true],
63
            ['http://www.example.com/path#frag', true],
64
            ['http://www.example.com/path?param=1', true],
65
            ['http://www.example.com/path?param=1#fra', true],
66
            ['http://t.co', true],
67
            ['http://www.t.co', true],
68
            ['http://a-b.c.t.co', true],
69
            ['http://aa.com', true],
70
            ['http://www.example', true], // this is indeed valid because `example` could be a TLD
71
            ['https://example.com:4200/subscribe', true],
72
            ['https://example-.com', true], // this is not conform to rfc1738, but we tolerate it for simplicity sake
73
74
            ['www.example.com', false],
75
            ['example.com', false],
76
            ['www.example', false],
77
            ['http://example', false],
78
            ['www.example#.com', false],
79
            ['www.t.co', false],
80
            ['file:///C:/folder/file.pdf', false],
81
82
            ['', false],
83
            [null, false],
84
            [' ', false],
85
        ];
86
    }
87
}
88