Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Oct 26, 2023
1 parent 26e4568 commit 93284dc
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;

/**
Expand Down Expand Up @@ -32,7 +33,7 @@ public function get(string $path, ?ContextInterface $context = null): mixed
{
foreach ($this->adapters as $adapter) {
if ($adapter->has($path, $context)) {
return $this->adapter->get($path, $context);
return $adapter->get($path, $context);
}
}

Expand All @@ -42,14 +43,14 @@ public function get(string $path, ?ContextInterface $context = null): mixed
public function remove(string $path, ?ContextInterface $context = null): void
{
foreach ($this->adapters as $adapter) {
$this->adapter->remove($path, $context);
$adapter->remove($path, $context);
}
}

public function has(string $path, ?ContextInterface $context = null): bool
{
foreach ($this->adapters as $adapter) {
if ($this->adapter->has($path, $context)) {
if ($adapter->has($path, $context)) {
return true;
}
}
Expand All @@ -60,7 +61,7 @@ public function has(string $path, ?ContextInterface $context = null): bool
public function isFile(string $path, ?ContextInterface $context = null): bool
{
foreach ($this->adapters as $adapter) {
if ($this->adapter->isFile($path, $context)) {
if ($adapter->isFile($path, $context)) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public function copy(string $source, string $destination, ?ContextInterface $con
public function isDirectory(string $path, ?ContextInterface $context = null): bool
{
if ($this->adapter instanceof DirectoryAwareInterface) {
return $this->adapter->isDirectory($path);
return $this->adapter->isDirectory($path, $context);
}

return false;
return !$this->isFile($path, $context);
}

public function move(string $source, string $destination, ?ContextInterface $context = null): void
Expand Down
139 changes: 138 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Tests/Adapter/ChainAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\ChainAdapter;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;

/**
* @coversDefaultClass \SonsOfPHP\Component\Filesystem\Adapter\ChainAdapter
Expand All @@ -17,13 +22,145 @@ final class ChainAdapterTest extends TestCase
{
private iterable $adapters = [];

public function setUp(): void
{
$this->adapters[] = new InMemoryAdapter();
}

/**
* @coversNothing
* @covers ::__construct
*/
public function testItHasTheCorrectInterface(): void
{
$adapter = new ChainAdapter($this->adapters);

$this->assertInstanceOf(AdapterInterface::class, $adapter);
$this->assertInstanceOf(CopyAwareInterface::class, $adapter);
$this->assertInstanceOf(DirectoryAwareInterface::class, $adapter);
$this->assertInstanceOf(MoveAwareInterface::class, $adapter);
}

/**
* @covers ::add
*/
public function testItCanAdd(): void
{
$adp = $this->createMock(AdapterInterface::class);
$adp->expects($this->once())->method('add');
$this->adapters[] = $adp;

$adapter = new ChainAdapter($this->adapters);
$adapter->add('/path/to/file.txt', 'testing');
}

/**
* @covers ::get
*/
public function testItCanGetFile(): void
{
$adp = $this->createMock(AdapterInterface::class);
$adp->method('has')->willReturn(true);
$adp->expects($this->once())->method('get');
$this->adapters[] = $adp;

$adapter = new ChainAdapter($this->adapters);
$adapter->get('/path/to/file.txt');
}

/**
* @covers ::get
*/
public function testItWillThrowExceptionWhenFileNotFound(): void
{
$adp = $this->createMock(AdapterInterface::class);
$adp->method('has')->willReturn(false);
$this->adapters[] = $adp;

$adapter = new ChainAdapter($this->adapters);
$this->expectException(FileNotFoundException::class);
$adapter->get('/path/to/file.txt');
}

/**
* @covers ::remove
*/
public function testItCanRemoveFiles(): void
{
$adapter = new ChainAdapter($this->adapters);
$this->assertFalse($adapter->has('/path/to/file.txt'));
$adapter->add('/path/to/file.txt', 'testing');
$this->assertTrue($adapter->has('/path/to/file.txt'));
$adapter->remove('/path/to/file.txt');
$this->assertFalse($adapter->has('/path/to/file.txt'));
}

/**
* @covers ::has
*/
public function testItCanHas(): void
{
$adapter = new ChainAdapter($this->adapters);
$this->assertFalse($adapter->has('/path/to/file.txt'));
$adapter->add('/path/to/file.txt', 'testing');
$this->assertTrue($adapter->has('/path/to/file.txt'));
}

/**
* @covers ::isFile
*/
public function testItCanIsFile(): void
{
$adapter = new ChainAdapter($this->adapters);
$this->assertFalse($adapter->isFile('/path/to/file.txt'));
$adapter->add('/path/to/file.txt', 'testing');
$this->assertFalse($adapter->isFile('/path/to'));
$this->assertTrue($adapter->isFile('/path/to/file.txt'));
}

