Skip to content

Commit

Permalink
Renaming scope to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent d64f0c5 commit 78724d7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cake/ORM/Table.php
Expand Up @@ -1056,7 +1056,7 @@ protected function _update($entity, $data) {
$statement->closeCursor();
return $success;
}

/**
* Validates the $entity if the 'validate' key is not set to false in $options
* If not empty it will construct a default validation object or get one with
Expand Down
36 changes: 18 additions & 18 deletions Cake/Test/TestCase/Validation/ValidationRuleTest.php
Expand Up @@ -61,19 +61,19 @@ public function myTestRule3() {
*/
public function testCustomMethods() {
$data = 'some data';
$scopes = ['default' => $this];
$providers = ['default' => $this];

$Rule = new ValidationRule(['rule' => 'myTestRule']);
$this->assertFalse($Rule->process($data, $scopes, true));
$this->assertFalse($Rule->process($data, $providers, true));

$Rule = new ValidationRule(['rule' => 'myTestRule2']);
$this->assertTrue($Rule->process($data, $scopes, true));
$this->assertTrue($Rule->process($data, $providers, true));

$Rule = new ValidationRule(['rule' => 'myTestRule3']);
$this->assertEquals('string', $Rule->process($data, $scopes, true));
$this->assertEquals('string', $Rule->process($data, $providers, true));

$Rule = new ValidationRule(['rule' => 'myTestRule', 'message' => 'foo']);
$this->assertEquals('foo', $Rule->process($data, $scopes, true));
$this->assertEquals('foo', $Rule->process($data, $providers, true));
}

/**
Expand All @@ -86,10 +86,10 @@ public function testCustomMethods() {
public function testCustomMethodMissingError() {
$def = ['rule' => ['totallyMissing']];
$data = 'some data';
$scopes = ['default' => $this];
$providers = ['default' => $this];

$Rule = new ValidationRule($def);
$Rule->process($data, $scopes, true);
$Rule->process($data, $providers, true);
}

/**
Expand All @@ -99,25 +99,25 @@ public function testCustomMethodMissingError() {
*/
public function testSkip() {
$data = 'some data';
$scopes = ['default' => $this];
$providers = ['default' => $this];

$Rule = new ValidationRule([
'rule' => 'myTestRule',
'on' => 'create'
]);
$this->assertFalse($Rule->process($data, $scopes, true));
$this->assertFalse($Rule->process($data, $providers, true));

$Rule = new ValidationRule([
'rule' => 'myTestRule',
'on' => 'update'
]);
$this->assertTrue($Rule->process($data, $scopes, true));
$this->assertTrue($Rule->process($data, $providers, true));

$Rule = new ValidationRule([
'rule' => 'myTestRule',
'on' => 'update'
]);
$this->assertFalse($Rule->process($data, $scopes, false));
$this->assertFalse($Rule->process($data, $providers, false));
}

