Skip to content

Commit 977d3b8

Browse files
authored
Merge pull request #15 from faissaloux/tobeordered
`toBeOrdered()`
2 parents e1f0f89 + efc3c69 commit 977d3b8

File tree

11 files changed

+189
-5
lines changed

11 files changed

+189
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This plugin checks what's inside the files.
55

66
[![Tests](https://github.com/faissaloux/pest-plugin-inside/actions/workflows/tests.yml/badge.svg)](https://github.com/faissaloux/pest-plugin-inside/actions/workflows/tests.yml) ![Codecov](https://img.shields.io/codecov/c/github/faissaloux/pest-plugin-inside) ![Packagist Version](https://img.shields.io/packagist/v/faissaloux/pest-plugin-inside) ![Packagist License](https://img.shields.io/packagist/l/faissaloux/pest-plugin-inside)
77

8-
## Available Methods
8+
## Available Expectations
99
### toReturnLowercase
1010
Make sure a file or directory files returns an array with all lowercase values.
1111
```php
@@ -24,6 +24,12 @@ Make sure a file or directory files returns an array with single words.
2424
expect('file.php')->toReturnSingleWords();
2525
```
2626

27+
### toBeOrdered
28+
Make sure a file or directory files returns an array with words that are ordered.
29+
```php
30+
expect('file.php')->toBeOrdered();
31+
```
32+
2733
----
2834

2935
### Success

src/Autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Pest\Expectation as PestExpectation;
77

88
$expectations = get_class_methods(Expectation::class);
9-
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn'));
9+
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn') || str_starts_with($function, 'toBe'));
1010

1111
foreach ($expectations as $expectation) {
1212
expect()->extend(

src/Expectation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public function toReturnSingleWords(int $depth = -1): void
3434
'Not single words detected'
3535
);
3636
}
37+
38+
public function toBeOrdered(int $depth = -1): void
39+
{
40+
$this->applyOnDirectory(
41+
$depth,
42+
fn (array $content): array => $this->dataNotOrderedIn($content),
43+
'Your data is not ordered'
44+
);
45+
}
3746
}

src/Investigator.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,44 @@ private function multipleWordsIn(array $array): array
8282

8383
return $unwanted;
8484
}
85+
86+
/**
87+
* @param array<string|array<string>> $array
88+
* @return array<string>
89+
*/
90+
private function dataNotOrderedIn(array $array): array
91+
{
92+
if (count($array) < 2) {
93+
return [];
94+
}
95+
96+
$unwanted = [];
97+
$lastWord = $array[0];
98+
99+
foreach ($array as $key => $value) {
100+
if ($key === 0) {
101+
continue;
102+
}
103+
104+
$currentWord = $value;
105+
if (is_array($currentWord) && is_string($lastWord)) {
106+
if ($lastWord > $key) {
107+
$unwanted[] = "$lastWord <=> $key";
108+
}
109+
$lastWord = $key;
110+
111+
array_push($unwanted, ...$this->dataNotOrderedIn($currentWord));
112+
113+
continue;
114+
}
115+
116+
if (is_string($currentWord) && is_string($lastWord) && $lastWord > $currentWord) {
117+
$unwanted[] = "$lastWord <=> $currentWord";
118+
}
119+
120+
$lastWord = $currentWord;
121+
}
122+
123+
return $unwanted;
124+
}
85125
}

test.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
$array = [
4+
'',
5+
'f@issA!oux',
6+
'pest',
7+
'plugin',
8+
'inside',
9+
'lowercase',
10+
'lower',
11+
'case',
12+
];
13+
14+
sort($array);
15+
16+
// array(8) {
17+
// [0]=>
18+
// string(0) ""
19+
// [1]=>
20+
// string(4) "case"
21+
// [2]=>
22+
// string(10) "f@issA!oux"
23+
// [3]=>
24+
// string(6) "inside"
25+
// [4]=>
26+
// string(5) "lower"
27+
// [5]=>
28+
// string(9) "lowercase"
29+
// [6]=>
30+
// string(4) "pest"
31+
// [7]=>
32+
// string(6) "plugin"
33+
// }
34+
var_dump($array);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issA!oux',
8+
'pest',
9+
'plugin',
10+
'inside',
11+
'lowercase',
12+
'lower',
13+
'case',
14+
];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'case',
8+
'f@issA!oux',
9+
'inside',
10+
'lower',
11+
'lowercase',
12+
'pest',
13+
'plugin',
14+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'case',
8+
'f@issA!oux',
9+
'inside',
10+
'lower',
11+
'lowercase',
12+
'pest',
13+
'plugin' => [
14+
'pest',
15+
'inSide',
16+
],
17+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'case',
8+
'f@issA!oux',
9+
'inside',
10+
'lower',
11+
'lowercase',
12+
'pest',
13+
'plugin' => [
14+
'inSide',
15+
'pest',
16+
],
17+
];

tests/Unit/helpers/getFilesIn.php

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

8-
expect($files)->toBeArray()->toHaveCount(18);
8+
expect($files)->toBeArray()->toHaveCount(22);
99
});
1010

1111
it('gets all direct php files in directory', function (): void {
@@ -17,11 +17,11 @@
1717
it('gets all php files in directory depth 1', function (): void {
1818
$files = getFilesIn('tests/Fixtures', depth: 1);
1919

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

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

26-
expect($files)->toBeArray()->toHaveCount(18);
26+
expect($files)->toBeArray()->toHaveCount(22);
2727
});

0 commit comments

Comments
 (0)