Browse Source

Add accessor for protected properties of ValidationRule

ADmad 11 years ago
parent
commit
a89bf35e77
2 changed files with 35 additions and 0 deletions
  1. 14 0
      src/Validation/ValidationRule.php
  2. 21 0
      tests/TestCase/Validation/ValidationRuleTest.php

+ 14 - 0
src/Validation/ValidationRule.php

@@ -193,4 +193,18 @@ class ValidationRule
             }
         }
     }
+
+    /**
+     * Returns the value of a property by name
+     *
+     * @param string $property The name of the property to retrieve.
+     * @return mixed
+     */
+    public function get($property)
+    {
+        $property = '_' . $property;
+        if (isset($this->{$property})) {
+            return $this->{$property};
+        }
+    }
 }

+ 21 - 0
tests/TestCase/Validation/ValidationRuleTest.php

@@ -171,4 +171,25 @@ class ValidationRuleTest extends TestCase
         ]);
         $this->assertTrue($Rule->process($data, $providers, ['newRecord' => true]));
     }
+
+    /**
+     * testGet
+     *
+     * @return void
+     */
+    public function testGet() {
+        $Rule = new ValidationRule(['rule' => 'myTestRule', 'message' => 'foo']);
+
+        $this->assertEquals('myTestRule', $Rule->get('rule'));
+        $this->assertEquals('foo', $Rule->get('message'));
+        $this->assertEquals('default', $Rule->get('provider'));
+        $this->assertEquals([], $Rule->get('pass'));
+        $this->assertNull($Rule->get('non-existent'));
+
+        $Rule = new ValidationRule(['rule' => ['myTestRule2', 'param'], 'message' => 'bar']);
+
+        $this->assertEquals('myTestRule2', $Rule->get('rule'));
+        $this->assertEquals('bar', $Rule->get('message'));
+        $this->assertEquals(['param'], $Rule->get('pass'));
+    }
 }