Skip to content

Commit

Permalink
Fix #4: Non emoji text surrounded by colons is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaail committed Jan 30, 2020
1 parent 3bcb78a commit 08445a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Emoticon.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public function get(string $name)
*/
public function emojify(string $text): string
{
return preg_replace_callback('/:([a-zA-Z0-9_\-\+]+):/', function ($match) {
return $this->get($match[1]);
return preg_replace_callback('/:([a-zA-Z0-9_\-+]+):/', function ($match) {
$emoji = $this->get($match[1]);

return false !== $emoji ? $emoji : $match[0];
}, $text);
}

Expand Down
29 changes: 25 additions & 4 deletions tests/EmoticonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,35 @@ public function it_can_show_all_the_matching_emojies_if_a_text_provide()
$this->assertArrayHasKey('blue_heart', $result);
}

/** @test */
public function it_can_make_emojies_inside_of_a_string()
/**
* @test
* @dataProvider texts_with_emojis
*
* @param string $text
* @param string $expected
*/
public function it_can_make_emojies_inside_of_a_string($text, $expected)
{
$emoji = new Emoticon();

$result = $emoji->emojify('I am :laughing:');
$result = $emoji->emojify($text);

$this->assertSame($expected, $result);
}

$this->assertSame('I am πŸ˜†', $result);
/**
* Data Provider
*
* @return array
*/
public function texts_with_emojis(): array
{
return [
['I am :laughing:', 'I am πŸ˜†'],
[':+1: for the Project', 'πŸ‘ for the Project'],
['I am not a :emoji:', 'I am not a :emoji:'],
[':FOO: :laughing:', ':FOO: πŸ˜†'],
];
}

/** @test */
Expand Down

0 comments on commit 08445a9

Please sign in to comment.