From 49f39776d128eaeadc452da4d6cda9d7f63009b0 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 13 Dec 2023 15:54:33 +0700 Subject: [PATCH] Throw UnknownPropertyException when setting unknown property --- src/BaseActiveRecordTrait.php | 2 ++ tests/ActiveRecordTest.php | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/BaseActiveRecordTrait.php b/src/BaseActiveRecordTrait.php index c5061b1ac..899df4ed2 100644 --- a/src/BaseActiveRecordTrait.php +++ b/src/BaseActiveRecordTrait.php @@ -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); } /** diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 4d054d29e..700a0d00a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -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 @@ -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