Skip to content

Commit

Permalink
make sure error messages are tested, make sure that object mutation d…
Browse files Browse the repository at this point in the history
…oes not screw up tests
  • Loading branch information
Jonas committed Jun 3, 2016
1 parent a248513 commit 7f480af
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/ORM/RulesChecker.php
Expand Up @@ -78,19 +78,19 @@ public function isUnique(array $fields, $message = null)
* @param string|array $field The field or list of fields to check for existence by
* primary key lookup in the other table.
* @param object|string $table The table name where the fields existence will be checked.
* @param array|string|null $options List of options or error message string to show in case the rule does not pass.
* @param @param string|array|null $message The error message to show in case the rule does not pass. Can
* also be an array of options. When an array, the 'message' key can be used to provide a message.
* @return callable
*/
public function existsIn($field, $table, $options = null)
public function existsIn($field, $table, $message = null)
{
if (is_string($options)) {
$options = ['message' => $options];
$options = [];
if (is_array($message)) {
$options = $message + ['message' => null];
$message = $options['message'];
unset($options['message']);
}

$options = (array)$options + ['message' => null];
$message = $options['message'];
unset($options['message']);

if (!$message) {
if ($this->_useI18n) {
$message = __d('cake', 'This value does not exist');
Expand Down
191 changes: 181 additions & 10 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Expand Up @@ -832,7 +832,7 @@ public function testExistsInErrorWithArrayField()
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentIdNull()
public function testExistsInAllowSqlNullsWithParentIdNullA()
{
$entity = new Entity([
'id' => 10,
Expand All @@ -844,22 +844,111 @@ public function testExistsInAllowSqlNullsWithParentIdNull()
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowPartialNulls' => true]));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save(clone $entity));
$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
'allowPartialNulls' => true
]));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowPartialNulls' => false]));
$this->assertFalse($table->save(clone $entity));
/**
* Tests new allowPartialNulls flag with author id set to null
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentIdNullB()
{
$entity = new Entity([
'id' => 10,
'author_id' => null,
'site_id' => 1,
'name' => 'New Site Article without Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
'allowPartialNulls' => false
]));
$this->assertFalse($table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to null
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentIdNullC()
{
$entity = new Entity([
'id' => 10,
'author_id' => null,
'site_id' => 1,
'name' => 'New Site Article without Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors'));
$this->assertFalse($table->save(clone $entity));
$this->assertFalse($table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to null
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentIdNullD()
{
$entity = new Entity([
'id' => 10,
'author_id' => null,
'site_id' => 1,
'name' => 'New Site Article without Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
'allowPartialNulls' => false,
'message' => 'Niente'
]));
$this->assertFalse($table->save($entity));
$this->assertEquals(['author_id' => ['_existsIn' => 'Niente']], $entity->errors());
}

/**
* Tests new allowPartialNulls flag with author id set to null
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentIdNullE()
{
$entity = new Entity([
'id' => 10,
'author_id' => null,
'site_id' => 1,
'name' => 'New Site Article without Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
'allowPartialNulls' => true,
'message' => 'Niente'
]));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to 1
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentId1()
public function testExistsInAllowSqlNullsWithParentId1A()
{
$entity = new Entity([
'id' => 10,
Expand All @@ -872,13 +961,95 @@ public function testExistsInAllowSqlNullsWithParentId1()
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowPartialNulls' => true]));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save(clone $entity));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to 1
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentIdB()
{
$entity = new Entity([
'id' => 10,
'author_id' => 1,
'site_id' => 1,
'name' => 'New Site Article with Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', ['allowPartialNulls' => false]));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save(clone $entity));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to 1
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentId1C()
{
$entity = new Entity([
'id' => 10,
'author_id' => 1,
'site_id' => 1,
'name' => 'New Site Article with Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors'));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save(clone $entity));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to 1
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentId1E()
{
$entity = new Entity([
'id' => 10,
'author_id' => 1,
'site_id' => 1,
'name' => 'New Site Article with Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
'allowPartialNulls' => true,
'message' => 'will not occur']));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

/**
* Tests new allowPartialNulls flag with author id set to 1
*
* @return
*/
public function testExistsInAllowSqlNullsWithParentId1G()
{
$entity = new Entity([
'id' => 10,
'author_id' => 1,
'site_id' => 1,
'name' => 'New Site Article with Author',
]);
$table = TableRegistry::get('SiteArticles');
$table->belongsTo('SiteAuthors');
$rules = $table->rulesChecker();

$rules->add($rules->existsIn(['author_id', 'site_id'], 'SiteAuthors', [
'allowPartialNulls' => false,
'message' => 'will not occur']));
$this->assertInstanceOf('Cake\ORM\Entity', $table->save($entity));
}

/**
Expand Down

0 comments on commit 7f480af

Please sign in to comment.