HostAndPortValidatorTest::provideInvalidValues()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Validation;
6
7
use Laminas\Validator\Exception\RuntimeException;
8
use PHPUnit\Framework\TestCase;
9
use Shlinkio\Shlink\Common\Validation\HostAndPortValidator;
10
use stdClass;
11
12
use function array_values;
13
use function gettype;
14
use function sprintf;
15
16
class HostAndPortValidatorTest extends TestCase
17
{
18
    /**
19
     * @test
20
     * @dataProvider provideInvalidValues
21
     */
22
    public function failsToValidateWhenProvidedDataIsInvalid(string $value, string $expectedError): void
23
    {
24
        $validator = new HostAndPortValidator();
25
26
        $this->assertFalse($validator->isValid($value));
27
        $this->assertContains($expectedError, array_values($validator->getMessages()));
28
    }
29
30
    public function provideInvalidValues(): iterable
31
    {
32
        yield ['foo:bar:baz', 'Provided value, once split using the ":" separator, returned more than 2 parts'];
33
        yield ['foo:bar:baz:foo', 'Provided value, once split using the ":" separator, returned more than 2 parts'];
34
        yield ['$%&/', 'The host part of the value is not valid'];
35
        yield ['192.168.1.20', 'The host part of the value is not valid'];
36
        yield ['8.8.8.8', 'The host part of the value is not valid'];
37
        yield ['example.com:80000', 'The port part of the value is not valid. Must be a number between 1 and 65535'];
38
        yield ['example.com:65536', 'The port part of the value is not valid. Must be a number between 1 and 65535'];
39
        yield ['example.com:0', 'The port part of the value is not valid. Must be a number between 1 and 65535'];
40
        yield ['example.com:10.4', 'The port part of the value is not valid. Must be a number between 1 and 65535'];
41
        yield ['example.com:-1', 'The port part of the value is not valid. Must be a number between 1 and 65535'];
42
        yield ['example.com:-2000', 'The port part of the value is not valid. Must be a number between 1 and 65535'];
43
    }
44
45
    /**
46
     * @test
47
     * @dataProvider provideValidValues
48
     */
49
    public function succeedsWhenProvidingValidValues(string $value): void
50
    {
51
        $validator = new HostAndPortValidator();
52
        $this->assertTrue($validator->isValid($value));
53
    }
54
55
    public function provideValidValues(): iterable
56
    {
57
        yield ['localhost'];
58
        yield ['localhost:3000'];
59
        yield ['example.com'];
60
        yield ['example.com:8080'];
61
    }
62
63
    /**
64
     * @param mixed $value
65
     * @test
66
     * @dataProvider provideIncompatibleValues
67
     */
68
    public function throwsExceptionWhenProvidedValuesIsNotString($value, string $expectedExceptionMessage): void
69
    {
70
        $validator = new HostAndPortValidator();
71
72
        $this->expectException(RuntimeException::class);
73
        $this->expectExceptionMessage($expectedExceptionMessage);
74
75
        $validator->isValid($value);
76
    }
77
78
    public function provideIncompatibleValues(): iterable
79
    {
80
        yield [new stdClass(), sprintf('Expected value to be a string. %s provided', stdClass::class)];
81
        yield [1, sprintf('Expected value to be a string. %s provided', gettype(1))];
82
        yield [1.1, sprintf('Expected value to be a string. %s provided', gettype(1.1))];
83
        yield [false, sprintf('Expected value to be a string. %s provided', gettype(false))];
84
    }
85
}
86