Skip to content

Commit

Permalink
3.next - Add deprecation warnings to the Validation package
Browse files Browse the repository at this point in the history
- Added missing cakephp/core dependency so that the deprecationWarning() is pulled in
- Added missing docblock deprecation message to Validation::blank()

Refs #11385
  • Loading branch information
josegonzalez committed Oct 29, 2017
1 parent 15d5e2b commit 6446063
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 58 deletions.
17 changes: 11 additions & 6 deletions src/Validation/Validation.php
Expand Up @@ -67,7 +67,10 @@ class Validation
*/
public static function notEmpty($check)
{
trigger_error('Validation::notEmpty() is deprecated. Use Validation::notBlank() instead.', E_USER_DEPRECATED);
deprecationWarning(
'Validation::notEmpty() is deprecated. ' .
'Use Validation::notBlank() instead.'
);

return static::notBlank($check);
}
Expand Down Expand Up @@ -132,11 +135,13 @@ public static function lengthBetween($check, $min, $max)
*
* @param string $check Value to check
* @return bool Success
* @deprecated 3.0.2
* @deprecated 3.0.2 Validation::blank() is deprecated.
*/
public static function blank($check)
{
trigger_error('Validation::blank() is deprecated.', E_USER_DEPRECATED);
deprecationWarning(
'Validation::blank() is deprecated.'
);

return !static::_check($check, '/[^\\s]/');
}
Expand Down Expand Up @@ -976,9 +981,9 @@ public static function inList($check, array $list, $caseInsensitive = false)
*/
public static function userDefined($check, $object, $method, $args = null)
{
trigger_error(
'Validation::userDefined() is deprecated. Just set a callable for `rule` key when adding validators instead.',
E_USER_DEPRECATED
deprecationWarning(
'Validation::userDefined() is deprecated. ' .
'You can just set a callable for `rule` key when adding validators.'
);

return $object->$method($check, $args);
Expand Down
4 changes: 4 additions & 0 deletions src/Validation/Validator.php
Expand Up @@ -271,6 +271,10 @@ public static function getDefaultProviders()
*/
public function provider($name, $object = null)
{
deprecationWarning(
'Validator::provider() is deprecated. ' .
'Use Validator::setProvider()/getProvider() instead.'
);
if ($object !== null) {
return $this->setProvider($name, $object);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Validation/ValidatorAwareTrait.php
Expand Up @@ -83,7 +83,7 @@ trait ValidatorAwareTrait
* ->add('email', 'valid-email', ['rule' => 'email'])
* ->add('password', 'valid', ['rule' => 'notBlank'])
* ->allowEmpty('bio');
* $table->validator('forSubscription', $validator);
* $table->setValidator('forSubscription', $validator);
* ```
*
* You can implement the method in `validationDefault` in your Table subclass
Expand All @@ -99,6 +99,10 @@ trait ValidatorAwareTrait
*/
public function validator($name = null, Validator $validator = null)
{
deprecationWarning(
'ValidatorAwareTrait::validator() is deprecated. ' .
'Use ValidatorAwareTrait::getValidator()/setValidator() instead.'
);
if ($validator !== null) {
$name = $name ?: self::DEFAULT_VALIDATOR;
$this->setValidator($name, $validator);
Expand Down
1 change: 1 addition & 0 deletions src/Validation/composer.json
Expand Up @@ -23,6 +23,7 @@
},
"require": {
"php": ">=5.6.0",
"cakephp/core": "^3.0.0",
"cakephp/utility": "^3.0.0",
"psr/http-message": "^1.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -1623,7 +1623,7 @@ public function testBuildMarshalMapBuildEntitiesValidationErrors()
'validator' => 'custom'
]);
$validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
$table->validator('custom', $validator);
$table->setValidator('custom', $validator);
$translate = $table->behaviors()->get('Translate');

$entity = $table->newEntity();
Expand Down Expand Up @@ -1706,7 +1706,7 @@ public function testBuildMarshalMapUpdateEntitiesValidationErrors()
'validator' => 'custom'
]);
$validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
$table->validator('custom', $validator);
$table->setValidator('custom', $validator);
$translate = $table->behaviors()->get('Translate');

$entity = $table->newEntity();
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/Locator/TableLocatorTest.php
Expand Up @@ -441,7 +441,7 @@ public function testConfigWithSingleValidator()
$this->_locator->config('users', ['validator' => $validator]);
$table = $this->_locator->get('users');

$this->assertSame($table->validator('default'), $validator);
$this->assertSame($table->getValidator('default'), $validator);
}

/**
Expand All @@ -464,9 +464,9 @@ public function testConfigWithMultipleValidators()
]);
$table = $this->_locator->get('users');

$this->assertSame($table->validator('default'), $validator1);
$this->assertSame($table->validator('secondary'), $validator2);
$this->assertSame($table->validator('tertiary'), $validator3);
$this->assertSame($table->getValidator('default'), $validator1);
$this->assertSame($table->getValidator('secondary'), $validator2);
$this->assertSame($table->getValidator('tertiary'), $validator3);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -2903,7 +2903,7 @@ public function testValidationFail()
'body' => 'hey'
];

$this->articles->validator()->requirePresence('thing');
$this->articles->getValidator()->requirePresence('thing');
$marshall = new Marshaller($this->articles);
$entity = $marshall->one($data);
$this->assertNotEmpty($entity->errors('thing'));
Expand Down Expand Up @@ -2944,12 +2944,12 @@ public function testValidateWithAssociationsAndCustomValidator()
]
];
$validator = (new Validator)->add('body', 'numeric', ['rule' => 'numeric']);
$this->articles->validator('custom', $validator);
$this->articles->setValidator('custom', $validator);

$validator2 = (new Validator)->requirePresence('thing');
$this->articles->Users->validator('customThing', $validator2);
$this->articles->Users->setValidator('customThing', $validator2);

$this->articles->Comments->validator('default', $validator2);
$this->articles->Comments->setValidator('default', $validator2);

$entity = (new Marshaller($this->articles))->one($data, [
'validate' => 'custom',
Expand Down Expand Up @@ -2985,8 +2985,8 @@ public function testSkipValidation()
],
];
$validator = (new Validator)->requirePresence('thing');
$this->articles->validator('default', $validator);
$this->articles->Users->validator('default', $validator);
$this->articles->setValidator('default', $validator);
$this->articles->Users->setValidator('default', $validator);

$entity = (new Marshaller($this->articles))->one($data, [
'validate' => false,
Expand Down Expand Up @@ -3014,7 +3014,7 @@ public function testPassingCustomValidator()
'body' => 'hey'
];

$validator = clone $this->articles->validator();
$validator = clone $this->articles->getValidator();
$validator->requirePresence('thing');
$marshall = new Marshaller($this->articles);
$entity = $marshall->one($data, ['validate' => $validator]);
Expand Down Expand Up @@ -3064,7 +3064,7 @@ public function testMergeWithValidation()
$entity->isNew(false);
$entity->clean();

$this->articles->validator()
$this->articles->getValidator()
->requirePresence('thing', 'update')
->requirePresence('id', 'update')
->add('author_id', 'numeric', ['rule' => 'numeric'])
Expand All @@ -3078,7 +3078,7 @@ public function testMergeWithValidation()
$this->assertNotEmpty($result->errors('thing'));
$this->assertEmpty($result->errors('id'));

$this->articles->validator()->requirePresence('thing', 'create');
$this->articles->getValidator()->requirePresence('thing', 'create');
$result = $marshall->merge($entity, $data, []);

$this->assertEmpty($result->errors('thing'));
Expand Down Expand Up @@ -3106,7 +3106,7 @@ public function testMergeWithCreate()
$entity->isNew(true);
$entity->clean();

$this->articles->validator()
$this->articles->getValidator()
->requirePresence('thing', 'update')
->add('author_id', 'numeric', ['rule' => 'numeric', 'on' => 'update']);

Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -3355,7 +3355,7 @@ public function testValidatorDefault()
{
$table = new Table();
$validator = $table->getValidator();
$this->assertSame($table, $validator->provider('table'));
$this->assertSame($table, $validator->getProvider('table'));
$this->assertInstanceOf('Cake\Validation\Validator', $validator);
$default = $table->getValidator('default');
$this->assertSame($validator, $default);
Expand Down Expand Up @@ -3391,7 +3391,7 @@ public function testValidationWithDefiner()
$other = $table->getValidator('forOtherStuff');
$this->assertInstanceOf('Cake\Validation\Validator', $other);
$this->assertNotSame($other, $table->getValidator());
$this->assertSame($table, $other->provider('table'));
$this->assertSame($table, $other->getProvider('table'));
}

/**
Expand Down Expand Up @@ -3435,7 +3435,7 @@ public function testValidatorSetter()
$validator = new \Cake\Validation\Validator;
$table->setValidator('other', $validator);
$this->assertSame($validator, $table->getValidator('other'));
$this->assertSame($table, $validator->provider('table'));
$this->assertSame($table, $validator->getProvider('table'));
}

/**
Expand Down Expand Up @@ -5991,7 +5991,7 @@ public function testValidateUnique()
$table = TableRegistry::get('Users');
$validator = new Validator;
$validator->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator->provider('table', $table);
$validator->setProvider('table', $table);

$data = ['username' => ['larry', 'notthere']];
$this->assertNotEmpty($validator->errors($data));
Expand Down Expand Up @@ -6031,7 +6031,7 @@ public function testValidateUniqueScope()
'rule' => ['validateUnique', ['derp' => 'erp', 'scope' => 'id']],
'provider' => 'table'
]);
$validator->provider('table', $table);
$validator->setProvider('table', $table);
$data = ['username' => 'larry', 'id' => 3];
$this->assertNotEmpty($validator->errors($data));

Expand Down Expand Up @@ -6071,7 +6071,7 @@ public function testValidateUniqueMultipleNulls()
'provider' => 'table',
'message' => 'Must be unique.',
]);
$validator->provider('table', $table);
$validator->setProvider('table', $table);

$data = ['site_id' => 1, 'author_id' => null, 'title' => 'Null dupe'];
$expected = ['site_id' => ['unique' => 'Must be unique.']];
Expand Down
24 changes: 12 additions & 12 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -68,7 +68,7 @@ public function testAddNestedSingle()
public function testAddNestedSingleProviders()
{
$validator = new Validator();
$validator->provider('test', $this);
$validator->setProvider('test', $this);

$inner = new Validator();
$inner->add('username', 'not-blank', ['rule' => function () use ($inner, $validator) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public function testAddNestedMany()
public function testAddNestedManyProviders()
{
$validator = new Validator();
$validator->provider('test', $this);
$validator->setProvider('test', $this);

$inner = new Validator();
$inner->add('comment', 'not-blank', ['rule' => function () use ($inner, $validator) {
Expand Down Expand Up @@ -926,15 +926,15 @@ public function testProvider()
{
$validator = new Validator();
$object = new \stdClass;
$this->assertSame($validator, $validator->provider('foo', $object));
$this->assertSame($object, $validator->provider('foo'));
$this->assertNull($validator->provider('bar'));
$this->assertSame($validator, $validator->setProvider('foo', $object));
$this->assertSame($object, $validator->getProvider('foo'));
$this->assertNull($validator->getProvider('bar'));

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

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

/**
Expand Down Expand Up @@ -997,7 +997,7 @@ public function testErrorsFromCustomProvider()
return "That ain't cool, yo";
}));

$validator->provider('thing', $thing);
$validator->setProvider('thing', $thing);
$errors = $validator->errors(['email' => '!', 'title' => 'bar']);
$expected = [
'email' => ['alpha' => 'The provided value is invalid'],
Expand Down Expand Up @@ -1044,7 +1044,7 @@ public function testMethodsWithExtraArguments()

return "That ain't cool, yo";
}));
$validator->provider('thing', $thing);
$validator->setProvider('thing', $thing);
$errors = $validator->errors(['email' => '!', 'title' => 'bar']);
$expected = [
'title' => ['cool' => "That ain't cool, yo"]
Expand Down Expand Up @@ -1220,8 +1220,8 @@ public function testCompareWithIntegration()
public function testDebugInfo()
{
$validator = new Validator();
$validator->provider('test', $this);
$validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
$validator->setProvider('test', $this);
$validator->add('title', 'not-empty', ['rule' => 'notBlank']);
$validator->requirePresence('body');
$validator->allowEmpty('published');

Expand Down

0 comments on commit 6446063

Please sign in to comment.