| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of coisa/http. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) Felipe Sayão Lobato Abreu <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This source file is subject to the license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace CoiSA\Http\Test\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use CoiSA\Http\Handler\PsrHttpClientHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Prophecy\Prophecy\ObjectProphecy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Psr\Http\Client\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Psr\Http\Message\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Psr\Http\Server\RequestHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Class PsrHttpClientHandlerTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @package CoiSA\Http\Test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | final class PsrHttpClientHandlerTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var ClientInterface|ObjectProphecy */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var ObjectProphecy|RequestInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var ObjectProphecy|ServerRequestInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $serverRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** @var ObjectProphecy|ResponseInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** @var PsrHttpClientHandler */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $handler; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     public function setUp(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->client        = $this->prophesize(ClientInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->request       = $this->prophesize(RequestInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->serverRequest = $this->prophesize(ServerRequestInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->response      = $this->prophesize(ResponseInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->handler       = new PsrHttpClientHandler($this->client->reveal()); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $this->client->sendRequest($this->request->reveal())->will([$this->response, 'reveal']); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $this->client->sendRequest($this->serverRequest->reveal())->will([$this->response, 'reveal']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function testHandlerImplementInterfaces(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertInstanceOf(ClientInterface::class, $this->handler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertInstanceOf(RequestHandlerInterface::class, $this->handler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function testHandleReturnResponseFromClient(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $response = $this->handler->handle($this->serverRequest->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $client   = $this->client->reveal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertEquals($client->sendRequest($this->serverRequest->reveal()), $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function testSendRequestReturnResponseFromClient(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $response = $this->handler->sendRequest($this->request->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $client   = $this->client->reveal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertEquals($client->sendRequest($this->request->reveal()), $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $response = $this->handler->sendRequest($this->serverRequest->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertEquals($client->sendRequest($this->serverRequest->reveal()), $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |