Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new class to avoid calling Validation with incorrect arguments
- Loading branch information
Showing
4 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters