Skip to content

Commit

Permalink
Throw exceptions when and & or conditions are mixed.
Browse files Browse the repository at this point in the history
Previous implementations of magic finders would just do the wrong thing.
Throwing an exceptino feels safer and more explicit.
  • Loading branch information
markstory committed Dec 1, 2013
1 parent 3bb1ad3 commit 168cca3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Cake/ORM/Table.php
Expand Up @@ -1266,6 +1266,13 @@ public function _magicFinder($method, $args) {
return [$conditions, $order];
};

if ($hasOr !== false && $hasAnd !== false) {
throw new \Cake\Error\Exception(__d(
'cake_dev',
'Cannot mix "and" & "or" in a magic finder. Use find() instead.'
));
}

if ($hasOr === false && $hasAnd === false) {
list($conditions, $order) = $makeConditions([$fields], $args);
} elseif ($hasOr !== false) {
Expand Down
17 changes: 15 additions & 2 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -1958,7 +1958,7 @@ public function testMagicFindFirst() {
}

/**
* Test magic findByXX method.
* Test magic findByXX errors on missing arguments.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage Not enough arguments to magic finder. Got 0 required 1
Expand All @@ -1971,7 +1971,7 @@ public function testMagicFindError() {
}

/**
* Test magic findByXX method.
* Test magic findByXX errors on missing arguments.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage Not enough arguments to magic finder. Got 1 required 2
Expand All @@ -1983,6 +1983,19 @@ public function testMagicFindErrorMissingField() {
$table->findByUsernameAndId('garrett');
}

/**
* Test magic findByXX errors when there is a mix of or & and.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage Cannot mix "and" & "or" in a magic finder. Use find() instead.
* @return void
*/
public function testMagicFindErrorMixOfOperators() {
$table = TableRegistry::get('Users');

$table->findByUsernameAndIdOrPassword('garrett', 1, 'sekret');
}

/**
* Test magic findByXX method.
*
Expand Down

0 comments on commit 168cca3

Please sign in to comment.