Skip to content
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
8 changes: 6 additions & 2 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ private function appClasses($paths)
$classes = array_merge($classes, $this->filesystem->allFiles($path));
}

return array_map(function (\SplFIleInfo $file) {
return array_filter(array_map(function (\SplFIleInfo $file) {
if ($file->getExtension() !== 'php') {
return;
}

$content = $this->filesystem->get($file->getPathName());
preg_match("/namespace ([\w\\\\]+)/", $content, $namespace);
preg_match("/class (\w+)/", $content, $class);

return ($namespace[1] ?? '').'\\'.($class[1] ?? '');
}, $classes);
}, $classes));
}

private function loadModel(string $class)
Expand Down
49 changes: 48 additions & 1 deletion tests/Feature/Commands/TraceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Blueprint\Tracer;
use Illuminate\Support\Facades\File;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Symfony\Component\Yaml\Yaml;
use Tests\TestCase;

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ public function relative_class_name_removes_models_namespace()
$this->assertEquals($method->invoke(new Tracer(), app('App\Comment')), 'Comment');
$this->assertEquals($method->invoke(new Tracer(), app('App\Models\Tag')), 'Tag');
}

public function it_passes_the_command_path_to_tracer()
{
$this->filesystem->shouldReceive('exists')
Expand All @@ -85,4 +86,50 @@ public function it_passes_the_command_path_to_tracer()
$this->artisan('blueprint:trace --path=vendor/package/src/app/Models')
->assertExitCode(0);
}

/** @test */
public function it_traces_models_with_differente_namespaces()
{
$this->requireFixture('models/comment.php');
$this->requireFixture('models/custom-models-namespace.php');

$expectedBlueprint = Yaml::dump([
'models' => [
'Comment' => [],
'Models\Tag' => [],
],
]);

$this->filesystem->shouldReceive('exists')
->with('app')
->andReturnTrue();

$this->filesystem->shouldReceive('allFiles')
->with('app')
->andReturn([
new \SplFileInfo('Comment.php'),
new \SplFileInfo('Models\Tag.php'),
new \SplFileInfo('OtherFile.ext'),
]);

$this->filesystem->shouldReceive('get')
->with('Comment.php')
->andReturn($this->fixture('models/comment.php'));

$this->filesystem->shouldReceive('get')
->with('Models\Tag.php')
->andReturn($this->fixture('models/custom-models-namespace.php'));

$this->filesystem->shouldReceive('exists')
->with('.blueprint')
->andReturnFalse();

$this->filesystem->shouldReceive('put')
->with('.blueprint', $expectedBlueprint)
->andReturnTrue();

$this->artisan('blueprint:trace')
->assertExitCode(0)
->expectsOutput('Traced 2 models');
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('blueprint.generate_phpdocs', false);
$app['config']->set('blueprint.use_constraints', false);
$app['config']->set('blueprint.fake_nullables', true);
$app['config']->set('database.default', 'testing');
}

public function fixture(string $path)
Expand Down