Skip to content

Commit b524f51

Browse files
authored
Merge pull request #8 from faissaloux/refactor-expectations
Refactor expectations
2 parents 8832be5 + 5777972 commit b524f51

File tree

3 files changed

+88
-47
lines changed

3 files changed

+88
-47
lines changed

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ parameters:
55

66
ignoreErrors:
77
- "#Undefined variable: \\$this#"
8-
- "#Variable \\$this might not be defined#"

src/Autoload.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
declare(strict_types=1);
44

5-
namespace Faissaloux\PestInside;
5+
use Faissaloux\PestInside\Expectation;
6+
use Pest\Expectation as PestExpectation;
67

7-
require_once __DIR__.'/Expectation.php';
8+
$expectations = get_class_methods(Expectation::class);
9+
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn'));
10+
11+
foreach ($expectations as $expectation) {
12+
expect()->extend(
13+
$expectation,
14+
function (int $depth = -1) use ($expectation): PestExpectation {
15+
if (is_callable($callback = [new Expectation($this->value), $expectation])) {
16+
call_user_func($callback, ...func_get_args());
17+
}
18+
19+
return $this;
20+
}
21+
);
22+
}

src/Expectation.php

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,65 @@
22

33
declare(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

Comments
 (0)