Simplify test by mocking filesystem always (#1019)

This commit is contained in:
Barry vd. Heuvel
2020-08-25 17:43:39 +02:00
committed by GitHub
parent c852650d7e
commit 9599cfbbc9
21 changed files with 100 additions and 593 deletions
@@ -4,11 +4,18 @@ declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
use Barryvdh\LaravelIdeHelper\Tests\SnapshotPhpDriver;
use Barryvdh\LaravelIdeHelper\Tests\SnapshotTxtDriver;
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
use Mockery;
abstract class AbstractModelsCommand extends TestCase
{
protected $mockFilesystemOutput;
protected function setUp(): void
{
parent::setUp();
@@ -21,6 +28,22 @@ abstract class AbstractModelsCommand extends TestCase
$this->loadMigrationsFrom(__DIR__ . '/migrations');
$this->artisan('migrate');
$this->mockFilesystem();
}
/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [IdeHelperServiceProvider::class];
}
protected function getEnvironmentSetUp($app)
@@ -35,5 +58,45 @@ abstract class AbstractModelsCommand extends TestCase
'database' => ':memory:',
'prefix' => '',
]);
// Load the Models from the Test dir
$config->set('ide-helper.model_locations', [
dirname((new \ReflectionClass(static::class))->getFileName()) . '/Models',
]);
// Don't override integer -> int for tests
$config->set('ide-helper.type_overrides', []);
}
protected function mockFilesystem()
{
$this->mockOutput = '';
$mockFilesystem = Mockery::mock(Filesystem::class);
$mockFilesystem
->shouldReceive('get')
->andReturnUsing(function ($file) {
return file_get_contents($file);
});
$mockFilesystem
->shouldReceive('put')
->with(
Mockery::any(),
Mockery::any()
)
->andReturnUsing(function ($path, $contents) {
$this->mockFilesystemOutput .= $contents;
return strlen($contents);
});
$this->instance(Filesystem::class, $mockFilesystem);
}
protected function assertMatchesMockedSnapshot()
{
$this->assertMatchesSnapshot($this->mockFilesystemOutput, new SnapshotPhpDriver());
}
}