2019-11-14 12:14:02 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Aviat\Kilo\Tests\Tokens;
|
|
|
|
|
|
|
|
use Aviat\Kilo\Tokens\PHP;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class PHPTest extends TestCase {
|
2019-11-19 15:57:51 -05:00
|
|
|
public function testGetFileTokens(): void
|
2019-11-14 12:14:02 -05:00
|
|
|
{
|
2019-11-19 15:57:51 -05:00
|
|
|
$filename = realpath(__DIR__ . '/../../test.php');
|
|
|
|
$file = file_get_contents($filename);
|
|
|
|
$tokens = PHP::getFileTokens($filename);
|
2019-11-14 12:14:02 -05:00
|
|
|
|
|
|
|
$lines = explode("\n", $file);
|
|
|
|
array_unshift($lines, '');
|
|
|
|
|
2019-11-19 15:57:51 -05:00
|
|
|
$this->verifyTokens($tokens, $lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFileTokensEmptyFile(): void
|
|
|
|
{
|
|
|
|
$filename = __DIR__ . '/../../foobarbaz.php';
|
|
|
|
$this->assertEmpty(PHP::getFileTokens($filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function verifyTokens(array $tokens, array $lines): void
|
|
|
|
{
|
2019-11-14 12:14:02 -05:00
|
|
|
$misplacedTokens = [];
|
|
|
|
|
|
|
|
foreach ($tokens as $index => $lineTokens)
|
|
|
|
{
|
|
|
|
if (empty($lineTokens))
|
|
|
|
{
|
2019-11-15 11:35:08 -05:00
|
|
|
$this->assertEmpty(trim($lines[$index]), 'Token is empty for non-empty line');
|
2019-11-14 12:14:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($lineTokens as $token)
|
|
|
|
{
|
|
|
|
// don't compare whitespace-only tokens
|
|
|
|
if (empty(trim($token['char'])))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertIsArray($token, 'All outputted tokens should be arrays');
|
|
|
|
|
|
|
|
// Make sure the matched string for the token is on the correct line
|
|
|
|
if (strpos($lines[$index], trim($token['char'])) === FALSE)
|
|
|
|
{
|
|
|
|
$token['misplaced_line'] = $index;
|
|
|
|
$misplacedTokens[] = $token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty($misplacedTokens, 'Not all tokens are on the correct lines: ' . print_r($misplacedTokens, TRUE));
|
|
|
|
}
|
|
|
|
}
|