Skip to content

Commit

Permalink
Check invalid value
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Sep 30, 2019
1 parent 18f5a10 commit ba7e642
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/LinkManyBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Yii;
use yii\base\Behavior;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\base\UnknownPropertyException;
use yii\db\ActiveRecord;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function afterValidate()
}

$errors = [];
$models = isset($this->_inserteds[$name]) ? $this->_inserteds[$name] : [];
$models = ArrayHelper::getValue($this->_inserteds, $name, []);

foreach ($models as $model) {
if (!$model->validate()) {
Expand All @@ -149,6 +150,10 @@ public function __set($name, $value)
try {
parent::__set($name, $value);
} catch (UnknownPropertyException $exception) {
if (!is_array($value)) {
throw new InvalidArgumentException("The '$name' property must be an array");
}

$definition = $this->findDefinition($name);
if ($definition !== null) {
$this->prepareRelation($definition, [$name => $value]);
Expand Down
30 changes: 18 additions & 12 deletions tests/LinkManyBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,41 @@

use solutosoft\linkmany\LinkManyBehavior;
use solutosoft\linkmany\tests\models\Post;
use solutosoft\linkmany\tests\models\Tag;

//use solutosoft\linkmany\tests\models\PostLanguage;
//use solutosoft\linkmany\tests\models\Tag;

class LinkManyBehaviorTest extends TestCase
{
public function testInitException()
public function testInitSuccess()
{
$this->setExpectedException('\yii\base\InvalidConfigException');

new LinkManyBehavior([
'relations' => [
['invalid' => true]
'relation_1',
'relation_2' => [
'formName' => 'test'
]
]
]);
}

public function testInitSuccess()
public function testInitException()
{
$this->setExpectedException('\yii\base\InvalidConfigException');

new LinkManyBehavior([
'relations' => [
'relation_1',
'relation_2' => [
'formName' => 'test'
]
['invalid' => true]
]
]);
}

public function testInvalidRelationValue()
{
$this->setExpectedException('\yii\base\InvalidArgumentException');

$post = new Post();
$post->comments = null;
}

public function testFillNewRecord()
{
$post = new Post();
Expand Down Expand Up @@ -234,6 +239,7 @@ public function testSetRelation()
$post->tags = [1];

$post->save();
$post->refresh();
$this->assertCount(1, $post->tags);
}
}

0 comments on commit ba7e642

Please sign in to comment.