Skip to content

Commit

Permalink
Throw UnknownPropertyException when setting unknown property
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Dec 13, 2023
1 parent 41c6691 commit 49f3977
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ public function __set(string $name, mixed $value): void
if (method_exists($this, 'get' . ucfirst($name))) {
throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name);
}

throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\UnknownPropertyException;
use Yiisoft\Db\Query\Query;

abstract class ActiveRecordTest extends TestCase
Expand Down Expand Up @@ -595,6 +596,10 @@ public function testAttributeAccess(): void
$this->assertSame([], $customer->orderItems);
$this->assertFalse($customer->canGetProperty('non_existing_property'));
$this->assertFalse($customer->canSetProperty('non_existing_property'));

$this->expectException(UnknownPropertyException::class);
$this->expectExceptionMessage('Setting unknown property: ' . Customer::class . '::non_existing_property');
$customer->non_existing_property = null;
}

public function testHasAttribute(): void
Expand Down

0 comments on commit 49f3977

Please sign in to comment.