Skip to content

Commit

Permalink
Merge branch 'master' into php7
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve "uru" West committed Jan 26, 2016
2 parents 20b0e87 + 0e4ad25 commit c4f39d3
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 89 deletions.
45 changes: 45 additions & 0 deletions src/Rule/RequiredIf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @package Fuel\Validation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2013 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Validation\Rule;

/**
* Checks that the given field exists if the conditional field is passed and is not empty
*
* @package Fuel\Validation\Rule
* @author Fuel Development Team
*
* @since 2.0
*/
class RequiredIf extends Required
{

/**
* @param mixed $value
* @param null $field
* @param null $allFields
*
* @return bool
*
* @since 2.0
*/
public function validate($value, $field = null, $allFields = null)
{
$requiredField = $this->getParameter();

if ($allFields !== null and array_key_exists($requiredField, $allFields) and ! empty($allFields[$requiredField]))
{
return parent::validate($value, $field, $allFields);
}

return true;
}

}
102 changes: 102 additions & 0 deletions tests/_helpers/AbstractRequiredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* @package Fuel\Validation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2013 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Validation\Rule;

class AbstractRequiredTest extends AbstractRuleTest
{

/**
* {@inheritdocs}
*/
protected $message = 'The field is required and has not been specified.';

protected function _before()
{
$this->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('[email protected]', 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)),
);
}

}
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/_helpers/_generated/CodeGuyActions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php //[STAMP] 8cfe98b3e6ddaa718333872e2edd6e01
<?php //[STAMP] a8cf3c7461a512a056a510cd9f14df23
namespace _generated;

// This class was automatically generated by build task
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/Rule/RequiredIfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @package Fuel\Validation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2013 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Validation\Rule;

class RequiredIfTest extends AbstractRequiredTest
{

protected function _before()
{
$this->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' => '')),
);
}

}
88 changes: 1 addition & 87 deletions tests/unit/Rule/RequiredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,7 @@

namespace Fuel\Validation\Rule;

class RequiredTest extends AbstractRuleTest
class RequiredTest extends AbstractRequiredTest
{

/**
* {@inheritdocs}
*/
protected $message = 'The field is required and has not been specified.';

protected function _before()
{
$this->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('[email protected]', 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)),
);
}

}
2 changes: 1 addition & 1 deletion tests/unit/Rule/RequiredWithTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Fuel\Validation\Rule;

class RequiredWithTest extends RequiredTest
class RequiredWithTest extends AbstractRequiredTest
{

protected function _before()
Expand Down

0 comments on commit c4f39d3

Please sign in to comment.