Skip to content

Commit 1eeeb64

Browse files
author
Brian Faust
authored
Throw an exception when model is missing (#622)
1 parent aefd9d7 commit 1eeeb64

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protected function buildRelationships(Model $model)
229229
$relationship .= sprintf('%s->using(%s::class)', PHP_EOL . str_pad(' ', 12), $column_name);
230230
$relationship .= sprintf('%s->as(\'%s\')', PHP_EOL . str_pad(' ', 12), Str::snake($column_name));
231231

232-
$foreign = $this->tree->modelForContext($column_name);
232+
$foreign = $this->tree->modelForContext($column_name, true);
233233
$columns = $this->pivotColumns($foreign->columns(), $foreign->relationships());
234234
if ($columns) {
235235
$relationship .= sprintf('%s->withPivot(\'%s\')', PHP_EOL . str_pad(' ', 12), implode("', '", $columns));

src/Generators/Statements/ResourceGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected function buildData(ResourceStatement $resource)
8080
/**
8181
* @var \Blueprint\Models\Model $model
8282
*/
83-
$model = $this->tree->modelForContext($context);
83+
$model = $this->tree->modelForContext($context, true);
8484

8585
$data = [];
8686
if ($resource->collection()) {

src/Tree.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function seeders()
3535
return $this->tree['seeders'];
3636
}
3737

38-
public function modelForContext(string $context)
38+
public function modelForContext(string $context, bool $throwWhenMissing = false)
3939
{
4040
if (isset($this->models[Str::studly($context)])) {
4141
return $this->models[Str::studly($context)];
@@ -50,6 +50,15 @@ public function modelForContext(string $context)
5050
if (count($matches) === 1) {
5151
return $this->models[current($matches)];
5252
}
53+
54+
if ($throwWhenMissing) {
55+
throw new \InvalidArgumentException(
56+
sprintf(
57+
'The [%s] model class could not be found or autoloaded. Please ensure that the model class name is correctly spelled, adheres to the appropriate namespace, and that the file containing the class is properly located within the "app/Models" directory or another relevant directory as configured.',
58+
$this->fqcnForContext($context),
59+
)
60+
);
61+
}
5362
}
5463

5564
public function fqcnForContext(string $context)

tests/Unit/TreeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Blueprint\Tree;
6+
use InvalidArgumentException;
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Tests\TestCase;
10+
11+
#[CoversClass(\Blueprint\Tree::class)]
12+
final class TreeTest extends TestCase
13+
{
14+
#[Test]
15+
public function it_throws_when_a_referenced_model_cannot_be_found(): void
16+
{
17+
$this->expectException(InvalidArgumentException::class);
18+
$this->expectExceptionMessage('The [App\Models\Unknown] model class could not be found or autoloaded. Please ensure that the model class name is correctly spelled, adheres to the appropriate namespace, and that the file containing the class is properly located within the "app/Models" directory or another relevant directory as configured.');
19+
20+
$tree = new Tree(['models' => []]);
21+
$tree->modelForContext('Unknown', true);
22+
}
23+
}

0 commit comments

Comments
 (0)