Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Added unit tests for issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Alban Jubert committed Nov 11, 2018
1 parent 1fe4827 commit 1afd088
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 41 additions & 0 deletions tests/SaveRelationsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,4 +977,45 @@ public function testMarkRelationDirty()
$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");
}
}
1 change: 1 addition & 0 deletions tests/models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function rules()
return [
['name', 'required'],
['name', 'unique', 'targetClass' => '\tests\models\Company'],
[['users'], 'safe']
];
}

Expand Down
6 changes: 5 additions & 1 deletion tests/models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace tests\models;

use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsTrait;

class Project extends \yii\db\ActiveRecord
{
use SaveRelationsTrait;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -50,6 +53,7 @@ public function rules()
return [
[['name', 'company_id'], 'required'],
[['name'], 'unique', 'targetAttribute' => ['company_id', 'name']],
[['company', 'links', 'users'], 'safe']
];
}

Expand Down Expand Up @@ -129,4 +133,4 @@ public function getTags()
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('project_tags', ['project_id' => 'id']);
}

}
}
3 changes: 2 additions & 1 deletion tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public function rules()
{
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']
];
}

Expand Down

0 comments on commit 1afd088

Please sign in to comment.