feat: adds toHaveFileSystemPermissions expectation

This commit is contained in:
Nuno Maduro
2024-07-14 23:16:04 +01:00
parent d3be6b72dd
commit 0331a87be1
9 changed files with 58 additions and 4 deletions

View File

@ -3,4 +3,5 @@
3. `pest()->`
4. `->notes()`, `->issue`, `->pr`, etc
5. `arch()->preset`
6. `toHaveFileSystemPermissions`

View File

@ -23,7 +23,7 @@
"nunomaduro/termwind": "^2.0.1",
"pestphp/pest-plugin": "^3.0.0",
"pestphp/pest-plugin-arch": "^3.0.0",
"phpunit/phpunit": "^11.2.6"
"phpunit/phpunit": "^11.2.7"
},
"conflict": {
"sebastian/exporter": "<6.0.0",

View File

@ -20,7 +20,7 @@ final class Base extends AbstractPreset
'debug_print_backtrace',
'dump',
'ray',
'ds',
'ds',
'die',
'goto',
'global',

View File

@ -37,5 +37,11 @@ final class Security extends AbstractPreset
'dl',
'assert',
])->not->toBeUsed();
foreach ($this->userNamespaces as $namespace) {
$this->expectations[] = expect($namespace)
->not
->toHaveFileSystemPermissions('0777');
}
}
}

View File

@ -434,6 +434,19 @@ final class Expectation
return ToUse::make($this, $targets);
}
/**
* Asserts that the given expectation target does have the given permissions
*/
public function toHaveFileSystemPermissions(string $permissions): ArchExpectation
{
return Targeted::make(
$this,
fn (ObjectDescription $object): bool => substr(sprintf('%o', fileperms($object->path)), -4) === $permissions,
sprintf('permissions to be [%s]', $permissions),
FileLineFinder::where(fn (string $line): bool => str_contains($line, '<?php')),
);
}
/**
* Asserts that the given expectation target use the "declare(strict_types=1)" declaration.
*/

View File

@ -75,6 +75,19 @@ final class OppositeExpectation
), is_string($targets) ? [$targets] : $targets));
}
/**
* Asserts that the given expectation target does not have the given permissions
*/
public function toHaveFileSystemPermissions(string $permissions): ArchExpectation
{
return Targeted::make(
$this->original,
fn (ObjectDescription $object): bool => substr(sprintf('%o', fileperms($object->path)), -4) !== $permissions,
sprintf('permissions not to be [%s]', $permissions),
FileLineFinder::where(fn (string $line): bool => str_contains($line, '<?php')),
);
}
/**
* Asserts that the given expectation target does not use the "declare(strict_types=1)" declaration.
*/

View File

@ -741,6 +741,11 @@
✓ class has destructor
✓ class has no destructor
PASS Tests\Features\Expect\toHaveFileSystemPermissions
✓ pass
✓ failures
✓ not failures
PASS Tests\Features\Expect\toHaveKebabCaseKeys
✓ pass
✓ failures
@ -1516,4 +1521,4 @@
WARN Tests\Visual\Version
- visual snapshot of help command output
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1063 passed (2602 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1066 passed (2611 assertions)

View File

@ -0,0 +1,16 @@
<?php
use Pest\Arch\Exceptions\ArchExpectationFailedException;
test('pass', function () {
expect(Pest\Preset::class)->toHaveFileSystemPermissions('0644')
->and('Pest')->not->toHaveFileSystemPermissions('0777');
});
test('failures', function () {
expect(Pest\Preset::class)->toHaveFileSystemPermissions('0755');
})->throws(ArchExpectationFailedException::class, "Expecting 'src/Preset.php' permissions to be [0755].");
test('not failures', function () {
expect(Pest\Preset::class)->not->toHaveFileSystemPermissions('0644');
})->throws(ArchExpectationFailedException::class, "Expecting 'src/Preset.php' permissions not to be [0644].");

View File

@ -16,7 +16,7 @@ $run = function () {
test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1049 passed (2570 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1052 passed (2579 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();