Skip to content

Commit

Permalink
Refactor to unit test and use filesystem mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Sep 9, 2024
1 parent b3f839f commit ccbc74a
Showing 1 changed file with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Unit;

use Hyde\Testing\UnitTestCase;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
Expand All @@ -12,72 +13,82 @@
/**
* @covers \Hyde\Framework\Concerns\InteractsWithDirectories
*/
class InteractsWithDirectoriesConcernTest extends TestCase
class InteractsWithDirectoriesConcernTest extends UnitTestCase
{
use InteractsWithDirectories;

protected function setUp(): void
{
parent::setUp();
protected static bool $needsKernel = true;

File::deleteDirectory(Hyde::path('foo'));
}
/** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */
protected $filesystem;

protected function tearDown(): void
protected function setUp(): void
{
File::deleteDirectory(Hyde::path('foo'));

parent::tearDown();
$this->filesystem = $this->mockFilesystemStrict();
}

public function testMethodsCanBeCalledStatically()
protected function tearDown(): void
{
static::needsDirectory('foo');
$this->assertDirectoryExists(Hyde::path('foo'));

static::needsDirectories(['foo']);
$this->assertDirectoryExists(Hyde::path('foo'));
$this->verifyMockeryExpectations();
}

public function testNeedsDirectoryCreatesTheDirectory()
{
$this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse();
$this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true);
$this->needsDirectory('foo');
$this->assertDirectoryExists(Hyde::path('foo'));
}

public function testNeedsDirectoryCreatesTheDirectoryRecursively()
{
$this->filesystem->expects('exists')->with(Hyde::path('foo/bar/baz'))->andReturnFalse();
$this->filesystem->expects('makeDirectory')->with(Hyde::path('foo/bar/baz'), 0755, true);

$this->needsDirectory('foo/bar/baz');
$this->assertDirectoryExists(Hyde::path('foo/bar/baz'));
}

public function testNeedsDirectoryHandlesExistingDirectory()
{
$this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnTrue()->twice();
$this->filesystem->expects('makeDirectory')->never();

$this->needsDirectory('foo');
$this->needsDirectory('foo');

$this->assertDirectoryExists(Hyde::path('foo'));
}

public function testNeedsDirectoriesCreatesSingleDirectory()
{
$this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse();
$this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true);

$this->needsDirectories(['foo']);
$this->assertDirectoryExists(Hyde::path('foo'));
}

public function testNeedsDirectoriesCreatesMultipleDirectories()
{
$this->needsDirectories(['foo', 'bar']);
$this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse();
$this->filesystem->expects('exists')->with(Hyde::path('bar'))->andReturnFalse();
$this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true);
$this->filesystem->expects('makeDirectory')->with(Hyde::path('bar'), 0755, true);

$this->assertDirectoryExists(Hyde::path('foo'));
$this->assertDirectoryExists(Hyde::path('bar'));

File::deleteDirectory(Hyde::path('bar'));
$this->needsDirectories(['foo', 'bar']);
}

public function testNeedsParentDirectoryCreatesDirectoryForTheParentFile()
{
$this->filesystem->expects('exists')->with(Hyde::path('foo/bar'))->andReturnFalse();
$this->filesystem->expects('makeDirectory')->with(Hyde::path('foo/bar'), 0755, true);

$this->needsParentDirectory(Hyde::path('foo/bar/baz.txt'));
$this->assertDirectoryExists(Hyde::path('foo/bar'));
}

public function testMethodsCanBeCalledStatically()
{
$this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse()->twice();
$this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true)->twice();

static::needsDirectory('foo');
static::needsDirectories(['foo']);
}
}

0 comments on commit ccbc74a

Please sign in to comment.