Browse Source

coverage improvements

tags/2.0.0-beta
Suralc 12 years ago
parent
commit
06682d0ba9
  1. 10
      tests/unit/data/sqlite.sql
  2. 6
      tests/unit/framework/validators/CompareValidatorTest.php
  3. 17
      tests/unit/framework/validators/FileValidatorTest.php
  4. 20
      tests/unit/framework/validators/RequiredValidatorTest.php
  5. 1
      tests/unit/framework/validators/UniqueValidatorTest.php

10
tests/unit/data/sqlite.sql

@ -95,16 +95,14 @@ DROP TABLE IF EXISTS tbl_validator_main;
DROP TABLE IF EXISTS tbl_validator_ref; DROP TABLE IF EXISTS tbl_validator_ref;
CREATE TABLE tbl_validator_main ( CREATE TABLE tbl_validator_main (
id INT(11) NOT NULL, id INTEGER PRIMARY KEY ,
field1 VARCHAR(255), field1 VARCHAR(255)
PRIMARY KEY (id)
); );
CREATE TABLE tbl_validator_ref ( CREATE TABLE tbl_validator_ref (
id INT(11) NOT NULL, id INTEGER PRIMARY KEY ,
a_field VARCHAR(255), a_field VARCHAR(255),
ref INT(11), ref INT(11)
PRIMARY KEY (id)
); );
INSERT INTO tbl_validator_main (id, field1) VALUES (1, 'just a string1'); INSERT INTO tbl_validator_main (id, field1) VALUES (1, 'just a string1');

6
tests/unit/framework/validators/CompareValidatorTest.php

@ -128,6 +128,12 @@ class CompareValidatorTest extends TestCase
$val->validateAttribute($model, 'attr_test'); $val->validateAttribute($model, 'attr_test');
$this->assertTrue($model->hasErrors('attr_test')); $this->assertTrue($model->hasErrors('attr_test'));
$this->assertFalse($model->hasErrors('attr_test_repeat')); $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() public function testValidateAttributeOperators()

17
tests/unit/framework/validators/FileValidatorTest.php

@ -205,6 +205,23 @@ class FileValidatorTest extends TestCase
$this->assertSame(FileValidator::className() . '::validateFile', $log['messages'][0][2]); $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() protected function createModelForAttributeTest()
{ {
return FakedValidationModel::createWithAttributes( return FakedValidationModel::createWithAttributes(

20
tests/unit/framework/validators/RequiredValidatorTest.php

@ -3,6 +3,7 @@ namespace yiiunit\framework\validators;
use yii\validators\RequiredValidator; use yii\validators\RequiredValidator;
use yiiunit\data\validators\models\FakedValidationModel;
use yiiunit\TestCase; use yiiunit\TestCase;
class RequiredValidatorTest extends TestCase class RequiredValidatorTest extends TestCase
@ -31,4 +32,23 @@ class RequiredValidatorTest extends TestCase
$this->assertFalse($val->validateValue("should fail")); $this->assertFalse($val->validateValue("should fail"));
$this->assertFalse($val->validateValue(true)); $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'));
}
} }

1
tests/unit/framework/validators/UniqueValidatorTest.php

@ -44,6 +44,7 @@ class UniqueValidatorTest extends DatabaseTestCase
$val->validateAttribute($m, 'ref'); $val->validateAttribute($m, 'ref');
$this->assertTrue($m->hasErrors('ref')); $this->assertTrue($m->hasErrors('ref'));
$m = new ValidatorTestRefModel(); $m = new ValidatorTestRefModel();
$m->id = 7;
$m->ref = 12121; $m->ref = 12121;
$val->validateAttribute($m, 'ref'); $val->validateAttribute($m, 'ref');
$this->assertFalse($m->hasErrors('ref')); $this->assertFalse($m->hasErrors('ref'));

Loading…
Cancel
Save