| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ShlinkioTest\Shlink\Common\Validation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Shlinkio\Shlink\Common\Validation\HostAndPortValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Zend\Validator\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @dataProvider provideIncompatibleValues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function throwsExceptionWhenProvidedValuesIsNotString($value, string $expectedExceptionMessage): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $validator = new HostAndPortValidator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->expectException(RuntimeException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->expectExceptionMessage($expectedExceptionMessage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $validator->isValid($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function provideIncompatibleValues(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         yield [new stdClass(), sprintf('Expected value to be a string. %s provided', stdClass::class)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         yield [1, sprintf('Expected value to be a string. %s provided', gettype(1))]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         yield [1.1, sprintf('Expected value to be a string. %s provided', gettype(1.1))]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         yield [false, sprintf('Expected value to be a string. %s provided', gettype(false))]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |