Skip to content

Commit

Permalink
Add test case for the limit option of Text::highlight().
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark authored and chinpei215 committed Mar 7, 2017
1 parent 242cd2a commit 78f3e04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Utility/Text.php
Expand Up @@ -456,7 +456,8 @@ protected static function _wordWrap($text, $width = 72, $break = "\n", $cut = fa
*
* - `format` The piece of HTML with that the phrase will be highlighted
* - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
* - `regex` a custom regex rule that is used to match words, default is '|$tag|iu'
* - `regex` A custom regex rule that is used to match words, default is '|$tag|iu'
* - `limit` A limit, optional, defaults to -1 (none)
*
* @param string $text Text to search the phrase in.
* @param string|array $phrase The phrase or phrases that will be searched.
Expand All @@ -473,7 +474,7 @@ public static function highlight($text, $phrase, array $options = [])
$defaults = [
'format' => '<span class="highlight">\1</span>',
'html' => false,
'regex' => "|%s|iu",
'regex' => '|%s|iu',
'limit' => -1,
];
$options += $defaults;
Expand Down
20 changes: 19 additions & 1 deletion tests/TestCase/Utility/TextTest.php
Expand Up @@ -711,7 +711,7 @@ public function testTail()
}

/**
* testHighlight method
* Tests highlight() method.
*
* @return void
*/
Expand Down Expand Up @@ -745,6 +745,24 @@ public function testHighlight()
$this->assertEquals($expected, $result);
}

/**
* Tests highlight() method with limit.
*
* @return void
*/
public function testHighlightLimit()
{
$text = 'This is a test text with some more text';
$phrases = ['This', 'text'];
$result = $this->Text->highlight($text, $phrases, ['format' => '<b>\1</b>']);
$expected = '<b>This</b> is a test <b>text</b> with some more <b>text</b>';
$this->assertEquals($expected, $result);

$result = $this->Text->highlight($text, $phrases, ['format' => '<b>\1</b>', 'limit' => 1]);
$expected = '<b>This</b> is a test <b>text</b> with some more text';
$this->assertEquals($expected, $result);
}

/**
* testHighlightHtml method
*
Expand Down

0 comments on commit 78f3e04

Please sign in to comment.