Skip to content

Commit

Permalink
Merge f4d94ce into be9bcc4
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] committed Nov 2, 2021
2 parents be9bcc4 + f4d94ce commit 0304e74
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -38,7 +38,7 @@
"nikic/php-parser": "^4.4"
},
"require-dev": {
"phpstan/phpstan": "^0.12",
"phpstan/phpstan": "^0.12 || ^1.0",
"phpunit/phpunit": "^8.5 || ^9.3",
"squizlabs/php_codesniffer": "^3.5"
},
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon.dist
@@ -1,6 +1,9 @@
parameters:
level: max
ignoreErrors:
- message: '/Parameter \#3 \$microseconds of function stream_set_timeout expects int, int\|null given./'
- message: '/Parameter \#3 \$microseconds of function stream_set_timeout expects int, int\|null given/'
path: src/FileStreamWrapper.php
count: 1
- message: '/Parameter \#2 \$size of function ftruncate expects int\<0, max\>, int given/'
path: src/FileStreamWrapper.php
count: 1
40 changes: 27 additions & 13 deletions src/FileStreamWrapper.php
Expand Up @@ -10,6 +10,7 @@

namespace AdrianSuter\Autoload\Override;

use InvalidArgumentException;
use RuntimeException;

use function chgrp;
Expand All @@ -26,6 +27,7 @@
use function fstat;
use function ftruncate;
use function fwrite;
use function is_int;
use function is_resource;
use function is_string;
use function lstat;
Expand Down Expand Up @@ -81,7 +83,7 @@ public function dir_closedir(): bool
* Open directory handle.
*
* @param string $path
* @param int $options
* @param int $options
*
* @return bool
* @noinspection PhpUnusedParameterInspection
Expand Down Expand Up @@ -147,8 +149,8 @@ public function dir_rewinddir(): bool
* Create a directory.
*
* @param string $path
* @param int $mode
* @param int $options
* @param int $mode
* @param int $options
*
* @return bool
*/
Expand Down Expand Up @@ -197,7 +199,7 @@ public function rename(string $path_from, string $path_to): bool
* Remove a directory.
*
* @param string $path
* @param int $options
* @param int $options
*
* @return bool
* @noinspection PhpUnusedParameterInspection
Expand Down Expand Up @@ -237,6 +239,7 @@ public function stream_cast(int $cast_as)

/**
* Close a resource.
*
* @noinspection PhpUnused
*/
public function stream_close(): void
Expand Down Expand Up @@ -310,9 +313,9 @@ public function stream_lock(int $operation): bool
/**
* Change stream metadata.
*
* @param string $path
* @param int $option
* @param mixed $value
* @param string $path
* @param int $option
* @param array<int|null>|string|int $value
*
* @return bool
* @noinspection PhpUnused
Expand All @@ -324,24 +327,34 @@ public function stream_metadata(string $path, int $option, $value): bool
$r = false;
switch ($option) {
case STREAM_META_TOUCH:
/** @var array<int|null> $value */
if (!isset($value[0]) || is_null($value[0])) {
$r = touch($path);
} else {
$r = touch($path, $value[0], $value[1]);
$r = touch($path, (int)$value[0], (int)$value[1]);
}
break;

case STREAM_META_OWNER_NAME:
case STREAM_META_OWNER:
if (!is_int($value) && !is_string($value)) {
throw new InvalidArgumentException('Parameter #3 is expected to be an `int|string`.');
}
$r = chown($path, $value);
break;

case STREAM_META_GROUP_NAME:
case STREAM_META_GROUP:
if (!is_int($value) && !is_string($value)) {
throw new InvalidArgumentException('Parameter #3 is expected to be an `int|string`.');
}
$r = chgrp($path, $value);
break;

