diff --git a/lib/ModelTrait.php b/lib/ModelTrait.php index f3f33641b..e3d6d9b2d 100644 --- a/lib/ModelTrait.php +++ b/lib/ModelTrait.php @@ -800,7 +800,7 @@ protected function executeDestroy() */ protected function set($name, $value, bool $throwException = true) { - if ($this->coll) { + if ($this->coll && $value instanceof static) { return $this->setCollValue($name, $value); } diff --git a/tests/unit/Model/CollTest.php b/tests/unit/Model/CollTest.php index 589a665a6..64730e1ed 100644 --- a/tests/unit/Model/CollTest.php +++ b/tests/unit/Model/CollTest.php @@ -4,6 +4,7 @@ namespace WeiTest\Model; +use Wei\Logger; use WeiTest\Fixtures\DbTrait; use WeiTest\Model\Fixture\TestUser; use WeiTest\TestCase; @@ -73,7 +74,7 @@ public function testOffsetSet() public function testOffsetSetInvalid() { - $this->expectException(\TypeError::class); + $this->expectExceptionObject(new \InvalidArgumentException('Invalid property: key')); $users = TestUser::newColl(); $users['key'] = 'test'; @@ -137,7 +138,7 @@ public function testSet() public function testSetInvalid() { - $this->expectException(\TypeError::class); + $this->expectExceptionObject(new \InvalidArgumentException('Invalid property: key')); $users = TestUser::newColl(); $users->set('key', 'test'); @@ -262,7 +263,7 @@ public function testAddNotModelToCollection() $users = TestUser::newColl(); - $this->expectException(\TypeError::class); + $this->expectExceptionObject(new \InvalidArgumentException('Invalid property: [null]')); // Assign non record value to raise an exception $users[] = 234; @@ -623,6 +624,15 @@ public function testSerialize() $this->assertSqlSame('SELECT * FROM `test_users` WHERE `id` IN (?, ?)', $sql); } + public function testGetService() + { + $this->initFixtures(); + + $users = TestUser::newColl(); + + $this->assertInstanceOf(Logger::class, $users->logger); + } + protected function assertSqlSame($expected, $actual, string $message = '') { $this->assertSame($expected, str_replace($this->db->getTablePrefix(), '', $actual), $message); diff --git a/tests/unit/Model/Fixture/TestUser.php b/tests/unit/Model/Fixture/TestUser.php index 3a9087b92..8f000e8ab 100644 --- a/tests/unit/Model/Fixture/TestUser.php +++ b/tests/unit/Model/Fixture/TestUser.php @@ -18,6 +18,7 @@ * @property string|null $name * @property int|null $group_id * @property string|null $address + * @mixin \LoggerPropMixin */ class TestUser extends BaseModel {