/**
* @covers ::copy
*/
public function testItCanCopyFile(): void
{
$adapter = new ChainAdapter($this->adapters);
$this->assertFalse($adapter->has('/path/to/source.txt'));
$this->assertFalse($adapter->has('/path/to/destination.txt'));

$adapter->add('/path/to/source.txt', 'testing');
$this->assertTrue($adapter->has('/path/to/source.txt'));
$this->assertFalse($adapter->has('/path/to/destination.txt'));

$adapter->copy('/path/to/source.txt', '/path/to/destination.txt');
$this->assertTrue($adapter->has('/path/to/source.txt'));
$this->assertTrue($adapter->has('/path/to/destination.txt'));
}

/**
* @covers ::isDirectory
*/
public function testItCanCheckIfIsDirectoryExists(): void
{
$adapter = new ChainAdapter($this->adapters);
$this->assertFalse($adapter->isDirectory('/path/to'));
$adapter->add('/path/to/file.txt', 'testing');
$this->assertTrue($adapter->isDirectory('/path/to'));
}

/**
* @covers ::move
*/
public function testItCanMoveFiles(): void
{
$adapter = new ChainAdapter($this->adapters);
$this->assertFalse($adapter->has('/path/to/source.txt'));
$this->assertFalse($adapter->has('/path/to/destination.txt'));

$adapter->add('/path/to/source.txt', 'testing');
$this->assertTrue($adapter->has('/path/to/source.txt'));
$this->assertFalse($adapter->has('/path/to/destination.txt'));

$adapter->move('/path/to/source.txt', '/path/to/destination.txt');
$this->assertFalse($adapter->has('/path/to/source.txt'));
$this->assertTrue($adapter->has('/path/to/destination.txt'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,51 @@ public function testItHasTheCorrectInterface(): void

$this->assertInstanceOf(AdapterInterface::class, $adapter);
}

/**
* @covers ::add
* @doesNotPerformAssertions
*/
public function testItCanAddFile(): void
{
$adapter = new NullAdapter();
$adapter->add('/path/to/file.txt', 'contents');
}

/**
* @covers ::remove
* @doesNotPerformAssertions
*/
public function testItCanRemoveFiles(): void
{
$adapter = new NullAdapter();
$adapter->remove('/path/to/file.txt');
}

/**
* @covers ::get
*/
public function testItCanGetFileContents(): void
{
$adapter = new NullAdapter();
$this->assertSame('', $adapter->get('/path/to/file.ext'));
}

/**
* @covers ::has
*/
public function testItHasNoFiles(): void
{
$adapter = new NullAdapter();
$this->assertFalse($adapter->has('/path/to/file.ext'));
}

/**
* @covers ::isFile
*/
public function testItReturnsFalseForIsFile(): void
{
$adapter = new NullAdapter();
$this->assertFalse($adapter->isFile('/path/to/file.ext'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace SonsOfPHP\Component\Filesystem\Tests;

use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\ReadOnlyAdapter;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;

/**
* @coversDefaultClass \SonsOfPHP\Component\Filesystem\Adapter\ReadOnlyAdapter
Expand All @@ -23,12 +28,97 @@ public function setUp(): void
}

/**
* @coversNothing
* @covers ::__construct
*/
public function testItHasTheCorrectInterface(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);

$this->assertInstanceOf(AdapterInterface::class, $adapter);
$this->assertInstanceOf(CopyAwareInterface::class, $adapter);
$this->assertInstanceOf(DirectoryAwareInterface::class, $adapter);
$this->assertInstanceOf(MoveAwareInterface::class, $adapter);
}

/**
* @covers ::add
*/
public function testItWillThrowExceptionWhenAddingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$adapter->add('/path/to/file.ext', 'contents');
}

/**
* @covers ::remove
*/
public function testItWillThrowExceptionWhenRemovingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$adapter->remove('/path/to/file.ext');
}

/**
* @covers ::copy
*/
public function testItWillThrowExceptionWhenCopyingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$adapter->copy('/path/to/file.ext', '/path/to/dest.ext');
}

/**
* @covers ::move
*/
public function testItWillThrowExceptionWhenMovingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$adapter->move('/path/to/file.ext', '/path/to/dest.ext');
}

/**
* @covers ::get
*/
public function testItWillGetFileContents(): void
{
$this->adapter->method('get')->willReturn('contents');

$adapter = new ReadOnlyAdapter($this->adapter);
$this->assertSame('contents', $adapter->get('/path/to/file.ext'));
}

/**
* @covers ::has
*/
public function testItCanHas(): void
{
$this->adapter->method('has')->willReturn(true);

$adapter = new ReadOnlyAdapter($this->adapter);
$this->assertTrue($adapter->has('/path/to/file.ext'));
}

/**
* @covers ::isFile
*/
public function testItCanIsFile(): void
{
$this->adapter->method('isFile')->willReturn(true);

$adapter = new ReadOnlyAdapter($this->adapter);
$this->assertTrue($adapter->isFile('/path/to/file.ext'));
}

/**
* @covers ::isDirectory
*/
public function testItCanCheckIfIsDirectory(): void
{
$adapter = new ReadOnlyAdapter(new InMemoryAdapter());
$this->assertFalse($adapter->isDirectory('/path/to/file.ext'));
}
}
Loading

0 comments on commit 93284dc

Please sign in to comment.