Skip to content

Commit 5fdcd00

Browse files
authored
Merge pull request #20 from faissaloux/toReturnUppercase
`toReturnUppercase()`
2 parents 61b65ef + 4e3e7d1 commit 5fdcd00

File tree

10 files changed

+199
-4
lines changed

10 files changed

+199
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Make sure a file or directory files returns an array with all lowercase values.
2525
expect('file.php')->toReturnLowercase();
2626
```
2727

28+
### toReturnUppercase
29+
Make sure a file or directory files returns an array with all uppercase values.
30+
```php
31+
expect('file.php')->toReturnUppercase();
32+
```
33+
2834
### toReturnUnique
2935
Make sure a file or directory files returns an array with unique values.
3036
```php

src/Expectation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ public function toReturnLowercase(int $depth = -1): void
1919
);
2020
}
2121

22+
public function toReturnUppercase(int $depth = -1): void
23+
{
24+
$this->applyOnDirectory(
25+
$depth,
26+
fn (Content $content): array => $this->notUppercasesIn($content),
27+
'Not uppercase detected'
28+
);
29+
}
30+
2231
public function toReturnUnique(int $depth = -1): void
2332
{
2433
$this->applyOnDirectory(

src/Investigator.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ private function notLowercasesIn(Content|array $content): array
3636
return $unwanted;
3737
}
3838

39+
/**
40+
* @param Content|array<int|string, string|array<string, string>> $content
41+
* @return array<string>
42+
*/
43+
private function notUppercasesIn(Content|array $content): array
44+
{
45+
$unwanted = [];
46+
47+
foreach ($content as $word) {
48+
if (is_array($word)) {
49+
array_push($unwanted, ...$this->notUppercasesIn($word));
50+
51+
continue;
52+
}
53+
54+
$clean = preg_replace('/[^A-Za-z]/', '', $word);
55+
56+
if ($clean !== '' && ! ctype_upper($clean)) {
57+
$unwanted[] = $word;
58+
}
59+
}
60+
61+
return $unwanted;
62+
}
63+
3964
/**
4065
* @param Content|array<int|string, string|array<string, string>> $content
4166
* @return array<string>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'F@ISSA!OUX',
8+
'PESTPHP',
9+
'PEST',
10+
'PLUGIN INSIDE',
11+
'PLUGIN',
12+
'العربية',
13+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'F@ISSA!OUX',
8+
'PESTPHP',
9+
'PEST',
10+
'PLUGIN INSIDE',
11+
'PLUGIN' => [
12+
'PEST',
13+
'INsIDE',
14+
],
15+
'العربية',
16+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'F@ISSA!OUX',
8+
'PESTPHP',
9+
'PEST',
10+
'PLUGIN INSIDE',
11+
'PLUGIN' => [
12+
'PEST',
13+
'INSIDE',
14+
],
15+
'العربية',
16+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
F@ISSA!OUX
3+
PESTPHP
4+
PEST
5+
PLUGIN INSIDE
6+
PLUGIN
7+
العربية
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'F@ISSA!OUX',
8+
'PESTPHP',
9+
'PEST',
10+
'PLUGIN INSIDE',
11+
'PLUGIN',
12+
'العربية',
13+
];

tests/Unit/helpers/getFilesIn.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
it('gets all supported files in directory and subdirectories', function (): void {
66
$files = getFilesIn('tests/Fixtures');
77

8-
expect($files)->toBeArray()->toHaveCount(29);
8+
expect($files)->toBeArray()->toHaveCount(34);
99
});
1010

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

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

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

20-
expect($files)->toBeArray()->toHaveCount(27);
20+
expect($files)->toBeArray()->toHaveCount(32);
2121
});
2222

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

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

tests/toReturnUppercase.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (string $file): void {
6+
expect($file)->toReturnUppercase();
7+
})->with([
8+
'tests/Fixtures/returnsArrayOnlyUppercase.php',
9+
'tests/Fixtures/text/returnsOnlyUppercase.stub',
10+
]);
11+
12+
it('passes with not', function (string $file): void {
13+
expect($file)->not->toReturnUppercase();
14+
})->with([
15+
'tests/Fixtures/returnsArrayLowercaseWithUppercase.php',
16+
'tests/Fixtures/returnsArrayOnlyLowercase.php',
17+
'tests/Fixtures/text/returnsArrayLowercaseWithUppercase.stub',
18+
]);
19+
20+
it('passes when all nested arrays content is uppercase', function (): void {
21+
expect('tests/Fixtures/returnsNestedUppercase.php')
22+
->toReturnUppercase();
23+
});
24+
25+
it('passes when directory is empty', function (): void {
26+
expect('tests/Fixtures/empty')
27+
->toReturnUppercase();
28+
});
29+
30+
it('passes when all directory files content are uppercase', function (): void {
31+
expect('tests/Fixtures/uppercase/allUppercase.php')
32+
->toReturnUppercase(depth: 0);
33+
});
34+
35+
it('fails', function (string $file): void {
36+
expect($file)->toReturnUppercase();
37+
})->with([
38+
'tests/Fixtures/returnsArrayLowercaseWithUppercase.php',
39+
'tests/Fixtures/text/returnsArrayLowercaseWithUppercase.stub',
40+
])
41+
->throws(ExpectationFailedException::class);
42+
43+
it('fails with not', function (string $file): void {
44+
expect($file)->not->toReturnUppercase();
45+
})->with([
46+
'tests/Fixtures/returnsArrayOnlyUppercase.php',
47+
'tests/Fixtures/text/returnsOnlyUppercase.stub',
48+
])->throws(ExpectationFailedException::class);
49+
50+
it('fails when not all nested arrays content is uppercase', function (): void {
51+
expect('tests/Fixtures/returnsNestedNotAllUppercase.php')
52+
->toReturnUppercase();
53+
})->throws(ExpectationFailedException::class, 'Not uppercase detected: INsIDE');
54+
55+
describe('fails when file does not exist', function (): void {
56+
test('php file', function (): void {
57+
expect('tests/Fixtures/notExist.php')->toReturnUppercase();
58+
})->throws(ExpectationFailedException::class, 'tests/Fixtures/notExist.php not found');
59+
60+
test('text file', function (): void {
61+
expect('tests/Fixtures/notExist.stub')->toReturnUppercase();
62+
})->throws(ExpectationFailedException::class, 'tests/Fixtures/notExist.stub not found');
63+
});
64+
65+
it('fails when directory does not exist', function (): void {
66+
expect('tests/Fixtures/notExist')
67+
->toReturnUppercase();
68+
})->throws(ExpectationFailedException::class);
69+
70+
it('fails when not all directory files content are uppercase', function (string $directory): void {
71+
expect($directory)->toReturnUppercase();
72+
})->with([
73+
'tests/Fixtures/directory1',
74+
'tests/Fixtures/text',
75+
])->throws(ExpectationFailedException::class);
76+
77+
it('fails when not all subdirectories files content are uppercase', function (string $directory): void {
78+
expect($directory)->toReturnUppercase();
79+
})->with([
80+
'tests/Fixtures/directory1',
81+
'tests/Fixtures/text',
82+
])->throws(ExpectationFailedException::class);
83+
84+
it('displays words detected', function (): void {
85+
expect('tests/Fixtures/directory')->toReturnUppercase();
86+
})->throws(ExpectationFailedException::class, 'Not uppercase detected: f@issa!oux, pest, plugin, inside, lowercase, loWer, case, nOt');
87+
88+
it('displays file where error detected', function (): void {
89+
expect('tests/Fixtures/directory')->toReturnUppercase();
90+
})->throws(ExpectationFailedException::class, 'notAllLowerCase.php');

0 commit comments

Comments
 (0)