Skip to content

Speed up running the framework validation test suite #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2022
Merged
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
40 changes: 24 additions & 16 deletions tests/ParserFrameworkValidationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,33 @@ public function testFrameworkErrors($testCaseFile, $frameworkName) {
$parser = new \Microsoft\PhpParser\Parser();
$sourceFile = $parser->parseSourceFile($fileContents);

$directory = __DIR__ . "/output/$frameworkName/";
if (!file_exists($dir = __DIR__ . "/output")) {
mkdir($dir);
}
if (!file_exists($directory)) {
mkdir($directory);
}
$outFile = $directory . basename($testCaseFile);
file_put_contents($outFile, $fileContents);

foreach ($sourceFile->getDescendantNodesAndTokens() as $child) {
if ($child instanceof Token) {
$this->assertNotEquals(\Microsoft\PhpParser\TokenKind::Unknown, $child->kind, "input: $testCaseFile\r\nexpected: ");
$this->assertNotInstanceOf(\Microsoft\PhpParser\SkippedToken::class, $child, "input: $testCaseFile\r\nexpected: ");
$this->assertNotInstanceOf(\Microsoft\PhpParser\MissingToken::class, $child, "input: $testCaseFile\r\nexpected: ");
try {
foreach ($sourceFile->getDescendantNodesAndTokens() as $child) {
if ($child instanceof Token) {
if (get_class($child) === Token::class && $child->kind !== \Microsoft\PhpParser\TokenKind::Unknown) {
// NOTE: This parsing many tokens each from 10000+ files - the PHPUnit assert method calls are slow and
// Without this optimization, the test suite takes 93 seconds.
// With this optimization, the method takes 30 seconds.
continue;
}
$this->assertNotEquals(\Microsoft\PhpParser\TokenKind::Unknown, $child->kind, "input: $testCaseFile\r\nexpected: ");
$this->assertNotInstanceOf(\Microsoft\PhpParser\SkippedToken::class, $child, "input: $testCaseFile\r\nexpected: ");
$this->assertNotInstanceOf(\Microsoft\PhpParser\MissingToken::class, $child, "input: $testCaseFile\r\nexpected: ");
}
}
} catch (Throwable $e) {
$directory = __DIR__ . "/output/$frameworkName/";
if (!file_exists($dir = __DIR__ . "/output")) {
mkdir($dir);
}
if (!file_exists($directory)) {
mkdir($directory);
}
$outFile = $directory . basename($testCaseFile);
file_put_contents($outFile, $fileContents);
throw $e;
}

unlink($outFile);
// echo json_encode($parser->getErrors($sourceFile));
}
}