Skip to content

Commit

Permalink
Inside class
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Oct 21, 2024
1 parent ffdb46b commit e2de4b4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 99 deletions.
109 changes: 10 additions & 99 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,11 @@

namespace Faissaloux\PestInside;

use Pest\Expectation as PestExpectation;

final class Expectation
final class Expectation extends Inside
{
/**
* @var array<string>
*/
private array $files;

public function __construct(private string $value)
{
$this->files = [$value];
}

private function fetchFilesIfDirectory(int $depth): void
public function toReturnLowercase(int $depth = -1): void
{
if (is_dir($this->files[0])) {
$this->files = getFilesIn($this->files[0], $depth);
}
}

private function checkFileExistence(string $file): void
{
if (! file_exists($file)) {
expect(true)->toBeFalse("$file not found!");
}
}

/**
* @return array<string>
*/
private function getContentFrom(string $file): array
{
$content = include $file;

expect($content)->toBeArray();

return $content;
}

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

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

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

foreach ($this->files as $file) {
$this->checkFileExistence($file);

$content = $this->getContentFrom($file);

$this->applyOnDirectory($depth, function ($file, $content) {
// Clean up content from numerics and special characters.
$cleanContent = array_map(function (string $word): string {
return (string) preg_replace('/[^A-Za-z]/', '', $word);
Expand All @@ -74,62 +21,26 @@ public function toReturnLowercase(int $depth = -1): PestExpectation

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

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

/**
* @return PestExpectation<string>
*/
public function toReturnUnique(int $depth = -1): PestExpectation
public function toReturnUnique(int $depth = -1): void
{
$this->fetchFilesIfDirectory($depth);

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

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

foreach ($this->files as $file) {
$this->checkFileExistence($file);

$content = $this->getContentFrom($file);

$this->applyOnDirectory($depth, function (string $file, array $content): void {
$duplicates = array_diff_assoc($content, array_unique($content));

expect($duplicates)->toBeEmpty('Duplicates found: '.implode(',', $duplicates)." in $file");
}

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

/**
* @return PestExpectation<string>
*/
public function toReturnSingleWords(int $depth = -1): PestExpectation
public function toReturnSingleWords(int $depth = -1): void
{
$this->fetchFilesIfDirectory($depth);

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

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

foreach ($this->files as $file) {
$this->checkFileExistence($file);

$content = $this->getContentFrom($file);

$this->applyOnDirectory($depth, function (string $file, array $content): void {
$notSingle = array_filter($content, function (string $word): bool {
return str_contains(trim($word), ' ');
});

expect($notSingle)->toBeEmpty('Not single words detected: '.implode(',', $notSingle)." in $file");
}

return new PestExpectation($this->value);
});
}
}
70 changes: 70 additions & 0 deletions src/Inside.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Faissaloux\PestInside;

use Pest\Expectation as PestExpectation;

class Inside
{
/**
* @var array<string>
*/
protected array $files;

public function __construct(protected string $value)
{
$this->files = [$value];
}

protected function fetchFilesIfDirectory(int $depth): void
{
if (is_dir($this->files[0])) {
$this->files = getFilesIn($this->files[0], $depth);
}
}

protected function checkFileExistence(string $file): void
{
if (! file_exists($file)) {
expect(true)->toBeFalse("$file not found!");
}
}

/**
* @return array<string>
*/
protected function getContentFrom(string $file): array
{
$content = include $file;

expect($content)->toBeArray();

return $content;
}

/**
* @return PestExpectation<string>
*/
protected function applyOnDirectory(int $depth, callable $callback): PestExpectation
{
$this->fetchFilesIfDirectory($depth);

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

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

foreach ($this->files as $file) {
$this->checkFileExistence($file);

$content = $this->getContentFrom($file);

$callback($file, $content);
}

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

0 comments on commit e2de4b4

Please sign in to comment.