Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit bab17d7

Browse files
committed
Test that simple required files throw an error
1 parent 4b5f875 commit bab17d7

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"require": {
1111
"php": ">=7.4",
12-
"phpstan/phpstan": "^1.11"
12+
"phpstan/phpstan": "^1.11",
13+
"ext-json": "*"
1314
},
1415
"license": "Apache-2.0",
1516
"autoload": {

tests/MainTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,49 @@ class MainTest extends TestCase
1010
{
1111
public function testHappyPath(): void
1212
{
13-
$this->assertTrue(true);
13+
$expectedTotalErrors = 4;
14+
$expectedErrorsPerFile = [
15+
'included_file_does_not_exist.php' => [
16+
'errors' => 4,
17+
'messages' => [
18+
[
19+
'message' => 'Included or required file "a_file_that_does_not_exist.php" does not exist.',
20+
'line' => 5,
21+
'ignorable' => true
22+
],
23+
[
24+
'message' => 'Included or required file "a_file_that_does_not_exist_once.php" does not exist.',
25+
'line' => 6,
26+
'ignorable' => true
27+
],
28+
[
29+
'message' => 'Included or required file "a_file_that_does_not_exist.php" does not exist.',
30+
'line' => 7,
31+
'ignorable' => true
32+
],
33+
[
34+
'message' => 'Included or required file "a_file_that_does_not_exist_once.php" does not exist.',
35+
'line' => 8,
36+
'ignorable' => true
37+
],
38+
]
39+
],
40+
];
41+
42+
$output = [];
43+
exec('php vendor/bin/phpstan analyse -c tests/phpstan-testing.neon --error-format=json', $output);
44+
45+
$jsonOutput = json_decode(implode(' ', $output), true, 10, JSON_THROW_ON_ERROR);
46+
47+
$this->assertSame($expectedTotalErrors, $jsonOutput['totals']['file_errors']);
48+
$this->assertCount(count($expectedErrorsPerFile), $jsonOutput['files']);
49+
50+
foreach ($jsonOutput['files'] as $filename => $errors) {
51+
$basename = basename($filename);
52+
53+
$this->assertArrayHasKey($basename, $expectedErrorsPerFile);
54+
$this->assertSame($expectedErrorsPerFile[$basename]['errors'], $errors['errors']);
55+
$this->assertSame($expectedErrorsPerFile[$basename]['messages'], $errors['messages']);
56+
}
1457
}
1558
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include 'a_file_that_does_not_exist.php';
6+
include_once 'a_file_that_does_not_exist_once.php';
7+
require 'a_file_that_does_not_exist.php';
8+
require_once 'a_file_that_does_not_exist_once.php';

tests/phpstan-testing.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
-
3+
class: Bellangelo\PHPStanRequireFileExists\RequireFileExistsRule
4+
arguments:
5+
- @reflectionProvider
6+
tags: [phpstan.rules.rule]
7+
8+
parameters:
9+
phpVersion: 70400
10+
level: 9
11+
paths:
12+
- files

0 commit comments

Comments
 (0)