Created
September 6, 2018 07:27
-
-
Save prisis/277b3657722af4fa3e2510c949ca8b89 to your computer and use it in GitHub Desktop.
php-cs-fixer output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) src/Viserio/Component/HttpFactory/StreamFactory.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
{ | |
- $stream = \fopen('php://memory', 'rb+'); | |
+ $stream = \fopen('php://memory', 'b+r'); | |
@@ @@ | |
if ($type === 'NULL') { | |
- return new Stream(\fopen('php://temp', 'rb+')); | |
+ return new Stream(\fopen('php://temp', 'b+r')); | |
@@ @@ | |
{ | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
if ($resource !== '') { | |
\fwrite($stream, $resource); | |
\fseek($stream, 0); | |
} | |
return new Stream($stream); | |
} | |
} | |
----------- end diff ----------- | |
2) src/Viserio/Component/Filesystem/FilesystemAdapter.php (fopen_flags) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
// https://bugs.php.net/bug.php?id=64634 | |
- if (@\fopen($orginal, 'r') === false) { | |
+ if (@\fopen($orginal, 'rb') === false) { | |
@@ @@ | |
// Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default | |
- if (@\fopen($target, 'w', false, \stream_context_create(['ftp' => ['overwrite' => true]])) === false) { | |
+ if (@\fopen($target, 'wb', false, \stream_context_create(['ftp' => ['overwrite' => true]])) === false) { | |
throw new ViserioIOException(\sprintf( | |
'Failed to copy [%s] to [%s] because target file could not be opened for writing.', | |
$orginal, | |
$target | |
), 0, null, $orginal); | |
} | |
$this->driver->copy($originFile, $targetFile); | |
if (! \is_file($target)) { | |
throw new ViserioIOException(\sprintf( | |
'Failed to copy [%s] to [%s].', | |
$originFile, | |
$target | |
), 0, null, $originFile); | |
} | |
return true; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function move(string $from, string $to): bool | |
{ | |
return $this->driver->rename($from, $to); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getSize(string $path) | |
{ | |
$size = $this->driver->getSize($path); | |
if ($size !== false) { | |
return $size['size']; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getMimetype(string $path) | |
{ | |
if (! $this->has($path)) { | |
throw new FileNotFoundException($path); | |
} | |
$mimetype = $this->driver->getMimetype($path); | |
if ($mimetype !== false) { | |
return $mimetype['mimetype']; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getTimestamp(string $path) | |
{ | |
$this->fileNotFound($path); | |
$getTimestamp = $this->driver->getTimestamp($path); | |
if ($getTimestamp !== false) { | |
return $getTimestamp['timestamp']; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
* | |
* @throws \RuntimeException | |
*/ | |
public function url(string $path): string | |
{ | |
$adapter = $this->driver; | |
if ($adapter instanceof AwsS3Adapter) { | |
$path = $adapter->getPathPrefix() . $path; | |
return $adapter->getClient()->getObjectUrl($adapter->getBucket(), $path); | |
} | |
if ($adapter instanceof LocalAdapter) { | |
if (isset($this->config['url'])) { | |
return self::normalizeDirectorySeparator( | |
$this->config['url'] . '/' . $path | |
); | |
} | |
return $adapter->getPathPrefix() . $path; | |
} | |
if ($adapter instanceof RackspaceAdapter) { | |
return $this->getRackspaceUrl($adapter, $path); | |
} | |
if (\method_exists($adapter, 'getUrl')) { | |
return $adapter->getUrl($path); | |
} | |
throw new RuntimeException('This driver does not support retrieving URLs.'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function delete($paths): bool | |
{ | |
$paths = (array) $paths; | |
$success = true; | |
foreach ($paths as $path) { | |
try { | |
if (! $this->driver->delete($path)) { | |
$success = false; | |
} | |
} catch (FileNotFoundException $e) { | |
$success = false; | |
} | |
} | |
return $success; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function files(string $directory): array | |
{ | |
return $this->getContents($directory, 'file'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function allFiles(string $directory, bool $showHiddenFiles = false): array | |
{ | |
return $this->getContents($directory, 'file', true); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function directories(string $directory): array | |
{ | |
return $this->getContents($directory, 'dir'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function allDirectories(string $directory): array | |
{ | |
return $this->getContents($directory, 'dir', true); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function createDirectory(string $path, array $config = []): bool | |
{ | |
return $this->driver->createDir($path, new FlyConfig($config)) !== false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function deleteDirectory(string $directory): bool | |
{ | |
return $this->driver->deleteDir($directory); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function cleanDirectory(string $dirname): bool | |
{ | |
if (! $this->isDirectory($dirname)) { | |
return false; | |
} | |
$directories = $this->allDirectories($dirname); | |
foreach ($directories as $directoryName) { | |
@\rmdir($this->getNormalizedOrPrefixedPath($directoryName)); | |
} | |
return true; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function isDirectory(string $dirname): bool | |
{ | |
return \is_dir($this->getNormalizedOrPrefixedPath($dirname)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function copyDirectory(string $directory, string $destination, array $options = []): bool | |
{ | |
if (! $this->isDirectory($directory)) { | |
return false; | |
} | |
if (! \is_dir($destination)) { | |
$this->createDirectory($destination, ['visibility' => 'public']); | |
} | |
$recursive = $options['recursive'] ?? true; | |
$contents = $this->driver->listContents($directory, $recursive); | |
foreach ($contents as $item) { | |
if ($item['type'] === 'dir') { | |
$this->createDirectory( | |
$destination . \str_replace($directory, '', $item['path']), | |
['visibility' => $this->getVisibility($item['path'])] | |
); | |
} | |
if ($item['type'] === 'file') { | |
$this->copy( | |
$item['path'], | |
$destination . \str_replace($directory, '', $item['path']) | |
); | |
} | |
} | |
return true; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function moveDirectory(string $directory, string $destination, array $options = []): bool | |
{ | |
$overwrite = $options['overwrite'] ?? false; | |
if ($overwrite && $this->isDirectory($destination) && ! $this->deleteDirectory($destination)) { | |
return false; | |
} | |
$copy = $this->copyDirectory( | |
$directory, | |
$destination, | |
['visibility' => $this->getVisibility($directory)] | |
); | |
$delete = $this->deleteDirectory($directory); | |
return ! (! $copy && ! $delete); | |
} | |
/** | |
* Get normalize or prefixed path. | |
* | |
* @param string $path | |
* | |
* @return string | |
*/ | |
protected function getNormalizedOrPrefixedPath(string $path): string | |
{ | |
$prefix = ''; | |
if (\method_exists($this->driver, 'getPathPrefix')) { | |
$prefix = $this->driver->getPathPrefix(); | |
} | |
$path = $prefix . $path; | |
return self::normalizeDirectorySeparator($path); | |
} | |
/** | |
* Get the URL for the file at the given path. | |
* | |
* @param \League\Flysystem\Rackspace\RackspaceAdapter $adapter | |
* @param string $path | |
* | |
* @return string | |
*/ | |
protected function getRackspaceUrl(RackspaceAdapter $adapter, string $path): string | |
{ | |
return (string) $adapter->getContainer()->getObject($path)->getPublicUrl(); | |
} | |
/** | |
* Parse the given visibility value. | |
* | |
* @param null|string $visibility | |
* | |
* @throws \Viserio\Component\Contract\Filesystem\Exception\InvalidArgumentException | |
* | |
* @return null|string | |
*/ | |
private function parseVisibility(string $visibility = null): ?string | |
{ | |
if ($visibility === null) { | |
return null; | |
} | |
if ($visibility === FilesystemContract::VISIBILITY_PUBLIC) { | |
return AdapterInterface::VISIBILITY_PUBLIC; | |
} | |
if ($visibility === FilesystemContract::VISIBILITY_PRIVATE) { | |
return AdapterInterface::VISIBILITY_PRIVATE; | |
} | |
throw new InvalidArgumentException('Unknown visibility: ' . $visibility); | |
} | |
/** | |
* Get content from a dir. | |
* | |
* @param string $directory | |
* @param string $typ | |
* @param bool $recursive | |
* | |
* @return array | |
*/ | |
private function getContents(string $directory, string $typ, bool $recursive = false): array | |
{ | |
$contents = $this->driver->listContents($directory, $recursive); | |
return $this->filterContentsByType($contents, $typ); | |
} | |
/** | |
* Filter directory contents by type. | |
* | |
* @param array $contents | |
* @param string $type | |
* | |
* @return array | |
*/ | |
private function filterContentsByType(array $contents, string $type): array | |
{ | |
$results = []; | |
foreach ($contents as $key => $value) { | |
if (isset($value['type']) && $value['type'] === $type) { | |
$results[$key] = $value['path']; | |
} | |
} | |
return $results; | |
} | |
/** | |
* Throws a exception if file is not found. | |
* | |
* @param string $path | |
* | |
* @throws \Viserio\Component\Contract\Filesystem\Exception\FileNotFoundException | |
* | |
* @return void | |
*/ | |
private function fileNotFound(string $path): void | |
{ | |
if (! $this->has($path)) { | |
throw new FileNotFoundException($path); | |
} | |
} | |
} | |
----------- end diff ----------- | |
3) src/Viserio/Component/Http/Tests/ResponseTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
$streamIsRead = false; | |
- $body = FnStream::decorate(new Stream(\fopen('php://temp', 'rb+')), [ | |
+ $body = FnStream::decorate(new Stream(\fopen('php://temp', 'b+r')), [ | |
@@ @@ | |
$body = '0'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$response = (new Response())->withBody(new Stream($stream)); | |
static::assertInstanceOf(StreamInterface::class, $response->getBody()); | |
static::assertSame('0', (string) $response->getBody()); | |
} | |
public function testSameInstanceWhenSameBody(): void | |
{ | |
$response = new Response(); | |
$body = $response->getBody(); | |
static::assertSame($response, $response->withBody($body)); | |
} | |
public function testWithHeader(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar']); | |
$response2 = $response->withHeader('baZ', 'Bam'); | |
static::assertSame(['Foo' => ['Bar']], $response->getHeaders()); | |
static::assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam']], $response2->getHeaders()); | |
static::assertSame('Bam', $response2->getHeaderLine('baz')); | |
static::assertSame(['Bam'], $response2->getHeader('baz')); | |
} | |
public function testWithHeaderAsArray(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar']); | |
$response2 = $response->withHeader('baZ', ['Bam', 'Bar']); | |
static::assertSame(['Foo' => ['Bar']], $response->getHeaders()); | |
static::assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam', 'Bar']], $response2->getHeaders()); | |
static::assertSame('Bam,Bar', $response2->getHeaderLine('baz')); | |
static::assertSame(['Bam', 'Bar'], $response2->getHeader('baz')); | |
} | |
public function testWithHeaderReplacesDifferentCase(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar']); | |
$response2 = $response->withHeader('foO', 'Bam'); | |
static::assertSame(['Foo' => ['Bar']], $response->getHeaders()); | |
static::assertSame(['foO' => ['Bam']], $response2->getHeaders()); | |
static::assertSame('Bam', $response2->getHeaderLine('foo')); | |
static::assertSame(['Bam'], $response2->getHeader('foo')); | |
} | |
public function testWithAddedHeader(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar']); | |
$response2 = $response->withAddedHeader('foO', 'Baz'); | |
static::assertSame(['Foo' => ['Bar']], $response->getHeaders()); | |
static::assertSame(['Foo' => ['Bar', 'Baz']], $response2->getHeaders()); | |
static::assertSame('Bar,Baz', $response2->getHeaderLine('foo')); | |
static::assertSame(['Bar', 'Baz'], $response2->getHeader('foo')); | |
} | |
public function testWithAddedHeaderAsArray(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar']); | |
$response2 = $response->withAddedHeader('foO', ['Baz', 'Bam']); | |
static::assertSame(['Foo' => ['Bar']], $response->getHeaders()); | |
static::assertSame(['Foo' => ['Bar', 'Baz', 'Bam']], $response2->getHeaders()); | |
static::assertSame('Bar,Baz,Bam', $response2->getHeaderLine('foo')); | |
static::assertSame(['Bar', 'Baz', 'Bam'], $response2->getHeader('foo')); | |
} | |
public function testWithAddedHeaderThatDoesNotExist(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar']); | |
$response2 = $response->withAddedHeader('nEw', 'Baz'); | |
static::assertSame(['Foo' => ['Bar']], $response->getHeaders()); | |
static::assertSame(['Foo' => ['Bar'], 'nEw' => ['Baz']], $response2->getHeaders()); | |
static::assertSame('Baz', $response2->getHeaderLine('new')); | |
static::assertSame(['Baz'], $response2->getHeader('new')); | |
} | |
public function testWithoutHeaderThatExists(): void | |
{ | |
$response = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']); | |
$response2 = $response->withoutHeader('foO'); | |
static::assertTrue($response->hasHeader('foo')); | |
static::assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $response->getHeaders()); | |
static::assertFalse($response2->hasHeader('foo')); | |
static::assertSame(['Baz' => ['Bam']], $response2->getHeaders()); | |
} | |
public function testWithoutHeaderThatDoesNotExist(): void | |
{ | |
$response = new Response(200, ['Baz' => 'Bam']); | |
$response2 = $response->withoutHeader('foO'); | |
static::assertSame($response, $response2); | |
static::assertFalse($response2->hasHeader('foo')); | |
static::assertSame(['Baz' => ['Bam']], $response2->getHeaders()); | |
} | |
public function testSameInstanceWhenRemovingMissingHeader(): void | |
{ | |
$response = new Response(); | |
static::assertSame($response, $response->withoutHeader('foo')); | |
} | |
public function testHeaderValuesAreTrimmed(): void | |
{ | |
$response1 = new Response(200, ['OWS' => " \t \tFoo\t \t "]); | |
$response2 = (new Response())->withHeader('OWS', " \t \tFoo\t \t "); | |
$response3 = (new Response())->withAddedHeader('OWS', " \t \tFoo\t \t "); | |
foreach ([$response1, $response2, $response3] as $response) { | |
static::assertSame(['OWS' => ['Foo']], $response->getHeaders()); | |
static::assertSame('Foo', $response->getHeaderLine('OWS')); | |
static::assertSame(['Foo'], $response->getHeader('OWS')); | |
} | |
} | |
} | |
----------- end diff ----------- | |
4) src/Viserio/Component/Http/Tests/UploadedFileTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
{ | |
- $stream = \fopen('php://temp', 'wb+'); | |
+ $stream = \fopen('php://temp', 'b+w'); | |
@@ @@ | |
$body = 'Foo bar!'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'Foo bar!'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'Foo bar!'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'Foo bar!'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$stream = new Stream($stream); | |
$upload = new UploadedFile($stream, 0, \UPLOAD_ERR_OK); | |
$this->cleanup[] = $to = \tempnam(\sys_get_temp_dir(), 'diac'); | |
$upload->moveTo($to); | |
static::assertFileExists($to); | |
$upload->getStream(); | |
} | |
public function nonOkErrorStatus() | |
{ | |
return [ | |
'UPLOAD_ERR_INI_SIZE' => [\UPLOAD_ERR_INI_SIZE], | |
'UPLOAD_ERR_FORM_SIZE' => [\UPLOAD_ERR_FORM_SIZE], | |
'UPLOAD_ERR_PARTIAL' => [\UPLOAD_ERR_PARTIAL], | |
'UPLOAD_ERR_NO_FILE' => [\UPLOAD_ERR_NO_FILE], | |
'UPLOAD_ERR_NO_TMP_DIR' => [\UPLOAD_ERR_NO_TMP_DIR], | |
'UPLOAD_ERR_CANT_WRITE' => [\UPLOAD_ERR_CANT_WRITE], | |
'UPLOAD_ERR_EXTENSION' => [\UPLOAD_ERR_EXTENSION], | |
]; | |
} | |
/** | |
* @dataProvider nonOkErrorStatus | |
* | |
* @param mixed $status | |
*/ | |
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status): void | |
{ | |
$uploadedFile = new UploadedFile('not ok', 0, $status); | |
static::assertSame($status, $uploadedFile->getError()); | |
} | |
/** | |
* @dataProvider nonOkErrorStatus | |
* | |
* @param mixed $status | |
*/ | |
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status): void | |
{ | |
$this->expectException(\RuntimeException::class); | |
$this->expectExceptionMessage('upload error'); | |
$uploadedFile = new UploadedFile('not ok', 0, $status); | |
$uploadedFile->moveTo(__DIR__ . '/' . \uniqid('', true)); | |
} | |
/** | |
* @dataProvider nonOkErrorStatus | |
* | |
* @param mixed $status | |
*/ | |
public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status): void | |
{ | |
$this->expectException(\RuntimeException::class); | |
$this->expectExceptionMessage('upload error'); | |
$uploadedFile = new UploadedFile('not ok', 0, $status); | |
$uploadedFile->getStream(); | |
} | |
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided(): void | |
{ | |
$this->cleanup[] = $from = \tempnam(\sys_get_temp_dir(), 'copy_from'); | |
$this->cleanup[] = $to = \tempnam(\sys_get_temp_dir(), 'copy_to'); | |
\copy(__FILE__, $from); | |
$uploadedFile = new UploadedFile($from, 100, \UPLOAD_ERR_OK, \basename($from), 'text/plain'); | |
$uploadedFile->moveTo($to); | |
static::assertFileEquals(__FILE__, $to); | |
} | |
} | |
----------- end diff ----------- | |
5) src/Viserio/Component/Http/Tests/RequestTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
- $body = FnStream::decorate(new Stream(\fopen('php://temp', 'rb+')), [ | |
+ $body = FnStream::decorate(new Stream(\fopen('php://temp', 'b+r')), [ | |
'__toString' => function () use (&$streamIsRead) { | |
$streamIsRead = true; | |
return ''; | |
}, | |
]); | |
$request = new Request('/', 'GET', [], $body); | |
static::assertFalse($streamIsRead); | |
static::assertSame($body, $request->getBody()); | |
} | |
public function testEmptyRequestHostEmptyUriHostPreserveHostFalse(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
$requestAfterUri = $this->getEmptyHostHeader()->withUri($uri); | |
static::assertEquals('', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testEmptyRequestHostEmptyUriHostPreserveHostTrue(): void | |
{ | |
$requestAfterUri = $this->getEmptyHostHeader()->withUri($this->mock(UriInterface::class), true); | |
static::assertEquals('', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testEmptyRequestHostDefaultUriHostPreserveHostFalse(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
$requestAfterUri = (new Request($uri))->withUri($this->getDefaultUriHost()); | |
static::assertEquals('baz.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testEmptyRequestHostDefaultUriHostPreserveHostTrue(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
$requestAfterUri = (new Request($uri))->withUri($this->getDefaultUriHost()); | |
static::assertEquals('baz.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testDefaultRequestHostEmptyUriHostPreserveHostFalse(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
/** @var Request $request */ | |
$request = (new Request($uri))->withHeader('Host', 'foo.com'); | |
$requestAfterUri = $request->withUri($uri, false); | |
static::assertEquals('foo.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testDefaultRequestHostEmptyUriHostPreserveHostTrue(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
/** @var Request $request */ | |
$request = (new Request($uri))->withHeader('Host', 'foo.com'); | |
$requestAfterUri = $request->withUri($uri, true); | |
static::assertEquals('foo.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testDefaultRequestHostDefaultUriHostPreserveHostFalse(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
$request = (new Request($uri))->withHeader('Host', 'foo.com'); | |
$requestAfterUri = $request->withUri($this->getDefaultUriHost(), false); | |
static::assertEquals('baz.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testDefaultRequestHostDefaultUriHostPreserveHostTrue(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
$request = (new Request($uri))->withHeader('Host', 'foo.com'); | |
$requestAfterUri = $request->withUri($this->getDefaultUriHost(), true); | |
static::assertEquals('foo.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testURIPortIsIgnoredIfHostIsEmpty(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once(); | |
$uri->shouldReceive('getPort') | |
->once(); | |
$request = (new Request($uri))->withHeader('Host', 'foo.com'); | |
$requestAfterUri = $request->withUri($this->getDefaultUriHost(), false); | |
static::assertEquals('baz.com', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testURIPortIsUsedForBuildHostHeader(): void | |
{ | |
$uri = $this->mock(UriInterface::class); | |
$uri->shouldReceive('getHost') | |
->once() | |
->andReturn(''); | |
$request = (new Request($uri))->withHeader('Host', 'foo.com'); | |
$requestAfterUri = $request->withUri($this->getDefaultUriHostAndPort(), false); | |
static::assertEquals('baz.com:8080', $requestAfterUri->getHeaderLine('Host')); | |
} | |
public function testHostHeaderSetFromUriOnCreationIfNoHostHeaderSpecified(): void | |
{ | |
$request = new Request('http://www.example.com'); | |
static::assertTrue($request->hasHeader('Host')); | |
static::assertEquals('www.example.com', $request->getHeaderLine('host')); | |
} | |
public function testHostHeaderNotSetFromUriOnCreationIfHostHeaderSpecified(): void | |
{ | |
$request = new Request('http://www.example.com', null, ['Host' => 'www.test.com'], 'php://memory'); | |
static::assertEquals('www.test.com', $request->getHeaderLine('host')); | |
} | |
public function testRequestUriMayBeString(): void | |
{ | |
$request = new Request('/', 'GET'); | |
static::assertEquals('/', (string) $request->getUri()); | |
} | |
public function testRequestUriMayBeUri(): void | |
{ | |
$uri = Uri::createFromString('/'); | |
$request = new Request($uri, 'GET'); | |
static::assertSame($uri, $request->getUri()); | |
} | |
public function testValidateRequestUri(): void | |
{ | |
$this->expectException(\InvalidArgumentException::class); | |
$this->expectExceptionMessage('Invalid URI: The submitted uri `///` is invalid for the following scheme(s): `http, https`'); | |
new Request('///', 'GET'); | |
} | |
public function testWithNotValidMethodRequest(): void | |
{ | |
$this->expectException(\InvalidArgumentException::class); | |
$this->expectExceptionMessage('Unsupported HTTP method [BOGUS METHOD].'); | |
new Request('/', 'BOGUS METHOD'); | |
} | |
/** | |
* @dataProvider customRequestMethods | |
* | |
* @param mixed $method | |
*/ | |
public function testAllowsCustomRequestMethodsThatFollowSpec($method): void | |
{ | |
$request = new Request(null, $method); | |
static::assertSame($method, $request->getMethod()); | |
} | |
public function customRequestMethods() | |
{ | |
return[ | |
// WebDAV methods | |
'TRACE' => ['TRACE'], | |
'PROPFIND' => ['PROPFIND'], | |
'PROPPATCH' => ['PROPPATCH'], | |
'MKCOL' => ['MKCOL'], | |
'COPY' => ['COPY'], | |
'MOVE' => ['MOVE'], | |
'LOCK' => ['LOCK'], | |
'UNLOCK' => ['UNLOCK'], | |
// Arbitrary methods | |
'#!ALPHA-1234&%' => ['#!ALPHA-1234&%'], | |
]; | |
} | |
public function testCanConstructWithBody(): void | |
{ | |
$request = new Request('/', 'GET', [], 'baz'); | |
static::assertInstanceOf(StreamInterface::class, $request->getBody()); | |
static::assertEquals('baz', (string) $request->getBody()); | |
} | |
public function testNullBody(): void | |
{ | |
$request = new Request('/', 'GET', [], null); | |
static::assertInstanceOf(StreamInterface::class, $request->getBody()); | |
static::assertSame('', (string) $request->getBody()); | |
} | |
public function testFalseyBody(): void | |
{ | |
$request = new Request('/', 'GET', [], '0'); | |
static::assertInstanceOf(StreamInterface::class, $request->getBody()); | |
static::assertSame('0', (string) $request->getBody()); | |
} | |
public function testCapitalizesMethod(): void | |
{ | |
$request = new Request('/', 'get'); | |
static::assertEquals('GET', $request->getMethod()); | |
} | |
public function testCapitalizesWithMethod(): void | |
{ | |
$request = new Request('/', 'GET'); | |
static::assertEquals('PUT', $request->withMethod('put')->getMethod()); | |
} | |
public function testWithUri(): void | |
{ | |
$request1 = new Request('/', 'GET'); | |
$uri1 = $request1->getUri(); | |
$uri2 = Uri::createFromString('http://www.example.com'); | |
$request2 = $request1->withUri($uri2); | |
static::assertNotSame($request1, $request2); | |
static::assertSame($uri2, $request2->getUri()); | |
static::assertSame($uri1, $request1->getUri()); | |
} | |
public function testSameInstanceWhenSameUri(): void | |
{ | |
$request1 = new Request('http://foo.com', 'GET'); | |
$request2 = $request1->withUri($request1->getUri()); | |
static::assertSame($request1, $request2); | |
} | |
public function testWithRequestTarget(): void | |
{ | |
$request1 = new Request('/', 'GET'); | |
$request2 = $request1->withRequestTarget('*'); | |
static::assertEquals('*', $request2->getRequestTarget()); | |
static::assertEquals('/', $request1->getRequestTarget()); | |
} | |
public function testWithRequestNullUri(): void | |
{ | |
$request = new Request(null, 'GET'); | |
static::assertEquals('/', $request->getRequestTarget()); | |
} | |
public function testRequestToThrowException(): void | |
{ | |
$this->expectException(\InvalidArgumentException::class); | |
$this->expectExceptionMessage('Invalid URI provided; must be null, a string or a [\\Psr\\Http\\Message\\UriInterface] instance.'); | |
new Request(new stdClass(), 'GET'); | |
} | |
public function testRequestTargetDoesNotAllowSpaces(): void | |
{ | |
$this->expectException(\InvalidArgumentException::class); | |
$this->expectExceptionMessage('Invalid request target provided; cannot contain whitespace'); | |
$request1 = new Request('/', 'GET'); | |
$request1->withRequestTarget('/foo bar'); | |
} | |
public function testRequestTargetDefaultsToSlash(): void | |
{ | |
$request1 = new Request('', 'GET'); | |
static::assertEquals('/', $request1->getRequestTarget()); | |
$request2 = new Request('*', 'GET'); | |
static::assertEquals('*', $request2->getRequestTarget()); | |
$request3 = new Request('http://foo.com/bar baz/', 'GET'); | |
static::assertEquals('/bar%20baz/', $request3->getRequestTarget()); | |
} | |
public function testBuildsRequestTarget(): void | |
{ | |
$request1 = new Request('http://foo.com/baz?bar=bam', 'GET'); | |
static::assertEquals('/baz?bar=bam', $request1->getRequestTarget()); | |
} | |
public function testBuildsRequestTargetWithFalseyQuery(): void | |
{ | |
$request1 = new Request('http://foo.com/baz?0', 'GET'); | |
static::assertEquals('/baz?0', $request1->getRequestTarget()); | |
} | |
public function testHostIsAddedFirst(): void | |
{ | |
$request = new Request('http://foo.com/baz?bar=bam', 'GET', ['Foo' => 'Bar']); | |
static::assertEquals([ | |
'Host' => ['foo.com'], | |
'Foo' => ['Bar'], | |
], $request->getHeaders()); | |
} | |
public function testCanGetHeaderAsCsv(): void | |
{ | |
$request = new Request('http://foo.com/baz?bar=bam', 'GET', [ | |
'Foo' => ['a', 'b', 'c'], | |
]); | |
static::assertEquals('a,b,c', $request->getHeaderLine('Foo')); | |
static::assertEquals('', $request->getHeaderLine('Bar')); | |
} | |
public function testHostIsNotOverwrittenWhenPreservingHost(): void | |
{ | |
$request = new Request('http://foo.com/baz?bar=bam', 'GET', ['Host' => 'a.com']); | |
static::assertEquals(['Host' => ['a.com']], $request->getHeaders()); | |
$request2 = $request->withUri(Uri::createFromString('http://www.foo.com/bar'), true); | |
static::assertEquals('a.com', $request2->getHeaderLine('Host')); | |
} | |
public function testOverridesHostWithUri(): void | |
{ | |
$request = new Request('http://foo.com/baz?bar=bam', 'GET'); | |
static::assertEquals(['Host' => ['foo.com']], $request->getHeaders()); | |
$request2 = $request->withUri(Uri::createFromString('http://www.baz.com/bar')); | |
static::assertEquals('www.baz.com', $request2->getHeaderLine('Host')); | |
} | |
public function testAggregatesHeaders(): void | |
{ | |
$request = new Request('', 'GET', [ | |
'ZOO' => 'zoobar', | |
'zoo' => ['foobar', 'zoobar'], | |
]); | |
static::assertEquals(['ZOO' => ['zoobar', 'foobar', 'zoobar']], $request->getHeaders()); | |
static::assertEquals('zoobar,foobar,zoobar', $request->getHeaderLine('zoo')); | |
} | |
public function testAddsPortToHeader(): void | |
{ | |
$request = new Request('http://foo.com:8124/bar', 'GET'); | |
static::assertEquals('foo.com:8124', $request->getHeaderLine('host')); | |
} | |
public function testAddsPortToHeaderAndReplacePreviousPort(): void | |
{ | |
$request = new Request('http://foo.com:8124/bar', 'GET'); | |
$request = $request->withUri(Uri::createFromString('http://foo.com:8125/bar')); | |
static::assertEquals('foo.com:8125', $request->getHeaderLine('host')); | |
} | |
private function getEmptyHostHeader() | |
{ | |
$emptyHostHeaderMockUri = $this->mock(UriInterface::class); | |
$emptyHostHeaderMockUri->shouldReceive('getHost') | |
->andReturn(''); | |
return new Request($emptyHostHeaderMockUri); | |
} | |
private function getDefaultUriHost() | |
{ | |
$defaultUriHost = $this->mock(UriInterface::class); | |
$defaultUriHost->shouldReceive('getHost') | |
->andReturn('baz.com'); | |
$defaultUriHost->shouldReceive('getPort') | |
->andReturn(null); | |
return $defaultUriHost; | |
} | |
private function getDefaultUriHostAndPort() | |
{ | |
$defaultUriHostAndPort = $this->mock(UriInterface::class); | |
$defaultUriHostAndPort->shouldReceive('getHost') | |
->andReturn('baz.com'); | |
$defaultUriHostAndPort->shouldReceive('getPort') | |
->andReturn('8080'); | |
return $defaultUriHostAndPort; | |
} | |
} | |
----------- end diff ----------- | |
6) src/Viserio/Component/Http/Tests/UtilTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
$body = 'foobaz'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foobaz'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foobaz'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$s1 = new Stream($stream); | |
- $s2 = new Stream(\fopen('php://temp', 'rb+')); | |
+ $s2 = new Stream(\fopen('php://temp', 'b+r')); | |
Util::copyToStream($s1, $s2); | |
static::assertEquals('foobaz', (string) $s2); | |
- $s2 = new Stream(\fopen('php://temp', 'rb+')); | |
+ $s2 = new Stream(\fopen('php://temp', 'b+r')); | |
@@ @@ | |
- $s2 = new Stream(\fopen('php://temp', 'rb+')); | |
+ $s2 = new Stream(\fopen('php://temp', 'b+r')); | |
@@ @@ | |
$body = 'foobaz'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$s1 = new Stream($stream); | |
- $s2 = new Stream(\fopen('php://temp', 'rb+')); | |
+ $s2 = new Stream(\fopen('php://temp', 'b+r')); | |
@@ @@ | |
$body = 'foobaz'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$s1 = new Stream($stream); | |
- $s2 = new Stream(\fopen('php://temp', 'rb+')); | |
+ $s2 = new Stream(\fopen('php://temp', 'b+r')); | |
@@ @@ | |
$body = 'foobaz'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
}]); | |
- $s2 = new Stream(\fopen('php://temp', 'rb+')); | |
+ $s2 = new Stream(\fopen('php://temp', 'b+r')); | |
Util::copyToStream($s1, $s2, 10); | |
static::assertEquals('', (string) $s2); | |
} | |
public function testOpensFilesSuccessfully(): void | |
{ | |
$r = Util::tryFopen(__FILE__, 'r'); | |
static::assertInternalType('resource', $r); | |
\fclose($r); | |
} | |
public function testThrowsExceptionNotWarning(): void | |
{ | |
$this->expectException(\RuntimeException::class); | |
$this->expectExceptionMessage('Unable to open [/path/to/does/not/exist] using mode r'); | |
Util::tryFopen('/path/to/does/not/exist', 'r'); | |
} | |
/** | |
* @return array | |
*/ | |
public function dataNormalizeFiles(): array | |
{ | |
return [ | |
'Single file' => [ | |
[ | |
'file' => [ | |
'name' => 'MyFile.txt', | |
'type' => 'text/plain', | |
'tmp_name' => '/tmp/php/php1h4j1o', | |
'error' => '0', | |
'size' => '123', | |
], | |
], | |
[ | |
'file' => new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
], | |
], | |
'Empty file' => [ | |
[ | |
'image_file' => [ | |
'name' => '', | |
'type' => '', | |
'tmp_name' => '', | |
'error' => '4', | |
'size' => '0', | |
], | |
], | |
[ | |
'image_file' => new UploadedFile( | |
'', | |
0, | |
\UPLOAD_ERR_NO_FILE, | |
'', | |
'' | |
), | |
], | |
], | |
'Already Converted' => [ | |
[ | |
'file' => new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
], | |
[ | |
'file' => new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
], | |
], | |
'Already Converted array' => [ | |
[ | |
'file' => [ | |
new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
new UploadedFile( | |
'', | |
0, | |
\UPLOAD_ERR_NO_FILE, | |
'', | |
'' | |
), | |
], | |
], | |
[ | |
'file' => [ | |
new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
new UploadedFile( | |
'', | |
0, | |
\UPLOAD_ERR_NO_FILE, | |
'', | |
'' | |
), | |
], | |
], | |
], | |
'Multiple files' => [ | |
[ | |
'text_file' => [ | |
'name' => 'MyFile.txt', | |
'type' => 'text/plain', | |
'tmp_name' => '/tmp/php/php1h4j1o', | |
'error' => '0', | |
'size' => '123', | |
], | |
'image_file' => [ | |
'name' => '', | |
'type' => '', | |
'tmp_name' => '', | |
'error' => '4', | |
'size' => '0', | |
], | |
], | |
[ | |
'text_file' => new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
'image_file' => new UploadedFile( | |
'', | |
0, | |
\UPLOAD_ERR_NO_FILE, | |
'', | |
'' | |
), | |
], | |
], | |
'Nested files' => [ | |
[ | |
'file' => [ | |
'name' => [ | |
0 => 'MyFile.txt', | |
1 => 'Image.png', | |
], | |
'type' => [ | |
0 => 'text/plain', | |
1 => 'image/png', | |
], | |
'tmp_name' => [ | |
0 => '/tmp/php/hp9hskjhf', | |
1 => '/tmp/php/php1h4j1o', | |
], | |
'error' => [ | |
0 => '0', | |
1 => '0', | |
], | |
'size' => [ | |
0 => '123', | |
1 => '7349', | |
], | |
], | |
'nested' => [ | |
'name' => [ | |
'other' => 'Flag.txt', | |
'test' => [ | |
0 => 'Stuff.txt', | |
1 => '', | |
], | |
], | |
'type' => [ | |
'other' => 'text/plain', | |
'test' => [ | |
0 => 'text/plain', | |
1 => '', | |
], | |
], | |
'tmp_name' => [ | |
'other' => '/tmp/php/hp9hskjhf', | |
'test' => [ | |
0 => '/tmp/php/asifu2gp3', | |
1 => '', | |
], | |
], | |
'error' => [ | |
'other' => '0', | |
'test' => [ | |
0 => '0', | |
1 => '4', | |
], | |
], | |
'size' => [ | |
'other' => '421', | |
'test' => [ | |
0 => '32', | |
1 => '0', | |
], | |
], | |
], | |
], | |
[ | |
'file' => [ | |
0 => new UploadedFile( | |
'/tmp/php/hp9hskjhf', | |
123, | |
\UPLOAD_ERR_OK, | |
'MyFile.txt', | |
'text/plain' | |
), | |
1 => new UploadedFile( | |
'/tmp/php/php1h4j1o', | |
7349, | |
\UPLOAD_ERR_OK, | |
'Image.png', | |
'image/png' | |
), | |
], | |
'nested' => [ | |
'other' => new UploadedFile( | |
'/tmp/php/hp9hskjhf', | |
421, | |
\UPLOAD_ERR_OK, | |
'Flag.txt', | |
'text/plain' | |
), | |
'test' => [ | |
0 => new UploadedFile( | |
'/tmp/php/asifu2gp3', | |
32, | |
\UPLOAD_ERR_OK, | |
'Stuff.txt', | |
'text/plain' | |
), | |
1 => new UploadedFile( | |
'', | |
0, | |
\UPLOAD_ERR_NO_FILE, | |
'', | |
'' | |
), | |
], | |
], | |
], | |
], | |
]; | |
} | |
/** | |
* @dataProvider dataNormalizeFiles | |
* | |
* @param mixed $files | |
* @param mixed $expected | |
*/ | |
public function testNormalizeFiles($files, $expected): void | |
{ | |
static::assertEquals($expected, Util::normalizeFiles($files)); | |
} | |
public function testNormalizeFilesRaisesException(): void | |
{ | |
$this->expectException(InvalidArgumentException::class); | |
$this->expectExceptionMessage('Invalid value in files specification'); | |
Util::normalizeFiles(['test' => 'something']); | |
} | |
public function testFlatFile(): void | |
{ | |
$files = [ | |
'avatar' => [ | |
'tmp_name' => 'phpUxcOty', | |
'name' => 'my-avatar.png', | |
'size' => 90996, | |
'type' => 'image/png', | |
'error' => 0, | |
], | |
]; | |
$normalised = Util::normalizeFiles($files); | |
static::assertCount(1, $normalised); | |
static::assertInstanceOf(UploadedFileInterface::class, $normalised['avatar']); | |
static::assertEquals('my-avatar.png', $normalised['avatar']->getClientFilename()); | |
} | |
public function testNestedFile(): void | |
{ | |
$files = [ | |
'my-form' => [ | |
'details' => [ | |
'avatar' => [ | |
'tmp_name' => 'phpUxcOty', | |
'name' => 'my-avatar.png', | |
'size' => 90996, | |
'type' => 'image/png', | |
'error' => 0, | |
], | |
], | |
], | |
]; | |
$normalised = Util::normalizeFiles($files); | |
static::assertCount(1, $normalised); | |
static::assertEquals('my-avatar.png', $normalised['my-form']['details']['avatar']->getClientFilename()); | |
} | |
public function testNumericIndexedFiles(): void | |
{ | |
$files = [ | |
'my-form' => [ | |
'details' => [ | |
'avatars' => [ | |
'tmp_name' => [ | |
0 => 'abc123', | |
1 => 'duck123', | |
2 => 'goose123', | |
], | |
'name' => [ | |
0 => 'file1.txt', | |
1 => 'file2.txt', | |
2 => 'file3.txt', | |
], | |
'size' => [ | |
0 => 100, | |
1 => 240, | |
2 => 750, | |
], | |
'type' => [ | |
0 => 'plain/txt', | |
1 => 'image/jpg', | |
2 => 'image/png', | |
], | |
'error' => [ | |
0 => 0, | |
1 => 0, | |
2 => 0, | |
], | |
], | |
], | |
], | |
]; | |
$normalised = Util::normalizeFiles($files); | |
static::assertCount(3, $normalised['my-form']['details']['avatars']); | |
static::assertEquals('file1.txt', $normalised['my-form']['details']['avatars'][0]->getClientFilename()); | |
static::assertEquals('file2.txt', $normalised['my-form']['details']['avatars'][1]->getClientFilename()); | |
static::assertEquals('file3.txt', $normalised['my-form']['details']['avatars'][2]->getClientFilename()); | |
} | |
/** | |
* This case covers upfront numeric index which moves the tmp_name/size/etc fields further up the array tree. | |
*/ | |
public function testNumericFirstIndexedFiles(): void | |
{ | |
$files = [ | |
'slide-shows' => [ | |
'tmp_name' => [ | |
// Note: Nesting *under* tmp_name/etc | |
0 => [ | |
'slides' => [ | |
0 => '/tmp/phpYzdqkD', | |
1 => '/tmp/phpYzdfgh', | |
], | |
], | |
], | |
'error' => [ | |
0 => [ | |
'slides' => [ | |
0 => 0, | |
1 => 0, | |
], | |
], | |
], | |
'name' => [ | |
0 => [ | |
'slides' => [ | |
0 => 'foo.txt', | |
1 => 'bar.txt', | |
], | |
], | |
], | |
'size' => [ | |
0 => [ | |
'slides' => [ | |
0 => 123, | |
1 => 200, | |
], | |
], | |
], | |
'type' => [ | |
0 => [ | |
'slides' => [ | |
0 => 'text/plain', | |
1 => 'text/plain', | |
], | |
], | |
], | |
], | |
]; | |
$normalised = Util::normalizeFiles($files); | |
static::assertCount(2, $normalised['slide-shows'][0]['slides']); | |
static::assertEquals('foo.txt', $normalised['slide-shows'][0]['slides'][0]->getClientFilename()); | |
static::assertEquals('bar.txt', $normalised['slide-shows'][0]['slides'][1]->getClientFilename()); | |
} | |
} | |
----------- end diff ----------- | |
7) src/Viserio/Component/Http/Tests/StreamTest.php (fopen_flag_order, fopen_flags) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
{ | |
- $handle = \fopen('php://temp', 'rb+'); | |
+ $handle = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
{ | |
- $handle = \fopen('php://temp', 'wb+'); | |
+ $handle = \fopen('php://temp', 'b+w'); | |
@@ @@ | |
{ | |
- $handle = \fopen('php://temp', 'wb+'); | |
+ $handle = \fopen('php://temp', 'b+w'); | |
@@ @@ | |
{ | |
- $handle = \fopen('php://temp', 'wb+'); | |
+ $handle = \fopen('php://temp', 'b+w'); | |
@@ @@ | |
{ | |
- $h = \fopen('php://temp', 'wb+'); | |
+ $h = \fopen('php://temp', 'b+w'); | |
@@ @@ | |
{ | |
- $handle = \fopen('php://temp', 'wb+'); | |
+ $handle = \fopen('php://temp', 'b+w'); | |
@@ @@ | |
$body = 'foo'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
- $fileStream = new Stream(\fopen(__FILE__, 'r')); | |
+ $fileStream = new Stream(\fopen(__FILE__, 'rb')); | |
static::assertFalse(NSA::invokeMethod($fileStream, 'isPipe')); | |
} | |
public function testIsPipeReadable(): void | |
{ | |
$stream = new Stream($this->pipeFh); | |
static::assertTrue($stream->isReadable()); | |
} | |
public function testPipeIsNotSeekable(): void | |
{ | |
$stream = new Stream($this->pipeFh); | |
static::assertFalse($stream->isSeekable()); | |
} | |
public function testCannotSeekPipe(): void | |
{ | |
$this->expectException(\Viserio\Component\Contract\Http\Exception\RuntimeException::class); | |
$this->expectExceptionMessage('Stream is not seekable.'); | |
$stream = new Stream($this->pipeFh); | |
$stream->seek(0); | |
} | |
public function testCannotTellPipe(): void | |
{ | |
$this->expectException(\Viserio\Component\Contract\Http\Exception\RuntimeException::class); | |
$this->expectExceptionMessage('Unable to determine stream position.'); | |
$stream = new Stream($this->pipeFh); | |
$stream->tell(); | |
} | |
public function testCannotRewindPipe(): void | |
{ | |
$this->expectException(\Viserio\Component\Contract\Http\Exception\RuntimeException::class); | |
$this->expectExceptionMessage('Stream is not seekable.'); | |
$stream = new Stream($this->pipeFh); | |
$stream->rewind(); | |
} | |
public function testPipeGetSizeYieldsNull(): void | |
{ | |
$stream = new Stream($this->pipeFh); | |
static::assertNull($stream->getSize()); | |
} | |
public function testClosePipe(): void | |
{ | |
$stream = new Stream($this->pipeFh); | |
\stream_get_contents($this->pipeFh); // prevent broken pipe error message | |
$stream->close(); | |
$this->pipeFh = null; | |
static::assertFalse(NSA::invokeMethod($stream, 'isPipe')); | |
} | |
public function testPipeToString(): void | |
{ | |
$stream = new Stream($this->pipeFh); | |
static::assertSame("Could not open input file: StreamTest.php\n", $stream->__toString()); | |
} | |
public function testPipeGetContents(): void | |
{ | |
$stream = new Stream($this->pipeFh); | |
$contents = \trim($stream->getContents()); | |
static::assertSame('Could not open input file: StreamTest.php', $contents); | |
} | |
private static function assertStreamStateAfterClosedOrDetached(Stream $stream): void | |
{ | |
static::assertFalse($stream->isReadable()); | |
static::assertFalse($stream->isWritable()); | |
static::assertFalse($stream->isSeekable()); | |
static::assertNull($stream->getSize()); | |
static::assertSame([], $stream->getMetadata()); | |
static::assertNull($stream->getMetadata('foo')); | |
$throws = function (callable $fn): void { | |
try { | |
$fn(); | |
} catch (Throwable $e) { | |
static::assertContains('Stream is detached', $e->getMessage()); | |
return; | |
} | |
static::fail('Exception should be thrown after the stream is detached.'); | |
}; | |
$throws(function () use ($stream): void { | |
$stream->read(10); | |
}); | |
$throws(function () use ($stream): void { | |
$stream->write('bar'); | |
}); | |
$throws(function () use ($stream): void { | |
$stream->seek(10); | |
}); | |
$throws(function () use ($stream): void { | |
$stream->tell(); | |
}); | |
$throws(function () use ($stream): void { | |
$stream->eof(); | |
}); | |
$throws(function () use ($stream): void { | |
$stream->getContents(); | |
}); | |
\set_error_handler(function ($errno, $errstr, $errfile, $errline) { | |
if ($errno === \E_USER_ERROR) { | |
static::assertContains('::__toString exception: ', $errstr); | |
return ''; | |
} | |
}); | |
static::assertSame('', (string) $stream); | |
\restore_error_handler(); | |
} | |
} | |
namespace Viserio\Component\Http; | |
use Viserio\Component\Http\Tests\StreamTest; | |
function fread($handle, $length) | |
{ | |
return StreamTest::$isFreadError ? false : \fread($handle, $length); | |
} | |
----------- end diff ----------- | |
8) src/Viserio/Component/Http/Tests/Stream/FnStreamTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
$body = 'foo'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foo'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$stream1 = new Stream($stream); | |
$stream2 = FnStream::decorate($stream1, [ | |
'read' => function ($len) use (&$called, $stream1) { | |
$called = true; | |
return $stream1->read($len); | |
}, | |
]); | |
static::assertEquals('foo', $stream2->read(3)); | |
static::assertTrue($called); | |
} | |
} | |
----------- end diff ----------- | |
9) src/Viserio/Component/Http/Tests/Stream/LimitStreamTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
$body = 'foo'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foo_baz_bar'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
- new LimitStream(new Stream(\fopen('php://temp', 'rb+')), 0, 10); | |
+ new LimitStream(new Stream(\fopen('php://temp', 'b+r')), 0, 10); | |
} | |
public function testReturnsSubsetOfEmptyBodyWhenCastToString(): void | |
{ | |
$body = '01234567891234'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = '0123456789abcdef'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foo_bar'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foo_bar'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foobazbar'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
$body = 'foo_bar'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$a = new Stream($stream); | |
$b = new LimitStream($a, -1, 4); | |
static::assertEquals(3, $b->getSize()); | |
} | |
} | |
----------- end diff ----------- | |
10) src/Viserio/Component/Http/Tests/Stream/NoSeekStreamTest.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
$body = 'foo'; | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
\fwrite($stream, $body); | |
\fseek($stream, 0); | |
$s = new Stream($stream); | |
$s->seek(1); | |
$wrapped = new NoSeekStream($s); | |
static::assertEquals('oo', (string) $wrapped); | |
$wrapped->close(); | |
} | |
} | |
----------- end diff ----------- | |
11) src/Viserio/Component/Http/AbstractMessage.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
if (empty($this->stream)) { | |
- $this->stream = new Stream(\fopen('php://temp', 'rb+')); | |
+ $this->stream = new Stream(\fopen('php://temp', 'b+r')); | |
@@ @@ | |
if (\is_string($body)) { | |
- $stream = \fopen('php://temp', 'rb+'); | |
+ $stream = \fopen('php://temp', 'b+r'); | |
@@ @@ | |
if ($type === 'NULL') { | |
- return new Stream(\fopen('php://temp', 'rb+')); | |
+ return new Stream(\fopen('php://temp', 'b+r')); | |
} | |
if ($type === 'resource') { | |
return new Stream($body); | |
} | |
throw new InvalidArgumentException('Invalid resource type: ' . \gettype($body)); | |
} | |
/** | |
* Validate the HTTP protocol version. | |
* | |
* @param string $version | |
* | |
* @throws \Viserio\Component\Contract\Http\Exception\InvalidArgumentException on invalid HTTP protocol version | |
* | |
* @return void | |
*/ | |
private function validateProtocolVersion(string $version): void | |
{ | |
if ($version === '') { | |
throw new InvalidArgumentException(\sprintf( | |
'HTTP protocol version can not be empty' | |
)); | |
} | |
if (! isset(self::$validProtocolVersions[$version])) { | |
throw new InvalidArgumentException( | |
'Invalid HTTP version. Must be one of: ' | |
. \implode(', ', \array_keys(self::$validProtocolVersions)) | |
); | |
} | |
} | |
/** | |
* Check all header values and header name. | |
* | |
* @param string $header | |
* @param array|string $value | |
* | |
* @throws \Viserio\Component\Contract\Http\Exception\UnexpectedValueException | |
* | |
* @return array | |
*/ | |
private function checkHeaderData(string $header, $value): array | |
{ | |
if (\is_string($value)) { | |
$value = [$value]; | |
} | |
if (! $this->arrayContainsOnlyStrings($value)) { | |
throw new UnexpectedValueException( | |
'Invalid header value; must be a string or array of strings.' | |
); | |
} | |
HeaderSecurity::assertValidName(\trim($header)); | |
$this->assertValidHeaderValue($value); | |
return $this->trimHeaderValues($value); | |
} | |
/** | |
* Test that an array contains only strings. | |
* | |
* @param array $array | |
* | |
* @return bool | |
*/ | |
private function arrayContainsOnlyStrings(array $array): bool | |
{ | |
// Test if a value is a string. | |
$filterStringValue = function (bool $carry, $item) { | |
if (! \is_string($item)) { | |
return false; | |
} | |
return $carry; | |
}; | |
return \array_reduce($array, $filterStringValue, true); | |
} | |
/** | |
* Assert that the provided header values are valid. | |
* | |
* @see http://tools.ietf.org/html/rfc7230#section-3.2 | |
* | |
* @param array $values | |
* | |
* @throws \Viserio\Component\Contract\Http\Exception\UnexpectedValueException | |
* | |
* @return void | |
*/ | |
private function assertValidHeaderValue(array $values): void | |
{ | |
/** @var callable $callback */ | |
$callback = __NAMESPACE__ . '\HeaderSecurity::assertValid'; | |
\array_walk($values, $callback); | |
} | |
/** | |
* Filter array headers. | |
* | |
* @param array $values | |
* | |
* @return array | |
*/ | |
private function filterHeaderValue(array $values): array | |
{ | |
$values = \array_filter($values, function ($value) { | |
return null !== $value; | |
}); | |
return \array_map([HeaderSecurity::class, 'filter'], \array_values($values)); | |
} | |
/** | |
* Trims whitespace from the header values. | |
* | |
* Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. | |
* | |
* header-field = field-name ":" OWS field-value OWS | |
* OWS = *( SP / HTAB ) | |
* | |
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4 | |
* | |
* @param array $values Header values | |
* | |
* @return array Trimmed header values | |
*/ | |
private function trimHeaderValues(array $values): array | |
{ | |
return \array_map(function ($value) { | |
return \trim($value, " \t"); | |
}, $values); | |
} | |
} | |
----------- end diff ----------- | |
12) src/Viserio/Component/Http/Response/TextResponse.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
- $body = new Stream(\fopen('php://temp', 'wb+')); | |
+ $body = new Stream(\fopen('php://temp', 'b+w')); | |
$body->write($text); | |
$body->rewind(); | |
return $body; | |
} | |
} | |
----------- end diff ----------- | |
13) src/Viserio/Component/Http/Response/HtmlResponse.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
- $body = new Stream(\fopen('php://temp', 'wb+')); | |
+ $body = new Stream(\fopen('php://temp', 'b+w')); | |
$body->write($html); | |
$body->rewind(); | |
return $body; | |
} | |
} | |
----------- end diff ----------- | |
14) src/Viserio/Component/Http/Response/XmlResponse.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
- $body = new Stream(\fopen('php://temp', 'wb+')); | |
+ $body = new Stream(\fopen('php://temp', 'b+w')); | |
$body->write($text); | |
$body->rewind(); | |
return $body; | |
} | |
} | |
----------- end diff ----------- | |
15) src/Viserio/Component/Http/Response/JsonResponse.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
) { | |
- $body = new Stream(\fopen('php://temp', 'wb+')); | |
+ $body = new Stream(\fopen('php://temp', 'b+w')); | |
$body->write($this->jsonEncode($data, $encodingOptions)); | |
$body->rewind(); | |
$headers = $this->injectContentType('application/json; charset=' . ($charset ?? 'utf-8'), $headers); | |
parent::__construct($status, $headers, $body, $version); | |
} | |
/** | |
* Encode the provided data to JSON. | |
* | |
* @param mixed $data | |
* @param int $encodingOptions | |
* | |
* @throws \Viserio\Component\Contract\Http\Exception\InvalidArgumentException | |
* @throws \Viserio\Component\Contract\Http\Exception\RuntimeException if unable to encode the $data to JSON | |
* | |
* @return string | |
*/ | |
private function jsonEncode($data, $encodingOptions): string | |
{ | |
if (\is_resource($data)) { | |
throw new InvalidArgumentException('Cannot JSON encode resources.'); | |
} | |
// Clear json_last_error() | |
\json_encode(null); | |
$json = \json_encode($data, $encodingOptions); | |
if (\JSON_ERROR_NONE !== \json_last_error()) { | |
throw new RuntimeException(\sprintf( | |
'Unable to encode data to JSON in %s: %s', | |
__CLASS__, | |
\json_last_error_msg() | |
)); | |
} | |
return $json; | |
} | |
} | |
----------- end diff ----------- | |
16) src/Viserio/Component/Http/Response/RedirectResponse.php (fopen_flag_order) | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ @@ | |
- parent::__construct($status, $headers, new Stream(\fopen('php://temp', 'rb+')), $version); | |
+ parent::__construct($status, $headers, new Stream(\fopen('php://temp', 'b+r')), $version); | |
} | |
} | |
----------- end diff ----------- | |
Checked all files in 4.769 seconds, 16.000 MB memory used |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment