Skip to content
Permalink
Browse files

Add tests for #3288 and remove nested ternaries.

Nested ternaries are complicated to maintain and hard to read. Break
down the nested ternary into two conditionals.
  • Loading branch information...
markstory committed Apr 11, 2014
1 parent db450a9 commit c6173a0054ca61c2b05c1cd3d1ed6d651ce03618
Showing with 40 additions and 1 deletion.
  1. +33 −0 lib/Cake/Test/Case/Utility/HashTest.php
  2. +7 −1 lib/Cake/Utility/Hash.php
@@ -831,6 +831,39 @@ public function testExtractAttributeEquality() {
$this->assertEquals(5, $result[3]['id']);
}
/**
* Test extracting based on attributes with boolean values.
*
* @return void
*/
public function testExtractAttributeBoolean() {
$users = array(
array(
'id' => 2,
'username' => 'johndoe',
'active' => true
),
array(
'id' => 5,
'username' => 'kevin',
'active' => true
),
array(
'id' => 9,
'username' => 'samantha',
'active' => false
),
);
$result = Hash::extract($users, '{n}[active=false]');
$this->assertCount(1, $result);
$this->assertEquals($users[2], $result[0]);
$result = Hash::extract($users, '{n}[active=true]');
$this->assertCount(2, $result);
$this->assertEquals($users[0], $result[0]);
$this->assertEquals($users[1], $result[1]);
}
/**
* Test that attribute matchers don't cause errors on scalar data.
*
@@ -192,7 +192,13 @@ protected static function _matches(array $data, $selector) {
return false;
}
$prop = isset($data[$attr]) ? ( is_bool($data[$attr]) ? (($data[$attr]) ? 'true' : 'false') : $data[$attr] ) : null;
$prop = null;
if (isset($data[$attr])) {
$prop = $data[$attr];
}
if ($prop === true || $prop === false) {
$prop = $prop ? 'true' : 'false';
}
// Pattern matches and other operators.
if ($op === '=' && $val && $val[0] === '/') {

0 comments on commit c6173a0

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.