/**
Expand All @@ -127,24 +127,24 @@ public function testSkip() {
*/
public function testCallableOn() {
$data = 'some data';
$scopes = ['default' => $this];
$providers = ['default' => $this];

$Rule = new ValidationRule([
'rule' => 'myTestRule',
'on' => function($s) use ($scopes) {
$this->assertEquals($scopes, $s);
'on' => function($s) use ($providers) {
$this->assertEquals($providers, $s);
return true;
}
]);
$this->assertFalse($Rule->process($data, $scopes, true));
$this->assertFalse($Rule->process($data, $providers, true));

$Rule = new ValidationRule([
'rule' => 'myTestRule',
'on' => function($s) use ($scopes) {
$this->assertEquals($scopes, $s);
'on' => function($s) use ($providers) {
$this->assertEquals($providers, $s);
return false;
}
]);
$this->assertTrue($Rule->process($data, $scopes, true));
$this->assertTrue($Rule->process($data, $providers, true));
}
}
54 changes: 27 additions & 27 deletions Cake/Test/TestCase/Validation/ValidatorTest.php
Expand Up @@ -207,31 +207,31 @@ public function testErrorsWithEmptyAllowed() {
}

/**
* Test the scope() method
* Test the provider() method
*
* @return void
*/
public function testScope() {
public function testProvider() {
$validator = new Validator;
$object = new \stdClass;
$this->assertSame($validator, $validator->scope('foo', $object));
$this->assertSame($object, $validator->scope('foo'));
$this->assertNull($validator->scope('bar'));
$this->assertSame($validator, $validator->provider('foo', $object));
$this->assertSame($object, $validator->provider('foo'));
$this->assertNull($validator->provider('bar'));

$another = new \stdClass;
$this->assertSame($validator, $validator->scope('bar', $another));
$this->assertSame($another, $validator->scope('bar'));
$this->assertSame($validator, $validator->provider('bar', $another));
$this->assertSame($another, $validator->provider('bar'));

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

/**
* Tests errors() method when using validators from the default scope, this proves
* Tests errors() method when using validators from the default provider, this proves
* that it returns a default validation message and the custom one set in the rule
*
* @return void
*/
public function testErrorsFromDefaultScope() {
public function testErrorsFromDefaultProvider() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
Expand All @@ -248,30 +248,30 @@ public function testErrorsFromDefaultScope() {
}

/**
* Tests using validation methods from different scopes and returning the error
* Tests using validation methods from different providers and returning the error
* as a string
*
* @return void
*/
public function testErrorsFromCustomScope() {
public function testErrorsFromCustomProvider() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'scope' => 'thing']);
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);

$thing = $this->getMock('\stdClass', ['isCool']);
$thing->expects($this->once())->method('isCool')
->will($this->returnCallback(function($data, $scopes) use ($thing) {
->will($this->returnCallback(function($data, $providers) use ($thing) {
$this->assertEquals('bar', $data);
$expected = [
'default' => '\Cake\Validation\Validation',
'thing' => $thing
];
$this->assertEquals($expected, $scopes);
$this->assertEquals($expected, $providers);
return "That ain't cool, yo";
}));

$validator->scope('thing', $thing);
$validator->provider('thing', $thing);
$errors = $validator->errors(['email' => '!', 'title' => 'bar']);
$expected = [
'email' => ['alpha' => 'The provided value is invalid'],
Expand All @@ -282,30 +282,30 @@ public function testErrorsFromCustomScope() {

/**
* Tests that it is possible to pass extra arguments to the validation function
* and it still gets the scopes as last argument
* and it still gets the providers as last argument
*
* @return void
*/
public function testMethodsWithExtraArguments() {
$validator = new Validator;
$validator->add('title', 'cool', [
'rule' => ['isCool', 'and', 'awesome'],
'scope' => 'thing'
'provider' => 'thing'
]);
$thing = $this->getMock('\stdClass', ['isCool']);
$thing->expects($this->once())->method('isCool')
->will($this->returnCallback(function($data, $a, $b, $scopes) use ($thing) {
->will($this->returnCallback(function($data, $a, $b, $providers) use ($thing) {
$this->assertEquals('bar', $data);
$this->assertEquals('and', $a);
$this->assertEquals('awesome', $b);
$expected = [
'default' => '\Cake\Validation\Validation',
'thing' => $thing
];
$this->assertEquals($expected, $scopes);
$this->assertEquals($expected, $providers);
return "That ain't cool, yo";
}));
$validator->scope('thing', $thing);
$validator->provider('thing', $thing);
$errors = $validator->errors(['email' => '!', 'title' => 'bar']);
$expected = [
'title' => ['cool' => "That ain't cool, yo"]
Expand All @@ -321,7 +321,7 @@ public function testMethodsWithExtraArguments() {
public function testUsingClosureAsRule() {
$validator = new Validator;
$validator->add('name', 'myRule', [
'rule' => function($data, $scopes) {
'rule' => function($data, $provider) {
$this->assertEquals('foo', $data);
return 'You fail';
}
Expand Down Expand Up @@ -359,7 +359,7 @@ public function testArrayAccessGet() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'scope' => 'thing']);
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertSame($validator['email'], $validator->field('email'));
$this->assertSame($validator['title'], $validator->field('title'));
}
Expand All @@ -373,7 +373,7 @@ public function testArrayAccessExists() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'scope' => 'thing']);
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertTrue(isset($validator['email']));
$this->assertTrue(isset($validator['title']));
$this->assertFalse(isset($validator['foo']));
Expand All @@ -388,7 +388,7 @@ public function testArrayAccessSet() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'scope' => 'thing']);
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$validator['name'] = $validator->field('title');
$this->assertSame($validator->field('title'), $validator->field('name'));
$validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
Expand All @@ -404,7 +404,7 @@ public function testArrayAccessUset() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'scope' => 'thing']);
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertTrue(isset($validator['title']));
unset($validator['title']);
$this->assertFalse(isset($validator['title']));
Expand All @@ -419,7 +419,7 @@ public function testCount() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'scope' => 'thing']);
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertCount(2, $validator);
}

Expand Down
24 changes: 12 additions & 12 deletions Cake/Validation/ValidationRule.php
Expand Up @@ -60,7 +60,7 @@ class ValidationRule {
*
* @var string
*/
protected $_scope = 'default';
protected $_provider = 'default';

/**
* Extra arguments to be passed to the validation method
Expand Down Expand Up @@ -94,25 +94,25 @@ public function isLast() {
* it is assumed that the rule failed and the error message was given as a result.
*
* @param mixed $data The data to validate
* @param array $scopes associative array with objects or class names that will
* @param array $providers associative array with objects or class names that will
* be passed as the last argument for the validation method
* @param boolean $newRecord whether or not the data to be validated belongs to
* a new record
* @return boolean|string
* @throws \InvalidArgumentException when the supplied rule is not a valid
* callable for the configured scope
*/
public function process($data, $scopes, $newRecord) {
if ($this->_skip($newRecord, $scopes)) {
public function process($data, $providers, $newRecord) {
if ($this->_skip($newRecord, $providers)) {
return true;
}

if (is_callable($this->_rule)) {
$callable = $this->_rule;
$isCallable = true;
} else {
$scope = $scopes[$this->_scope];
$callable = [$scope, $this->_rule];
$provider = $providers[$this->_provider];
$callable = [$provider, $this->_rule];
$isCallable = is_callable($callable);
}

Expand All @@ -121,10 +121,10 @@ public function process($data, $scopes, $newRecord) {
}

if ($this->_pass) {
$args = array_merge([$data], $this->_pass, [$scopes]);
$args = array_merge([$data], $this->_pass, [$providers]);
$result = call_user_func_array($callable, $args);
} else {
$result = $callable($data, $scopes);
$result = $callable($data, $providers);
}

if ($result === false) {
Expand All @@ -137,14 +137,14 @@ public function process($data, $scopes, $newRecord) {
* Checks if the validation rule should be skipped
*
* @param boolean $newRecord whether the rule to be processed is new or pre-existent
* @param array $scopes associative array with objects or class names that will
* @param array $providers associative array with objects or class names that will
* be passed as the last argument for the validation method
* @return boolean True if the ValidationRule should be skipped
*/
protected function _skip($newRecord, $scopes) {
protected function _skip($newRecord, $providers) {
if (is_callable($this->_on)) {
$function = $this->_on;
return !$function($scopes);
return !$function($providers);
}

if (!empty($this->_on)) {
Expand All @@ -170,7 +170,7 @@ protected function _addValidatorProps($validator = array()) {
$this->_pass = array_slice($value, 1);
$value = array_shift($value);
}
if (in_array($key, ['rule', 'on', 'message', 'last', 'scope', 'pass'])) {
if (in_array($key, ['rule', 'on', 'message', 'last', 'provider', 'pass'])) {
$this->{"_$key"} = $value;
}
}
Expand Down

0 comments on commit 78724d7

Please sign in to comment.