Skip to content

Commit

Permalink
Adding new class to avoid calling Validation with incorrect arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 5, 2014
1 parent 51fa76c commit ae0007f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
58 changes: 58 additions & 0 deletions Cake/Test/TestCase/Validation/RulesProviderTest.php
@@ -0,0 +1,58 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license infValidationation, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Validation;

use Cake\TestSuite\TestCase;
use Cake\Validation\RulesProvider;

/**
* Tests RulesProvider class
*
*/
class RulesProviderTest extends TestCase {

/**
* Tests that RulesProvider proxies the method correctly and removes the
* extra arguments that are passed according to the signature of validation
* methods.
*
* @return void
*/
public function testProxyToValidation() {
$provider = new RulesProvider;
$this->assertTrue($provider->extension('foo.jpg', compact('provider')));
$this->assertFalse($provider->extension('foo.jpg', ['png'], compact('provider')));
}

/**
* Tests that it is possible to use a custom object as the provider to
* be decorated
*
* @return void
*/
public function testCustomObject() {
$mock = $this->getMock('\Cake\Validation\Validator', ['field']);
$mock->expects($this->once())
->method('Field')
->with('first', null)
->will($this->returnValue(true));

$provider = new RulesProvider($mock);
$provider->field('first', compact('provider'));
}

}
6 changes: 3 additions & 3 deletions Cake/Test/TestCase/Validation/ValidatorTest.php
Expand Up @@ -270,7 +270,7 @@ public function testProvider() {
$this->assertSame($validator, $validator->provider('bar', $another));
$this->assertSame($another, $validator->provider('bar'));

$this->assertEquals('\Cake\Validation\Validation', $validator->provider('default'));
$this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
}

/**
Expand Down Expand Up @@ -312,7 +312,7 @@ public function testErrorsFromCustomProvider() {
->will($this->returnCallback(function($data, $providers) use ($thing) {
$this->assertEquals('bar', $data);
$expected = [
'default' => '\Cake\Validation\Validation',
'default' => new \Cake\Validation\RulesProvider,
'thing' => $thing
];
$this->assertEquals($expected, $providers);
Expand Down Expand Up @@ -347,7 +347,7 @@ public function testMethodsWithExtraArguments() {
$this->assertEquals('and', $a);
$this->assertEquals('awesome', $b);
$expected = [
'default' => '\Cake\Validation\Validation',
'default' => new \Cake\Validation\RulesProvider,
'thing' => $thing
];
$this->assertEquals($expected, $providers);
Expand Down
48 changes: 48 additions & 0 deletions Cake/Validation/RulesProvider.php
@@ -0,0 +1,48 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Validation;

/**
* A Proxy class used to remove any extra arguments when the user intended to call
* a method in another class that is not aware of validation providers signature
*/
class RulesProvider {

protected $_class;

/**
* Constructor, sets the default class to use for calling methods
*
* @param string $class the default class to proxy
*/
public function __construct($class = '\Cake\Validation\Validation') {
$this->_class = $class;
}

/**
* Proxies validation method calls to the Validation class, it slices
* the arguments array to avoid passing more arguments than required to
* the validation methods.
*
* @param string $method the validation method to call
* @param array $arguments the list of arguments to pass to the method
* @return boolean whether or not the validation rule passed
*/
public function __call($method, $arguments) {
$arguments = array_slice($arguments, 0, -1);
return call_user_func_array([$this->_class, $method], $arguments);
}

}
3 changes: 2 additions & 1 deletion Cake/Validation/Validator.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Validation;

use Cake\Validation\RulesProvider;
use Cake\Validation\ValidationSet;

/**
Expand Down Expand Up @@ -128,7 +129,7 @@ public function provider($name, $object = null) {
return $this->_providers[$name];
}
if ($name === 'default') {
return $this->_providers[$name] = '\Cake\Validation\Validation';
return $this->_providers[$name] = new RulesProvider;
}
return null;
}
Expand Down

0 comments on commit ae0007f

Please sign in to comment.