Skip to content

Commit

Permalink
Return null when traversing entity 1:1 not found
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 29, 2022
1 parent fe73681 commit 6a18632
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Data/HintableModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ public function &__get(string $name)
/** @var Model */
$model = $this->ref($hProp->fieldName);

// HasOne/ContainsOne::ref() method returns an unloaded entity when traversing entity not found
if ($model->isEntity()) {
$this->assertIsEntity();

if (!$model->isLoaded()) {
$res = null;

return $res;
}
}

if ($hProp->refType === HintablePropertyDef::REF_TYPE_ONE) {
// TODO this requires checking all parents!
// // ensure no more than one record can load
Expand Down
50 changes: 44 additions & 6 deletions tests/Data/HintableModelArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Data\Exception;
use Atk4\Data\Persistence;
use Mvorisek\Atk4\Hintable\Phpstan\PhpstanUtil;

/**
* @coversDefaultClass \Mvorisek\Atk4\Hintable\Data\HintableModelTrait
Expand All @@ -33,7 +34,9 @@ public function testFieldName(): void
public function testFieldNameUndeclaredException(): void
{
$model = new Model\Simple();

$this->expectException(Exception::class);
$this->expectExceptionMessage('Hintable property is not defined');
$model->fieldName()->undeclared; // @phpstan-ignore-line
}

Expand Down Expand Up @@ -73,6 +76,11 @@ protected function createDatabaseForRefTest(): Persistence
$standard12 = (clone $standardTemplate)
->set(Model\Standard::hinting()->fieldName()->simpleOneId, $simple3->id)
->save();
/* 13 - null simpleOneId */ (clone $standardTemplate)
->save();
/* 14 - invalid simpleOneId */ (clone $standardTemplate)
->set(Model\Standard::hinting()->fieldName()->simpleOneId, 999)
->save();

$simple1
->set(Model\Simple::hinting()->fieldName()->refId, $standard11->id)
Expand Down Expand Up @@ -135,27 +143,57 @@ public function testRefMany(): void
}, iterator_to_array($model->load(12)->simpleMany)));
}

public function testRefManyIsNotEntity(): void
public function testRefOneLoadOneException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$this->assertFalse($model->load(12)->simpleMany->isEntity());
$modelSimple = $model->simpleOne;

$this->expectException(Exception::class);
$this->expectExceptionMessage('more than one record can be loaded');
$modelSimple->loadOne();
}

public function testRefOneLoadOneException(): void
public function testRefManyLoadOneException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$modelSimple = $model->simpleMany;

$this->expectException(Exception::class);
$model->simpleOne->loadOne();
$this->expectExceptionMessage('more than one record can be loaded');
$modelSimple->loadOne();
}

public function testRefManyLoadOneException(): void
public function testRefOneTraverseNullException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$entity13 = $model->load(13);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Unable to traverse on null value');
PhpstanUtil::ignoreUnusedVariable($entity13->simpleOne);
}

public function testRefOneTraverseInvalidException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$entity14 = $model->load(14);

$this->assertNull($entity14->simpleOne); // TODO should throw
}

public function testRefManyTraverseNullException(): void
{
$db = $this->createDatabaseForRefTest();
$model = new Model\Standard($db);
$entityNull = $model->createEntity();

$this->expectException(Exception::class);
$model->simpleMany->loadOne();
$this->expectExceptionMessage('Unable to traverse on null value');
PhpstanUtil::ignoreUnusedVariable($entityNull->simpleMany);
}

public function testPhpstanModelIteratorAggregate(): void
Expand Down

0 comments on commit 6a18632

Please sign in to comment.