Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ skipped = ghost
pass = monkey
```

Note that the values in the emojiset are snale-cased emoji short codes *without the opening and closing colons*; most existing short codes are supported, a list of supported shortcodes will be added soon.
Note that the values in the emojiset are snake-cased emoji short codes *without the opening and closing colons*; most existing short codes are supported, a list of supported shortcodes will be added soon.

You may choose to override one or more of the packaged emojisets (as in the case of "phumoji", above, which overrides the default emojiset), or you may define your own (as in the case of "weird", above).

Expand Down
70 changes: 56 additions & 14 deletions src/EmojiPrinter.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<?php declare(strict_types=1);
<?php declare(strict_types = 1);

namespace Coderabbi\Phpumoji;

use DOMElement;
use LitEmoji\LitEmoji;
use PHPUnit\Framework\TestSuite;
use PHPUnit\TextUI\ResultPrinter;
use PHPUnit\Util\Xml;

final class EmojiPrinter extends ResultPrinter
{
const CONFIG = __DIR__ . '/../config/';
const EMOJIFILE = '.emojifile';
const EMOJISET = 'phpumoji';
const SPACER = ' ';

private $emojis;

public function startTestSuite(TestSuite $suite)
{
$phpunit = Xml::loadFile($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], false, true, true)->documentElement;

$emojiset = $phpunit->hasAttribute('emojiset') ? $phpunit->getAttribute('emojiset') : 'phpumoji';

$emojifile = array_merge(parse_ini_file(__DIR__ . '/../config/.emojifile', true), file_exists(substr($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], 0, strrpos($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], '/')) . '/.emojifile') ?
parse_ini_file(substr($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], 0, strrpos($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], '/')) . '/.emojifile', true) : []);

$this->emojis = $emojifile[$emojiset] ?? $emojifile['phpumoji'];
$this->emojis = $this->emojis(
$this->emojifile($this->projectroot()),
$this->emojiset($this->phpunitxml())
);

if ($this->numTests == -1) {
$this->numTests = count($suite);
Expand All @@ -33,18 +35,58 @@ public function startTestSuite(TestSuite $suite)

protected function writeProgress($progress)
{
return parent::writeProgress($this->emojify($progress));
return parent::writeProgress($this->emojify($progress) . self::SPACER);
}

protected function writeProgressWithColor($color, $progress)
{
return $this->writeProgress($progress);
}

private function emojify(string $progress) :string
private function projectroot(): string
{
return substr($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], 0, strrpos(
$GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], '/')
);
}

private function emojifile(string $projectroot): array
{
return array_merge(
$this->parse(self::CONFIG . self::EMOJIFILE),
$this->parse($projectroot . self::EMOJIFILE)
);
}

private function phpunitxml(): DOMElement
{
return Xml::loadFile($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], false, true, true)->documentElement;
}

private function parse(string $filepath): array
{
return file_exists($filepath) ? parse_ini_file($filepath, true) : [];
}

private function emojiset(DOMElement $phpunitxml): string
{
return $phpunitxml->hasAttribute('emojiset') ? $phpunitxml->getAttribute('emojiset') : self::EMOJISET;
}

private function emojis(array $emojifile, string $emojiset): array
{
return $emojifile[$emojiset] ?? $emojifile[self::EMOJISET];
}

private function emojify(string $result): string
{
return LitEmoji::encodeUnicode(":{$this->shortcode($result)}:");
}

private function shortcode(string $character): string
{
return LitEmoji::encodeUnicode(':' . (array_values(array_filter($this->emojis, function ($key) use ($progress) {
return strrpos(strtoupper($key), $progress) === 0;
}, ARRAY_FILTER_USE_KEY))[0] ?? $this->emojis['pass']) . ': ') ;
return array_values(array_filter($this->emojis, function($emojikey) use ($character) {
return strrpos(strtoupper($emojikey), $character) === 0;
}, ARRAY_FILTER_USE_KEY))[0] ?? $this->emojis['pass'];
}
}