wip toward lvl9

This commit is contained in:
Fabio Ivona
2021-11-18 01:01:56 +01:00
parent d4a8a3ec37
commit 7bcd3ebaee
8 changed files with 39 additions and 7 deletions

View File

@ -0,0 +1,11 @@
<?php
namespace Pest\Exceptions;
class ExpectationException extends \Exception
{
public static function invalidValue($expectationName, $valueRequired): ExpectationException
{
return new ExpectationException(sprintf('%s expectation requires a %s value.', $expectationName, $valueRequired));
}
}

View File

@ -9,6 +9,7 @@ use Closure;
use InvalidArgumentException; use InvalidArgumentException;
use Pest\Concerns\Extendable; use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues; use Pest\Concerns\RetrievesValues;
use Pest\Exceptions\ExpectationException;
use Pest\Support\Arr; use Pest\Support\Arr;
use Pest\Support\NullClosure; use Pest\Support\NullClosure;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
@ -68,6 +69,10 @@ final class Expectation
*/ */
public function json(): Expectation public function json(): Expectation
{ {
if (!is_string($this->value)) {
throw ExpectationException::invalidValue('json', 'string');
}
return $this->toBeJson()->and(json_decode($this->value, true)); return $this->toBeJson()->and(json_decode($this->value, true));
} }

View File

@ -113,7 +113,10 @@ if (!function_exists('it')) {
{ {
$description = sprintf('it %s', $description); $description = sprintf('it %s', $description);
return test($description, $closure); /** @var TestCall $test */
$test = test($description, $closure);
return $test;
} }
} }

View File

@ -80,7 +80,10 @@ final class HigherOrderExpectation
} }
if (!$this->expectationHasMethod($name)) { if (!$this->expectationHasMethod($name)) {
return new self($this->original, $this->retrieve($name, $this->getValue())); /** @var array<string, mixed>|object $value */
$value = $this->getValue();
return new self($this->original, $this->retrieve($name, $value));
} }
return $this->performAssertion($name, []); return $this->performAssertion($name, []);

View File

@ -80,7 +80,10 @@ final class Coverage implements AddsOutput, HandlesArguments
} }
if ($input->getOption(self::MIN_OPTION) !== null) { if ($input->getOption(self::MIN_OPTION) !== null) {
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION); /** @var int|float $min_option */
$min_option = $input->getOption(self::MIN_OPTION);
$this->coverageMin = (float) $min_option;
} }
return $originals; return $originals;

View File

@ -41,11 +41,13 @@ final class Container
*/ */
public function get(string $id) public function get(string $id)
{ {
if (array_key_exists($id, $this->instances)) { if (!array_key_exists($id, $this->instances)) {
return $this->instances[$id]; $this->instances[$id] = $this->build($id);
} }
$this->instances[$id] = $this->build($id); if (!is_object($this->instances[$id])) {
throw ShouldNotHappen::fromMessage('Cannot resolve a non-object from container');
}
return $this->instances[$id]; return $this->instances[$id];
} }

View File

@ -50,6 +50,8 @@ final class ExceptionTrace
$property = new ReflectionProperty($t, 'serializableTrace'); $property = new ReflectionProperty($t, 'serializableTrace');
$property->setAccessible(true); $property->setAccessible(true);
/** @var array<array<string>> $trace */
$trace = $property->getValue($t); $trace = $property->getValue($t);
$cleanedTrace = []; $cleanedTrace = [];

View File

@ -31,7 +31,10 @@ final class HigherOrderCallables
*/ */
public function expect(mixed $value): Expectation public function expect(mixed $value): Expectation
{ {
return new Expectation($value instanceof Closure ? Reflection::bindCallableWithData($value) : $value); /** @var TValue $value */
$value = $value instanceof Closure ? Reflection::bindCallableWithData($value) : $value;
return new Expectation($value);
} }
/** /**