mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-27 04:10:30 +08:00
Finished refactoring find() .
This commit is contained in:
@@ -380,6 +380,7 @@ class ArrayHelperTest extends TestCase
|
||||
|
||||
public function testIsAssociative()
|
||||
{
|
||||
$this->assertFalse(ArrayHelper::isAssociative('test'));
|
||||
$this->assertFalse(ArrayHelper::isAssociative([]));
|
||||
$this->assertFalse(ArrayHelper::isAssociative([1, 2, 3]));
|
||||
$this->assertTrue(ArrayHelper::isAssociative(['name' => 1, 'value' => 'test']));
|
||||
@@ -389,6 +390,7 @@ class ArrayHelperTest extends TestCase
|
||||
|
||||
public function testIsIndexed()
|
||||
{
|
||||
$this->assertFalse(ArrayHelper::isIndexed('test'));
|
||||
$this->assertTrue(ArrayHelper::isIndexed([]));
|
||||
$this->assertTrue(ArrayHelper::isIndexed([1, 2, 3]));
|
||||
$this->assertTrue(ArrayHelper::isIndexed([2 => 'a', 3 => 'b']));
|
||||
|
||||
@@ -57,38 +57,38 @@ class ExistValidatorTest extends DatabaseTestCase
|
||||
{
|
||||
// existing value on different table
|
||||
$val = new ExistValidator(['targetClass' => ValidatorTestMainModel::className(), 'targetAttribute' => 'id']);
|
||||
$m = ValidatorTestRefModel::find(['id' => 1]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 1]);
|
||||
$val->validateAttribute($m, 'ref');
|
||||
$this->assertFalse($m->hasErrors());
|
||||
// non-existing value on different table
|
||||
$val = new ExistValidator(['targetClass' => ValidatorTestMainModel::className(), 'targetAttribute' => 'id']);
|
||||
$m = ValidatorTestRefModel::find(['id' => 6]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 6]);
|
||||
$val->validateAttribute($m, 'ref');
|
||||
$this->assertTrue($m->hasErrors('ref'));
|
||||
// existing value on same table
|
||||
$val = new ExistValidator(['targetAttribute' => 'ref']);
|
||||
$m = ValidatorTestRefModel::find(['id' => 2]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 2]);
|
||||
$val->validateAttribute($m, 'test_val');
|
||||
$this->assertFalse($m->hasErrors());
|
||||
// non-existing value on same table
|
||||
$val = new ExistValidator(['targetAttribute' => 'ref']);
|
||||
$m = ValidatorTestRefModel::find(['id' => 5]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 5]);
|
||||
$val->validateAttribute($m, 'test_val_fail');
|
||||
$this->assertTrue($m->hasErrors('test_val_fail'));
|
||||
// check for given value (true)
|
||||
$val = new ExistValidator();
|
||||
$m = ValidatorTestRefModel::find(['id' => 3]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 3]);
|
||||
$val->validateAttribute($m, 'ref');
|
||||
$this->assertFalse($m->hasErrors());
|
||||
// check for given defaults (false)
|
||||
$val = new ExistValidator();
|
||||
$m = ValidatorTestRefModel::find(['id' => 4]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 4]);
|
||||
$m->a_field = 'some new value';
|
||||
$val->validateAttribute($m, 'a_field');
|
||||
$this->assertTrue($m->hasErrors('a_field'));
|
||||
// check array
|
||||
$val = new ExistValidator(['targetAttribute' => 'ref']);
|
||||
$m = ValidatorTestRefModel::find(['id' => 2]);
|
||||
$m = ValidatorTestRefModel::findOne(['id' => 2]);
|
||||
$m->test_val = [1,2,3];
|
||||
$val->validateAttribute($m, 'test_val');
|
||||
$this->assertTrue($m->hasErrors('test_val'));
|
||||
|
||||
@@ -35,7 +35,7 @@ class UniqueValidatorTest extends DatabaseTestCase
|
||||
$m = ValidatorTestMainModel::find()->one();
|
||||
$val->validateAttribute($m, 'id');
|
||||
$this->assertFalse($m->hasErrors('id'));
|
||||
$m = ValidatorTestRefModel::find(1);
|
||||
$m = ValidatorTestRefModel::findOne(1);
|
||||
$val->validateAttribute($m, 'ref');
|
||||
$this->assertTrue($m->hasErrors('ref'));
|
||||
// new record:
|
||||
@@ -70,10 +70,10 @@ class UniqueValidatorTest extends DatabaseTestCase
|
||||
public function testValidateNonDatabaseAttribute()
|
||||
{
|
||||
$val = new UniqueValidator(['targetClass' => ValidatorTestRefModel::className(), 'targetAttribute' => 'ref']);
|
||||
$m = ValidatorTestMainModel::find(1);
|
||||
$m = ValidatorTestMainModel::findOne(1);
|
||||
$val->validateAttribute($m, 'testMainVal');
|
||||
$this->assertFalse($m->hasErrors('testMainVal'));
|
||||
$m = ValidatorTestMainModel::find(1);
|
||||
$m = ValidatorTestMainModel::findOne(1);
|
||||
$m->testMainVal = 4;
|
||||
$val->validateAttribute($m, 'testMainVal');
|
||||
$this->assertTrue($m->hasErrors('testMainVal'));
|
||||
@@ -94,7 +94,7 @@ class UniqueValidatorTest extends DatabaseTestCase
|
||||
'targetAttribute' => ['order_id', 'item_id'],
|
||||
]);
|
||||
// validate old record
|
||||
$m = OrderItem::find(['order_id' => 1, 'item_id' => 2]);
|
||||
$m = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]);
|
||||
$val->validateAttribute($m, 'order_id');
|
||||
$this->assertFalse($m->hasErrors('order_id'));
|
||||
$m->item_id = 1;
|
||||
@@ -114,14 +114,14 @@ class UniqueValidatorTest extends DatabaseTestCase
|
||||
'targetAttribute' => ['id' => 'order_id'],
|
||||
]);
|
||||
// validate old record
|
||||
$m = Order::find(1);
|
||||
$m = Order::findOne(1);
|
||||
$val->validateAttribute($m, 'id');
|
||||
$this->assertTrue($m->hasErrors('id'));
|
||||
$m = Order::find(1);
|
||||
$m = Order::findOne(1);
|
||||
$m->id = 2;
|
||||
$val->validateAttribute($m, 'id');
|
||||
$this->assertTrue($m->hasErrors('id'));
|
||||
$m = Order::find(1);
|
||||
$m = Order::findOne(1);
|
||||
$m->id = 10;
|
||||
$val->validateAttribute($m, 'id');
|
||||
$this->assertFalse($m->hasErrors('id'));
|
||||
|
||||
Reference in New Issue
Block a user