Skip to content

Commit

Permalink
Refactoring code.
Browse files Browse the repository at this point in the history
  • Loading branch information
voskobovich committed Nov 24, 2016
1 parent 34c17fc commit 8705762
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
10 changes: 5 additions & 5 deletions tests/_data/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* @property string $name
* @property integer $year
*
* @property integer[] $review_list
* @property integer[] $author_list
* @property integer[] $review_ids
* @property integer[] $author_ids
*/
class Book extends ActiveRecord
{
Expand All @@ -31,7 +31,7 @@ public static function tableName()
public function rules()
{
return [
[['author_list', 'review_list'], 'safe'],
[['author_ids', 'review_ids'], 'safe'],
[['name', 'year'], 'required'],
[['year'], 'integer'],
[['name'], 'string', 'max' => 150]
Expand Down Expand Up @@ -76,8 +76,8 @@ public function behaviors()
'linkerBehavior' => [
'class' => LinkerBehavior::className(),
'relations' => [
'author_list' => ['authors'],
'review_list' => ['reviews'],
'author_ids' => ['authors'],
'review_ids' => ['reviews'],
]
]
];
Expand Down
4 changes: 2 additions & 2 deletions tests/_data/BookBadFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function behaviors()
'author' => [
'authors',
'fields' => [
'list_json' => [
'ids_json' => [
'get' => function ($value) {
return Json::encode($value);
},
Expand All @@ -33,7 +33,7 @@ public function behaviors()
],
],
],
'author_list' => [
'author_ids' => [
'reviews',
'fields' => [
'json' => [
Expand Down
10 changes: 5 additions & 5 deletions tests/_data/BookCustomDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BookCustomDefaults extends Book
public function rules()
{
return [
[['review_list_none', 'review_list_null', 'review_list_constant', 'review_list_closure'], 'safe'],
[['review_ids_none', 'review_ids_null', 'review_ids_constant', 'review_ids_closure'], 'safe'],
[['name', 'year'], 'required'],
[['year'], 'integer'],
[['name'], 'string', 'max' => 150]
Expand All @@ -33,22 +33,22 @@ public function behaviors()
'linkerBehavior' => [
'class' => LinkerBehavior::className(),
'relations' => [
'review_list_none' => [
'review_ids_none' => [
'reviews',
],
'review_list_null' => [
'review_ids_null' => [
'reviews',
'updated' => [
'defaultValue' => null,
]
],
'review_list_constant' => [
'review_ids_constant' => [
'reviews',
'updater' => [
'defaultValue' => 7,
]
],
'review_list_closure' => [
'review_ids_closure' => [
'reviews',
'updater' => [
'defaultValue' => function ($updater) {
Expand Down
4 changes: 2 additions & 2 deletions tests/_data/BookJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function behaviors()
'linkerBehavior' => [
'class' => LinkerBehavior::className(),
'relations' => [
'author_list' => [
'author_ids' => [
'authors',
'get' => function ($value) {
return Json::encode($value);
Expand All @@ -29,7 +29,7 @@ public function behaviors()
return Json::decode($value);
},
],
'review_list' => [
'review_ids' => [
'reviews',
'get' => function ($value) {
return Json::encode($value);
Expand Down
12 changes: 6 additions & 6 deletions tests/_data/BookJsonFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* Class BookJsonFields
* @package data
*
* @property string $review_list_json
* @property string $review_list_implode
* @property string $author_list_json
* @property string $author_list_implode
* @property string $review_ids_json
* @property string $review_ids_implode
* @property string $author_ids_json
* @property string $author_ids_implode
*/
class BookJsonFields extends Book
{
Expand All @@ -25,7 +25,7 @@ public function behaviors()
'linkerBehavior' => [
'class' => LinkerBehavior::className(),
'relations' => [
'author_list' => [
'author_ids' => [
'authors',
'fields' => [
'json' => [
Expand All @@ -38,7 +38,7 @@ public function behaviors()
],
],
],
'review_list' => [
'review_ids' => [
'reviews',
'fields' => [
'json' => [
Expand Down
62 changes: 31 additions & 31 deletions tests/unit/BehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testSaveManyToMany()
5,
[
'Book' => [
'author_list' => [7, 9, 8]
'author_ids' => [7, 9, 8]
]
]
);
Expand All @@ -98,7 +98,7 @@ public function testResetManyToMany()
5,
[
'Book' => [
'author_list' => []
'author_ids' => []
]
]
);
Expand All @@ -117,7 +117,7 @@ public function testSaveOneToMany()
3,
[
'Book' => [
'review_list' => [2, 4]
'review_ids' => [2, 4]
]
]
);
Expand All @@ -141,7 +141,7 @@ public function testResetOneToMany()
3,
[
'Book' => [
'review_list' => []
'review_ids' => []
]
]
);
Expand All @@ -160,7 +160,7 @@ public function testSaveManyToManyJson()
5,
[
'BookJson' => [
'author_list' => '[7, 9, 8]'
'author_ids' => '[7, 9, 8]'
]
]
);
Expand All @@ -185,7 +185,7 @@ public function testResetManyToManyJson()
5,
[
'BookJson' => [
'author_list' => '[]'
'author_ids' => '[]'
]
]
);
Expand All @@ -204,7 +204,7 @@ public function testSaveOneToManyJson()
3,
[
'BookJson' => [
'review_list' => '[2, 4]'
'review_ids' => '[2, 4]'
]
]
);
Expand All @@ -228,7 +228,7 @@ public function testResetOneToManyJson()
3,
[
'BookJson' => [
'review_list' => '[]'
'review_ids' => '[]'
]
]
);
Expand All @@ -245,7 +245,7 @@ public function testResetWithDefaultNone()

//this model is attached to reviews 1, 2 and 3

$this->assertTrue($model->load(['BookCustomDefaults' => [ 'review_list_none' => [] ]]), 'Load POST data');
$this->assertTrue($model->load(['BookCustomDefaults' => ['review_ids_none' => []]]), 'Load POST data');
$this->assertTrue($model->save(), 'Save model');

$result = Yii::$app->db
Expand All @@ -265,7 +265,7 @@ public function testResetWithDefaultNull()

//this model is attached to reviews 1, 2 and 3

$this->assertTrue($model->load(['BookCustomDefaults' => [ 'review_list_null' => [] ]]), 'Load POST data');
$this->assertTrue($model->load(['BookCustomDefaults' => ['review_ids_null' => []]]), 'Load POST data');
$this->assertTrue($model->save(), 'Save model');

$result = Yii::$app->db
Expand All @@ -284,7 +284,7 @@ public function testResetWithDefaultConstant()
$this->assertNotEmpty($model, 'Load model');

//this model is attached to reviews 1, 2 and 3
$this->assertTrue($model->load(['BookCustomDefaults' => [ 'review_list_constant' => [] ]]), 'Load POST data');
$this->assertTrue($model->load(['BookCustomDefaults' => ['review_ids_constant' => []]]), 'Load POST data');
$this->assertTrue($model->save(), 'Save model');

$result = Yii::$app->db
Expand All @@ -304,7 +304,7 @@ public function testResetWithDefaultClosure()

//this model is attached to reviews 1, 2 and 3

$this->assertTrue($model->load(['BookCustomDefaults' => [ 'review_list_closure' => [] ]]), 'Load POST data');
$this->assertTrue($model->load(['BookCustomDefaults' => ['review_ids_closure' => []]]), 'Load POST data');
$this->assertTrue($model->save(), 'Save model');

$result = Yii::$app->db
Expand All @@ -319,41 +319,41 @@ public function testResetWithDefaultClosure()

public function testCustomGettersSetters()
{
$reviewList = [1, 2, 4];
$reviewListJson = Json::encode($reviewList);
$reviewListImplode = implode(',', $reviewList);
$reviewIds = [1, 2, 4];
$reviewIdsJson = Json::encode($reviewIds);
$reviewIdsImplode = implode(',', $reviewIds);

$authorList = [5, 6];
$authorListJson = Json::encode($authorList);
$authorIds = [5, 6];
$authorIdsJson = Json::encode($authorIds);

//assign and getters
$model = new BookJsonFields;
$model->review_list = $reviewList;
$model->author_list = $authorList;
$model->review_ids = $reviewIds;
$model->author_ids = $authorIds;

$this->assertEquals($model->review_list, $reviewList, 'Direct getter');
$this->assertEquals($model->author_list, $authorList, 'Direct getter');
$this->assertEquals($model->review_ids, $reviewIds, 'Direct getter');
$this->assertEquals($model->author_ids, $authorIds, 'Direct getter');

$this->assertEquals($model->author_list_json, $authorListJson, 'JSON getter');
$this->assertEquals($model->review_list_json, $reviewListJson, 'JSON getter');
$this->assertEquals($model->author_ids_json, $authorIdsJson, 'JSON getter');
$this->assertEquals($model->review_ids_json, $reviewIdsJson, 'JSON getter');

$this->assertEquals($model->review_list_implode, $reviewListImplode, 'Implode getter');
$this->assertEquals($model->review_ids_implode, $reviewIdsImplode, 'Implode getter');

//test json setters
$model = new BookJsonFields;
$model->review_list_json = $reviewListJson;
$this->assertEquals($model->review_list, $reviewList, 'JSON setter');
$model->author_list_json = $authorListJson;
$this->assertEquals($model->author_list, $authorList, 'JSON setter');
$model->review_ids_json = $reviewIdsJson;
$this->assertEquals($model->review_ids, $reviewIds, 'JSON setter');
$model->author_ids_json = $authorIdsJson;
$this->assertEquals($model->author_ids, $authorIds, 'JSON setter');

//test implode setter for non-existence where appropriate
$model = new BookJsonFields;
$this->assertFalse(isset($model->author_list_implode), 'Non-existence of setter where not declared');
$this->assertFalse(isset($model->author_ids_implode), 'Non-existence of setter where not declared');

//test implode setter
$model = new BookJsonFields;
$model->review_list_implode = $reviewListImplode;
$this->assertEquals($model->review_list, $reviewList, 'Implode setter');
$model->review_ids_implode = $reviewIdsImplode;
$this->assertEquals($model->review_ids, $reviewIds, 'Implode setter');
}

/**
Expand Down

0 comments on commit 8705762

Please sign in to comment.