Browse Source

Added unit tests for issue #23

tags/1.7.1 1.7.1
Alban Jubert 6 years ago
parent
commit
1afd088dbf
  1. 4
      CHANGELOG.md
  2. 41
      tests/SaveRelationsBehaviorTest.php
  3. 1
      tests/models/Company.php
  4. 6
      tests/models/Project.php
  5. 3
      tests/models/User.php

4
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

41
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");
}
}

1
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']
];
}

6
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']);
}
}
}

3
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']
];
}

Loading…
Cancel
Save