Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace deprecated method.
  • Loading branch information
ADmad committed Dec 5, 2017
1 parent 4a1825c commit fc6d4d2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/View/Form/FormContext.php
Expand Up @@ -123,7 +123,7 @@ protected function _schemaDefault($field)
*/
public function isRequired($field)
{
$validator = $this->_form->validator();
$validator = $this->_form->getValidator();
if (!$validator->hasField($field)) {
return false;
}
Expand Down
150 changes: 72 additions & 78 deletions tests/TestCase/View/Form/FormContextTest.php
Expand Up @@ -138,20 +138,18 @@ public function testValDefault()
*/
public function testIsRequired()
{
$this->deprecated(function () {
$form = new Form();
$form->validator()
->requirePresence('name')
->add('email', 'format', ['rule' => 'email']);
$form = new Form();
$form->getValidator()
->requirePresence('name')
->add('email', 'format', ['rule' => 'email']);

$context = new FormContext($this->request, [
'entity' => $form
]);
$this->assertTrue($context->isRequired('name'));
$this->assertTrue($context->isRequired('email'));
$this->assertFalse($context->isRequired('body'));
$this->assertFalse($context->isRequired('Prefix.body'));
});
$context = new FormContext($this->request, [
'entity' => $form
]);
$this->assertTrue($context->isRequired('name'));
$this->assertTrue($context->isRequired('email'));
$this->assertFalse($context->isRequired('body'));
$this->assertFalse($context->isRequired('Prefix.body'));
}

/**
Expand Down Expand Up @@ -208,48 +206,46 @@ public function testAttributes()
*/
public function testError()
{
$this->deprecated(function () {
$nestedValidator = new Validator();
$nestedValidator
->add('password', 'length', ['rule' => ['minLength', 8]])
->add('confirm', 'length', ['rule' => ['minLength', 8]]);
$form = new Form();
$form->validator()
->add('email', 'format', ['rule' => 'email'])
->add('name', 'length', ['rule' => ['minLength', 10]])
->addNested('pass', $nestedValidator);
$form->validate([
'email' => 'derp',
'name' => 'derp',
'pass' => [
'password' => 'short',
'confirm' => 'long enough',
],
]);
$nestedValidator = new Validator();
$nestedValidator
->add('password', 'length', ['rule' => ['minLength', 8]])
->add('confirm', 'length', ['rule' => ['minLength', 8]]);
$form = new Form();
$form->getValidator()
->add('email', 'format', ['rule' => 'email'])
->add('name', 'length', ['rule' => ['minLength', 10]])
->addNested('pass', $nestedValidator);
$form->validate([
'email' => 'derp',
'name' => 'derp',
'pass' => [
'password' => 'short',
'confirm' => 'long enough',
],
]);

$context = new FormContext($this->request, ['entity' => $form]);
$this->assertEquals([], $context->error('empty'));
$this->assertEquals(['The provided value is invalid'], $context->error('email'));
$this->assertEquals(['The provided value is invalid'], $context->error('name'));
$this->assertEquals(['The provided value is invalid'], $context->error('pass.password'));
$this->assertEquals([], $context->error('Alias.name'));
$this->assertEquals([], $context->error('nope.nope'));
$context = new FormContext($this->request, ['entity' => $form]);
$this->assertEquals([], $context->error('empty'));
$this->assertEquals(['The provided value is invalid'], $context->error('email'));
$this->assertEquals(['The provided value is invalid'], $context->error('name'));
$this->assertEquals(['The provided value is invalid'], $context->error('pass.password'));
$this->assertEquals([], $context->error('Alias.name'));
$this->assertEquals([], $context->error('nope.nope'));

$mock = $this->getMockBuilder('Cake\Validation\Validator')
->setMethods(['errors'])
->getMock();
$mock->expects($this->once())
->method('errors')
->willReturn(['key' => 'should be an array, not a string']);
$form->validator($mock);
$form->validate([]);
$context = new FormContext($this->request, ['entity' => $form]);
$this->assertEquals(
['should be an array, not a string'],
$context->error('key'),
'This test should not produce a PHP warning from array_values().'
);
});
$mock = $this->getMockBuilder('Cake\Validation\Validator')
->setMethods(['errors'])
->getMock();
$mock->expects($this->once())
->method('errors')
->willReturn(['key' => 'should be an array, not a string']);
$form->setValidator('default', $mock);
$form->validate([]);
$context = new FormContext($this->request, ['entity' => $form]);
$this->assertEquals(
['should be an array, not a string'],
$context->error('key'),
'This test should not produce a PHP warning from array_values().'
);
}

/**
Expand All @@ -259,31 +255,29 @@ public function testError()
*/
public function testHasError()
{
$this->deprecated(function () {
$nestedValidator = new Validator();
$nestedValidator
->add('password', 'length', ['rule' => ['minLength', 8]])
->add('confirm', 'length', ['rule' => ['minLength', 8]]);
$form = new Form();
$form->validator()
->add('email', 'format', ['rule' => 'email'])
->add('name', 'length', ['rule' => ['minLength', 10]])
->addNested('pass', $nestedValidator);
$form->validate([
'email' => 'derp',
'name' => 'derp',
'pass' => [
'password' => 'short',
'confirm' => 'long enough',
],
]);
$nestedValidator = new Validator();
$nestedValidator
->add('password', 'length', ['rule' => ['minLength', 8]])
->add('confirm', 'length', ['rule' => ['minLength', 8]]);
$form = new Form();
$form->getValidator()
->add('email', 'format', ['rule' => 'email'])
->add('name', 'length', ['rule' => ['minLength', 10]])
->addNested('pass', $nestedValidator);
$form->validate([
'email' => 'derp',
'name' => 'derp',
'pass' => [
'password' => 'short',
'confirm' => 'long enough',
],
]);

$context = new FormContext($this->request, ['entity' => $form]);
$this->assertTrue($context->hasError('email'));
$this->assertTrue($context->hasError('name'));
$this->assertFalse($context->hasError('nope'));
$this->assertFalse($context->hasError('nope.nope'));
$this->assertTrue($context->hasError('pass.password'));
});
$context = new FormContext($this->request, ['entity' => $form]);
$this->assertTrue($context->hasError('email'));
$this->assertTrue($context->hasError('name'));
$this->assertFalse($context->hasError('nope'));
$this->assertFalse($context->hasError('nope.nope'));
$this->assertTrue($context->hasError('pass.password'));
}
}

0 comments on commit fc6d4d2

Please sign in to comment.