Skip to content

Commit

Permalink
group expectation in Expectation class
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Oct 15, 2024
1 parent 8832be5 commit 7e2abc2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
19 changes: 17 additions & 2 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

declare(strict_types=1);

namespace Faissaloux\PestInside;
use Faissaloux\PestInside\Expectation;
use Pest\Expectation as PestExpectation;

require_once __DIR__.'/Expectation.php';
$expectations = get_class_methods(Expectation::class);
$expectations = array_filter($expectations, fn ($function): bool => str_starts_with($function, 'toReturn'));

foreach ($expectations as $expectation) {
expect()->extend(
$expectation,
function (int $depth = -1) use ($expectation): PestExpectation {
if (is_callable($callback = [new Expectation($this->value), $expectation])) {
call_user_func($callback, ...func_get_args());
}

return $this;
}
);
}
63 changes: 36 additions & 27 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

declare(strict_types=1);

namespace Faissaloux\PestInside;

use Pest\Expectation as PestExpectation;

expect()->extend(
'toReturnUnique',
function (int $depth = -1): PestExpectation {
$files = [$this->value];
final class Expectation
{
public function __construct(private string $value) {}

/**
* @return PestExpectation<string>
*/
public function toReturnLowercase(int $depth = -1): PestExpectation
{
$files = [$this->value];

if (is_dir($files[0])) {
$files = getFilesIn($files[0], $depth);

if ($files === []) {
expect(true)->toBeTrue();

return $this;
return new PestExpectation($this->value);
}
}

Expand All @@ -28,27 +36,36 @@ function (int $depth = -1): PestExpectation {

expect($content)->toBeArray();

$duplicates = array_diff_assoc($content, array_unique($content));
// Clean up content from numerics and special characters.
$cleanContent = array_map(function (string $word): string {
return (string) preg_replace('/[^A-Za-z]/', '', $word);
}, $content);

expect($duplicates)->toBeEmpty('Duplicates found:'.implode(',', $duplicates)." in $file");
foreach ($cleanContent as $key => $word) {
if ($word === '') {
continue;
}

expect($word)->toBeLowercase("Not lowercase word detected: $content[$key] in $file");
}
}

return $this;
return new PestExpectation($this->value);
}
);

expect()->extend(
'toReturnLowercase',
function (int $depth = -1): PestExpectation {
/**
* @return PestExpectation<string>
*/
public function toReturnUnique(int $depth = -1): PestExpectation {
$files = [$this->value];

if (is_dir($files[0])) {
$files = getFilesIn($files[0], $depth);

if ($files === []) {
expect(true)->toBeTrue();

return $this;
return new PestExpectation($this->value);
}
}

Expand All @@ -61,20 +78,12 @@ function (int $depth = -1): PestExpectation {

expect($content)->toBeArray();

// Clean up content from numerics and special characters.
$cleanContent = array_map(function (string $word): string {
return (string) preg_replace('/[^A-Za-z]/', '', $word);
}, $content);

foreach ($cleanContent as $key => $word) {
if ($word === '') {
continue;
}
$duplicates = array_diff_assoc($content, array_unique($content));

expect($word)->toBeLowercase("Not lowercase word detected: $content[$key] in $file");
}
expect($duplicates)->toBeEmpty('Duplicates found:'.implode(',', $duplicates)." in $file");
}

return $this;
return new PestExpectation($this->value);
}
);

}

0 comments on commit 7e2abc2

Please sign in to comment.