Skip to content

Commit 9c32489

Browse files
authored
Fix Trace command to ignore non-PHP files (#498)
1 parent c4a3081 commit 9c32489

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/Tracer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ private function appClasses($paths)
6060
$classes = array_merge($classes, $this->filesystem->allFiles($path));
6161
}
6262

63-
return array_map(function (\SplFIleInfo $file) {
63+
return array_filter(array_map(function (\SplFIleInfo $file) {
64+
if ($file->getExtension() !== 'php') {
65+
return;
66+
}
67+
6468
$content = $this->filesystem->get($file->getPathName());
6569
preg_match("/namespace ([\w\\\\]+)/", $content, $namespace);
6670
preg_match("/class (\w+)/", $content, $class);
6771

6872
return ($namespace[1] ?? '').'\\'.($class[1] ?? '');
69-
}, $classes);
73+
}, $classes));
7074
}
7175

7276
private function loadModel(string $class)

tests/Feature/Commands/TraceCommandTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Blueprint\Tracer;
99
use Illuminate\Support\Facades\File;
1010
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
11+
use Symfony\Component\Yaml\Yaml;
1112
use Tests\TestCase;
1213

1314
/**
@@ -69,7 +70,7 @@ public function relative_class_name_removes_models_namespace()
6970
$this->assertEquals($method->invoke(new Tracer(), app('App\Comment')), 'Comment');
7071
$this->assertEquals($method->invoke(new Tracer(), app('App\Models\Tag')), 'Tag');
7172
}
72-
73+
7374
public function it_passes_the_command_path_to_tracer()
7475
{
7576
$this->filesystem->shouldReceive('exists')
@@ -85,4 +86,50 @@ public function it_passes_the_command_path_to_tracer()
8586
$this->artisan('blueprint:trace --path=vendor/package/src/app/Models')
8687
->assertExitCode(0);
8788
}
89+
90+
/** @test */
91+
public function it_traces_models_with_differente_namespaces()
92+
{
93+
$this->requireFixture('models/comment.php');
94+
$this->requireFixture('models/custom-models-namespace.php');
95+
96+
$expectedBlueprint = Yaml::dump([
97+
'models' => [
98+
'Comment' => [],
99+
'Models\Tag' => [],
100+
],
101+
]);
102+
103+
$this->filesystem->shouldReceive('exists')
104+
->with('app')
105+
->andReturnTrue();
106+
107+
$this->filesystem->shouldReceive('allFiles')
108+
->with('app')
109+
->andReturn([
110+
new \SplFileInfo('Comment.php'),
111+
new \SplFileInfo('Models\Tag.php'),
112+
new \SplFileInfo('OtherFile.ext'),
113+
]);
114+
115+
$this->filesystem->shouldReceive('get')
116+
->with('Comment.php')
117+
->andReturn($this->fixture('models/comment.php'));
118+
119+
$this->filesystem->shouldReceive('get')
120+
->with('Models\Tag.php')
121+
->andReturn($this->fixture('models/custom-models-namespace.php'));
122+
123+
$this->filesystem->shouldReceive('exists')
124+
->with('.blueprint')
125+
->andReturnFalse();
126+
127+
$this->filesystem->shouldReceive('put')
128+
->with('.blueprint', $expectedBlueprint)
129+
->andReturnTrue();
130+
131+
$this->artisan('blueprint:trace')
132+
->assertExitCode(0)
133+
->expectsOutput('Traced 2 models');
134+
}
88135
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected function getEnvironmentSetUp($app)
3030
$app['config']->set('blueprint.generate_phpdocs', false);
3131
$app['config']->set('blueprint.use_constraints', false);
3232
$app['config']->set('blueprint.fake_nullables', true);
33+
$app['config']->set('database.default', 'testing');
3334
}
3435

3536
public function fixture(string $path)

0 commit comments

Comments
 (0)