case STREAM_META_ACCESS:
if (!is_int($value)) {
throw new InvalidArgumentException('Parameter #3 is expected to be an `int`.');
}
$r = chmod($path, $value);
break;
}
Expand All @@ -355,9 +368,9 @@ public function stream_metadata(string $path, int $option, $value): bool
/**
* Open file or URL.
*
* @param string $path
* @param string $mode
* @param int $options
* @param string $path
* @param string $mode
* @param int $options
* @param string|null $opened_path
*
* @return bool
Expand Down Expand Up @@ -403,7 +416,8 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
/**
* Read from stream.
*
* @param int $count
* @param int $count
* @psalm-param positive-int $count
*
* @return string
* @noinspection PhpUnused
Expand Down Expand Up @@ -616,7 +630,7 @@ public function unlink(string $path): bool
* Retrieve information about a file.
*
* @param string $path
* @param int $flags
* @param int $flags
*
* @return array<int|string, int>|false
* @noinspection PhpUnused
Expand Down
81 changes: 60 additions & 21 deletions tests/FileStreamWrapperTest.php
Expand Up @@ -11,6 +11,8 @@
namespace AdrianSuter\Autoload\Override\Tests;

use AdrianSuter\Autoload\Override\FileStreamWrapper;
use InvalidArgumentException;
use PHPUnit\Framework\IncompleteTestError;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use RuntimeException;
Expand All @@ -27,6 +29,8 @@
use function ftruncate;
use function fwrite;
use function is_dir;
use function is_resource;
use function is_string;
use function lstat;
use function mkdir;
use function opendir;
Expand All @@ -48,6 +52,10 @@
use function touch;
use function unlink;

use const STREAM_META_ACCESS;
use const STREAM_META_GROUP;
use const STREAM_META_OWNER;

final class FileStreamWrapperTest extends TestCase
{
/**
Expand All @@ -71,10 +79,11 @@ private function registerWrapper(): void

private function createTempFile(bool $registerWrapper = true): void
{
$this->tempFilePath = tempnam(sys_get_temp_dir(), 'FSW');
if (false === $this->tempFilePath) {
$tempFilePath = tempnam(sys_get_temp_dir(), 'FSW');
if (false === $tempFilePath) {
throw new RuntimeException('Temporary file could not be created');
}
$this->tempFilePath = $tempFilePath;

if ($registerWrapper) {
$this->registerWrapper();
Expand All @@ -91,21 +100,24 @@ private function deleteTempFile(): void
$this->tempFilePath = null;
}

public function testDir()
public function testDir(): void
{
$this->registerWrapper();

$fp = opendir(__DIR__);
$this->assertTrue(\is_resource($fp));
$this->assertTrue(is_resource($fp));
if (!is_resource($fp)) {
throw new IncompleteTestError();
}

$item = readdir($fp);
$this->assertTrue(\is_string($item));
$this->assertTrue(is_string($item));

rewinddir($fp);
closedir($fp);
}

public function testDirOpenDirWithoutContext()
public function testDirOpenDirWithoutContext(): void
{
$fileStreamWrapper = new FileStreamWrapper();
$this->assertTrue($fileStreamWrapper->dir_opendir(__DIR__, 0));
Expand All @@ -114,7 +126,7 @@ public function testDirOpenDirWithoutContext()
$fileStreamWrapper->dir_closedir();
}

public function testMkdirRenameRmdir()
public function testMkdirRenameRmdir(): void
{
$directory = sys_get_temp_dir() . '/fileStreamWrapper';
$directory2 = sys_get_temp_dir() . '/fileStreamWrapper2';
Expand All @@ -134,7 +146,7 @@ public function testMkdirRenameRmdir()
$this->assertTrue(rmdir($directory2));
}

public function testMkdirRenameRmdirWithoutContext()
public function testMkdirRenameRmdirWithoutContext(): void
{
$directory = sys_get_temp_dir() . '/fileStreamWrapper';
$directory2 = sys_get_temp_dir() . '/fileStreamWrapper2';
Expand All @@ -153,7 +165,7 @@ public function testMkdirRenameRmdirWithoutContext()
$this->assertTrue($fileStreamWrapper->rmdir($directory2, 0));
}

public function testStreamCast()
public function testStreamCast(): void
{
$fileStreamWrapper = new FileStreamWrapper();
$this->assertFalse($fileStreamWrapper->stream_cast(0));
Expand All @@ -169,7 +181,7 @@ public function testStreamCast()
fclose($resource);
}

public function testTouch()
public function testTouch(): void
{
$this->createTempFile();

Expand All @@ -178,7 +190,7 @@ public function testTouch()
$this->assertTrue(touch($this->tempFilePath, time(), time()));
}

public function testChown()
public function testChown(): void
{
$this->createTempFile(false);

Expand All @@ -190,7 +202,7 @@ public function testChown()
$this->assertIsBool(chown($this->tempFilePath, $stat['uid']));
}

public function testChgrp()
public function testChgrp(): void
{
$this->createTempFile(false);

Expand All @@ -202,14 +214,41 @@ public function testChgrp()
$this->assertIsBool(chgrp($this->tempFilePath, $stat['gid']));
}

public function testChmod()
public function testChmod(): void
{
$this->createTempFile();

$this->assertTrue(chmod($this->tempFilePath, 0755));
}

public function testFlush()
public function testStreamMetaDataMetaOwnerWithInvalidThirdParameter(): void
{
$fileStreamWrapper = new FileStreamWrapper();
$this->createTempFile(false);

$this->expectException(InvalidArgumentException::class);
$fileStreamWrapper->stream_metadata((string)$this->tempFilePath, STREAM_META_OWNER, [null]);
}

public function testStreamMetaDataMetaGroupWithInvalidThirdParameter(): void
{
$fileStreamWrapper = new FileStreamWrapper();
$this->createTempFile(false);

$this->expectException(InvalidArgumentException::class);
$fileStreamWrapper->stream_metadata((string)$this->tempFilePath, STREAM_META_GROUP, [null]);
}

public function testStreamMetaDataMetaAccessWithInvalidThirdParameter(): void
{
$fileStreamWrapper = new FileStreamWrapper();
$this->createTempFile(false);

$this->expectException(InvalidArgumentException::class);
$fileStreamWrapper->stream_metadata((string)$this->tempFilePath, STREAM_META_ACCESS, [null]);
}

public function testFlush(): void
{
$this->createTempFile();

Expand All @@ -218,7 +257,7 @@ public function testFlush()
fclose($fp);
}

public function testSeek()
public function testSeek(): void
{
$this->createTempFile();

Expand All @@ -227,7 +266,7 @@ public function testSeek()
fclose($fp);
}

public function testTruncate()
public function testTruncate(): void
{
$this->createTempFile();

Expand All @@ -236,7 +275,7 @@ public function testTruncate()
fclose($fp);
}

public function testWrite()
public function testWrite(): void
{
$this->createTempFile();

Expand All @@ -245,7 +284,7 @@ public function testWrite()
fclose($fp);
}

public function testLock()
public function testLock(): void
{
$this->createTempFile();

Expand All @@ -254,7 +293,7 @@ public function testLock()
fclose($fp);
}

public function testSetOption()
public function testSetOption(): void
{
$this->createTempFile();

Expand All @@ -270,15 +309,15 @@ public function testSetOption()
fclose($fp);
}

public function testUnlink()
public function testUnlink(): void
{
$this->createTempFile();

$this->assertTrue(unlink($this->tempFilePath));
$this->tempFilePath = null;
}

public function testUrlStat()
public function testUrlStat(): void
{
$this->createTempFile();

Expand Down

0 comments on commit 0304e74

Please sign in to comment.