ソースを参照

Leverage `Validation::comparison()` for field value comparison
in `Validation::compareFields()` method.

Robert Pustułka 8 年 前
コミット
c58a1e135e

+ 4 - 6
src/Validation/Validation.php

@@ -314,7 +314,7 @@ class Validation
      */
     public static function compareWith($check, $field, $context)
     {
-        return self::compareFields($check, $field, true, $context);
+        return self::compareFields($check, $field, 'equalto', $context);
     }
 
     /**
@@ -324,20 +324,18 @@ class Validation
      *
      * @param mixed $check The value to find in $field.
      * @param string $field The field to check $check against. This field must be present in $context.
-     * @param bool $result The expected result of field comparison.
+     * @param string $operator Comparison operator.
      * @param array $context The validation context.
      * @return bool
      * @since 3.6.0
      */
-    public static function compareFields($check, $field, $result, $context)
+    public static function compareFields($check, $field, $operator, $context)
     {
         if (!isset($context['data'][$field])) {
             return false;
         }
 
-        $comparison = $context['data'][$field] === $check;
-
-        return $comparison === $result;
+        return static::comparison($check, $operator, $context['data'][$field]);
     }
 
     /**

+ 2 - 2
src/Validation/Validator.php

@@ -983,7 +983,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'sameAs', $extra + [
-            'rule' => ['compareFields', $secondField, true]
+            'rule' => ['compareFields', $secondField, 'equalto']
         ]);
     }
 
@@ -1004,7 +1004,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'notSameAs', $extra + [
-            'rule' => ['compareFields', $secondField, false]
+            'rule' => ['compareFields', $secondField, 'notequal']
         ]);
     }
 

+ 10 - 10
tests/TestCase/Validation/ValidationTest.php

@@ -2742,53 +2742,53 @@ class ValidationTest extends TestCase
     }
 
     /**
-     * Test the compareFields method with true result.
+     * Test the compareFields method with eual result.
      *
      * @return void
      */
-    public function testCompareFieldsTrue()
+    public function testCompareFieldsEqualTo()
     {
         $context = [
             'data' => [
                 'other' => 'a value'
             ]
         ];
-        $this->assertTrue(Validation::compareFields('a value', 'other', true, $context));
+        $this->assertTrue(Validation::compareFields('a value', 'other', 'equalto', $context));
 
         $context = [
             'data' => [
                 'other' => 'different'
             ]
         ];
-        $this->assertFalse(Validation::compareFields('a value', 'other', true, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', 'equalto', $context));
 
         $context = [];
-        $this->assertFalse(Validation::compareFields('a value', 'other', true, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', 'equalto', $context));
     }
 
     /**
-     * Test the compareFields method with false result.
+     * Test the compareFields method with not equal result.
      *
      * @return void
      */
-    public function testCompareFieldsFalse()
+    public function testCompareFieldsNotEqual()
     {
         $context = [
             'data' => [
                 'other' => 'different'
             ]
         ];
-        $this->assertTrue(Validation::compareFields('a value', 'other', false, $context));
+        $this->assertTrue(Validation::compareFields('a value', 'other', 'notequal', $context));
 
         $context = [
             'data' => [
                 'other' => 'a value'
             ]
         ];
-        $this->assertFalse(Validation::compareFields('a value', 'other', false, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', 'notequal', $context));
 
         $context = [];
-        $this->assertFalse(Validation::compareFields('a value', 'other', false, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', 'notequal', $context));
     }
 
     /**

+ 2 - 2
tests/TestCase/Validation/ValidatorTest.php

@@ -1425,7 +1425,7 @@ class ValidatorTest extends TestCase
     public function testSameAs()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'sameAs', 'other', ['other', true], 'compareFields');
+        $this->assertProxyMethod($validator, 'sameAs', 'other', ['other', 'equalto'], 'compareFields');
         $this->assertNotEmpty($validator->errors(['username' => 'foo']));
     }
 
@@ -1437,7 +1437,7 @@ class ValidatorTest extends TestCase
     public function testNotSameAs()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', false], 'compareFields');
+        $this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', 'notequal'], 'compareFields');
         $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
     }