Skip to content

Commit 5b9c5fe

Browse files
committed
Updating the tests to assert better error messages
1 parent d6fe138 commit 5b9c5fe

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/Validator/Php/PhpClassExistsValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PhpParser\Node\Stmt\UseUse;
1515
use PhpParser\Parser;
1616
use PhpParser\ParserFactory;
17-
use Webmozart\Assert\Assert;
1817

1918
final class PhpClassExistsValidator implements ValidatorInterface
2019
{
@@ -47,7 +46,9 @@ public function validate(Block $block): array
4746
$phpCode = $this->codeEnsurer->getPHPCode($block->getContent());
4847
try {
4948
$statements = $this->parser->parse($phpCode);
50-
Assert::notNull($statements, 'The parsing of the php file failed.');
49+
if ($statements === null) {
50+
throw new \InvalidArgumentException('The parsing of the php file failed.');
51+
}
5152
} catch (\Throwable $exception) {
5253
return $this->processSyntaxErrors($block, $exception->getMessage());
5354
}

tests/Mamazu/DocumentationParser/FileListTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public function testGetsAllValidFiles(): void
3535
$this->fileList->addFile('vfs://workDir/test.php');
3636
$this->fileList->addFile('vfs://workDir/bananas.php');
3737

38-
$this->expectWarning();
39-
$this->expectWarningMessage('Could not find file: vfs://workDir/bananas.php');
40-
4138
$this->fileList->getAllValidFiles();
4239
}
4340

tests/Mamazu/DocumentationParser/Validator/Bash/BashValidatorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function testValidatesABlockWithErrors(): void
4444
$this->assertEquals(
4545
[
4646
new Error('test.sh', 11, 'unexpected EOF while looking for matching `"\'', 'bash'),
47-
new Error('test.sh', 12, 'syntax error: unexpected end of file', 'bash'),
4847
],
4948
$this->bashValidator->validate($block)
5049
);

tests/Mamazu/DocumentationParser/Validator/XML/XMLValidValidatorTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ public function testReturnsErrorsIfTheXmlIsInvalid(): void
5050
$block->expects($this->atLeastOnce())->method('getFileName')->willReturn('abc.xml');
5151
$block->expects($this->atLeastOnce())->method('getRelativeLineNumber')->willReturn(10);
5252
$result = $this->xMLValidValidator->validate($block);
53-
$this->assertIsIterable($result);
54-
$this->assertCount(4, $result);
53+
54+
$expectedOutput = [
55+
'[xml] error parsing attribute name',
56+
"[xml] Couldn't find end of Start Tag Hello line 2",
57+
];
58+
$this->assertErrorMessages($expectedOutput, $result);
5559
}
5660

5761
public function testValidatesConsecutiveBlocksCorrectly(): void
@@ -69,13 +73,25 @@ public function testValidatesConsecutiveBlocksCorrectly(): void
6973
$block->expects($this->atLeastOnce())->method('getFileName')->willReturn('abc.xml');
7074
$block->expects($this->atLeastOnce())->method('getRelativeLineNumber')->willReturn(10);
7175
$result = $this->xMLValidValidator->validate($block);
72-
$this->assertIsIterable($result);
73-
$this->assertCount(4, $result);
76+
77+
$expectedOutput = [
78+
'[xml] error parsing attribute name',
79+
"[xml] Couldn't find end of Start Tag Hello line 2",
80+
];
81+
$this->assertErrorMessages($expectedOutput, $result);
7482
$block2->expects($this->atLeastOnce())->method('getContent')->willReturn('
7583
<Hello>
7684
This is text
7785
</Hello>
7886
');
7987
$this->assertCount(0, $this->xMLValidValidator->validate($block2));
8088
}
89+
90+
private function assertErrorMessages(array $errorMessages, iterable $result): void
91+
{
92+
foreach ($result as $index => $error) {
93+
$this->assertSame($errorMessages[$index] ?? null, $error->getMessage());
94+
}
95+
$this->assertCount(count($errorMessages), $result);
96+
}
8197
}

0 commit comments

Comments
 (0)