mirror of
https://github.com/yiisoft/yii2.git
synced 2025-12-19 07:07:58 +08:00
Refactor validateAttribute method in UniqueValidator (#13202)
* Refactor validateAttribute method in UniqueValidator Extract prepareParams and prepareQuery from validateAttribute, so they can be tested separately. * Added issue number to changelog * Eliminated unneeded variable * Renamed methods and parameters, update PHPDocs
This commit is contained in:
committed by
Boudewijn Vahrmeijer
parent
01997441cc
commit
bfba0aa711
@@ -303,4 +303,61 @@ abstract class UniqueValidatorTest extends DatabaseTestCase
|
||||
$val->validateAttribute($m, 'ref');
|
||||
$this->assertTrue($m->hasErrors('ref'));
|
||||
}
|
||||
}
|
||||
|
||||
public function testPrepareParams()
|
||||
{
|
||||
$model = new FakedValidationModel();
|
||||
$model->val_attr_a = 'test value a';
|
||||
$model->val_attr_b = 'test value b';
|
||||
$model->val_attr_c = 'test value c';
|
||||
$attribute = 'val_attr_a';
|
||||
|
||||
$targetAttribute = 'val_attr_b';
|
||||
$result = $this->invokeMethod(new UniqueValidator(), 'prepareConditions', [$targetAttribute, $model, $attribute]);
|
||||
$expected = ['val_attr_b' => 'test value a'];
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$targetAttribute = ['val_attr_b', 'val_attr_c'];
|
||||
$result = $this->invokeMethod(new UniqueValidator(), 'prepareConditions', [$targetAttribute, $model, $attribute]);
|
||||
$expected = ['val_attr_b' => 'test value b', 'val_attr_c' => 'test value c'];
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$targetAttribute = ['val_attr_a' => 'val_attr_b'];
|
||||
$result = $this->invokeMethod(new UniqueValidator(), 'prepareConditions', [$targetAttribute, $model, $attribute]);
|
||||
$expected = ['val_attr_b' => 'test value a'];
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
|
||||
$targetAttribute = ['val_attr_b', 'val_attr_a' => 'val_attr_c'];
|
||||
$result = $this->invokeMethod(new UniqueValidator(), 'prepareConditions', [$targetAttribute, $model, $attribute]);
|
||||
$expected = ['val_attr_b' => 'test value b', 'val_attr_c' => 'test value a'];
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testPrepareQuery()
|
||||
{
|
||||
$schema = $this->getConnection()->schema;
|
||||
|
||||
$model = new ValidatorTestMainModel();
|
||||
$query = $this->invokeMethod(new UniqueValidator(), 'prepareQuery', [$model,['val_attr_b' => 'test value a']]);
|
||||
$expected = "SELECT * FROM {$schema->quoteTableName('validator_main')} WHERE {$schema->quoteColumnName('val_attr_b')}=:qp0";
|
||||
$this->assertEquals($expected, $query->createCommand()->getSql());
|
||||
|
||||
$params = ['val_attr_b' => 'test value b', 'val_attr_c' => 'test value a'];
|
||||
$query = $this->invokeMethod(new UniqueValidator(), 'prepareQuery', [$model, $params]);
|
||||
$expected = "SELECT * FROM {$schema->quoteTableName('validator_main')} WHERE ({$schema->quoteColumnName('val_attr_b')}=:qp0) AND ({$schema->quoteColumnName('val_attr_c')}=:qp1)";
|
||||
$this->assertEquals($expected, $query->createCommand()->getSql());
|
||||
|
||||
$params = ['val_attr_b' => 'test value b'];
|
||||
$query = $this->invokeMethod(new UniqueValidator(['filter' => 'val_attr_a > 0']), 'prepareQuery', [$model, $params]);
|
||||
$expected = "SELECT * FROM {$schema->quoteTableName('validator_main')} WHERE ({$schema->quoteColumnName('val_attr_b')}=:qp0) AND (val_attr_a > 0)";
|
||||
$this->assertEquals($expected, $query->createCommand()->getSql());
|
||||
|
||||
$params = ['val_attr_b' => 'test value b'];
|
||||
$query = $this->invokeMethod(new UniqueValidator(['filter' => function($query) {
|
||||
$query->orWhere('val_attr_a > 0');
|
||||
}]), 'prepareQuery', [$model, $params]);
|
||||
$expected = "SELECT * FROM {$schema->quoteTableName('validator_main')} WHERE ({$schema->quoteColumnName('val_attr_b')}=:qp0) OR (val_attr_a > 0)";
|
||||
$this->assertEquals($expected, $query->createCommand()->getSql());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user