Skip to content

Commit

Permalink
test(overrides): Added a test for the filesystem manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Jan 25, 2025
1 parent 291c153 commit b3dda9c
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions tests/Unit/Overrides/SproutFilesystemManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);

namespace Sprout\Tests\Unit\Overrides;

use Illuminate\Config\Repository;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Filesystem\LocalFilesystemAdapter;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Overrides\Filesystem\SproutFilesystemManager;
use Sprout\Tests\Unit\UnitTestCase;

class SproutFilesystemManagerTest extends UnitTestCase
{
#[Test]
public function canBySyncedFromOriginal(): void
{
$original = Mockery::mock(FilesystemManager::class);

$app = Mockery::mock(Application::class);

$sprout = new SproutFilesystemManager($app, $original);

$this->assertTrue($sprout->wasSyncedFromOriginal());
}

#[Test]
public function addsNameWhenResolvingDriver(): void
{
$app = Mockery::mock(Application::class, static function (Mockery\MockInterface $mock) {
$mock->shouldReceive('offsetGet')
->with('config')
->andReturn(
Mockery::mock(Repository::class, static function (Mockery\MockInterface $mock) {
$mock->shouldReceive('offsetGet')
->with('filesystems.disks.fake-disk')
->andReturn([
'driver' => 'fake',
])
->once();
})
)
->once();
});

$sprout = new SproutFilesystemManager($app);

$sprout->extend('fake', function (Application $app, array $config) {
$this->assertArrayHasKey('name', $config);
$this->assertSame('fake-disk', $config['name']);

return Mockery::mock(Filesystem::class);
});

$sprout->disk('fake-disk');
}

#[Test]
public function throwsAnExceptionWhenTheresNoDriverInTheConfig(): void
{
$app = Mockery::mock(Application::class, static function (Mockery\MockInterface $mock) {
$mock->shouldReceive('offsetGet')
->with('config')
->andReturn(
Mockery::mock(Repository::class, static function (Mockery\MockInterface $mock) {
$mock->shouldReceive('offsetGet')
->with('filesystems.disks.fake-disk')
->andReturn([
'name' => 'hi',
])
->once();
})
)
->once();
});

$sprout = new SproutFilesystemManager($app);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Disk [fake-disk] does not have a configured driver.');

$sprout->disk('fake-disk');
}

#[Test]
public function throwsAnExceptionWhenTheresNoConfig(): void
{
$app = Mockery::mock(Application::class, static function (Mockery\MockInterface $mock) {
$mock->shouldReceive('offsetGet')
->with('config')
->andReturn(
Mockery::mock(Repository::class, static function (Mockery\MockInterface $mock) {
$mock->shouldReceive('offsetGet')
->with('filesystems.disks.fake-disk')
->andReturn(null)
->once();
})
)
->once();
});

$sprout = new SproutFilesystemManager($app);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Disk [fake-disk] does not have a configured driver.');

$sprout->disk('fake-disk');
}

#[Test]
public function canCreateDisksNormally(): void
{
$app = Mockery::mock($this->app, static function (Mockery\MockInterface $mock) {
$mock->makePartial();
});

$sprout = new SproutFilesystemManager($app);

$this->assertInstanceOf(LocalFilesystemAdapter::class, $sprout->disk('local'));
}

#[Test]
public function throwsAnExceptionForDriversThatDoNotExist(): void
{
$app = Mockery::mock($this->app, static function (Mockery\MockInterface $mock) {
$mock->makePartial();
});

$app['config']['filesystems.disks.local.driver'] = 'fake';

$sprout = new SproutFilesystemManager($app);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Driver [fake] is not supported.');

$sprout->disk('local');
}
}

0 comments on commit b3dda9c

Please sign in to comment.