| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ShlinkioTest\Shlink\Common\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\HandlerStack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Prophecy\Prophecy\ObjectProphecy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ReflectionObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Shlinkio\Shlink\Common\Http\Exception\InvalidHttpMiddlewareException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Shlinkio\Shlink\Common\Http\HttpClientFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function fopen; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class HttpClientFactoryTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private const BASE_HANDLERS_COUNT = 4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private HttpClientFactory $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private ObjectProphecy $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function setUp(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->factory = new HttpClientFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->container = $this->prophesize(ContainerInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @dataProvider provideConfig | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     public function properlyCreatesAFactoryWithExpectedNumberOfMiddlewares( | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         array $config, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         int $expectedMiddlewaresAmount | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     ): void { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->container->get('some_middleware')->willReturn(static function (): void { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $getConfig = $this->container->get('config')->willReturn(['http_client' => $config]); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $client = ($this->factory)($this->container->reveal()); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         /** @var HandlerStack $handler */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $handler = $client->getConfig('handler'); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $ref = new ReflectionObject($handler); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $stack = $ref->getProperty('stack'); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $stack->setAccessible(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->assertCount($expectedMiddlewaresAmount + self::BASE_HANDLERS_COUNT, $stack->getValue($handler)); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $getConfig->shouldHaveBeenCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function provideConfig(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $staticMiddleware = static function (): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         yield [[], 0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         yield [['request_middlewares' => []], 0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         yield [['response_middlewares' => []], 0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         yield [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'request_middlewares' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'response_middlewares' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ], 0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         yield [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'request_middlewares' => ['some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'response_middlewares' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ], 1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         yield [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'request_middlewares' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'response_middlewares' => ['some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ], 1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         yield [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'request_middlewares' => ['some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'response_middlewares' => ['some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ], 2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         yield [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'request_middlewares' => [$staticMiddleware], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'response_middlewares' => ['some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ], 2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         yield [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'request_middlewares' => ['some_middleware', $staticMiddleware], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'response_middlewares' => [$staticMiddleware, 'some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ], 4]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param mixed $middleware | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @dataProvider provideInvalidMiddlewares | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function exceptionIsThrownWhenNonCallableStaticMiddlewaresAreProvided($middleware): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $getService = $this->container->get('some_middleware')->willReturn(static function (): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $getConfig = $this->container->get('config')->willReturn(['http_client' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'request_middlewares' => [$middleware], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->expectException(InvalidHttpMiddlewareException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $getService->shouldNotBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $getConfig->shouldBeCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         ($this->factory)($this->container->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param mixed $middleware | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @dataProvider provideInvalidMiddlewares | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function exceptionIsThrownWhenNonCallableServiceMiddlewaresAreProvided($middleware): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $getService = $this->container->get('some_middleware')->willReturn($middleware); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $getConfig = $this->container->get('config')->willReturn(['http_client' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             'response_middlewares' => ['some_middleware'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         ]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->expectException(InvalidHttpMiddlewareException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $getService->shouldBeCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $getConfig->shouldBeCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ($this->factory)($this->container->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function provideInvalidMiddlewares(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         yield [1234]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         yield [new stdClass()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         yield [[]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         yield [fopen('php://memory', 'rb+')]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |  |