feat: improves not->toDependOn

This commit is contained in:
Nuno Maduro
2022-12-21 00:09:38 +00:00
parent 9596274b14
commit b04207d9ea
3 changed files with 39 additions and 21 deletions

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Pest\Exceptions;
use LogicException;
use NunoMaduro\Collision\Contracts\RenderlessEditor;
use NunoMaduro\Collision\Contracts\RenderlessTrace;
use Symfony\Component\Console\Exception\ExceptionInterface;
/**
* @internal
*/
final class InvalidExpectation extends LogicException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
{
/**
* @param array<int, string> $methods
* @throws self
*/
public static function fromMethods(array $methods): never
{
throw new self(sprintf('Expectation [%s] is not valid.', implode('->', $methods)));
}
}

View File

@ -6,7 +6,7 @@ namespace Pest;
use BadMethodCallException;
use Closure;
use Pest\Arch\ArchExpectation;
use Pest\Arch\Conctracts\ArchExpectation;
use Pest\Arch\Expectations\ToDependOn;
use Pest\Arch\Expectations\ToDependOnNothing;
use Pest\Arch\Expectations\ToOnlyDependOn;
@ -359,7 +359,6 @@ final class Expectation
* Asserts that the layer depends (not exclusively) on the given layers.
*
* @param array<int, string>|string $targets
* @return ArchExpectation<string>
*/
public function toDependOn(array|string $targets): ArchExpectation
{
@ -370,7 +369,6 @@ final class Expectation
* Asserts that the layer only depends on the given layers.
*
* @param array<int, string>|string $targets
* @return ArchExpectation<string>
*/
public function toOnlyDependOn(array|string $targets): ArchExpectation
{
@ -379,8 +377,6 @@ final class Expectation
/**
* Asserts that the layer is not allowed to depend on any other layer.
*
* @return ArchExpectation<string>
*/
public function toDependOnNothing(): ArchExpectation
{

View File

@ -4,10 +4,13 @@ declare(strict_types=1);
namespace Pest\Expectations;
use Pest\Arch\ArchExpectation;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Arch\Expectations\ToDependOn;
use Pest\Arch\Expectations\ToDependOnNothing;
use Pest\Arch\Expectations\ToOnlyDependOn;
use Pest\Arch\GroupArchExpectation;
use Pest\Arch\SingleArchExpectation;
use Pest\Exceptions\InvalidExpectation;
use Pest\Expectation;
use Pest\Support\Arr;
use PHPUnit\Framework\ExpectationFailedException;
@ -60,38 +63,32 @@ final class OppositeExpectation
* Asserts that the layer does not depend on the given layers.
*
* @param array<int, string>|string $targets
* @return ArchExpectation<string>
*/
public function toDependOn(array|string $targets): ArchExpectation
{
return ToDependOn::make($this->original, $targets)->opposite(
fn () => $this->throwExpectationFailedException('toDependOn', $targets),
);
return GroupArchExpectation::fromExpectations(array_map(function (string $target) : SingleArchExpectation {
return ToDependOn::make($this->original, $target)->opposite(
fn () => $this->throwExpectationFailedException('toDependOn', $target),
);
}, is_string($targets) ? [$targets] : $targets));
}
/**
* Asserts that the layer does not only depends on the given layers.
*
* @param array<int, string>|string $targets
* @return ArchExpectation<string>
*/
public function toOnlyDependOn(array|string $targets): ArchExpectation
public function toOnlyDependOn(array|string $targets): never
{
return ToOnlyDependOn::make($this->original, $targets)->opposite(
fn () => $this->throwExpectationFailedException('toOnlyDependOn', $targets),
);
throw InvalidExpectation::fromMethods(['not', 'toOnlyDependOn']);
}
/**
* Asserts that the layer is depends on at least one layer.
*
* @return ArchExpectation<string>
*/
public function toDependOnNothing(): ArchExpectation
public function toDependOnNothing(): never
{
return ToDependOnNothing::make($this->original)->opposite(
fn () => $this->throwExpectationFailedException('toDependOnNothing'),
);
throw InvalidExpectation::fromMethods(['not', 'toDependOnNothing']);
}
/**