Skip to content

Commit

Permalink
fix(Model): Coll 获取服务时出错
Browse files Browse the repository at this point in the history
XxxModel::setCollValue(): Argument #2 ($value) must be of type XxxModel, Yyy given
  • Loading branch information
twinh committed Mar 31, 2024
1 parent 49c709f commit 1e6f8f0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ModelTrait.php
Expand Up @@ -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);
}

Expand Down
16 changes: 13 additions & 3 deletions tests/unit/Model/CollTest.php
Expand Up @@ -4,6 +4,7 @@

namespace WeiTest\Model;

use Wei\Logger;
use WeiTest\Fixtures\DbTrait;
use WeiTest\Model\Fixture\TestUser;
use WeiTest\TestCase;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Model/Fixture/TestUser.php
Expand Up @@ -18,6 +18,7 @@
* @property string|null $name
* @property int|null $group_id
* @property string|null $address
* @mixin \LoggerPropMixin
*/
class TestUser extends BaseModel
{
Expand Down

0 comments on commit 1e6f8f0

Please sign in to comment.