| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByJG\ApiTools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ByJG\ApiTools\Base\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ByJG\ApiTools\Exception\DefinitionNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ByJG\ApiTools\Exception\GenericSwaggerException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ByJG\ApiTools\Exception\HttpMethodNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ByJG\ApiTools\Exception\InvalidDefinitionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use ByJG\ApiTools\Exception\NotMatchedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ByJG\ApiTools\Exception\PathNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use ByJG\ApiTools\Exception\StatusCodeNotMatchedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ByJG\Util\Psr7\MessageException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | abstract class ApiTestCase extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var Schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var AbstractRequester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $requester = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * configure the schema to use for requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * When set, all requests without an own schema use this one instead. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param Schema|null $schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function setSchema($schema) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->schema = $schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function setRequester(AbstractRequester $requester) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->requester = $requester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return AbstractRequester | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     protected function getRequester() | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if (is_null($this->requester)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $this->requester = new ApiRequester(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return $this->requester; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $path The REST path call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param int $statusExpected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param array|null $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param array|null $requestBody | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param array $requestHeader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws DefinitionNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws GenericSwaggerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws HttpMethodNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws InvalidDefinitionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @throws NotMatchedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @throws PathNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws StatusCodeNotMatchedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @throws MessageException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @deprecated Use assertRequest instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     protected function makeRequest( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $statusExpected = 200, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $query = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $requestBody = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $requestHeader = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->checkSchema(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $body = $this->requester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ->withSchema($this->schema) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ->withMethod($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             ->withPath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->withQuery($query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->withRequestBody($requestBody) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ->withRequestHeader($requestHeader) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->assertResponseCode($statusExpected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // Note: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         // This code is only reached if the send is successful and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // all matches are satisfied. Otherwise an error is throwed before | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         // reach this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->assertTrue(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param AbstractRequester $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @throws DefinitionNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws GenericSwaggerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @throws HttpMethodNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @throws InvalidDefinitionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @throws NotMatchedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @throws PathNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws StatusCodeNotMatchedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws MessageException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function assertRequest(AbstractRequester $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         // Add own schema if nothing is passed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if (!$request->hasSchema()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $this->checkSchema(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $request->withSchema($this->schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         // Request based on the Swagger Request definitios | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $body = $request->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         // Note: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         // This code is only reached if the send is successful and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         // all matches are satisfied. Otherwise an error is throwed before | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         // reach this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->assertTrue(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @throws GenericSwaggerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     protected function checkSchema() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (!$this->schema) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                        
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.