Skip to content

Commit

Permalink
Changes to StringTemplate::addClass method
Browse files Browse the repository at this point in the history
    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 dcbb89f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 37 deletions.
23 changes: 13 additions & 10 deletions src/View/StringTemplate.php
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down
110 changes: 83 additions & 27 deletions tests/TestCase/View/StringTemplateTest.php
Expand Up @@ -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',
Expand All @@ -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');
Expand All @@ -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.