Skip to content
Permalink
Browse files

Changes to StringTemplate::addClass method

    Defaults to using 'class' element
    Renamed `returnArray` to `asString`

Separated addClass test methods
Fixed tests after StringTemplate::addClass logic change
  • Loading branch information...
lilHermit committed Jan 31, 2017
1 parent 4fb6107 commit dcbb89f25485162af13221e3b9f9e8e11fba97e0
Showing with 96 additions and 37 deletions.
  1. +13 −10 src/View/StringTemplate.php
  2. +83 −27 tests/TestCase/View/StringTemplateTest.php
@@ -330,9 +330,9 @@ protected function _formatAttribute($key, $value, $escape = true)
*
* ### Options
*
* - `useIndex` if you are inputting an array with an 'element' called 'class' and want the same returning
* you should set this to 'class'
* - `returnArray` Setting this to `false` will return a space separated string (default is `true`)
* - `useIndex` if you are inputting an array with an 'element' other than 'class'.
* Also setting to 'false' will manipulate the whole array (default is 'class')
* - `asString` Setting this to `true` will return a space separated string (default is `false`)
*
* @param array|string $input The array or string to add the class to
* @param array|string $newClass the new class or classes to add
@@ -346,13 +346,13 @@ public function addClass($input, $newClass, $options = [])
return $input;
}
$options = $options + [
'useIndex' => null,
'returnArray' => true
];
$options += [
'useIndex' => 'class',
'asString' => false
];
$useIndex = $options['useIndex'];
if (is_string($useIndex)) {
if (is_string($useIndex) && is_array($input)) {
$class = Hash::get($input, $useIndex, []);
} else {
$class = $input;
@@ -373,11 +373,14 @@ public function addClass($input, $newClass, $options = [])
$class = array_unique(array_merge($class, $newClass));
if ($options['returnArray'] === false) {
$class = implode(" ", $class);
if ($options['asString'] === true) {
$class = implode(' ', $class);
}
if (is_string($useIndex)) {
if (!is_array($input)) {
$input = [];
}
$input = Hash::insert($input, $useIndex, $class);
} else {
$input = $class;
@@ -301,42 +301,70 @@ public function testPushPopTemplates()
$this->assertNull($this->template->pop());
}
public function testAddClassMethod()
/**
* Test addClass method newClass parameter
*
* Tests null, string, array and false for `input`
*/
public function testAddClassMethodNewClass()
{
// Test new class as null, string, array, false etc
$result = $this->template->addClass([], 'new_class');
$this->assertEquals($result, ['new_class']);
$this->assertEquals($result, ['class' => ['new_class']]);
$result = $this->template->addClass([], ['new_class']);
$this->assertEquals($result, ['new_class']);
$this->assertEquals($result, ['class' => ['new_class']]);
$result = $this->template->addClass([], false);
$this->assertEquals($result, []);
$result = $this->template->addClass([], null);
$this->assertEquals($result, []);
$result = $this->template->addClass(null, null);
$this->assertNull($result);
}
// Test current class as null, string, array, false etc
$result = $this->template->addClass(['current'], 'new_class');
$this->assertEquals($result, ['current', 'new_class']);
$result = $this->template->addClass('current', 'new_class');
$this->assertEquals($result, ['current', 'new_class']);
/**
* Test addClass method input (currentClass) parameter
*
* Tests null, string, array, false and object
*/
public function testAddClassMethodCurrentClass()
{
$result = $this->template->addClass(['class' => ['current']], 'new_class');
$this->assertEquals($result, ['class' => ['current', 'new_class']]);
$result = $this->template->addClass('', 'new_class');
$this->assertEquals($result, ['new_class']);
$this->assertEquals($result, ['class' => ['new_class']]);
$result = $this->template->addClass(null, 'new_class');
$this->assertEquals($result, ['new_class']);
$this->assertEquals($result, ['class' => ['new_class']]);
$result = $this->template->addClass(false, 'new_class');
$this->assertEquals($result, ['new_class']);
$this->assertEquals($result, ['class' => ['new_class']]);
$result = $this->template->addClass(new \StdClass(), 'new_class');
$this->assertEquals($result, ['new_class']);
$this->assertEquals($result, ['class' => ['new_class']]);
}
/**
* Test addClass method string parameter, it should fallback to string
*/
public function testAddClassMethodFallbackToString()
{
$result = $this->template->addClass('current', 'new_class');
$this->assertEquals($result, ['class' => ['current', 'new_class']]);
}
// Test we don't repeat the class
$result = $this->template->addClass(['new_class'], 'new_class');
$this->assertEquals($result, ['new_class']);
/**
* Test addClass method to make sure the returned array is unique
*/
public function testAddClassMethodUnique()
{
$result = $this->template->addClass(['class' => ['new_class']], 'new_class');
$this->assertEquals($result, ['class' => ['new_class']]);
}
public function testAddClassMethodOptions()
/**
* Test addClass method useIndex option
*
* Tests for useIndex being the default, 'my_class' and false
*/
public function testAddClassMethodUseIndexOption()
{
// Test useIndex option
$result = $this->template->addClass(
[
'class' => 'current_class',
@@ -348,38 +376,66 @@ public function testAddClassMethodOptions()
'useIndex' => 'class'
]
);
$this->assertEquals($result, [
'class' => ['current_class', 'new_class'],
'other_index1' => false,
'type' => 'text'
]);
// Test the use index which doesn't exist in input
$result = $this->template->addClass(
[
'class' => 'current_class',
'my_class' => 'current_class',
'other_index1' => false,
'type' => 'text'
],
'new_class',
[
'useIndex' => 'non_existent_class'
'useIndex' => 'my_class'
]
);
$this->assertEquals($result, [
'class' => 'current_class',
'other_index1' => false,
'type' => 'text',
'non_existent_class' => ['new_class']
'my_class' => ['current_class', 'new_class']
]);
$result = $this->template->addClass(
[
'current_class',
'text'
],
'new_class',
[
'useIndex' => false
]
);
$this->assertEquals($result, [
'current_class',
'text',
'new_class'
]);
}
/**
* Test addClass method to make sure `asString` option is handle correctly
*/
public function testAddClassMethodAsStringOption()
{
$result = $this->template->addClass(
['class' => 'current_class'],
'new_class',
[
'asString' => true
]
);
$this->assertEquals($result, ['class' => 'current_class new_class']);
// Test returnArray option (will return space separated string
$result = $this->template->addClass(
['current_class'],
'new_class',
[
'returnArray' => false
'useIndex' => false,
'asString' => true
]
);
$this->assertEquals($result, 'current_class new_class');
@@ -388,9 +444,9 @@ public function testAddClassMethodOptions()
null,
'new_class',
[
'returnArray' => false
'asString' => true
]
);
$this->assertEquals($result, 'new_class');
$this->assertEquals($result, ['class' => 'new_class']);
}
}

0 comments on commit dcbb89f

Please sign in to comment.
You can’t perform that action at this time.