Merge branch '2.x' into 2.x-fix-ignored-description-for-lazy-datasets

This commit is contained in:
Nuno Maduro
2022-12-23 23:01:39 +00:00
committed by GitHub
11 changed files with 153 additions and 21 deletions

View File

@ -13,7 +13,6 @@ trait Retrievable
* @template TRetrievableValue
*
* Safely retrieve the value at the given key from an object or array.
*
* @template TRetrievableValue
*
* @param array<string, TRetrievableValue>|object $value

View File

@ -0,0 +1,26 @@
<?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,6 +6,11 @@ namespace Pest;
use BadMethodCallException;
use Closure;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Arch\Expectations\ToDependOn;
use Pest\Arch\Expectations\ToDependOnNothing;
use Pest\Arch\Expectations\ToOnlyBeUsedOn;
use Pest\Arch\Expectations\ToOnlyDependOn;
use Pest\Concerns\Extendable;
use Pest\Concerns\Pipeable;
use Pest\Concerns\Retrievable;
@ -24,7 +29,7 @@ use PHPUnit\Framework\ExpectationFailedException;
*
* @template TValue
*
* @property Expectation $not Creates the opposite expectation.
* @property OppositeExpectation $not Creates the opposite expectation.
* @property EachExpectation $each Creates an expectation on each element on the traversable value.
*
* @mixin Mixins\Expectation<TValue>
@ -350,4 +355,42 @@ final class Expectation
{
return new Any();
}
/**
* Asserts that the given expectation target depends on the given dependencies.
*
* @param array<int, string>|string $dependencies
*/
public function toDependOn(array|string $dependencies): ArchExpectation
{
return ToDependOn::make($this, $dependencies);
}
/**
* Asserts that the given expectation target does not have any dependencies.
*/
public function toDependOnNothing(): ArchExpectation
{
return ToDependOnNothing::make($this);
}
/**
* Asserts that the given expectation dependency is only depended on by the given targets.
*
* @param array<int, string>|string $targets
*/
public function toOnlyBeUsedOn(array|string $targets): ArchExpectation
{
return ToOnlyBeUsedOn::make($this, $targets);
}
/**
* Asserts that the given expectation target does "only" depend on the given dependencies.
*
* @param array<int, string>|string $targets
*/
public function toOnlyDependOn(array|string $targets): ArchExpectation
{
return ToOnlyDependOn::make($this, $targets);
}
}

View File

@ -4,6 +4,11 @@ declare(strict_types=1);
namespace Pest\Expectations;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Arch\Expectations\ToDependOn;
use Pest\Arch\GroupArchExpectation;
use Pest\Arch\SingleArchExpectation;
use Pest\Exceptions\InvalidExpectation;
use Pest\Expectation;
use Pest\Support\Arr;
use PHPUnit\Framework\ExpectationFailedException;
@ -52,6 +57,46 @@ final class OppositeExpectation
return $this->original;
}
/**
* Asserts that the given expectation target depends on the given dependencies.
*
* @param array<int, string>|string $dependencies
*/
public function toDependOn(array|string $dependencies): ArchExpectation
{
return GroupArchExpectation::fromExpectations(array_map(fn (string $target): SingleArchExpectation => ToDependOn::make($this->original, $target)->opposite(
fn () => $this->throwExpectationFailedException('toDependOn', $target),
), is_string($dependencies) ? [$dependencies] : $dependencies));
}
/**
* Asserts that the given expectation dependency is only depended on by the given targets.
*
* @param array<int, string>|string $targets
*/
public function toOnlyBeUsedOn(array|string $targets): never
{
throw InvalidExpectation::fromMethods(['not', 'toOnlyBeUsedOn']);
}
/**
* Asserts that the given expectation target does "only" depend on the given dependencies.
*
* @param array<int, string>|string $dependencies
*/
public function toOnlyDependOn(array|string $dependencies): never
{
throw InvalidExpectation::fromMethods(['not', 'toOnlyDependOn']);
}
/**
* Asserts that the given expectation target does not have any dependencies.
*/
public function toDependOnNothing(): never
{
throw InvalidExpectation::fromMethods(['not', 'toDependOnNothing']);
}
/**
* Handle dynamic method calls into the original expectation.
*
@ -89,10 +134,12 @@ final class OppositeExpectation
/**
* Creates a new expectation failed exception with a nice readable message.
*
* @param array<int, mixed> $arguments
* @param array<int, mixed>|string $arguments
*/
private function throwExpectationFailedException(string $name, array $arguments = []): never
public function throwExpectationFailedException(string $name, array|string $arguments = []): never
{
$arguments = is_array($arguments) ? $arguments : [$arguments];
$exporter = new Exporter();
$toString = fn ($argument): string => $exporter->shortenedExport($argument);

View File

@ -46,7 +46,7 @@ final class Memory implements AddsOutput, HandlesArguments
{
if ($this->enabled) {
$this->output->writeln(sprintf(
' <fg=gray;options=bold>Memory:</> <fg=default>%s MB</>',
' <fg=gray>Memory:</> <fg=default>%s MB</>',
round(memory_get_usage(true) / 1000 ** 2, 3)
));
}

View File

@ -162,11 +162,10 @@ final class DatasetsRepository
$datasets[$index] = iterator_to_array($datasets[$index], $preserveKeysForArrayIterator);
}
// @phpstan-ignore-next-line
foreach ($datasets[$index] as $key => $values) {
$values = is_array($values) ? $values : [$values];
$processedDataset[] = [
'label' => self::getDatasetDescription($key, $values), // @phpstan-ignore-line
'label' => self::getDatasetDescription($key, $values),
'values' => $values,
];
}