Skip to content

Commit f5d940f

Browse files
committed
Test generator, update constructor
1 parent 0abcb74 commit f5d940f

File tree

9 files changed

+22778
-58
lines changed

9 files changed

+22778
-58
lines changed

src/Alias.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ public function __construct($config, $alias, $facade, $magicMethods = [], $inter
8181
$this->detectExtendsNamespace();
8282

8383
if (!empty($this->namespace)) {
84-
$this->classAliases = (new UsesResolver())->loadFromClass($this->root);
84+
try {
85+
$this->classAliases = (new UsesResolver())->loadFromClass($this->root);
86+
} catch (Throwable $e) {
87+
$this->classAliases = [];
88+
}
89+
8590

8691
//Create a DocBlock and serializer instance
8792
$this->phpdoc = new DocBlock(new ReflectionClass($alias), new Context($this->namespace, $this->classAliases));

src/Console/GeneratorCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Barryvdh\LaravelIdeHelper\Eloquent;
1515
use Barryvdh\LaravelIdeHelper\Generator;
1616
use Illuminate\Console\Command;
17+
use Illuminate\Contracts\Config\Repository;
1718
use Illuminate\Filesystem\Filesystem;
19+
use Illuminate\View\Factory;
1820
use Symfony\Component\Console\Input\InputArgument;
1921
use Symfony\Component\Console\Input\InputOption;
2022

@@ -45,7 +47,7 @@ class GeneratorCommand extends Command
4547
/** @var Filesystem */
4648
protected $files;
4749

48-
/** @var \Illuminate\View\Factory */
50+
/** @var Factory */
4951
protected $view;
5052

5153
protected $onlyExtend;
@@ -55,14 +57,12 @@ class GeneratorCommand extends Command
5557
*
5658
* @param \Illuminate\Config\Repository $config
5759
* @param Filesystem $files
58-
* @param \Illuminate\View\Factory $view
60+
* @param Factory $view
5961
*/
6062
public function __construct(
61-
/*ConfigRepository */
62-
$config,
63+
Repository $config,
6364
Filesystem $files,
64-
/* Illuminate\View\Factory */
65-
$view
65+
Factory $view
6666
) {
6767
$this->config = $config;
6868
$this->files = $files;

src/Console/MetaCommand.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Barryvdh\LaravelIdeHelper\Factories;
1515
use Illuminate\Console\Command;
16+
use Illuminate\Contracts\Config\Repository;
17+
use Illuminate\Contracts\View\Factory;
18+
use Illuminate\Filesystem\Filesystem;
1619
use RuntimeException;
1720
use Symfony\Component\Console\Input\InputOption;
1821
use Symfony\Component\Console\Output\OutputInterface;
@@ -41,10 +44,10 @@ class MetaCommand extends Command
4144
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
4245
protected $files;
4346

44-
/** @var \Illuminate\Contracts\View\Factory */
47+
/** @var Factory */
4548
protected $view;
4649

47-
/** @var \Illuminate\Contracts\Config\Repository */
50+
/** @var Repository */
4851
protected $config;
4952

5053
protected $methods = [
@@ -63,11 +66,15 @@ class MetaCommand extends Command
6366

6467
/**
6568
*
66-
* @param \Illuminate\Contracts\Filesystem\Filesystem $files
67-
* @param \Illuminate\Contracts\View\Factory $view
68-
* @param \Illuminate\Contracts\Config\Repository $config
69+
* @param Filesystem $files
70+
* @param Factory $view
71+
* @param Repository $config
6972
*/
70-
public function __construct($files, $view, $config)
73+
public function __construct(
74+
Filesystem $files,
75+
Factory $view,
76+
Repository $config
77+
)
7178
{
7279
$this->files = $files;
7380
$this->view = $view;

src/IdeHelperServiceProvider.php

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,41 +64,20 @@ public function register()
6464
{
6565
$configPath = __DIR__ . '/../config/ide-helper.php';
6666
$this->mergeConfigFrom($configPath, 'ide-helper');
67-
$localViewFactory = $this->createLocalViewFactory();
6867

69-
$this->app->singleton(
70-
'command.ide-helper.generate',
71-
function ($app) use ($localViewFactory) {
72-
return new GeneratorCommand($app['config'], $app['files'], $localViewFactory);
73-
}
74-
);
75-
76-
$this->app->singleton(
77-
'command.ide-helper.models',
78-
function ($app) {
79-
return new ModelsCommand($app['files']);
80-
}
81-
);
82-
83-
$this->app->singleton(
84-
'command.ide-helper.meta',
85-
function ($app) use ($localViewFactory) {
86-
return new MetaCommand($app['files'], $localViewFactory, $app['config']);
87-
}
88-
);
89-
90-
$this->app->singleton(
91-
'command.ide-helper.eloquent',
92-
function ($app) {
93-
return new EloquentCommand($app['files']);
94-
}
95-
);
68+
$this->app->when([GeneratorCommand::class, MetaCommand::class])
69+
->needs(\Illuminate\Contracts\View\Factory::class)
70+
->give(function () {
71+
return $this->createLocalViewFactory();
72+
});
9673

9774
$this->commands(
98-
'command.ide-helper.generate',
99-
'command.ide-helper.models',
100-
'command.ide-helper.meta',
101-
'command.ide-helper.eloquent'
75+
[
76+
GeneratorCommand::class,
77+
ModelsCommand::class,
78+
MetaCommand::class,
79+
EloquentCommand::class,
80+
]
10281
);
10382
}
10483

@@ -109,7 +88,12 @@ function ($app) {
10988
*/
11089
public function provides()
11190
{
112-
return ['command.ide-helper.generate', 'command.ide-helper.models'];
91+
return [
92+
GeneratorCommand::class,
93+
ModelsCommand::class,
94+
MetaCommand::class,
95+
EloquentCommand::class
96+
];
11397
}
11498

11599
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand;
6+
7+
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
8+
use Barryvdh\LaravelIdeHelper\Tests\SnapshotPhpDriver;
9+
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
10+
11+
abstract class AbstractGeneratorCommand extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
// $this->artisan('key:gen');
17+
18+
$this->mockFilesystem();
19+
}
20+
21+
/**
22+
* Get package providers.
23+
*
24+
* @param \Illuminate\Foundation\Application $app
25+
*
26+
* @return array
27+
*/
28+
protected function getPackageProviders($app)
29+
{
30+
return [IdeHelperServiceProvider::class];
31+
}
32+
33+
protected function assertMatchesMockedSnapshot()
34+
{
35+
$this->assertMatchesSnapshot($this->mockFilesystemOutput, new SnapshotPhpDriver());
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\GenerateIdeHelper;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\AbstractGeneratorCommand;
9+
10+
class Test extends AbstractGeneratorCommand
11+
{
12+
public function test(): void
13+
{
14+
$command = $this->app->make(GeneratorCommand::class);
15+
16+
$tester = $this->runCommand($command);
17+
18+
$this->assertSame(0, $tester->getStatusCode());
19+
$this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay());
20+
$this->assertMatchesMockedSnapshot();
21+
}
22+
}

0 commit comments

Comments
 (0)