Skip to content

Commit

Permalink
Updated tests for feature la-haute-societe#15 implementation coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sspat committed Jun 7, 2017
1 parent 92cdda5 commit 13f660e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tests/SaveRelationsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use tests\models\ProjectLink;
use tests\models\ProjectNoTransactions;
use tests\models\User;
use tests\models\Tag;
use Yii;
use yii\base\Model;
use yii\db\Migration;
Expand All @@ -36,6 +37,8 @@ protected function tearDown()
$db->createCommand()->dropTable('company')->execute();
$db->createCommand()->dropTable('link_type')->execute();
$db->createCommand()->dropTable('link')->execute();
$db->createCommand()->dropTable('project_tags')->execute();
$db->createCommand()->dropTable('tags')->execute();
$db->createCommand()->dropTable('project_link')->execute();
$db->createCommand()->dropTable('dummy')->execute();
parent::tearDown();
Expand Down Expand Up @@ -80,6 +83,17 @@ protected function setupDbData()
'PRIMARY KEY(language, name)'
])->execute();

$db->createCommand()->createTable('tags', [
'id' => $migration->primaryKey(),
'name' => $migration->string()->notNull()->unique()
])->execute();

$db->createCommand()->createTable('project_tags', [
'project_id' => $migration->integer()->notNull(),
'tag_id' => $migration->integer()->notNull(),
'order' => $migration->integer()->notNull()
])->execute();

$db->createCommand()->createTable('link_type', [
'id' => $migration->primaryKey(),
'name' => $migration->string()->notNull()->unique()
Expand Down Expand Up @@ -367,6 +381,28 @@ public function testSaveUpdatedHasManyRelationWithCompositeFksAsArrayShouldSucce
);
}

public function testSaveNewManyRelationJunctionTableColumnsShouldSucceed()
{
$project = Project::findOne(1);
$firstTag = new Tag();
$firstTag->name = 'Tag One';
$firstTag->setOrder(1);
$secondTag = new Tag();
$secondTag->name = 'Tag Two';
$secondTag->setOrder(3);
$project->tags = [
$firstTag,
$secondTag
];
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertCount(2, $project->tags, 'Project should have 4 tags after assignment');

$firstTagJunctionTableColumns = (new \yii\db\Query())->from('project_tags')->where(['tag_id' => $firstTag->id])->one();
$secondTagJunctionTableColumns = (new \yii\db\Query())->from('project_tags')->where(['tag_id' => $secondTag->id])->one();
$this->assertEquals($firstTag->getOrder(), $firstTagJunctionTableColumns['order']);
$this->assertEquals($secondTag->getOrder(), $secondTagJunctionTableColumns['order']);
}

public function testSaveMixedRelationsShouldSucceed()
{
$project = new Project();
Expand Down
18 changes: 17 additions & 1 deletion tests/models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ public function behaviors()
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['company', 'users', 'links']
'relations' => ['company', 'users', 'links', 'tags'],
'junctionTableColumns' => [
'tags' => function ($model) {
/** @var $model Tag */
return [
'order' => $model->getOrder()
];
}
]
],
];
}
Expand Down Expand Up @@ -88,4 +96,12 @@ public function getLinks()
return $this->hasMany(Link::className(), ['language' => 'language', 'name' => 'name'])->via('projectLinks');
}

/**
* @return \yii\db\ActiveQuery
*/
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('project_tags', ['project_id' => 'id']);
}

}
44 changes: 44 additions & 0 deletions tests/models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace tests\models;

class Tag extends \yii\db\ActiveRecord
{
/** @var int */
protected $order;

/**
* @inheritdoc
*/
public static function tableName()
{
return 'tags';
}

/**
* @param int $order
*/
public function setOrder($order)
{
$this->order = $order;
}

/**
* @return int
*/
public function getOrder()
{
return $this->order;
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['name'], 'string']
];
}
}

0 comments on commit 13f660e

Please sign in to comment.