22
33declare (strict_types=1 );
44
5- use Pest \ Expectation as PestExpectation ;
5+ namespace Faissaloux \ PestInside ;
66
7- expect ()->extend (
8- 'toReturnUnique ' ,
9- function (int $ depth = -1 ): PestExpectation {
10- $ files = [$ this ->value ];
7+ use Pest \Expectation as PestExpectation ;
118
12- if (is_dir ($ files [0 ])) {
13- $ files = getFilesIn ($ files [0 ], $ depth );
9+ final class Expectation
10+ {
11+ /**
12+ * @var array<string>
13+ */
14+ private array $ files ;
1415
15- if ($ files === []) {
16- expect (true )->toBeTrue ();
16+ public function __construct (private string $ value )
17+ {
18+ $ this ->files = [$ value ];
19+ }
1720
18- return $ this ;
19- }
21+ private function fetchFilesIfDirectory (int $ depth ): void
22+ {
23+ if (is_dir ($ this ->files [0 ])) {
24+ $ this ->files = getFilesIn ($ this ->files [0 ], $ depth );
2025 }
26+ }
2127
22- foreach ((array ) $ files as $ file ) {
23- if (! file_exists ($ file )) {
24- expect (true )->toBeFalse ("$ file not found! " );
25- }
26-
27- $ content = include $ file ;
28-
29- expect ($ content )->toBeArray ();
28+ private function checkFileExistence (string $ file ): void
29+ {
30+ if (! file_exists ($ file )) {
31+ expect (true )->toBeFalse ("$ file not found! " );
32+ }
33+ }
3034
31- $ duplicates = array_diff_assoc ($ content , array_unique ($ content ));
35+ /**
36+ * @return array<string>
37+ */
38+ private function getContentFrom (string $ file ): array
39+ {
40+ $ content = include $ file ;
3241
33- expect ($ duplicates )->toBeEmpty ('Duplicates found: ' .implode (', ' , $ duplicates )." in $ file " );
34- }
42+ expect ($ content )->toBeArray ();
3543
36- return $ this ;
44+ return $ content ;
3745 }
38- );
39-
40- expect ()->extend (
41- 'toReturnLowercase ' ,
42- function (int $ depth = -1 ): PestExpectation {
43- $ files = [$ this ->value ];
4446
45- if (is_dir ($ files [0 ])) {
46- $ files = getFilesIn ($ files [0 ], $ depth );
47+ /**
48+ * @return PestExpectation<string>
49+ */
50+ public function toReturnLowercase (int $ depth = -1 ): PestExpectation
51+ {
52+ $ this ->fetchFilesIfDirectory ($ depth );
4753
48- if ($ files === []) {
49- expect (true )->toBeTrue ();
54+ if ($ this -> files === []) {
55+ expect (true )->toBeTrue ();
5056
51- return $ this ;
52- }
57+ return new PestExpectation ($ this ->value );
5358 }
5459
55- foreach ((array ) $ files as $ file ) {
56- if (! file_exists ($ file )) {
57- expect (true )->toBeFalse ("$ file not found! " );
58- }
59-
60- $ content = include $ file ;
60+ foreach ($ this ->files as $ file ) {
61+ $ this ->checkFileExistence ($ file );
6162
62- expect ( $ content)-> toBeArray ( );
63+ $ content = $ this -> getContentFrom ( $ file );
6364
6465 // Clean up content from numerics and special characters.
6566 $ cleanContent = array_map (function (string $ word ): string {
@@ -75,6 +76,32 @@ function (int $depth = -1): PestExpectation {
7576 }
7677 }
7778
78- return $ this ;
79+ return new PestExpectation ($ this ->value );
80+ }
81+
82+ /**
83+ * @return PestExpectation<string>
84+ */
85+ public function toReturnUnique (int $ depth = -1 ): PestExpectation
86+ {
87+ $ this ->fetchFilesIfDirectory ($ depth );
88+
89+ if ($ this ->files === []) {
90+ expect (true )->toBeTrue ();
91+
92+ return new PestExpectation ($ this ->value );
93+ }
94+
95+ foreach ($ this ->files as $ file ) {
96+ $ this ->checkFileExistence ($ file );
97+
98+ $ content = $ this ->getContentFrom ($ file );
99+
100+ $ duplicates = array_diff_assoc ($ content , array_unique ($ content ));
101+
102+ expect ($ duplicates )->toBeEmpty ('Duplicates found: ' .implode (', ' , $ duplicates )." in $ file " );
103+ }
104+
105+ return new PestExpectation ($ this ->value );
79106 }
80- );
107+ }
0 commit comments