From 1afd088dbf20fa110389b14986c7ac777fcbbd60 Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Sun, 11 Nov 2018 16:31:57 +0100 Subject: [PATCH] Added unit tests for issue #23 --- CHANGELOG.md | 4 ++++ tests/SaveRelationsBehaviorTest.php | 41 +++++++++++++++++++++++++++++++++++++ tests/models/Company.php | 1 + tests/models/Project.php | 6 +++++- tests/models/User.php | 3 ++- 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4aa734..1fcfb1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Yii2 Active Record Save Relations Behavior Change Log +## [1.7.1] +### Fixed +- Fix #23: Relational data was not loaded for nested relational models (thx @toreonify) + ## [1.7.0] ### Added - Enh #42: Add the ability to retrieve old relations values diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index 00ca011..888ac16 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -977,4 +977,45 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertTrue($project->markRelationDirty('company')); $this->assertArrayHasKey('company', $project->getDirtyRelations()); } + + public function testLoadNestedDataModels() + { + $project = Project::findOne(1); + $data = [ + 'Project' => [ + 'name' => 'Other name', + 'company' => [ + 'name' => 'New Company', + 'users' => [ + [ + 'username' => 'New user' + ] + ] + ], + 'users' => [ + [ + 'username' => 'Another user', + 'company' => 1, + 'userProfile' => [ + 'bio' => 'Another user great story' + ] + ] + ] + ] + ]; + $project->load($data); + $this->assertEquals($project->name, "Other name"); + $this->assertEquals($project->company->name, "New Company"); + $this->assertCount(1, $project->users); + $this->assertEquals($project->users[0]->username, "Another user"); + $this->assertEquals($project->users[0]->company->name, "Apple"); + $this->assertEquals($project->company->users[0]->username, "New user"); + $this->assertTrue($project->save(), 'Project could not be saved ' . VarDumper::dumpAsString($project->getErrors())); + $project->refresh(); + $this->assertEquals($project->name, "Other name"); + $this->assertEquals($project->company->name, "New Company"); + $this->assertEquals($project->company->users[0]->username, "New user"); + $this->assertCount(1, $project->users); + $this->assertEquals($project->users[0]->username, "Another user"); + } } diff --git a/tests/models/Company.php b/tests/models/Company.php index 420d38a..fdb8fa4 100644 --- a/tests/models/Company.php +++ b/tests/models/Company.php @@ -36,6 +36,7 @@ class Company extends \yii\db\ActiveRecord return [ ['name', 'required'], ['name', 'unique', 'targetClass' => '\tests\models\Company'], + [['users'], 'safe'] ]; } diff --git a/tests/models/Project.php b/tests/models/Project.php index dc0b32a..a381876 100644 --- a/tests/models/Project.php +++ b/tests/models/Project.php @@ -3,9 +3,12 @@ namespace tests\models; use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior; +use lhs\Yii2SaveRelationsBehavior\SaveRelationsTrait; class Project extends \yii\db\ActiveRecord { + use SaveRelationsTrait; + /** * @inheritdoc */ @@ -50,6 +53,7 @@ class Project extends \yii\db\ActiveRecord return [ [['name', 'company_id'], 'required'], [['name'], 'unique', 'targetAttribute' => ['company_id', 'name']], + [['company', 'links', 'users'], 'safe'] ]; } @@ -129,4 +133,4 @@ class Project extends \yii\db\ActiveRecord return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('project_tags', ['project_id' => 'id']); } -} \ No newline at end of file +} diff --git a/tests/models/User.php b/tests/models/User.php index 9372b12..089ff46 100644 --- a/tests/models/User.php +++ b/tests/models/User.php @@ -37,9 +37,10 @@ class User extends \yii\db\ActiveRecord { return [ ['company_id', 'integer'], - ['username', 'required'], + [['username'], 'required'], ['username', 'unique', 'targetClass' => '\tests\models\User'], [['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']], + [['userProfile', 'company'], 'safe'] ]; }