| 1 |  |  | <?php namespace Limoncello\Testing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2015-2016 [email protected] (www.neomerx.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Limoncello\Core\Contracts\Application\ApplicationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Psr\Http\Message\StreamInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Zend\Diactoros\Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @package Limoncello\Testing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | abstract class PhpUnitTestCase extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** Header name */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     const HEADER_CONTENT_TYPE = 'CONTENT_TYPE'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     use MeasureExecutionTimeTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param array|null                      $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array|null                      $queryParams | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param array|object|null               $parsedBody | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param array|null                      $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param array|null                      $files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string|resource|StreamInterface $messageBody | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return Sapi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     abstract protected function createSapi( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         array $server = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         array $queryParams = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         array $parsedBody = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         array $cookies = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         array $files = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $messageBody = 'php://input' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param Sapi $sapi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return ApplicationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     abstract protected function createApplication(Sapi $sapi); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** @noinspection PhpTooManyParametersInspection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string                          $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string                          $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param array                           $queryParams | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param array                           $parsedBody | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param array                           $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param array                           $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param array                           $files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param array                           $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param string|resource|StreamInterface $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string                          $host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |     protected function call( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $uri, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         array $queryParams = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         array $parsedBody = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         array $headers = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         array $cookies = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         array $files = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         array $server = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $content = 'php://input', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $host = 'localhost' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 5 |  |         $headers['host'] = $host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 5 |  |         $server = $this->applyHeadersToServer($headers, $server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 5 |  |         $server['REQUEST_URI']    = $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 5 |  |         $server['REQUEST_METHOD'] = $method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 5 |  |         $sapi = $this->createSapi($server, $queryParams, $parsedBody, $cookies, $files, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 5 |  |         $app  = $this->createApplication($sapi); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 5 |  |         $app->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 5 |  |         unset($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 5 |  |         $response = $sapi->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 5 |  |         unset($sapi); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 5 |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param array $queryParams | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param array $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param array $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |     protected function get($uri, array $queryParams = [], array $headers = [], array $cookies = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         return $this->call('GET', $uri, $queryParams, [], $headers, $cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param array  $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param array  $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |     protected function post($uri, array $data = [], array $headers = [], array $cookies = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |         $headers[self::HEADER_CONTENT_TYPE] = 'application/x-www-form-urlencoded'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         return $this->call('POST', $uri, [], $data, $headers, $cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param array  $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param array  $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |     protected function put($uri, array $data = [], array $headers = [], array $cookies = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 1 |  |         $headers[self::HEADER_CONTENT_TYPE] = 'application/x-www-form-urlencoded'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |         return $this->call('PUT', $uri, [], $data, $headers, $cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param array  $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param array  $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |     protected function patch($uri, array $data = [], array $headers = [], array $cookies = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |         $headers[self::HEADER_CONTENT_TYPE] = 'application/x-www-form-urlencoded'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |         return $this->call('PATCH', $uri, [], $data, $headers, $cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param array  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param array  $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param array  $cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |     protected function delete($uri, array $data = [], array $headers = [], array $cookies = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 1 |  |         return $this->call('DELETE', $uri, [], $data, $headers, $cookies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param string $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @return StreamInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 178 | 1 |  |     protected function streamFromString($content) | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 180 | 1 |  |         $stream = new Stream('php://temp', 'wb+'); | 
            
                                                                        
                            
            
                                    
            
            
                | 181 | 1 |  |         $stream->write($content); | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 183 | 1 |  |         return $stream; | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param array $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * @param array $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 5 |  |     private function applyHeadersToServer(array $headers, array $server = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 5 |  |         $prefix = 'HTTP_'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 5 |  |         foreach ($headers as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 5 |  |             $name = strtr(strtoupper($name), '-', '_'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 5 |  |             if ($name !== self::HEADER_CONTENT_TYPE && strpos($name, $prefix) !== 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 5 |  |                 $name = $prefix . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 5 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 5 |  |             $server[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 5 |  |         return $server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 205 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  |