Skip to content

Commit

Permalink
fork and upgrade vfs adapter dependencies for laravel 10
Browse files Browse the repository at this point in the history
  • Loading branch information
jocoDomino committed May 17, 2023
1 parent f3b4f12 commit aba84f6
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 128 deletions.
Empty file removed .coveralls.yml
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor
.idea
build
composer.lock
.phpunit.result.cache
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
},
"minimum-stability": "stable",
"require": {
"php": ">=7.0.0",
"league/flysystem": "^1.0",
"illuminate/support": "^5.1 || ^6.0 || ^7.0",
"php": "^8.0",
"league/flysystem": "^3.0.0",
"illuminate/support": "^5.1 || ^6.0 || ^7.0 || ^9.0 || ^10.0",
"mikey179/vfsstream": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
"phpunit/phpunit": "~9.0",
"symfony/var-dumper": "^3",
"ext-fileinfo": "*",
"mockery/mockery": "~1.0",
"phpspec/phpspec": "^2.2"
"phpspec/phpspec": "^7.2"
}
}
55 changes: 21 additions & 34 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Library Test Suite">
<directory suffix=".php">./tests/Adapter</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
</blacklist>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
</php>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
<log type="coverage-html" target="build/html/coverage" showUncoveredFiles="true"/>
<log type="coverage-clover" target="build/logs/coverage.xml" showUncoveredFiles="true"/>
</logging>
<listeners>
<listener class="Mockery\Adapter\Phpunit\TestListener" file="./vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"></listener>
</listeners>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<report>
<clover outputFile="build/logs/coverage.xml"/>
<html outputDirectory="build/html/coverage"/>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>
<testsuites>
<testsuite name="Library Test Suite">
<directory suffix=".php">./tests/Adapter</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
</php>
<logging/>
<listeners>
<listener class="Mockery\Adapter\Phpunit\TestListener" file="./vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"/>
</listeners>
</phpunit>
4 changes: 2 additions & 2 deletions src/VfsFilesystemServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @codeCoverageIgnore
*/
class VfsFilesystemServiceProvider extends ServiceProvider {
class VfsFilesystemServiceProvider extends ServiceProvider {

//boot
public function boot()
Expand All @@ -33,4 +33,4 @@ public function register()
}


}
}
45 changes: 23 additions & 22 deletions src/VirtualFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use SplFileInfo;
Expand All @@ -20,7 +21,8 @@
* @property int $link_handling
* @property array $dirStructure
*/
class VirtualFilesystemAdapter extends Local {
class VirtualFilesystemAdapter extends LocalFilesystemAdapter
{

/**
* This adapters configuration
Expand All @@ -33,21 +35,21 @@ class VirtualFilesystemAdapter extends Local {
* @var array
*/
protected $defaultConfig = [
'dir_name' => 'root',
'dir_name' => 'root',
'dir_permissions' => 0755,
'dir_structure' => [],
'write_flags' => LOCK_EX,
'link_handling' => self::DISALLOW_LINKS,
'permissions' => [
'dir_structure' => [],
'write_flags' => 0, //LOCAK_EX doesn't work in this scenario
'link_handling' => self::DISALLOW_LINKS,
'permissions' => [
'file' => [
'public' => 0644,
'public' => 0644,
'private' => 0600,
],
'dir' => [
'public' => 0755,
'dir' => [
'public' => 0755,
'private' => 0700,
]
]
],
],
];

/**
Expand All @@ -63,7 +65,8 @@ public function __construct(array $config = [])
{
$this->config = collect(array_replace_recursive($this->defaultConfig, $config));
$this->vfsStreamDir = vfsStream::setup($this->dirName, $this->dirPermissions, $this->dirStructure);
parent::__construct($this->vfsStreamDir->url(), $this->writeFlags, $this->linkHandling, $this->permissions);

parent::__construct($this->vfsStreamDir->url(), PortableVisibilityConverter::fromArray($this->permissions), $this->writeFlags, $this->linkHandling);
}

/**
Expand Down Expand Up @@ -93,7 +96,7 @@ public function __get($name)
{
$response = $this->config->get(Str::snake($name));

if (is_null($response)){
if (is_null($response)) {
throw new \InvalidArgumentException(sprintf('%s is not a valid field.', $name));
}

Expand All @@ -116,29 +119,27 @@ public function getVfsStreamDir()
* @return void
* @throws \Exception in case the root directory can not be created
*/
protected function ensureDirectory($path)
protected function ensureDirectoryExists(string $dirname, int $visibility): void
{
if ($path === $this->dirName){
if ($dirname === $this->dirName) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
parent::ensureDirectory($path);
parent::ensureDirectoryExists($dirname, $visibility);
}

/**
* vfsStream has issues with SplFileInfo::getRealPath(), so we will just use
* SplFileInfo::getPathname()
* @param SplFileInfo $file
*/
protected function deleteFileInfoObject(SplFileInfo $file)
protected function deleteFileInfoObject(SplFileInfo $file): bool
{
if ($file->getType() === 'dir') {
rmdir($file->getPathname());

return;
return @rmdir($file->getPathname());
}

unlink($file->getPathname());
return @unlink($file->getPathname());
}
}
41 changes: 22 additions & 19 deletions tests/Adapter/BasicTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php namespace STS\Filesystem;

use Illuminate\Support\Str;
use League\Flysystem\Adapter\Local;
use League\Flysystem\FileAttributes;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\Filesystem;
use League\Flysystem\StorageAttributes;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

Expand All @@ -13,7 +15,7 @@ class BasicTest extends TestCase {
'dir_permissions' => 0700,
'dir_structure' => [],
'write_flags' => 0,
'link_handling' => Local::DISALLOW_LINKS,
'link_handling' => LocalFilesystemAdapter::DISALLOW_LINKS,
'permissions' => [
'file' => [
'public' => 0660,
Expand Down Expand Up @@ -71,26 +73,27 @@ public function throws_exception_on_bad_parameter(){

/** @test */
public function setup_new_vfs_directory_structure(){
$filesystem = new Filesystem(new VirtualFilesystemAdapter());
$filesystem->put('foo/bar/tile1.txt', 'FooBar');
$filesystem = new Filesystem($adapter = new VirtualFilesystemAdapter());
$filesystem->write('foo/bar/tile1.txt', 'FooBar');

$expected_1 = [
"type" => "file",
"path" => "foo/bar/tile1.txt",
"timestamp" => 1488331955,
"size" => 6,
"dirname" => "foo/bar",
"basename" => "tile1.txt",
"extension" => "txt",
"filename" => "tile1",
"file_size" => 6,
//"dirname" => "foo/bar",
//"basename" => "tile1.txt",
//"extension" => "txt",
//"filename" => "tile1",
];
$actual_1 = $filesystem->listContents('foo/bar')[0];
$actual_1 = $filesystem->listContents('foo/bar')->toArray()[0];

collect($expected_1)->each(
function ($value, $key) use ($actual_1) {
if ($key === 'timestamp'){
return;
}
/** @var FileAttributes $actual_1 */
$this->assertEquals($value, $actual_1[$key]);
}
);
Expand All @@ -107,26 +110,26 @@ function ($value, $key) use ($actual_1) {
]
];

$config = $filesystem->getAdapter()->getConfig();
$config = $adapter->getConfig();
$config['dir_structure'] = $newStructure;
vfsStream::setup($config['dir_name'], $config['dir_permissions'], $config['dir_structure']);
// Ensure the old file system is gone.
$this->assertFalse($filesystem->has('foo/bar'));
$this->assertFalse($filesystem->fileExists('foo/bar'));

$this->assertTrue($filesystem->has('Core/AbstractFactory/test.php'));
$this->assertTrue($filesystem->fileExists('Core/AbstractFactory/test.php'));
$this->assertEquals('some other text content', $filesystem->read('Core/AbstractFactory/other.php'));
$this->assertEquals('some bad voodoo', $filesystem->read('Core/somethingwicked.php'));
}

/** @test */
public function setup_new_vfs_from_local_filesystem()
{
$filesystem = new Filesystem(new VirtualFilesystemAdapter());
vfsStream::copyFromFileSystem(__DIR__ . '/../resources/filesystemcopy', $filesystem->getAdapter()->getVfsStreamDir(), 3145728);
$filesystem = new Filesystem($adapter = new VirtualFilesystemAdapter());
vfsStream::copyFromFileSystem(__DIR__ . '/../resources/filesystemcopy', $adapter->getVfsStreamDir(), 3145728);

$this->assertTrue($filesystem->has('withSubfolders/subfolder2/multipage2.pdf'));
$this->assertEquals('application/pdf', $filesystem->getMimetype('withSubfolders/subfolder2/multipage2.pdf'));
$this->assertEquals('image/tiff', $filesystem->getMimetype('withSubfolders/subfolder2/one.TIF'));
$this->assertEquals('571382', $filesystem->getSize('withSubfolders/subfolder2/singlepage1.pdf'));
$this->assertTrue($filesystem->fileExists('withSubfolders/subfolder2/multipage2.pdf'));
$this->assertEquals('application/pdf', $filesystem->mimetype('withSubfolders/subfolder2/multipage2.pdf'));
$this->assertEquals('image/tiff', $filesystem->mimetype('withSubfolders/subfolder2/one.TIF'));
$this->assertEquals('571382', $filesystem->fileSize('withSubfolders/subfolder2/singlepage1.pdf'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
{
function file_put_contents($name)
{
if (strpos($name, 'pleasefail') !== false) {
if (str_contains($name, 'pleasefail')) {
return false;
}
return call_user_func_array('file_put_contents', func_get_args());
return file_put_contents(...func_get_args());
}
function file_get_contents($name)
{
if (strpos($name, 'pleasefail') !== false) {
if (str_contains($name, 'pleasefail')) {
return false;
}
return call_user_func_array('file_get_contents', func_get_args());
return file_get_contents(...func_get_args());
}
}

Expand All @@ -30,7 +30,7 @@ public function ensure_we_fail_on_all_of_these()
{
$adapter = new VirtualFilesystemAdapter();
$this->assertFalse($adapter->write('pleasefail.txt', 'content', new Config()));
$this->assertFalse($adapter->update('pleasefail.txt', 'content', new Config()));
$this->assertFalse($adapter->write('pleasefail.txt', 'content', new Config()));
$this->assertFalse($adapter->read('pleasefail.txt'));
$this->assertFalse($adapter->deleteDir('non-existing'));
}
Expand Down
Loading

0 comments on commit aba84f6

Please sign in to comment.