From 1ca875fbca9fdae4d3eedc98aa4463d3ab568ecd Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Mon, 31 Aug 2020 06:20:02 +0200 Subject: [PATCH] Make createLocalViewFactory compatible with Laravel 8 (#1026) * Make createLocalViewFactory compatible with Laravel 8 Fixes https://github.com/barryvdh/laravel-ide-helper/issues/1024 * tests: move up mockFilesystem so it get be re-used in other tests Note: removed `$this->mockOutput = '';` as it was a no-op, that property doesn't exist (it was probably a typo, but I guess we don't need it anyway) * tests: show basic features for ide-helper:meta working --- src/IdeHelperServiceProvider.php | 7 ++- tests/Console/MetaCommand/MetaCommandTest.php | 51 +++++++++++++++++++ .../ModelsCommand/AbstractModelsCommand.php | 31 ----------- tests/TestCase.php | 30 +++++++++++ 4 files changed, 87 insertions(+), 32 deletions(-) create mode 100644 tests/Console/MetaCommand/MetaCommandTest.php diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index 2c9ed43..eb7abec 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -15,6 +15,7 @@ use Barryvdh\LaravelIdeHelper\Console\EloquentCommand; use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; use Barryvdh\LaravelIdeHelper\Console\MetaCommand; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; +use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\PhpEngine; @@ -116,7 +117,11 @@ class IdeHelperServiceProvider extends ServiceProvider { $resolver = new EngineResolver(); $resolver->register('php', function () { - return new PhpEngine(); + if ((int) Application::VERSION < 8) { + return new PhpEngine(); + } + + return new PhpEngine($this->app['files']); }); $finder = new FileViewFinder($this->app['files'], [__DIR__ . '/../resources/views']); $factory = new Factory($resolver, $finder, $this->app['events']); diff --git a/tests/Console/MetaCommand/MetaCommandTest.php b/tests/Console/MetaCommand/MetaCommandTest.php new file mode 100644 index 0000000..13c6671 --- /dev/null +++ b/tests/Console/MetaCommand/MetaCommandTest.php @@ -0,0 +1,51 @@ +mockFilesystem(); + + /** @var Filesystem|MockInterface $mockFileSystem */ + $mockFileSystem = $this->app->make(Filesystem::class); + $this->instance('files', $mockFileSystem); + + $mockFileSystem + ->shouldReceive('getRequire') + ->andReturnUsing(function ($__path, $__data) { + return (static function () use ($__path, $__data) { + extract($__data, EXTR_SKIP); + + return require $__path; + })(); + }); + + $this->artisan('ide-helper:meta'); + + // We're not testing the whole file, just some basic structure elements + self::assertStringContainsString("namespace PHPSTORM_META {\n", $this->mockFilesystemOutput); + self::assertStringContainsString("PhpStorm Meta file, to provide autocomplete information for PhpStorm\n", $this->mockFilesystemOutput); + self::assertStringContainsString('override(', $this->mockFilesystemOutput); + } + + /** + * Get package providers. + * + * @param \Illuminate\Foundation\Application $app + * + * @return array + */ + protected function getPackageProviders($app) + { + return [IdeHelperServiceProvider::class]; + } +} diff --git a/tests/Console/ModelsCommand/AbstractModelsCommand.php b/tests/Console/ModelsCommand/AbstractModelsCommand.php index d694c43..e52c9e6 100644 --- a/tests/Console/ModelsCommand/AbstractModelsCommand.php +++ b/tests/Console/ModelsCommand/AbstractModelsCommand.php @@ -7,13 +7,9 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand; use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider; use Barryvdh\LaravelIdeHelper\Tests\SnapshotPhpDriver; use Barryvdh\LaravelIdeHelper\Tests\TestCase; -use Illuminate\Filesystem\Filesystem; -use Mockery; abstract class AbstractModelsCommand extends TestCase { - protected $mockFilesystemOutput; - protected function setUp(): void { parent::setUp(); @@ -57,33 +53,6 @@ abstract class AbstractModelsCommand extends TestCase $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()); diff --git a/tests/TestCase.php b/tests/TestCase.php index e9e35e3..a44f35e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace Barryvdh\LaravelIdeHelper\Tests; use Illuminate\Console\Command; +use Illuminate\Filesystem\Filesystem; +use Mockery; use Orchestra\Testbench\TestCase as BaseTestCase; use Spatie\Snapshots\MatchesSnapshots; use Symfony\Component\Console\Tester\CommandTester; @@ -13,6 +15,8 @@ abstract class TestCase extends BaseTestCase { use MatchesSnapshots; + protected $mockFilesystemOutput; + /** * The `CommandTester` is directly returned, use methods like * `->getDisplay()` or `->getStatusCode()` on it. @@ -50,4 +54,30 @@ abstract class TestCase extends BaseTestCase { $this->assertMatchesSnapshot($actualContent, new SnapshotTxtDriver()); } + + protected function mockFilesystem() + { + $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); + $this->instance('files', $mockFilesystem); + } }