Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testsuite for Generator, simplify service provider and mock #1635

Merged
merged 7 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ public function __construct($config, $alias, $facade, $magicMethods = [], $inter
$this->detectExtendsNamespace();

if (!empty($this->namespace)) {
$this->classAliases = (new UsesResolver())->loadFromClass($this->root);
try {
$this->classAliases = (new UsesResolver())->loadFromClass($this->root);
} catch (Throwable $e) {
$this->classAliases = [];
}


//Create a DocBlock and serializer instance
$this->phpdoc = new DocBlock(new ReflectionClass($alias), new Context($this->namespace, $this->classAliases));
Expand Down
12 changes: 6 additions & 6 deletions src/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Barryvdh\LaravelIdeHelper\Eloquent;
use Barryvdh\LaravelIdeHelper\Generator;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Factory;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand Down Expand Up @@ -45,7 +47,7 @@ class GeneratorCommand extends Command
/** @var Filesystem */
protected $files;

/** @var \Illuminate\View\Factory */
/** @var Factory */
protected $view;

protected $onlyExtend;
Expand All @@ -55,14 +57,12 @@ class GeneratorCommand extends Command
*
* @param \Illuminate\Config\Repository $config
* @param Filesystem $files
* @param \Illuminate\View\Factory $view
* @param Factory $view
*/
public function __construct(
/*ConfigRepository */
$config,
Repository $config,
Filesystem $files,
/* Illuminate\View\Factory */
$view
Factory $view
) {
$this->config = $config;
$this->files = $files;
Expand Down
20 changes: 13 additions & 7 deletions src/Console/MetaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Barryvdh\LaravelIdeHelper\Factories;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\View\Factory;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -41,10 +44,10 @@ class MetaCommand extends Command
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
protected $files;

/** @var \Illuminate\Contracts\View\Factory */
/** @var Factory */
protected $view;

/** @var \Illuminate\Contracts\Config\Repository */
/** @var Repository */
protected $config;

protected $methods = [
Expand All @@ -63,12 +66,15 @@ class MetaCommand extends Command

/**
*
* @param \Illuminate\Contracts\Filesystem\Filesystem $files
* @param \Illuminate\Contracts\View\Factory $view
* @param \Illuminate\Contracts\Config\Repository $config
* @param Filesystem $files
* @param Factory $view
* @param Repository $config
*/
public function __construct($files, $view, $config)
{
public function __construct(
Filesystem $files,
Factory $view,
Repository $config
) {
$this->files = $files;
$this->view = $view;
$this->config = $config;
Expand Down
50 changes: 17 additions & 33 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,20 @@ public function register()
{
$configPath = __DIR__ . '/../config/ide-helper.php';
$this->mergeConfigFrom($configPath, 'ide-helper');
$localViewFactory = $this->createLocalViewFactory();

$this->app->singleton(
'command.ide-helper.generate',
function ($app) use ($localViewFactory) {
return new GeneratorCommand($app['config'], $app['files'], $localViewFactory);
}
);

$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->app->when([GeneratorCommand::class, MetaCommand::class])
->needs(\Illuminate\Contracts\View\Factory::class)
->give(function () {
return $this->createLocalViewFactory();
});

$this->commands(
'command.ide-helper.generate',
'command.ide-helper.models',
'command.ide-helper.meta',
'command.ide-helper.eloquent'
[
GeneratorCommand::class,
ModelsCommand::class,
MetaCommand::class,
EloquentCommand::class,
]
);
}

Expand All @@ -109,7 +88,12 @@ function ($app) {
*/
public function provides()
{
return ['command.ide-helper.generate', 'command.ide-helper.models'];
return [
GeneratorCommand::class,
ModelsCommand::class,
MetaCommand::class,
EloquentCommand::class,
];
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Console/GeneratorCommand/AbstractGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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->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());
}
}
34 changes: 34 additions & 0 deletions tests/Console/GeneratorCommand/GenerateIdeHelper/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 testGenerator(): 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->assertStringContainsString('public static function configure($basePath = null)', $this->mockFilesystemOutput);
}

public function testFilename(): void
{
$command = $this->app->make(GeneratorCommand::class);

$tester = $this->runCommand($command, [
'filename' => 'foo.php',
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('A new helper file was written to foo.php', $tester->getDisplay());
}
}
Loading
Loading