Test generator, update constructor

This commit is contained in:
Barry vd. Heuvel
2024-12-28 19:22:01 +01:00
parent 0abcb7497e
commit f5d940fe0f
9 changed files with 22779 additions and 59 deletions
+5
View File
@@ -81,7 +81,12 @@ class Alias
$this->detectExtendsNamespace(); $this->detectExtendsNamespace();
if (!empty($this->namespace)) { if (!empty($this->namespace)) {
try {
$this->classAliases = (new UsesResolver())->loadFromClass($this->root); $this->classAliases = (new UsesResolver())->loadFromClass($this->root);
} catch (Throwable $e) {
$this->classAliases = [];
}
//Create a DocBlock and serializer instance //Create a DocBlock and serializer instance
$this->phpdoc = new DocBlock(new ReflectionClass($alias), new Context($this->namespace, $this->classAliases)); $this->phpdoc = new DocBlock(new ReflectionClass($alias), new Context($this->namespace, $this->classAliases));
+6 -6
View File
@@ -14,7 +14,9 @@ namespace Barryvdh\LaravelIdeHelper\Console;
use Barryvdh\LaravelIdeHelper\Eloquent; use Barryvdh\LaravelIdeHelper\Eloquent;
use Barryvdh\LaravelIdeHelper\Generator; use Barryvdh\LaravelIdeHelper\Generator;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Factory;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
@@ -45,7 +47,7 @@ class GeneratorCommand extends Command
/** @var Filesystem */ /** @var Filesystem */
protected $files; protected $files;
/** @var \Illuminate\View\Factory */ /** @var Factory */
protected $view; protected $view;
protected $onlyExtend; protected $onlyExtend;
@@ -55,14 +57,12 @@ class GeneratorCommand extends Command
* *
* @param \Illuminate\Config\Repository $config * @param \Illuminate\Config\Repository $config
* @param Filesystem $files * @param Filesystem $files
* @param \Illuminate\View\Factory $view * @param Factory $view
*/ */
public function __construct( public function __construct(
/*ConfigRepository */ Repository $config,
$config,
Filesystem $files, Filesystem $files,
/* Illuminate\View\Factory */ Factory $view
$view
) { ) {
$this->config = $config; $this->config = $config;
$this->files = $files; $this->files = $files;
+13 -6
View File
@@ -13,6 +13,9 @@ namespace Barryvdh\LaravelIdeHelper\Console;
use Barryvdh\LaravelIdeHelper\Factories; use Barryvdh\LaravelIdeHelper\Factories;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\View\Factory;
use Illuminate\Filesystem\Filesystem;
use RuntimeException; use RuntimeException;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -41,10 +44,10 @@ class MetaCommand extends Command
/** @var \Illuminate\Contracts\Filesystem\Filesystem */ /** @var \Illuminate\Contracts\Filesystem\Filesystem */
protected $files; protected $files;
/** @var \Illuminate\Contracts\View\Factory */ /** @var Factory */
protected $view; protected $view;
/** @var \Illuminate\Contracts\Config\Repository */ /** @var Repository */
protected $config; protected $config;
protected $methods = [ protected $methods = [
@@ -63,11 +66,15 @@ class MetaCommand extends Command
/** /**
* *
* @param \Illuminate\Contracts\Filesystem\Filesystem $files * @param Filesystem $files
* @param \Illuminate\Contracts\View\Factory $view * @param Factory $view
* @param \Illuminate\Contracts\Config\Repository $config * @param Repository $config
*/ */
public function __construct($files, $view, $config) public function __construct(
Filesystem $files,
Factory $view,
Repository $config
)
{ {
$this->files = $files; $this->files = $files;
$this->view = $view; $this->view = $view;
+17 -33
View File
@@ -64,41 +64,20 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
{ {
$configPath = __DIR__ . '/../config/ide-helper.php'; $configPath = __DIR__ . '/../config/ide-helper.php';
$this->mergeConfigFrom($configPath, 'ide-helper'); $this->mergeConfigFrom($configPath, 'ide-helper');
$localViewFactory = $this->createLocalViewFactory();
$this->app->singleton( $this->app->when([GeneratorCommand::class, MetaCommand::class])
'command.ide-helper.generate', ->needs(\Illuminate\Contracts\View\Factory::class)
function ($app) use ($localViewFactory) { ->give(function () {
return new GeneratorCommand($app['config'], $app['files'], $localViewFactory); return $this->createLocalViewFactory();
} });
);
$this->app->singleton(
'command.ide-helper.models',
function ($app) {
return new ModelsCommand($app['files']);
}
);
$this->app->singleton(
'command.ide-helper.meta',
function ($app) use ($localViewFactory) {
return new MetaCommand($app['files'], $localViewFactory, $app['config']);
}
);
$this->app->singleton(
'command.ide-helper.eloquent',
function ($app) {
return new EloquentCommand($app['files']);
}
);
$this->commands( $this->commands(
'command.ide-helper.generate', [
'command.ide-helper.models', GeneratorCommand::class,
'command.ide-helper.meta', ModelsCommand::class,
'command.ide-helper.eloquent' MetaCommand::class,
EloquentCommand::class,
]
); );
} }
@@ -109,7 +88,12 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
*/ */
public function provides() public function provides()
{ {
return ['command.ide-helper.generate', 'command.ide-helper.models']; return [
GeneratorCommand::class,
ModelsCommand::class,
MetaCommand::class,
EloquentCommand::class
];
} }
/** /**
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand;
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
use Barryvdh\LaravelIdeHelper\Tests\SnapshotPhpDriver;
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
abstract class AbstractGeneratorCommand extends TestCase
{
protected function setUp(): void
{
parent::setUp();
// $this->artisan('key:gen');
$this->mockFilesystem();
}
/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [IdeHelperServiceProvider::class];
}
protected function assertMatchesMockedSnapshot()
{
$this->assertMatchesSnapshot($this->mockFilesystemOutput, new SnapshotPhpDriver());
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\GenerateIdeHelper;
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\AbstractGeneratorCommand;
class Test extends AbstractGeneratorCommand
{
public function test(): void
{
$command = $this->app->make(GeneratorCommand::class);
$tester = $this->runCommand($command);
$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay());
$this->assertMatchesMockedSnapshot();
}
}
File diff suppressed because it is too large Load Diff
@@ -12,13 +12,18 @@ use stdClass;
class MetaCommandTest extends TestCase class MetaCommandTest extends TestCase
{ {
protected function setUp(): void
{
parent::setUp();
$this->mockFilesystem();
}
public function testCommand(): void public function testCommand(): void
{ {
$this->mockFilesystem();
/** @var Filesystem|MockInterface $mockFileSystem */ /** @var Filesystem|MockInterface $mockFileSystem */
$mockFileSystem = $this->app->make(Filesystem::class); $mockFileSystem = $this->app->make(Filesystem::class);
$this->instance('files', $mockFileSystem); $this->instance(Filesystem::class, $mockFileSystem);
$mockFileSystem $mockFileSystem
->shouldReceive('getRequire') ->shouldReceive('getRequire')
@@ -49,11 +54,9 @@ class MetaCommandTest extends TestCase
return new stdClass(); return new stdClass();
}); });
$this->mockFilesystem();
/** @var Filesystem|MockInterface $mockFileSystem */ /** @var Filesystem|MockInterface $mockFileSystem */
$mockFileSystem = $this->app->make(Filesystem::class); $mockFileSystem = $this->app->make(Filesystem::class);
$this->instance('files', $mockFileSystem); $this->instance(Filesystem::class, $mockFileSystem);
$mockFileSystem $mockFileSystem
->shouldReceive('getRequire') ->shouldReceive('getRequire')
+1 -7
View File
@@ -57,13 +57,7 @@ abstract class TestCase extends BaseTestCase
protected function mockFilesystem() protected function mockFilesystem()
{ {
$mockFilesystem = Mockery::mock(Filesystem::class); $mockFilesystem = Mockery::mock(Filesystem::class)->makePartial();
$mockFilesystem
->shouldReceive('get')
->andReturnUsing(function ($file) {
return file_get_contents($file);
});
$mockFilesystem $mockFilesystem
->shouldReceive('put') ->shouldReceive('put')