From 06682d0ba9407279c06005ccf96e09d04e1f5002 Mon Sep 17 00:00:00 2001 From: Suralc Date: Sat, 24 Aug 2013 00:05:55 +0200 Subject: [PATCH] coverage improvements --- tests/unit/data/sqlite.sql | 10 ++++------ .../framework/validators/CompareValidatorTest.php | 6 ++++++ .../unit/framework/validators/FileValidatorTest.php | 17 +++++++++++++++++ .../framework/validators/RequiredValidatorTest.php | 20 ++++++++++++++++++++ .../framework/validators/UniqueValidatorTest.php | 1 + 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/tests/unit/data/sqlite.sql b/tests/unit/data/sqlite.sql index 8976519..71eaf46 100644 --- a/tests/unit/data/sqlite.sql +++ b/tests/unit/data/sqlite.sql @@ -95,16 +95,14 @@ DROP TABLE IF EXISTS tbl_validator_main; DROP TABLE IF EXISTS tbl_validator_ref; CREATE TABLE tbl_validator_main ( - id INT(11) NOT NULL, - field1 VARCHAR(255), - PRIMARY KEY (id) + id INTEGER PRIMARY KEY , + field1 VARCHAR(255) ); CREATE TABLE tbl_validator_ref ( - id INT(11) NOT NULL, + id INTEGER PRIMARY KEY , a_field VARCHAR(255), - ref INT(11), - PRIMARY KEY (id) + ref INT(11) ); INSERT INTO tbl_validator_main (id, field1) VALUES (1, 'just a string1'); diff --git a/tests/unit/framework/validators/CompareValidatorTest.php b/tests/unit/framework/validators/CompareValidatorTest.php index 46ab383..5a16a30 100644 --- a/tests/unit/framework/validators/CompareValidatorTest.php +++ b/tests/unit/framework/validators/CompareValidatorTest.php @@ -128,6 +128,12 @@ class CompareValidatorTest extends TestCase $val->validateAttribute($model, 'attr_test'); $this->assertTrue($model->hasErrors('attr_test')); $this->assertFalse($model->hasErrors('attr_test_repeat')); + // not existing op + $val = new CompareValidator(); + $val->operator = '<>'; + $model = FakedValidationModel::createWithAttributes(array('attr_o' => 5, 'attr_o_repeat' => 5 )); + $val->validateAttribute($model, 'attr_o'); + $this->assertTrue($model->hasErrors('attr_o')); } public function testValidateAttributeOperators() diff --git a/tests/unit/framework/validators/FileValidatorTest.php b/tests/unit/framework/validators/FileValidatorTest.php index 820afb5..4d74383 100644 --- a/tests/unit/framework/validators/FileValidatorTest.php +++ b/tests/unit/framework/validators/FileValidatorTest.php @@ -205,6 +205,23 @@ class FileValidatorTest extends TestCase $this->assertSame(FileValidator::className() . '::validateFile', $log['messages'][0][2]); } + public function testValidateAttributeType() + { + $val = new FileValidator(array('types' => 'jpeg, jpg')); + $m = FakedValidationModel::createWithAttributes( + array( + 'attr_jpg' => $this->createTestFiles(array(array('name' => 'one.jpeg'))), + 'attr_exe' => $this->createTestFiles(array(array('name' => 'bad.exe'))), + ) + ); + $val->validateAttribute($m, 'attr_jpg'); + $this->assertFalse($m->hasErrors('attr_jpg')); + $val->validateAttribute($m, 'attr_exe'); + $this->assertTrue($m->hasErrors('attr_exe')); + $this->assertTrue(stripos(current($m->getErrors('attr_exe')), 'Only files with these extensions ') !== false); + } + + protected function createModelForAttributeTest() { return FakedValidationModel::createWithAttributes( diff --git a/tests/unit/framework/validators/RequiredValidatorTest.php b/tests/unit/framework/validators/RequiredValidatorTest.php index 198727b..22c9d0b 100644 --- a/tests/unit/framework/validators/RequiredValidatorTest.php +++ b/tests/unit/framework/validators/RequiredValidatorTest.php @@ -3,6 +3,7 @@ namespace yiiunit\framework\validators; use yii\validators\RequiredValidator; +use yiiunit\data\validators\models\FakedValidationModel; use yiiunit\TestCase; class RequiredValidatorTest extends TestCase @@ -31,4 +32,23 @@ class RequiredValidatorTest extends TestCase $this->assertFalse($val->validateValue("should fail")); $this->assertFalse($val->validateValue(true)); } + + public function testValidateAttribute() + { + // empty req-value + $val = new RequiredValidator(); + $m = FakedValidationModel::createWithAttributes(array('attr_val' => null)); + $val->validateAttribute($m, 'attr_val'); + $this->assertTrue($m->hasErrors('attr_val')); + $this->assertTrue(stripos(current($m->getErrors('attr_val')), 'blank') !== false); + $val = new RequiredValidator(array('requiredValue' => 55)); + $m = FakedValidationModel::createWithAttributes(array('attr_val' => 56)); + $val->validateAttribute($m, 'attr_val'); + $this->assertTrue($m->hasErrors('attr_val')); + $this->assertTrue(stripos(current($m->getErrors('attr_val')), 'must be') !== false); + $val = new RequiredValidator(array('requiredValue' => 55)); + $m = FakedValidationModel::createWithAttributes(array('attr_val' => 55)); + $val->validateAttribute($m, 'attr_val'); + $this->assertFalse($m->hasErrors('attr_val')); + } } \ No newline at end of file diff --git a/tests/unit/framework/validators/UniqueValidatorTest.php b/tests/unit/framework/validators/UniqueValidatorTest.php index 325a15c..b0d3554 100644 --- a/tests/unit/framework/validators/UniqueValidatorTest.php +++ b/tests/unit/framework/validators/UniqueValidatorTest.php @@ -44,6 +44,7 @@ class UniqueValidatorTest extends DatabaseTestCase $val->validateAttribute($m, 'ref'); $this->assertTrue($m->hasErrors('ref')); $m = new ValidatorTestRefModel(); + $m->id = 7; $m->ref = 12121; $val->validateAttribute($m, 'ref'); $this->assertFalse($m->hasErrors('ref'));