|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class FactoryHelpersTest |
| 4 | + * |
| 5 | + * @filesource FactoryHelpersTest.php |
| 6 | + * @created 31.01.2019 |
| 7 | + * @package chillerlan\HTTPTest\Psr17 |
| 8 | + * @author smiley <smiley@chillerlan.net> |
| 9 | + * @copyright 2019 smiley |
| 10 | + * @license MIT |
| 11 | + */ |
| 12 | + |
| 13 | +namespace chillerlan\HTTPTest\Psr17; |
| 14 | + |
| 15 | +use chillerlan\HTTP\Psr17; |
| 16 | +use chillerlan\HTTP\Psr7\{UploadedFile, Uri}; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\Http\Message\StreamInterface; |
| 19 | + |
| 20 | +class FactoryHelpersTest extends TestCase{ |
| 21 | + |
| 22 | + public function dataGetUriFromGlobals(){ |
| 23 | + |
| 24 | + $server = [ |
| 25 | + 'REQUEST_URI' => '/blog/article.php?id=10&user=foo', |
| 26 | + 'SERVER_PORT' => '443', |
| 27 | + 'SERVER_ADDR' => '217.112.82.20', |
| 28 | + 'SERVER_NAME' => 'www.example.org', |
| 29 | + 'SERVER_PROTOCOL' => 'HTTP/1.1', |
| 30 | + 'REQUEST_METHOD' => 'POST', |
| 31 | + 'QUERY_STRING' => 'id=10&user=foo', |
| 32 | + 'DOCUMENT_ROOT' => '/path/to/your/server/root/', |
| 33 | + 'HTTP_HOST' => 'www.example.org', |
| 34 | + 'HTTPS' => 'on', |
| 35 | + 'REMOTE_ADDR' => '193.60.168.69', |
| 36 | + 'REMOTE_PORT' => '5390', |
| 37 | + 'SCRIPT_NAME' => '/blog/article.php', |
| 38 | + 'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php', |
| 39 | + 'PHP_SELF' => '/blog/article.php', |
| 40 | + 'REQUEST_TIME' => time(), // phpunit fix |
| 41 | + ]; |
| 42 | + |
| 43 | + return [ |
| 44 | + 'HTTPS request' => [ |
| 45 | + 'https://www.example.org/blog/article.php?id=10&user=foo', |
| 46 | + $server, |
| 47 | + ], |
| 48 | + 'HTTPS request with different on value' => [ |
| 49 | + 'https://www.example.org/blog/article.php?id=10&user=foo', |
| 50 | + array_merge($server, ['HTTPS' => '1']), |
| 51 | + ], |
| 52 | + 'HTTP request' => [ |
| 53 | + 'http://www.example.org/blog/article.php?id=10&user=foo', |
| 54 | + array_merge($server, ['HTTPS' => 'off', 'SERVER_PORT' => '80']), |
| 55 | + ], |
| 56 | + 'HTTP_HOST missing -> fallback to SERVER_NAME' => [ |
| 57 | + 'https://www.example.org/blog/article.php?id=10&user=foo', |
| 58 | + array_merge($server, ['HTTP_HOST' => null]), |
| 59 | + ], |
| 60 | + 'HTTP_HOST and SERVER_NAME missing -> fallback to SERVER_ADDR' => [ |
| 61 | + 'https://217.112.82.20/blog/article.php?id=10&user=foo', |
| 62 | + array_merge($server, ['HTTP_HOST' => null, 'SERVER_NAME' => null]), |
| 63 | + ], |
| 64 | + 'No query String' => [ |
| 65 | + 'https://www.example.org/blog/article.php', |
| 66 | + array_merge($server, ['REQUEST_URI' => '/blog/article.php', 'QUERY_STRING' => '']), |
| 67 | + ], |
| 68 | + 'Host header with port' => [ |
| 69 | + 'https://www.example.org:8324/blog/article.php?id=10&user=foo', |
| 70 | + array_merge($server, ['HTTP_HOST' => 'www.example.org:8324']), |
| 71 | + ], |
| 72 | + 'Different port with SERVER_PORT' => [ |
| 73 | + 'https://www.example.org:8324/blog/article.php?id=10&user=foo', |
| 74 | + array_merge($server, ['SERVER_PORT' => '8324']), |
| 75 | + ], |
| 76 | + 'REQUEST_URI missing query string' => [ |
| 77 | + 'https://www.example.org/blog/article.php?id=10&user=foo', |
| 78 | + array_merge($server, ['REQUEST_URI' => '/blog/article.php']), |
| 79 | + ], |
| 80 | + 'Empty server variable' => [ |
| 81 | + 'http://localhost', |
| 82 | + ['REQUEST_TIME' => time(), 'SCRIPT_NAME' => '/blog/article.php'], // phpunit fix |
| 83 | + ], |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @dataProvider dataGetUriFromGlobals |
| 89 | + * |
| 90 | + * @param string $expected |
| 91 | + * @param array $serverParams |
| 92 | + */ |
| 93 | + public function testCreateUriFromGlobals(string $expected, array $serverParams){ |
| 94 | + $_SERVER = $serverParams; |
| 95 | + |
| 96 | + $this->assertEquals(new Uri($expected), Psr17\create_uri_from_globals()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testCreateServerRequestFromGlobals(){ |
| 100 | + |
| 101 | + $_SERVER = [ |
| 102 | + 'REQUEST_URI' => '/blog/article.php?id=10&user=foo', |
| 103 | + 'SERVER_PORT' => '443', |
| 104 | + 'SERVER_ADDR' => '217.112.82.20', |
| 105 | + 'SERVER_NAME' => 'www.example.org', |
| 106 | + 'SERVER_PROTOCOL' => 'HTTP/1.1', |
| 107 | + 'REQUEST_METHOD' => 'POST', |
| 108 | + 'QUERY_STRING' => 'id=10&user=foo', |
| 109 | + 'DOCUMENT_ROOT' => '/path/to/your/server/root/', |
| 110 | + 'HTTP_HOST' => 'www.example.org', |
| 111 | + 'HTTPS' => 'on', |
| 112 | + 'REMOTE_ADDR' => '193.60.168.69', |
| 113 | + 'REMOTE_PORT' => '5390', |
| 114 | + 'SCRIPT_NAME' => '/blog/article.php', |
| 115 | + 'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php', |
| 116 | + 'PHP_SELF' => '/blog/article.php', |
| 117 | + 'REQUEST_TIME' => time(), // phpunit fix |
| 118 | + ]; |
| 119 | + |
| 120 | + $_COOKIE = [ |
| 121 | + 'logged-in' => 'yes!' |
| 122 | + ]; |
| 123 | + |
| 124 | + $_POST = [ |
| 125 | + 'name' => 'Pesho', |
| 126 | + 'email' => 'pesho@example.com', |
| 127 | + ]; |
| 128 | + |
| 129 | + $_GET = [ |
| 130 | + 'id' => 10, |
| 131 | + 'user' => 'foo', |
| 132 | + ]; |
| 133 | + |
| 134 | + $_FILES = [ |
| 135 | + 'file' => [ |
| 136 | + 'name' => 'MyFile.txt', |
| 137 | + 'type' => 'text/plain', |
| 138 | + 'tmp_name' => '/tmp/php/php1h4j1o', |
| 139 | + 'error' => UPLOAD_ERR_OK, |
| 140 | + 'size' => 123, |
| 141 | + ] |
| 142 | + ]; |
| 143 | + |
| 144 | + $server = Psr17\create_server_request_from_globals(); |
| 145 | + |
| 146 | + $this->assertSame('POST', $server->getMethod()); |
| 147 | + $this->assertEquals(['Host' => ['www.example.org']], $server->getHeaders()); |
| 148 | + $this->assertSame('', (string) $server->getBody()); |
| 149 | + $this->assertSame('1.1', $server->getProtocolVersion()); |
| 150 | + $this->assertEquals($_COOKIE, $server->getCookieParams()); |
| 151 | + $this->assertEquals($_POST, $server->getParsedBody()); |
| 152 | + $this->assertEquals($_GET, $server->getQueryParams()); |
| 153 | + |
| 154 | + $this->assertEquals( |
| 155 | + new Uri('https://www.example.org/blog/article.php?id=10&user=foo'), |
| 156 | + $server->getUri() |
| 157 | + ); |
| 158 | + |
| 159 | + $expectedFiles = [ |
| 160 | + 'file' => new UploadedFile( |
| 161 | + '/tmp/php/php1h4j1o', |
| 162 | + 123, |
| 163 | + UPLOAD_ERR_OK, |
| 164 | + 'MyFile.txt', |
| 165 | + 'text/plain' |
| 166 | + ), |
| 167 | + ]; |
| 168 | + |
| 169 | + $this->assertEquals($expectedFiles, $server->getUploadedFiles()); |
| 170 | + } |
| 171 | + |
| 172 | + public function testCreateStream(){ |
| 173 | + $stream = Psr17\create_stream('test'); |
| 174 | + |
| 175 | + $this->assertInstanceOf(Streaminterface::class, $stream); |
| 176 | + $this->assertSame('test', $stream->getContents()); |
| 177 | + } |
| 178 | + |
| 179 | + public function streamInputProvider(){ |
| 180 | + |
| 181 | + $fh = fopen('php://temp', 'r+'); |
| 182 | + fwrite($fh, 'resourcetest'); |
| 183 | + fseek($fh, 0); |
| 184 | + |
| 185 | + $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><root><foo>bar</foo></root>'); |
| 186 | + |
| 187 | + return [ |
| 188 | + 'string' => ['stringtest', 'stringtest'], |
| 189 | + 'file' => [__DIR__.'/streaminput.txt', 'filetest'."\r\n"], |
| 190 | + 'resource' => [$fh, 'resourcetest'], |
| 191 | + 'streaminterface' => [Psr17\create_stream('streaminterfacetest'), 'streaminterfacetest'], |
| 192 | + 'tostring' => [$xml->foo, 'bar'], |
| 193 | + ]; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @dataProvider streamInputProvider |
| 198 | + * |
| 199 | + * @param $input |
| 200 | + * @param string $content |
| 201 | + */ |
| 202 | + public function testCreateStreamFromInput($input, string $content){ |
| 203 | + $this->assertSame($content, Psr17\create_stream_from_input($input)->getContents()); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @expectedException \InvalidArgumentException |
| 208 | + * @expectedExceptionMessage Invalid resource type: object |
| 209 | + */ |
| 210 | + public function testCreateStreamFromInputException(){ |
| 211 | + Psr17\create_stream_from_input(new \stdClass()); |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments