Skip to content

Commit 526c72b

Browse files
committed
Update travis, bootstrap test and fix injection in easy-icon from master.
1 parent b0aef69 commit 526c72b

File tree

7 files changed

+70
-91
lines changed

7 files changed

+70
-91
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ php:
99
dist: trusty
1010

1111
env:
12-
- CAKEPHP_VERSION=3.7.* DEFAULT=1
12+
- CAKEPHP_VERSION=3.7.*
1313

1414
matrix:
1515
include:
@@ -57,11 +57,11 @@ before_install:
5757
install: composer update --prefer-dist --no-interaction
5858

5959
script:
60-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
61-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
60+
- if [[ $TRAVIS_PHP_VERSION = 7.0 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
61+
- if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
6262

6363
after_success:
64-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
64+
- if [[ $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
6565

6666
notifications:
6767
email: true

src/View/Helper/EasyIconTrait.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ trait EasyIconTrait {
2727
*/
2828
public $easyIcon = true;
2929

30+
/**
31+
* Remove the `easyIcon` option from the given array and return it together with
32+
* the array.
33+
*
34+
* @param array $options Array of options from which the easy-icon option should
35+
* be extracted.
36+
*
37+
* @return array An array containing the options and the easy-icon option.
38+
*/
39+
protected function _easyIconOption(array $options) {
40+
$options += [
41+
'easyIcon' => $this->easyIcon
42+
];
43+
$easyIcon = $options['easyIcon'];
44+
unset($options['easyIcon']);
45+
return [$options, $easyIcon];
46+
}
47+
3048
/**
3149
* Try to convert the specified string to a bootstrap icon. The string is converted if
3250
* it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if
@@ -41,7 +59,7 @@ trait EasyIconTrait {
4159
*
4260
* @return string The text after conversion.
4361
*/
44-
protected function _makeIcon($text, &$converted = false, $options = []) {
62+
protected function _makeIcon($text, &$converted = false) {
4563
$converted = false;
4664

4765
// If easyIcon mode is disable.
@@ -64,43 +82,26 @@ protected function _makeIcon($text, &$converted = false, $options = []) {
6482

6583
// Replace occurences.
6684
$text = preg_replace_callback(
67-
'#(^|\s+)i:([a-zA-Z0-9\\-_]+)(\s+|$)#', function ($matches) use ($ficon, $options) {
68-
return $matches[1].call_user_func($ficon, $matches[2], $options).$matches[3];
85+
'#(^|[>\s]\s*)i:([a-zA-Z0-9\\-_]+)(\s*[\s<]|$)#', function ($matches) use ($ficon) {
86+
return $matches[1].call_user_func($ficon, $matches[2]).$matches[3];
6987
}, $text, -1, $count);
7088
$converted = (bool)$count;
7189
return $text;
7290
}
7391

7492
/**
75-
* This method calls the given callback with the given list of parameters after
76-
* applying an easy-icon filter on them.
77-
*
78-
* Note: For compatibility issue, this function can still be called with a callback,
79-
* a string (title) and an array of options.
80-
*
81-
* @param callable $callback The callback.
82-
* @param int $indexTitle Index of the title to which the easy-icon processing
83-
* will be applied.
84-
* @param int $indexOptions Index of the options in the $args array.
85-
* @param array $args Arguments for the callback.
86-
*
87-
* @return mixed Whatever might be returned by $callback.
93+
* Inject icon into the given string.
94+
*
95+
* @param string $input Input string where icon should be injected following the
96+
* easy-icon process.
97+
* @param bool $easyIcon Boolean indicating if the easy-icon process should be
98+
* applied.
8899
*/
89-
protected function _easyIcon(callable $callback, $indexTitle, $indexOptions, $args = null) {
90-
if ($args === null) {
91-
$args = [$indexTitle, $indexOptions];
92-
$indexTitle = 0;
93-
$indexOptions = 1;
94-
}
95-
$title = &$args[$indexTitle];
96-
$options = &$args[$indexOptions];
97-
$title = $this->_makeIcon($title, $converted);
98-
if ($converted) {
99-
$options += [
100-
'escape' => false
101-
];
100+
protected function _injectIcon($title, $easyIcon) {
101+
if (!$easyIcon) {
102+
return $title;
102103
}
103-
return call_user_func_array($callback, $args);
104+
return $this->_makeIcon($title);
104105
}
105106

106107
}

src/View/Helper/FormHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ public function file($fieldName, array $options = []) {
592592
* @link http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-button-elements
593593
*/
594594
public function button($title, array $options = []) {
595-
return $this->_easyIcon('parent::button', $title,
596-
$this->_addButtonClasses($options));
595+
list($options, $easyIcon) = $this->_easyIconOption($options);
596+
return $this->_injectIcon(parent::button($title, $this->_addButtonClasses($options)), $easyIcon);
597597
}
598598

599599
/**

src/View/Helper/HtmlHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public function icon($icon, array $options = []) {
139139
* {@inheritDoc}
140140
*/
141141
public function link($title, $url = null, array $options = []) {
142-
return $this->_easyIcon('parent::link', 0, 2, [$title, $url, $options]);
142+
list($options, $easyIcon) = $this->_easyIconOption($options);
143+
return $this->_injectIcon(parent::link($title, $url, $options), $easyIcon);
143144
}
144145

145146
/**

src/View/Helper/PaginatorHelper.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ public function numbers(array $options = []) {
275275
* @link http://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-jump-links
276276
*/
277277
public function prev($title = '<< Previous', array $options = []) {
278-
return $this->_easyIcon('parent::prev', $title, $options);
278+
list($options, $easyIcon) = $this->_easyIconOption($options);
279+
return $this->_injectIcon(parent::prev($title, $options), $easyIcon);
279280
}
280281

281282
/**
@@ -301,7 +302,8 @@ public function prev($title = '<< Previous', array $options = []) {
301302
* @link http://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-jump-links
302303
*/
303304
public function next($title = 'Next >>', array $options = []) {
304-
return $this->_easyIcon('parent::next', $title, $options);
305+
list($options, $easyIcon) = $this->_easyIconOption($options);
306+
return $this->_injectIcon(parent::next($title, $options), $easyIcon);
305307
}
306308

307309
/**
@@ -336,7 +338,8 @@ public function next($title = 'Next >>', array $options = []) {
336338
* @link http://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-jump-links
337339
*/
338340
public function first($first = '<< first', array $options = []) {
339-
return $this->_easyIcon('parent::first', $first, $options);
341+
list($options, $easyIcon) = $this->_easyIconOption($options);
342+
return $this->_injectIcon(parent::first($first, $options), $easyIcon);
340343
}
341344

342345
/**
@@ -371,7 +374,8 @@ public function first($first = '<< first', array $options = []) {
371374
* @link http://book.cakephp.org/3.0/en/views/helpers/paginator.html#creating-jump-links
372375
*/
373376
public function last($last = 'last >>', array $options = []) {
374-
return $this->_easyIcon('parent::last', $last, $options);
377+
list($options, $easyIcon) = $this->_easyIconOption($options);
378+
return $this->_injectIcon(parent::last($last, $options), $easyIcon);
375379
}
376380

377381
}

tests/TestCase/View/Helper/EasyIconTraitTest.php

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function __construct($view) {
1717
$this->Html = new HtmlHelper($view);
1818
}
1919

20-
public function publicEasyIcon($callback, $title, $options) {
21-
return $this->_easyIcon($callback, $title, $options);
20+
public function publicMakeIcon($title, &$converted) {
21+
return $this->_makeIcon($title, $converted);
2222
}
2323

2424
};
@@ -71,58 +71,33 @@ public function setUp() {
7171
}
7272

7373
public function testEasyIcon() {
74+
$converted = false;
7475

75-
$that = $this;
76-
$callback = function($text, $options) use($that) {
77-
$that->assertEquals(isset($options['escape']) ? $options['escape'] : true,
78-
$options['expected']['escape']);
79-
$that->assertHtml($options['expected']['result'], $text);
80-
};
81-
82-
$this->trait->publicEasyIcon($callback, 'i:plus', [
83-
'expected' => [
84-
'escape' => false,
85-
'result' => [['i' => [
86-
'class' => 'fa fa-plus',
87-
'aria-hidden' => 'true'
88-
]], '/i']
89-
]
90-
]);
76+
$this->assertHtml(
77+
[['i' => [
78+
'class' => 'fa fa-plus',
79+
'aria-hidden' => 'true'
80+
]], '/i'], $this->trait->publicMakeIcon('i:plus', $converted));
81+
$this->assertTrue($converted);
9182

92-
$this->trait->publicEasyIcon($callback, 'Click Me!', [
93-
'expected' => [
94-
'escape' => true,
95-
'result' => 'Click Me!'
96-
]
97-
]);
83+
$this->assertHtml(['Click Me!'], $this->trait->publicMakeIcon('Click Me!', $converted));
84+
$this->assertFalse($converted);
9885

99-
$this->trait->publicEasyIcon($callback, 'i:plus Add', [
100-
'expected' => [
101-
'escape' => false,
102-
'result' => [['i' => [
103-
'class' => 'fa fa-plus',
104-
'aria-hidden' => 'true'
105-
]], '/i', ' Add']
106-
]
107-
]);
86+
$this->assertHtml([['i' => [
87+
'class' => 'fa fa-plus',
88+
'aria-hidden' => 'true'
89+
]], '/i', ' Add'], $this->trait->publicMakeIcon('i:plus Add', $converted));
90+
$this->assertTrue($converted);
10891

109-
$this->trait->publicEasyIcon($callback, 'Add i:plus', [
110-
'expected' => [
111-
'escape' => false,
112-
'result' => ['Add ', ['i' => [
113-
'class' => 'fa fa-plus',
114-
'aria-hidden' => 'true'
115-
]], '/i']
116-
]
117-
]);
92+
$this->assertHtml(['Add ', ['i' => [
93+
'class' => 'fa fa-plus',
94+
'aria-hidden' => 'true'
95+
]], '/i'], $this->trait->publicMakeIcon('Add i:plus', $converted));
96+
$this->assertTrue($converted);
11897

11998
$this->trait->easyIcon = false;
120-
$this->trait->publicEasyIcon($callback, 'i:plus', [
121-
'expected' => [
122-
'escape' => true,
123-
'result' => 'i:plus'
124-
]
125-
]);
99+
$this->assertHtml(['Add i:plus'], $this->trait->publicMakeIcon('Add i:plus', $converted));
100+
$this->assertFalse($converted);
126101
}
127102

128103
public function testHtmlHelperMethods() {

tests/bootstrap.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,3 @@
6464
Configure::write('debug', true);
6565

6666
ini_set('intl.default_locale', 'en_US');
67-
68-
Plugin::load('Search', ['path' => ROOT]);

0 commit comments

Comments
 (0)