From 897e5bad97dc743e7708040e08a463803dd19f4b Mon Sep 17 00:00:00 2001 From: WanWizard Date: Sat, 24 Oct 2015 22:14:20 +0100 Subject: [PATCH 1/2] added the "RequiredIf" rule Checks that the given field exists if the conditional field is passed as well, and is not empty --- src/Rule/RequiredIf.php | 45 ++++++++++++++++++++++++++++++ tests/unit/Rule/RequiredIfTest.php | 34 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/Rule/RequiredIf.php create mode 100644 tests/unit/Rule/RequiredIfTest.php diff --git a/src/Rule/RequiredIf.php b/src/Rule/RequiredIf.php new file mode 100644 index 0000000..09a3746 --- /dev/null +++ b/src/Rule/RequiredIf.php @@ -0,0 +1,45 @@ +getParameter(); + + if ($allFields !== null and array_key_exists($requiredField, $allFields) and ! empty($allFields[$requiredField])) + { + return parent::validate($value, $field, $allFields); + } + + return true; + } + +} diff --git a/tests/unit/Rule/RequiredIfTest.php b/tests/unit/Rule/RequiredIfTest.php new file mode 100644 index 0000000..651eb3d --- /dev/null +++ b/tests/unit/Rule/RequiredIfTest.php @@ -0,0 +1,34 @@ +object = new RequiredIf('field'); + } + + /** + * {@inheritdocs} + */ + public function validateProvider() + { + return array( + array('value', null, true, array('field' => 'value')), + array('', null, true, null), + array('value', null, true, array('field' => 'othervalue')), + array('value', null, false, array('field' => '')), + ); + } + +} From 0e4ad256d5f72a640ef59b86a4c7aab341d16d02 Mon Sep 17 00:00:00 2001 From: "Steve \"uru\" West" Date: Sun, 25 Oct 2015 12:24:37 +0000 Subject: [PATCH 2/2] Moves abstract classes to helper folder for better orgnisation and autoloading. [skip ci] --- .travis.yml | 2 + tests/_helpers/AbstractRequiredTest.php | 102 ++++++++++++++++++ .../Rule => _helpers}/AbstractRuleTest.php | 0 tests/_helpers/_generated/CodeGuyActions.php | 2 +- tests/unit/Rule/RequiredIfTest.php | 2 +- tests/unit/Rule/RequiredTest.php | 88 +-------------- tests/unit/Rule/RequiredWithTest.php | 2 +- 7 files changed, 108 insertions(+), 90 deletions(-) create mode 100644 tests/_helpers/AbstractRequiredTest.php rename tests/{unit/Rule => _helpers}/AbstractRuleTest.php (100%) diff --git a/.travis.yml b/.travis.yml index 3dce44e..5b1c333 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: php +sudo: false + php: - 5.4 - 5.5 diff --git a/tests/_helpers/AbstractRequiredTest.php b/tests/_helpers/AbstractRequiredTest.php new file mode 100644 index 0000000..6a0339c --- /dev/null +++ b/tests/_helpers/AbstractRequiredTest.php @@ -0,0 +1,102 @@ +object = new Required; + } + + /** + * @dataProvider validateProvider + */ + public function testValidate() + { + list($value, $field, $expected, $data) = func_get_args(); + + $this->assertEquals( + $expected, + $this->object->validate($value, $field, $data) + ); + } + + /** + * {@inheritdocs} + */ + public function validateProvider() + { + return array( + 0 => array('admin@test.com', null, false, null), + 1 => array('', null, false, null), + 2 => array(array(), null, false, null), + 3 => array(null, null, false, null), + 4 => array(false, null, false, null), + + 5 => array('test string 5', 'test', false, + array() + ), + + 6 => array('test string 6', 'test', false, + array( + 'foo' => 'bar', + 'baz' => 'bat', + ) + ), + + 7 => array('test string 7', 'test', true, + array( + 'foo' => 'bar', + 'test' => 'value', + 'baz' => 'bat', + ) + ), + + 8 => array('bla', 'test', true, null), + 9 => array('', 'test', false, null), + 10 => array('bla', null, false, array()), + 11 => array('', null, false, array()), + + 12 => array(false, 'foo', true, + array( + 'foo' => false, + ) + ), + 13 => array(true, 'foo', true, + array( + 'foo' => true, + ) + ), + + 14 => array(false, 'bar', true, + array( + 'bar' => false, + ) + ), + 15 => array(0, 'test', true, + array( + 'foo' => 'bar', + 'test' => 0, + 'baz' => 'bat', + ) + ), + 16 => array(0, 'test', true, array('test' => 0)), + ); + } + +} diff --git a/tests/unit/Rule/AbstractRuleTest.php b/tests/_helpers/AbstractRuleTest.php similarity index 100% rename from tests/unit/Rule/AbstractRuleTest.php rename to tests/_helpers/AbstractRuleTest.php diff --git a/tests/_helpers/_generated/CodeGuyActions.php b/tests/_helpers/_generated/CodeGuyActions.php index a7b2974..5a69424 100644 --- a/tests/_helpers/_generated/CodeGuyActions.php +++ b/tests/_helpers/_generated/CodeGuyActions.php @@ -1,4 +1,4 @@ -object = new Required; - } - - /** - * @dataProvider validateProvider - */ - public function testValidate() - { - list($value, $field, $expected, $data) = func_get_args(); - - $this->assertEquals( - $expected, - $this->object->validate($value, $field, $data) - ); - } - - /** - * {@inheritdocs} - */ - public function validateProvider() - { - return array( - 0 => array('admin@test.com', null, false, null), - 1 => array('', null, false, null), - 2 => array(array(), null, false, null), - 3 => array(null, null, false, null), - 4 => array(false, null, false, null), - - 5 => array('test string 5', 'test', false, - array() - ), - - 6 => array('test string 6', 'test', false, - array( - 'foo' => 'bar', - 'baz' => 'bat', - ) - ), - - 7 => array('test string 7', 'test', true, - array( - 'foo' => 'bar', - 'test' => 'value', - 'baz' => 'bat', - ) - ), - - 8 => array('bla', 'test', true, null), - 9 => array('', 'test', false, null), - 10 => array('bla', null, false, array()), - 11 => array('', null, false, array()), - - 12 => array(false, 'foo', true, - array( - 'foo' => false, - ) - ), - 13 => array(true, 'foo', true, - array( - 'foo' => true, - ) - ), - - 14 => array(false, 'bar', true, - array( - 'bar' => false, - ) - ), - 15 => array(0, 'test', true, - array( - 'foo' => 'bar', - 'test' => 0, - 'baz' => 'bat', - ) - ), - 16 => array(0, 'test', true, array('test' => 0)), - ); - } - } diff --git a/tests/unit/Rule/RequiredWithTest.php b/tests/unit/Rule/RequiredWithTest.php index 8c8bf57..cf4b8ee 100644 --- a/tests/unit/Rule/RequiredWithTest.php +++ b/tests/unit/Rule/RequiredWithTest.php @@ -10,7 +10,7 @@ namespace Fuel\Validation\Rule; -class RequiredWithTest extends RequiredTest +class RequiredWithTest extends AbstractRequiredTest { protected function _before()