Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Make sure a file or directory files returns an array with all lowercase values.
expect('file.php')->toReturnLowercase();
```

### toReturnUppercase
Make sure a file or directory files returns an array with all uppercase values.
```php
expect('file.php')->toReturnUppercase();
```

### toReturnUnique
Make sure a file or directory files returns an array with unique values.
```php
Expand Down
9 changes: 9 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public function toReturnLowercase(int $depth = -1): void
);
}

public function toReturnUppercase(int $depth = -1): void
{
$this->applyOnDirectory(
$depth,
fn (Content $content): array => $this->notUppercasesIn($content),
'Not uppercase detected'
);
}

public function toReturnUnique(int $depth = -1): void
{
$this->applyOnDirectory(
Expand Down
25 changes: 25 additions & 0 deletions src/Investigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ private function notLowercasesIn(Content|array $content): array
return $unwanted;
}

/**
* @param Content|array<int|string, string|array<string, string>> $content
* @return array<string>
*/
private function notUppercasesIn(Content|array $content): array
{
$unwanted = [];

foreach ($content as $word) {
if (is_array($word)) {
array_push($unwanted, ...$this->notUppercasesIn($word));

continue;
}

$clean = preg_replace('/[^A-Za-z]/', '', $word);

if ($clean !== '' && ! ctype_upper($clean)) {
$unwanted[] = $word;
}
}

return $unwanted;
}

/**
* @param Content|array<int|string, string|array<string, string>> $content
* @return array<string>
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/returnsArrayOnlyUppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

return [
'',
'F@ISSA!OUX',
'PESTPHP',
'PEST',
'PLUGIN INSIDE',
'PLUGIN',
'العربية',
];
16 changes: 16 additions & 0 deletions tests/Fixtures/returnsNestedNotAllUppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

return [
'',
'F@ISSA!OUX',
'PESTPHP',
'PEST',
'PLUGIN INSIDE',
'PLUGIN' => [
'PEST',
'INsIDE',
],
'العربية',
];
16 changes: 16 additions & 0 deletions tests/Fixtures/returnsNestedUppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

return [
'',
'F@ISSA!OUX',
'PESTPHP',
'PEST',
'PLUGIN INSIDE',
'PLUGIN' => [
'PEST',
'INSIDE',
],
'العربية',
];
7 changes: 7 additions & 0 deletions tests/Fixtures/text/returnsOnlyUppercase.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

F@ISSA!OUX
PESTPHP
PEST
PLUGIN INSIDE
PLUGIN
العربية
13 changes: 13 additions & 0 deletions tests/Fixtures/uppercase/allUppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

return [
'',
'F@ISSA!OUX',
'PESTPHP',
'PEST',
'PLUGIN INSIDE',
'PLUGIN',
'العربية',
];
8 changes: 4 additions & 4 deletions tests/Unit/helpers/getFilesIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
it('gets all supported files in directory and subdirectories', function (): void {
$files = getFilesIn('tests/Fixtures');

expect($files)->toBeArray()->toHaveCount(29);
expect($files)->toBeArray()->toHaveCount(34);
});

it('gets all direct supported files in directory', function (): void {
$files = getFilesIn('tests/Fixtures', depth: 0);

expect($files)->toBeArray()->toHaveCount(11);
expect($files)->toBeArray()->toHaveCount(14);
});

it('gets all supported files in directory depth 1', function (): void {
$files = getFilesIn('tests/Fixtures', depth: 1);

expect($files)->toBeArray()->toHaveCount(27);
expect($files)->toBeArray()->toHaveCount(32);
});

it('gets all supported files in directory and subdirectories on negative depth', function (): void {
$files = getFilesIn('tests/Fixtures', depth: -4);

expect($files)->toBeArray()->toHaveCount(29);
expect($files)->toBeArray()->toHaveCount(34);
});
90 changes: 90 additions & 0 deletions tests/toReturnUppercase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

it('passes', function (string $file): void {
expect($file)->toReturnUppercase();
})->with([
'tests/Fixtures/returnsArrayOnlyUppercase.php',
'tests/Fixtures/text/returnsOnlyUppercase.stub',
]);

it('passes with not', function (string $file): void {
expect($file)->not->toReturnUppercase();
})->with([
'tests/Fixtures/returnsArrayLowercaseWithUppercase.php',
'tests/Fixtures/returnsArrayOnlyLowercase.php',
'tests/Fixtures/text/returnsArrayLowercaseWithUppercase.stub',
]);

it('passes when all nested arrays content is uppercase', function (): void {
expect('tests/Fixtures/returnsNestedUppercase.php')
->toReturnUppercase();
});

it('passes when directory is empty', function (): void {
expect('tests/Fixtures/empty')
->toReturnUppercase();
});

it('passes when all directory files content are uppercase', function (): void {
expect('tests/Fixtures/uppercase/allUppercase.php')
->toReturnUppercase(depth: 0);
});

it('fails', function (string $file): void {
expect($file)->toReturnUppercase();
})->with([
'tests/Fixtures/returnsArrayLowercaseWithUppercase.php',
'tests/Fixtures/text/returnsArrayLowercaseWithUppercase.stub',
])
->throws(ExpectationFailedException::class);

it('fails with not', function (string $file): void {
expect($file)->not->toReturnUppercase();
})->with([
'tests/Fixtures/returnsArrayOnlyUppercase.php',
'tests/Fixtures/text/returnsOnlyUppercase.stub',
])->throws(ExpectationFailedException::class);

it('fails when not all nested arrays content is uppercase', function (): void {
expect('tests/Fixtures/returnsNestedNotAllUppercase.php')
->toReturnUppercase();
})->throws(ExpectationFailedException::class, 'Not uppercase detected: INsIDE');

describe('fails when file does not exist', function (): void {
test('php file', function (): void {
expect('tests/Fixtures/notExist.php')->toReturnUppercase();
})->throws(ExpectationFailedException::class, 'tests/Fixtures/notExist.php not found');

test('text file', function (): void {
expect('tests/Fixtures/notExist.stub')->toReturnUppercase();
})->throws(ExpectationFailedException::class, 'tests/Fixtures/notExist.stub not found');
});

it('fails when directory does not exist', function (): void {
expect('tests/Fixtures/notExist')
->toReturnUppercase();
})->throws(ExpectationFailedException::class);

it('fails when not all directory files content are uppercase', function (string $directory): void {
expect($directory)->toReturnUppercase();
})->with([
'tests/Fixtures/directory1',
'tests/Fixtures/text',
])->throws(ExpectationFailedException::class);

it('fails when not all subdirectories files content are uppercase', function (string $directory): void {
expect($directory)->toReturnUppercase();
})->with([
'tests/Fixtures/directory1',
'tests/Fixtures/text',
])->throws(ExpectationFailedException::class);

it('displays words detected', function (): void {
expect('tests/Fixtures/directory')->toReturnUppercase();
})->throws(ExpectationFailedException::class, 'Not uppercase detected: f@issa!oux, pest, plugin, inside, lowercase, loWer, case, nOt');

it('displays file where error detected', function (): void {
expect('tests/Fixtures/directory')->toReturnUppercase();
})->throws(ExpectationFailedException::class, 'notAllLowerCase.php');
Loading