Skip to content

Commit

Permalink
tests(filesytem): add tests for filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Oct 23, 2020
1 parent e74c68a commit 10edab6
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 7 deletions.
40 changes: 40 additions & 0 deletions tests/Filesystem/FtpTest.php
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ************************ package.
* _____________ _______________
* ______/ \__ _____ ____ ______ / /_ _________
* ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
* __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
* \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
* \_\ /_/_/ /_/
*
* The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
* (c) 2010-2020 http://queryphp.com All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Filesystem;

use League\Flysystem\Filesystem as LeagueFilesystem;
use Leevel\Filesystem\Ftp;
use Tests\TestCase;

class FtpTest extends TestCase
{
public function testBaseUse(): void
{
$this->expectException(\League\Flysystem\ConnectionRuntimeException::class);
$this->expectExceptionMessage(
'Could not connect to host: ftp.example.com, port:21'
);

$ftp = new Ftp();
$this->assertInstanceof(LeagueFilesystem::class, $ftp->getFilesystem());
$ftp->put('hello.txt', 'foo');
}
}
4 changes: 1 addition & 3 deletions tests/Filesystem/LocalTest.php
Expand Up @@ -31,7 +31,6 @@ public function testBaseUse(): void
$local = new Local([
'path' => $path = __DIR__,
]);

$this->assertInstanceof(LeagueFilesystem::class, $local->getFilesystem());

$local->put('hello.txt', 'foo');
Expand All @@ -40,14 +39,13 @@ public function testBaseUse(): void

$this->assertTrue(is_file($file));
$this->assertSame('foo', file_get_contents($file));

unlink($file);
}

public function testPathNotFound(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The local requires path option.');
$this->expectExceptionMessage('The local driver requires path option.');

$local = new Local([
'path' => '',
Expand Down
62 changes: 58 additions & 4 deletions tests/Filesystem/ManagerTest.php
Expand Up @@ -23,6 +23,7 @@
use League\Flysystem\Filesystem as LeagueFilesystem;
use Leevel\Di\Container;
use Leevel\Di\IContainer;
use Leevel\Filesystem\Helper;
use Leevel\Filesystem\Manager;
use Leevel\Option\Option;
use Tests\TestCase;
Expand Down Expand Up @@ -102,12 +103,28 @@ public function testBaseUse(): void

$this->assertTrue(is_file($file));
$this->assertSame('manager', file_get_contents($file));
$this->clearTempDir();
}

unlink($file);
rmdir($path);
public function testZip(): void
{
$manager = $this->createManager('zip');
$this->assertInstanceof(LeagueFilesystem::class, $manager->getFilesystem());
}

protected function createManager(): Manager
public function testFtp(): void
{
$manager = $this->createManager('ftp');
$this->assertInstanceof(LeagueFilesystem::class, $manager->getFilesystem());
}

public function testSftp(): void
{
$manager = $this->createManager('sftp');
$this->assertInstanceof(LeagueFilesystem::class, $manager->getFilesystem());
}

protected function createManager(string $connect = 'local'): Manager
{
$container = new Container();
$manager = new Manager($container);
Expand All @@ -117,12 +134,37 @@ protected function createManager(): Manager

$option = new Option([
'filesystem' => [
'default' => 'local',
'default' => $connect,
'connect' => [
'local' => [
'driver' => 'local',
'path' => __DIR__.'/forManager',
],
'zip' => [
'driver' => 'zip',
'path' => __DIR__.'/forManager2/filesystem.zip',
],
'ftp' => [
'driver' => 'ftp',
'host' => 'ftp.example.com',
'port' => 21,
'username' => 'your-username',
'password' => 'your-password',
'root' => '',
'passive' => true,
'ssl' => false,
'timeout' => 20,
],
'sftp' => [
'driver' => 'sftp',
'host' => 'sftp.example.com',
'port' => 22,
'username' => 'your-username',
'password' => 'your-password',
'root' => '',
'privateKey' => '',
'timeout' => 20,
],
],
],
]);
Expand All @@ -131,4 +173,16 @@ protected function createManager(): Manager

return $manager;
}

protected function clearTempDir(): void
{
$dirs = [
__DIR__.'/forManager',
];
foreach ($dirs as $dir) {
if (is_dir($dir)) {
Helper::deleteDirectory($dir);
}
}
}
}
42 changes: 42 additions & 0 deletions tests/Filesystem/SftpTest.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ************************ package.
* _____________ _______________
* ______/ \__ _____ ____ ______ / /_ _________
* ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
* __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
* \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
* \_\ /_/_/ /_/
*
* The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
* (c) 2010-2020 http://queryphp.com All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Filesystem;

use League\Flysystem\Filesystem as LeagueFilesystem;
use Leevel\Filesystem\Sftp;
use Tests\TestCase;

class SftpTest extends TestCase
{
public function testBaseUse(): void
{
$this->expectException(\League\Flysystem\Sftp\ConnectionErrorException::class);
$this->expectExceptionMessage(
'Could not login with username: your-username, host: sftp.example.com'
);

set_error_handler(function ($type, $msg) {});
$sftp = new Sftp();
$this->assertInstanceof(LeagueFilesystem::class, $sftp->getFilesystem());
$sftp->put('hello.txt', 'foo');
restore_error_handler();
}
}
50 changes: 50 additions & 0 deletions tests/Filesystem/ZipTest.php
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ************************ package.
* _____________ _______________
* ______/ \__ _____ ____ ______ / /_ _________
* ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
* __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
* \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
* \_\ /_/_/ /_/
*
* The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
* (c) 2010-2020 http://queryphp.com All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Filesystem;

use League\Flysystem\Filesystem as LeagueFilesystem;
use Leevel\Filesystem\Zip;
use Tests\TestCase;

class ZipTest extends TestCase
{
public function testBaseUse(): void
{
$zip = new Zip([
'path' => $path = __DIR__.'/hello.zip',
]);
$this->assertInstanceof(LeagueFilesystem::class, $zip->getFilesystem());

$zip->put('hello.txt', 'foo');
$this->assertTrue(is_file($path));
unlink($path);
}

public function testPathNotFound(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The zip driver requires path option.');

$zip = new zip([
'path' => '',
]);
}
}

0 comments on commit 10edab6

Please sign in to comment.