Browse Source

Replace deprecated method.

ADmad 8 years ago
parent
commit
fc6d4d2ca4
2 changed files with 73 additions and 79 deletions
  1. 1 1
      src/View/Form/FormContext.php
  2. 72 78
      tests/TestCase/View/Form/FormContextTest.php

+ 1 - 1
src/View/Form/FormContext.php

@@ -123,7 +123,7 @@ class FormContext implements ContextInterface
      */
     public function isRequired($field)
     {
-        $validator = $this->_form->validator();
+        $validator = $this->_form->getValidator();
         if (!$validator->hasField($field)) {
             return false;
         }

+ 72 - 78
tests/TestCase/View/Form/FormContextTest.php

@@ -138,20 +138,18 @@ class FormContextTest extends TestCase
      */
     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'));
     }
 
     /**
@@ -208,48 +206,46 @@ class FormContextTest extends TestCase
      */
     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().'
+        );
     }
 
     /**
@@ -259,31 +255,29 @@ class FormContextTest extends TestCase
      */
     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'));
     }
 }