Skip to content

Commit

Permalink
Cleanup form test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Jul 16, 2016
1 parent 18eaefc commit 4c91d5d
Showing 1 changed file with 47 additions and 60 deletions.
107 changes: 47 additions & 60 deletions tests/unit/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Phalcon\Test\Unit\Forms;

use Phalcon\Test\Module\UnitTest;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Validation\Message;
use Phalcon\Test\Module\UnitTest;
use Phalcon\Validation\Message\Group;
use Phalcon\Validation\Validator\Regex;

/**
Expand Down Expand Up @@ -43,65 +44,51 @@ protected function _after()
parent::_after();
}



/**
* Tests Form::hasMessagesFor
*
* @author Sid Roberts <Sid@SidRoberts.co.uk>
* @since 2016-04-03
*/
public function testFormHasMessagesFor()
{
$this->specify(
"hasMessagesFor works",
function () {
//First element
$telephone = new Text("telephone");

$telephone->addValidators(array(
new Regex(array(
'pattern' => '/\+44 [0-9]+ [0-9]+/',
'message' => 'The telephone has an invalid format'
))
));

//Second element
$address = new Text('address');

$form = new Form();

$form->add($telephone);
$form->add($address);

expect($form->isValid(array(
'telephone' => '12345',
'address' => 'hello'
)))->false();

expect($form->getMessagesFor("telephone"))->equals(
\Phalcon\Validation\Message\Group::__set_state(
array(
'_messages' => array(
0 => \Phalcon\Validation\Message::__set_state(
array(
'_type' => 'Regex',
'_message' => 'The telephone has an invalid format',
'_field' => 'telephone',
'_code' => 0,
)
),
),
)
)
);

expect($form->getMessagesFor("address"))->equals(
\Phalcon\Validation\Message\Group::__set_state(
array(
'_messages' => array(),
)
)
);

expect($form->hasMessagesFor("telephone"))->true();

expect($form->hasMessagesFor("address"))->false();
}
);
$this->specify('Form::hasMessagesFor does not check correctly if the Group is empty', function () {
// First element
$telephone = new Text('telephone');

$telephone->addValidators([
new Regex([
'pattern' => '/\+44 [0-9]+ [0-9]+/',
'message' => 'The telephone has an invalid format'
])
]);

// Second element
$address = new Text('address');
$form = new Form();

$form->add($telephone);
$form->add($address);

expect($form->isValid(['telephone' => '12345', 'address' => 'hello']))->false();

expect($form->getMessagesFor('telephone'))->equals(
Group::__set_state([
'_messages' => [
Message::__set_state([
'_type' => 'Regex',
'_message' => 'The telephone has an invalid format',
'_field' => 'telephone',
'_code' => 0,
])
],
])
);

expect($form->getMessagesFor('address'))->equals(Group::__set_state(['_messages' => []]));
expect($form->hasMessagesFor('telephone'))->true();
expect($form->hasMessagesFor('address'))->false();

});
}
}

0 comments on commit 4c91d5d

Please sign in to comment.