|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Upward\Test\Resolver; |
| 10 | + |
| 11 | +use Magento\Upward\Definition; |
| 12 | +use Magento\Upward\DefinitionIterator; |
| 13 | +use Magento\Upward\Resolver\Url; |
| 14 | +use Mockery; |
| 15 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use function BeBat\Verify\verify; |
| 18 | + |
| 19 | +class UrlTest extends TestCase |
| 20 | +{ |
| 21 | + use MockeryPHPUnitIntegration; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var DefinitionIterator|Mockery\MockInterface |
| 25 | + */ |
| 26 | + private $definitionIteratorMock; |
| 27 | + /** |
| 28 | + * @var Url |
| 29 | + */ |
| 30 | + private $resolver; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $this->resolver = new Url(); |
| 35 | + $this->definitionIteratorMock = Mockery::mock(DefinitionIterator::class); |
| 36 | + $this->definitionIteratorMock->shouldReceive('get') |
| 37 | + ->with(Mockery::type('string'), Mockery::type(Definition::class)) |
| 38 | + ->andReturnUsing(function (string $key, Definition $definition) { |
| 39 | + $returnValue = $definition->get($key); |
| 40 | + |
| 41 | + return $returnValue instanceof Definition ? $returnValue->toArray() : $returnValue; |
| 42 | + }); |
| 43 | + $this->resolver->setIterator($this->definitionIteratorMock); |
| 44 | + } |
| 45 | + |
| 46 | + public function isValidDataProvider() |
| 47 | + { |
| 48 | + return [ |
| 49 | + 'Valid' => [ |
| 50 | + 'definition' => new Definition([ |
| 51 | + 'baseUrl' => 'https://upward.test', |
| 52 | + 'query' => ['foo' => 'bar'], |
| 53 | + 'username' => 'user', |
| 54 | + 'password' => 'pass', |
| 55 | + ]), |
| 56 | + 'expected' => true, |
| 57 | + ], |
| 58 | + 'Invalid - Wrong Query Type' => [ |
| 59 | + 'definition' => new Definition([ |
| 60 | + 'baseUrl' => false, |
| 61 | + 'query' => 'foo=bar', |
| 62 | + ]), |
| 63 | + 'expected' => false, |
| 64 | + ], |
| 65 | + 'Invalid - Password w/o User' => [ |
| 66 | + 'definition' => new Definition([ |
| 67 | + 'baseUrl' => false, |
| 68 | + 'password' => 'pass', |
| 69 | + ]), |
| 70 | + 'expected' => false, |
| 71 | + ], |
| 72 | + 'Invalid - Relative Parts w/o Host' => [ |
| 73 | + 'definition' => new Definition([ |
| 74 | + 'baseUrl' => false, |
| 75 | + 'username' => 'user', |
| 76 | + ]), |
| 77 | + 'expected' => false, |
| 78 | + ], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + public function testIndicator(): void |
| 83 | + { |
| 84 | + verify($this->resolver->getIndicator())->is()->sameAs('baseUrl'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @dataProvider isValidDataProvider |
| 89 | + */ |
| 90 | + public function testIsValid(Definition $definition, bool $expected): void |
| 91 | + { |
| 92 | + verify($this->resolver->isValid($definition))->is()->sameAs($expected); |
| 93 | + } |
| 94 | + |
| 95 | + public function testResolveAbsoluteAllParams(): void |
| 96 | + { |
| 97 | + $definition = new Definition([ |
| 98 | + 'baseUrl' => 'https://upward.test', |
| 99 | + 'hostname' => 'new.host', |
| 100 | + 'protocol' => 'http', |
| 101 | + 'pathname' => '/path', |
| 102 | + 'search' => 'foo=bar&dog=cat', |
| 103 | + 'query' => ['foo' => 'baz'], |
| 104 | + 'port' => 8080, |
| 105 | + 'username' => 'user', |
| 106 | + 'password' => 'pass', |
| 107 | + 'hash' => '#hash', |
| 108 | + ]); |
| 109 | + |
| 110 | + verify($this->resolver->resolve($definition))->is()->sameAs( |
| 111 | + 'http://user:pass@new.host:8080/path?foo=baz&dog=cat#hash' |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + public function testResolveAbsoluteWithPathAppend(): void |
| 116 | + { |
| 117 | + $definition = new Definition([ |
| 118 | + 'baseUrl' => 'https://upward.test/path/', |
| 119 | + 'pathname' => 'append', |
| 120 | + ]); |
| 121 | + |
| 122 | + verify($this->resolver->resolve($definition))->is()->sameAs( |
| 123 | + 'https://upward.test/path/append' |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + public function testResolveAbsoluteWithPathSegmentReplace(): void |
| 128 | + { |
| 129 | + $definition = new Definition([ |
| 130 | + 'baseUrl' => 'https://upward.test/path/segment', |
| 131 | + 'pathname' => 'replace', |
| 132 | + ]); |
| 133 | + |
| 134 | + verify($this->resolver->resolve($definition))->is()->sameAs( |
| 135 | + 'https://upward.test/path/replace' |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + public function testResolveRelativeNoParams(): void |
| 140 | + { |
| 141 | + $definition = new Definition([ |
| 142 | + 'baseUrl' => false, |
| 143 | + 'pathname' => 'no-slash-path', |
| 144 | + ]); |
| 145 | + |
| 146 | + verify($this->resolver->resolve($definition))->is()->sameAs( |
| 147 | + '/no-slash-path' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + public function testResolveThrowsException(): void |
| 152 | + { |
| 153 | + $this->expectException(\InvalidArgumentException::class); |
| 154 | + $this->expectExceptionMessage('$definition must be an instance of Magento\\Upward\\Definition'); |
| 155 | + |
| 156 | + $this->resolver->resolve('Not a Definition'); |
| 157 | + } |
| 158 | +} |
0 commit comments