Browse Source

Add compareFields() method.

Robert Pustułka 8 years ago
parent
commit
a37d9554df
2 changed files with 51 additions and 6 deletions
  1. 21 1
      src/Validation/Validation.php
  2. 30 5
      tests/TestCase/Validation/ValidationTest.php

+ 21 - 1
src/Validation/Validation.php

@@ -306,14 +306,34 @@ class Validation
      * @param string $field The field to check $check against. This field must be present in $context.
      * @param array $context The validation context.
      * @return bool
+     * @deprecated 3.6.0 Use compareFields() instead.
      */
     public static function compareWith($check, $field, $context)
     {
+        return self::compareFields($check, $field, true, $context);
+    }
+
+    /**
+     * Compare one field to another.
+     *
+     * Return true if the comparison match the expected result.
+     *
+     * @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 array $context The validation context.
+     * @return bool
+     * @since 3.6.0
+     */
+    public static function compareFields($check, $field, $result, $context)
+    {
         if (!isset($context['data'][$field])) {
             return false;
         }
 
-        return $context['data'][$field] === $check;
+        $comparison = $context['data'][$field] === $check;
+
+        return $comparison === $result;
     }
 
     /**

+ 30 - 5
tests/TestCase/Validation/ValidationTest.php

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