Skip to content

Commit 4050b23

Browse files
fix(tests): fail fast on unreadable files, correct INFO checks, add pest script
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
1 parent caf77b7 commit 4050b23

4 files changed

Lines changed: 42 additions & 53 deletions

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
"files": [
1515
"tests/bootstrap.php"
1616
]
17+
},
18+
"scripts": {
19+
"test": "vendor/bin/pest"
1720
}
1821
}

tests/Security/Php74CompatibilityTest.php

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,79 +19,55 @@
1919
'setup.php',
2020
];
2121

22-
it('does not use str_contains (PHP 8.0)', function () use ($files) {
23-
foreach ($files as $relativeFile) {
24-
$path = realpath(__DIR__ . '/../../' . $relativeFile);
22+
$readRequiredFile = function (string $relativeFile): string {
23+
$path = realpath(__DIR__ . '/../../' . $relativeFile);
24+
25+
if ($path === false) {
26+
throw new RuntimeException("Unable to resolve required file: {$relativeFile}");
27+
}
28+
29+
$contents = file_get_contents($path);
2530

26-
if ($path === false) {
27-
continue;
28-
}
31+
if ($contents === false) {
32+
throw new RuntimeException("Unable to read required file: {$relativeFile}");
33+
}
2934

30-
$contents = file_get_contents($path);
35+
return $contents;
36+
};
3137

32-
if ($contents === false) {
33-
continue;
34-
}
38+
it('does not use str_contains (PHP 8.0)', function () use ($files, $readRequiredFile) {
39+
foreach ($files as $relativeFile) {
40+
$contents = $readRequiredFile($relativeFile);
3541

3642
expect(preg_match('/\bstr_contains\s*\(/', $contents))->toBe(0,
3743
"{$relativeFile} uses str_contains() which requires PHP 8.0"
3844
);
3945
}
4046
});
4147

42-
it('does not use str_starts_with (PHP 8.0)', function () use ($files) {
48+
it('does not use str_starts_with (PHP 8.0)', function () use ($files, $readRequiredFile) {
4349
foreach ($files as $relativeFile) {
44-
$path = realpath(__DIR__ . '/../../' . $relativeFile);
45-
46-
if ($path === false) {
47-
continue;
48-
}
49-
50-
$contents = file_get_contents($path);
51-
52-
if ($contents === false) {
53-
continue;
54-
}
50+
$contents = $readRequiredFile($relativeFile);
5551

5652
expect(preg_match('/\bstr_starts_with\s*\(/', $contents))->toBe(0,
5753
"{$relativeFile} uses str_starts_with() which requires PHP 8.0"
5854
);
5955
}
6056
});
6157

62-
it('does not use str_ends_with (PHP 8.0)', function () use ($files) {
58+
it('does not use str_ends_with (PHP 8.0)', function () use ($files, $readRequiredFile) {
6359
foreach ($files as $relativeFile) {
64-
$path = realpath(__DIR__ . '/../../' . $relativeFile);
65-
66-
if ($path === false) {
67-
continue;
68-
}
69-
70-
$contents = file_get_contents($path);
71-
72-
if ($contents === false) {
73-
continue;
74-
}
60+
$contents = $readRequiredFile($relativeFile);
7561

7662
expect(preg_match('/\bstr_ends_with\s*\(/', $contents))->toBe(0,
7763
"{$relativeFile} uses str_ends_with() which requires PHP 8.0"
7864
);
7965
}
8066
});
8167

82-
it('does not use nullsafe operator (PHP 8.0)', function () use ($files) {
68+
it('does not use nullsafe operator (PHP 8.0)', function () use ($files, $readRequiredFile) {
8369
foreach ($files as $relativeFile) {
84-
$path = realpath(__DIR__ . '/../../' . $relativeFile);
85-
86-
if ($path === false) {
87-
continue;
88-
}
89-
90-
$contents = file_get_contents($path);
91-
92-
if ($contents === false) {
93-
continue;
94-
}
70+
$contents = $readRequiredFile($relativeFile);
9571

9672
expect(preg_match('/\?->/', $contents))->toBe(0,
9773
"{$relativeFile} uses nullsafe operator which requires PHP 8.0"

tests/Security/PreparedStatementConsistencyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
$path = realpath(__DIR__ . '/../../' . $relativeFile);
2828

2929
if ($path === false) {
30-
continue;
30+
throw new RuntimeException("Unable to resolve required plugin file: {$relativeFile}");
3131
}
3232

3333
$contents = file_get_contents($path);
3434

3535
if ($contents === false) {
36-
continue;
36+
throw new RuntimeException("Unable to read required plugin file: {$relativeFile}");
3737
}
3838

3939
$lines = explode("\n", $contents);

tests/Security/SetupStructureTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@
1010
// Verify setup.php defines required plugin hooks and info function.
1111

1212
describe('maint setup.php structure', function () {
13-
$source = file_get_contents(realpath(__DIR__ . '/../../setup.php'));
13+
$setupPath = realpath(__DIR__ . '/../../setup.php');
14+
expect($setupPath)->not->toBeFalse();
15+
16+
$source = file_get_contents($setupPath);
17+
expect($source)->not->toBeFalse();
18+
19+
$infoPath = realpath(__DIR__ . '/../../INFO');
20+
expect($infoPath)->not->toBeFalse();
21+
22+
$info = parse_ini_file($infoPath, true);
23+
expect($info)->not->toBeFalse();
1424

1525
it('defines plugin_maint_install function', function () use ($source) {
1626
expect($source)->toContain('function plugin_maint_install');
@@ -24,11 +34,11 @@
2434
expect($source)->toContain('function plugin_maint_uninstall');
2535
});
2636

27-
it('returns version array with name key', function () use ($source) {
28-
expect($source)->toMatch('/[\'\""]name[\'\""]\s*=>/');
37+
it('declares a name in INFO', function () use ($info) {
38+
expect($info['info'])->toHaveKey('name');
2939
});
3040

31-
it('returns version array with version key', function () use ($source) {
32-
expect($source)->toMatch('/[\'\""]version[\'\""]\s*=>/');
41+
it('declares a version in INFO', function () use ($info) {
42+
expect($info['info'])->toHaveKey('version');
3343
});
3444
});

0 commit comments

Comments
 (0)