From a72d2f536a0ff3c6405253107b1ddf0a6792710f Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 29 Mar 2013 02:50:36 +0400 Subject: [PATCH 1/3] Some tests for Model --- tests/unit/data/base/Speaker.php | 40 ++++++++ tests/unit/framework/base/ModelTest.php | 172 ++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 tests/unit/data/base/Speaker.php create mode 100644 tests/unit/framework/base/ModelTest.php diff --git a/tests/unit/data/base/Speaker.php b/tests/unit/data/base/Speaker.php new file mode 100644 index 0000000..240b691 --- /dev/null +++ b/tests/unit/data/base/Speaker.php @@ -0,0 +1,40 @@ + 'This is the custom label', + ); + } + + public function rules() + { + return array( + + ); + } + + public function scenarios() + { + return array( + 'test' => array('firstName', 'lastName', '!underscore_style'), + ); + } +} diff --git a/tests/unit/framework/base/ModelTest.php b/tests/unit/framework/base/ModelTest.php new file mode 100644 index 0000000..bd10f45 --- /dev/null +++ b/tests/unit/framework/base/ModelTest.php @@ -0,0 +1,172 @@ +assertEquals('First Name', $speaker->getAttributeLabel('firstName')); + $this->assertEquals('This is the custom label', $speaker->getAttributeLabel('customLabel')); + $this->assertEquals('Underscore Style', $speaker->getAttributeLabel('underscore_style')); + } + + public function testGetAttributes() + { + $speaker = new Speaker(); + $speaker->firstName = 'Qiang'; + $speaker->lastName = 'Xue'; + + $this->assertEquals(array( + 'firstName' => 'Qiang', + 'lastName' => 'Xue', + 'customLabel' => null, + 'underscore_style' => null, + ), $speaker->getAttributes()); + + $this->assertEquals(array( + 'firstName' => 'Qiang', + 'lastName' => 'Xue', + ), $speaker->getAttributes(array('firstName', 'lastName'))); + + $this->assertEquals(array( + 'firstName' => 'Qiang', + 'lastName' => 'Xue', + ), $speaker->getAttributes(null, array('customLabel', 'underscore_style'))); + + $this->assertEquals(array( + 'firstName' => 'Qiang', + ), $speaker->getAttributes(array('firstName', 'lastName'), array('lastName', 'customLabel', 'underscore_style'))); + } + + public function testSetAttributes() + { + // by default mass assignment doesn't work at all + $speaker = new Speaker(); + $speaker->setAttributes(array('firstName' => 'Qiang', 'underscore_style' => 'test')); + $this->assertNull($speaker->firstName); + $this->assertNull($speaker->underscore_style); + + // in the test scenario + $speaker = new Speaker(); + $speaker->setScenario('test'); + $speaker->setAttributes(array('firstName' => 'Qiang', 'underscore_style' => 'test')); + $this->assertNull($speaker->underscore_style); + $this->assertEquals('Qiang', $speaker->firstName); + + $speaker->setAttributes(array('firstName' => 'Qiang', 'underscore_style' => 'test'), false); + $this->assertEquals('test', $speaker->underscore_style); + $this->assertEquals('Qiang', $speaker->firstName); + } + + public function testActiveAttributes() + { + // by default mass assignment doesn't work at all + $speaker = new Speaker(); + $this->assertEmpty($speaker->activeAttributes()); + + $speaker = new Speaker(); + $speaker->setScenario('test'); + $this->assertEquals(array('firstName', 'lastName', 'underscore_style'), $speaker->activeAttributes()); + } + + public function testIsAttributeSafe() + { + // by default mass assignment doesn't work at all + $speaker = new Speaker(); + $this->assertFalse($speaker->isAttributeSafe('firstName')); + + $speaker = new Speaker(); + $speaker->setScenario('test'); + $this->assertTrue($speaker->isAttributeSafe('firstName')); + + } + + public function testErrors() + { + $speaker = new Speaker(); + + $this->assertEmpty($speaker->getErrors()); + $this->assertEmpty($speaker->getErrors('firstName')); + $this->assertEmpty($speaker->getFirstErrors()); + + $this->assertFalse($speaker->hasErrors()); + $this->assertFalse($speaker->hasErrors('firstName')); + + $speaker->addError('firstName', 'Something is wrong!'); + $this->assertEquals(array('firstName' => array('Something is wrong!')), $speaker->getErrors()); + $this->assertEquals(array('Something is wrong!'), $speaker->getErrors('firstName')); + + $speaker->addError('firstName', 'Totally wrong!'); + $this->assertEquals(array('firstName' => array('Something is wrong!', 'Totally wrong!')), $speaker->getErrors()); + $this->assertEquals(array('Something is wrong!', 'Totally wrong!'), $speaker->getErrors('firstName')); + + $this->assertTrue($speaker->hasErrors()); + $this->assertTrue($speaker->hasErrors('firstName')); + $this->assertFalse($speaker->hasErrors('lastName')); + + $this->assertEquals(array('Something is wrong!'), $speaker->getFirstErrors()); + $this->assertEquals('Something is wrong!', $speaker->getFirstError('firstName')); + $this->assertNull($speaker->getFirstError('lastName')); + + $speaker->addError('lastName', 'Another one!'); + $this->assertEquals(array( + 'firstName' => array( + 'Something is wrong!', + 'Totally wrong!', + ), + 'lastName' => array('Another one!'), + ), $speaker->getErrors()); + + $speaker->clearErrors('firstName'); + $this->assertEquals(array( + 'lastName' => array('Another one!'), + ), $speaker->getErrors()); + + $speaker->clearErrors(); + $this->assertEmpty($speaker->getErrors()); + $this->assertFalse($speaker->hasErrors()); + } + + public function testArraySyntax() + { + $speaker = new Speaker(); + + // get + $this->assertNull($speaker['firstName']); + + // isset + $this->assertFalse(isset($speaker['firstName'])); + + // set + $speaker['firstName'] = 'Qiang'; + + $this->assertEquals('Qiang', $speaker['firstName']); + $this->assertTrue(isset($speaker['firstName'])); + + // iteration + $attributes = array(); + foreach($speaker as $key => $attribute) { + $attributes[$key] = $attribute; + } + $this->assertEquals(array( + 'firstName' => 'Qiang', + 'lastName' => null, + 'customLabel' => null, + 'underscore_style' => null, + ), $attributes); + + // unset + unset($speaker['firstName']); + + // exception isn't expected here + $this->assertNull($speaker['firstName']); + $this->assertFalse(isset($speaker['firstName'])); + } +} From 7b73fdff5c1e473ebda39ea6a12526e12b9033b5 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 29 Mar 2013 03:22:19 +0400 Subject: [PATCH 2/3] Better validation rules validity check --- framework/base/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/base/Model.php b/framework/base/Model.php index 7818293..13e567d 100644 --- a/framework/base/Model.php +++ b/framework/base/Model.php @@ -329,7 +329,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess foreach ($this->rules() as $rule) { if ($rule instanceof Validator) { $validators->add($rule); - } elseif (isset($rule[0], $rule[1])) { // attributes, validator type + } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type $validator = Validator::createValidator($rule[1], $this, $rule[0], array_slice($rule, 2)); $validators->add($validator); } else { From 99238886378845741f22d1dfc15f2970d72202ef Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 29 Mar 2013 03:22:50 +0400 Subject: [PATCH 3/3] More Model tests --- tests/unit/data/base/InvalidRulesModel.php | 17 ++++++++++++++++ tests/unit/data/base/Singer.php | 21 ++++++++++++++++++++ tests/unit/data/base/Speaker.php | 3 +-- tests/unit/framework/base/ModelTest.php | 31 ++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/unit/data/base/InvalidRulesModel.php create mode 100644 tests/unit/data/base/Singer.php diff --git a/tests/unit/data/base/InvalidRulesModel.php b/tests/unit/data/base/InvalidRulesModel.php new file mode 100644 index 0000000..f5a8438 --- /dev/null +++ b/tests/unit/data/base/InvalidRulesModel.php @@ -0,0 +1,17 @@ + 'Lennon'), + array('lastName', 'required'), + array('underscore_style', 'yii\validators\CaptchaValidator'), + ); + } +} \ No newline at end of file diff --git a/tests/unit/data/base/Speaker.php b/tests/unit/data/base/Speaker.php index 240b691..93dd496 100644 --- a/tests/unit/data/base/Speaker.php +++ b/tests/unit/data/base/Speaker.php @@ -1,11 +1,10 @@ assertNull($speaker['firstName']); $this->assertFalse(isset($speaker['firstName'])); } + + public function testDefaults() + { + $singer = new Model(); + $this->assertEquals(array(), $singer->rules()); + $this->assertEquals(array(), $singer->attributeLabels()); + } + + public function testDefaultScenarios() + { + $singer = new Singer(); + $this->assertEquals(array('default' => array('lastName', 'underscore_style')), $singer->scenarios()); + } + + public function testIsAttributeRequired() + { + $singer = new Singer(); + $this->assertFalse($singer->isAttributeRequired('firstName')); + $this->assertTrue($singer->isAttributeRequired('lastName')); + } + + public function testCreateValidators() + { + $this->setExpectedException('yii\base\InvalidConfigException', 'Invalid validation rule: a rule must be an array specifying both attribute names and validator type.'); + + $invalid = new InvalidRulesModel(); + $invalid->createValidators(); + } }