Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Oct 25, 2023
1 parent 0684589 commit 837492b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
14 changes: 8 additions & 6 deletions src/SonsOfPHP/Component/Filesystem/Adapter/InMemoryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

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

/**
Expand All @@ -18,7 +20,7 @@ final class InMemoryAdapter implements AdapterInterface
{
private array $files = [];

public function add(string $path, mixed $contents): void
public function add(string $path, mixed $contents, ?ContextInterface $context = null): void
{
$path = $this->normalizePath($path);

Expand All @@ -29,7 +31,7 @@ public function add(string $path, mixed $contents): void
$this->files[$path] = $contents;
}

public function get(string $path): string
public function get(string $path, ?ContextInterface $context = null): string
{
$path = $this->normalizePath($path);

Expand All @@ -40,7 +42,7 @@ public function get(string $path): string
return $this->files[$path];
}

public function remove(string $path): void
public function remove(string $path, ?ContextInterface $context = null): void
{
$path = $this->normalizePath($path);

Expand All @@ -58,15 +60,15 @@ public function copy(string $source, string $destination): void
public function move(string $source, string $destination): void
{
$this->copy($source, $destination);
$this->delete($source);
$this->remove($source);
}

public function has(string $path): bool
public function has(string $path, ?ContextInterface $context = null): bool
{
return $this->isFile($path) || $this->isDirectory($path);
}

public function isFile(string $path): bool
public function isFile(string $path, ?ContextInterface $context = null): bool
{
$path = $this->normalizePath($path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ public function testItHasTheCorrectInterface(): void
}

/**
* @covers ::write
* @covers ::read
* @covers ::add
* @covers ::get
*/
public function testItCanWriteAFileAndReadItLater(): void
{
$adapter = new InMemoryAdapter();

$contents = 'Pretend this is the contents of a file';

$adapter->write('/path/to/test.txt', $contents);
$this->assertSame($contents, $adapter->read('/path/to/test.txt'));
$adapter->add('/path/to/test.txt', $contents);
$this->assertSame($contents, $adapter->get('/path/to/test.txt'));
}

/**
* @covers ::read
* @covers ::get
*/
public function testItWillThrowExceptionWhenFileNotFound(): void
{
$adapter = new InMemoryAdapter();
$this->expectException(UnableToReadFileException::class);
$adapter->read('test.txt');
$adapter->get('test.txt');
}

/**
* @covers ::delete
* @covers ::remove
*/
public function testItCanDeleteFile(): void
{
$adapter = new InMemoryAdapter();
$adapter->write('/path/to/test.txt', 'testing');
$this->assertSame('testing', $adapter->read('/path/to/test.txt'));
$adapter->add('/path/to/test.txt', 'testing');
$this->assertSame('testing', $adapter->get('/path/to/test.txt'));

$adapter->delete('/path/to/test.txt');
$adapter->remove('/path/to/test.txt');
$this->expectException(UnableToReadFileException::class);
$adapter->read('/path/to/test.txt');
$adapter->get('/path/to/test.txt');
}

/**
Expand All @@ -71,10 +71,10 @@ public function testItCanDeleteFile(): void
public function testItCanCopyFile(): void
{
$adapter = new InMemoryAdapter();
$adapter->write('test.txt', 'testing');
$adapter->add('test.txt', 'testing');
$adapter->copy('test.txt', 'test2.txt');
$this->assertSame('testing', $adapter->read('test.txt'));
$this->assertSame('testing', $adapter->read('test2.txt'));
$this->assertSame('testing', $adapter->get('test.txt'));
$this->assertSame('testing', $adapter->get('test2.txt'));
}

/**
Expand All @@ -83,36 +83,36 @@ public function testItCanCopyFile(): void
public function testItCanMoveFile(): void
{
$adapter = new InMemoryAdapter();
$adapter->write('test.txt', 'testing');
$adapter->add('test.txt', 'testing');
$adapter->move('test.txt', 'test2.txt');
$this->assertSame('testing', $adapter->read('test2.txt'));
$this->assertSame('testing', $adapter->get('test2.txt'));
$this->expectException(UnableToReadFileException::class);
$adapter->read('test.txt');
$adapter->get('test.txt');
}

/**
* @covers ::read
* @covers ::get
*/
public function testItCanSupportStream(): void
{
$adapter = new InMemoryAdapter();
$stream = fopen('php://memory', 'w+');
fwrite($stream, 'Just a test');
$adapter->write('test.txt', $stream);
$adapter->add('test.txt', $stream);
fclose($stream);

$this->assertSame('Just a test', $adapter->read('test.txt'));
$this->assertSame('Just a test', $adapter->get('test.txt'));
}

/**
* @covers ::exists
* @covers ::has
*/
public function testItCanCheckIfFilesExist(): void
{
$adapter = new InMemoryAdapter();
$this->assertFalse($adapter->exists('test.txt'));
$adapter->write('test.txt', 'testing');
$this->assertTrue($adapter->exists('test.txt'));
$this->assertFalse($adapter->has('test.txt'));
$adapter->add('test.txt', 'testing');
$this->assertTrue($adapter->has('test.txt'));
}

/**
Expand All @@ -121,7 +121,7 @@ public function testItCanCheckIfFilesExist(): void
public function testItCanCheckIfIsFile(): void
{
$adapter = new InMemoryAdapter();
$adapter->write('/path/to/file.txt', 'testing');
$adapter->add('/path/to/file.txt', 'testing');

$this->assertTrue($adapter->isFile('/path/to/file.txt'));
}
Expand All @@ -132,7 +132,7 @@ public function testItCanCheckIfIsFile(): void
public function testItCanCheckIfIsDirectory(): void
{
$adapter = new InMemoryAdapter();
$adapter->write('/path/to/file.txt', 'testing');
$adapter->add('/path/to/file.txt', 'testing');

$this->assertTrue($adapter->isDirectory('/path/to'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
final class FilesystemTest extends TestCase
{
private AdapterInterface&MockObject $adapter;
private AdapterInterface|MockObject $adapter;

protected function setUp(): void
{
Expand Down

0 comments on commit 837492b

Please sign in to comment.