From e64b6fe924df553006db2cdeeee7713f5859f7ac Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sun, 5 Dec 2021 14:21:11 +0000 Subject: [PATCH] refacto: pipes --- src/Expectation.php | 9 +- src/Functions.php | 4 +- .../Expectation.php} | 206 +++++++++--------- 3 files changed, 110 insertions(+), 109 deletions(-) rename src/{BaseExpectation.php => Mixins/Expectation.php} (77%) diff --git a/src/Expectation.php b/src/Expectation.php index 818a1606..c0e0e5ec 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pest; use BadMethodCallException; +use Carbon\Traits\Mixin; use Closure; use Pest\Concerns\Extendable; use Pest\Concerns\Pipeable; @@ -23,7 +24,7 @@ use PHPUnit\Framework\ExpectationFailedException; * @property Expectation $not Creates the opposite expectation. * @property Each $each Creates an expectation on each element on the traversable value. * - * @mixin BaseExpectation + * @mixin Mixins\Expectation */ final class Expectation { @@ -279,9 +280,9 @@ final class Expectation private function getExpectationClosure(string $name): Closure { - if (method_exists(BaseExpectation::class, $name)) { + if (method_exists(Mixins\Expectation::class, $name)) { //@phpstan-ignore-next-line - return Closure::fromCallable([new BaseExpectation($this->value), $name]); + return Closure::fromCallable([new Mixins\Expectation($this->value), $name]); } if (self::hasExtend($name)) { @@ -317,7 +318,7 @@ final class Expectation public static function hasMethod(string $name): bool { return method_exists(self::class, $name) - || method_exists(BaseExpectation::class, $name) + || method_exists(Mixins\Expectation::class, $name) || self::hasExtend($name); } } diff --git a/src/Functions.php b/src/Functions.php index 00eed63d..4d07a05f 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -19,11 +19,11 @@ if (!function_exists('expect')) { * * @template TValue * - * @param TValue $value the Value + * @param TValue $value * * @return Expectation */ - function expect($value = null): Expectation + function expect(mixed $value = null): Expectation { return new Expectation($value); } diff --git a/src/BaseExpectation.php b/src/Mixins/Expectation.php similarity index 77% rename from src/BaseExpectation.php rename to src/Mixins/Expectation.php index 4787b8fe..3fbd2110 100644 --- a/src/BaseExpectation.php +++ b/src/Mixins/Expectation.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pest; +namespace Pest\Mixins; use BadMethodCallException; use Closure; @@ -23,9 +23,9 @@ use Throwable; * * @template TValue * - * @mixin Expectation + * @mixin \Pest\Expectation */ -final class BaseExpectation +final class Expectation { /** * The exporter instance, if any. @@ -50,9 +50,9 @@ final class BaseExpectation * value. Used on objects, it asserts that two * variables reference the same object. * - * @return BaseExpectation + * @return Expectation */ - public function toBe(mixed $expected): BaseExpectation + public function toBe(mixed $expected): Expectation { Assert::assertSame($expected, $this->value); @@ -62,9 +62,9 @@ final class BaseExpectation /** * Asserts that the value is empty. * - * @return BaseExpectation + * @return Expectation */ - public function toBeEmpty(): BaseExpectation + public function toBeEmpty(): Expectation { Assert::assertEmpty($this->value); @@ -74,9 +74,9 @@ final class BaseExpectation /** * Asserts that the value is true. * - * @return BaseExpectation + * @return Expectation */ - public function toBeTrue(): BaseExpectation + public function toBeTrue(): Expectation { Assert::assertTrue($this->value); @@ -86,9 +86,9 @@ final class BaseExpectation /** * Asserts that the value is truthy. * - * @return BaseExpectation + * @return Expectation */ - public function toBeTruthy(): BaseExpectation + public function toBeTruthy(): Expectation { Assert::assertTrue((bool) $this->value); @@ -98,9 +98,9 @@ final class BaseExpectation /** * Asserts that the value is false. * - * @return BaseExpectation + * @return Expectation */ - public function toBeFalse(): BaseExpectation + public function toBeFalse(): Expectation { Assert::assertFalse($this->value); @@ -110,9 +110,9 @@ final class BaseExpectation /** * Asserts that the value is falsy. * - * @return BaseExpectation + * @return Expectation */ - public function toBeFalsy(): BaseExpectation + public function toBeFalsy(): Expectation { Assert::assertFalse((bool) $this->value); @@ -122,9 +122,9 @@ final class BaseExpectation /** * Asserts that the value is greater than $expected. * - * @return BaseExpectation + * @return Expectation */ - public function toBeGreaterThan(int|float $expected): BaseExpectation + public function toBeGreaterThan(int|float $expected): Expectation { Assert::assertGreaterThan($expected, $this->value); @@ -134,9 +134,9 @@ final class BaseExpectation /** * Asserts that the value is greater than or equal to $expected. * - * @return BaseExpectation + * @return Expectation */ - public function toBeGreaterThanOrEqual(int|float $expected): BaseExpectation + public function toBeGreaterThanOrEqual(int|float $expected): Expectation { Assert::assertGreaterThanOrEqual($expected, $this->value); @@ -146,9 +146,9 @@ final class BaseExpectation /** * Asserts that the value is less than or equal to $expected. * - * @return BaseExpectation + * @return Expectation */ - public function toBeLessThan(int|float $expected): BaseExpectation + public function toBeLessThan(int|float $expected): Expectation { Assert::assertLessThan($expected, $this->value); @@ -158,9 +158,9 @@ final class BaseExpectation /** * Asserts that the value is less than $expected. * - * @return BaseExpectation + * @return Expectation */ - public function toBeLessThanOrEqual(int|float $expected): BaseExpectation + public function toBeLessThanOrEqual(int|float $expected): Expectation { Assert::assertLessThanOrEqual($expected, $this->value); @@ -170,9 +170,9 @@ final class BaseExpectation /** * Asserts that $needle is an element of the value. * - * @return BaseExpectation + * @return Expectation */ - public function toContain(mixed ...$needles): BaseExpectation + public function toContain(mixed ...$needles): Expectation { foreach ($needles as $needle) { if (is_string($this->value)) { @@ -194,9 +194,9 @@ final class BaseExpectation * * @param non-empty-string $expected * - *@return BaseExpectation + *@return Expectation */ - public function toStartWith(string $expected): BaseExpectation + public function toStartWith(string $expected): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -212,9 +212,9 @@ final class BaseExpectation * * @param non-empty-string $expected * - *@return BaseExpectation + *@return Expectation */ - public function toEndWith(string $expected): BaseExpectation + public function toEndWith(string $expected): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -228,9 +228,9 @@ final class BaseExpectation /** * Asserts that $number matches value's Length. * - * @return BaseExpectation + * @return Expectation */ - public function toHaveLength(int $number): BaseExpectation + public function toHaveLength(int $number): Expectation { if (is_string($this->value)) { Assert::assertEquals($number, mb_strlen($this->value)); @@ -260,9 +260,9 @@ final class BaseExpectation /** * Asserts that $count matches the number of elements of the value. * - * @return BaseExpectation + * @return Expectation */ - public function toHaveCount(int $count): BaseExpectation + public function toHaveCount(int $count): Expectation { if (!is_countable($this->value) && !is_iterable($this->value)) { InvalidExpectationValue::expected('string'); @@ -276,9 +276,9 @@ final class BaseExpectation /** * Asserts that the value contains the property $name. * - * @return BaseExpectation + * @return Expectation */ - public function toHaveProperty(string $name, mixed $value = null): BaseExpectation + public function toHaveProperty(string $name, mixed $value = null): Expectation { $this->toBeObject(); @@ -298,9 +298,9 @@ final class BaseExpectation * * @param iterable $names * - *@return BaseExpectation + *@return Expectation */ - public function toHaveProperties(iterable $names): BaseExpectation + public function toHaveProperties(iterable $names): Expectation { foreach ($names as $name) { $this->toHaveProperty($name); @@ -312,9 +312,9 @@ final class BaseExpectation /** * Asserts that two variables have the same value. * - * @return BaseExpectation + * @return Expectation */ - public function toEqual(mixed $expected): BaseExpectation + public function toEqual(mixed $expected): Expectation { Assert::assertEquals($expected, $this->value); @@ -330,9 +330,9 @@ final class BaseExpectation * are objects, each object is converted to an array containing all * private, protected and public attributes. * - * @return BaseExpectation + * @return Expectation */ - public function toEqualCanonicalizing(mixed $expected): BaseExpectation + public function toEqualCanonicalizing(mixed $expected): Expectation { Assert::assertEqualsCanonicalizing($expected, $this->value); @@ -343,9 +343,9 @@ final class BaseExpectation * Asserts that the absolute difference between the value and $expected * is lower than $delta. * - * @return BaseExpectation + * @return Expectation */ - public function toEqualWithDelta(mixed $expected, float $delta): BaseExpectation + public function toEqualWithDelta(mixed $expected, float $delta): Expectation { Assert::assertEqualsWithDelta($expected, $this->value, $delta); @@ -357,9 +357,9 @@ final class BaseExpectation * * @param iterable $values * - * @return BaseExpectation + * @return Expectation */ - public function toBeIn(iterable $values): BaseExpectation + public function toBeIn(iterable $values): Expectation { Assert::assertContains($this->value, $values); @@ -369,9 +369,9 @@ final class BaseExpectation /** * Asserts that the value is infinite. * - * @return BaseExpectation + * @return Expectation */ - public function toBeInfinite(): BaseExpectation + public function toBeInfinite(): Expectation { Assert::assertInfinite($this->value); @@ -383,9 +383,9 @@ final class BaseExpectation * * @param class-string $class * - * @return BaseExpectation + * @return Expectation */ - public function toBeInstanceOf(string $class): BaseExpectation + public function toBeInstanceOf(string $class): Expectation { Assert::assertInstanceOf($class, $this->value); @@ -395,9 +395,9 @@ final class BaseExpectation /** * Asserts that the value is an array. * - * @return BaseExpectation + * @return Expectation */ - public function toBeArray(): BaseExpectation + public function toBeArray(): Expectation { Assert::assertIsArray($this->value); @@ -407,9 +407,9 @@ final class BaseExpectation /** * Asserts that the value is of type bool. * - * @return BaseExpectation + * @return Expectation */ - public function toBeBool(): BaseExpectation + public function toBeBool(): Expectation { Assert::assertIsBool($this->value); @@ -419,9 +419,9 @@ final class BaseExpectation /** * Asserts that the value is of type callable. * - * @return BaseExpectation + * @return Expectation */ - public function toBeCallable(): BaseExpectation + public function toBeCallable(): Expectation { Assert::assertIsCallable($this->value); @@ -431,9 +431,9 @@ final class BaseExpectation /** * Asserts that the value is of type float. * - * @return BaseExpectation + * @return Expectation */ - public function toBeFloat(): BaseExpectation + public function toBeFloat(): Expectation { Assert::assertIsFloat($this->value); @@ -443,9 +443,9 @@ final class BaseExpectation /** * Asserts that the value is of type int. * - * @return BaseExpectation + * @return Expectation */ - public function toBeInt(): BaseExpectation + public function toBeInt(): Expectation { Assert::assertIsInt($this->value); @@ -455,9 +455,9 @@ final class BaseExpectation /** * Asserts that the value is of type iterable. * - * @return BaseExpectation + * @return Expectation */ - public function toBeIterable(): BaseExpectation + public function toBeIterable(): Expectation { Assert::assertIsIterable($this->value); @@ -467,9 +467,9 @@ final class BaseExpectation /** * Asserts that the value is of type numeric. * - * @return BaseExpectation + * @return Expectation */ - public function toBeNumeric(): BaseExpectation + public function toBeNumeric(): Expectation { Assert::assertIsNumeric($this->value); @@ -479,9 +479,9 @@ final class BaseExpectation /** * Asserts that the value is of type object. * - * @return BaseExpectation + * @return Expectation */ - public function toBeObject(): BaseExpectation + public function toBeObject(): Expectation { Assert::assertIsObject($this->value); @@ -491,9 +491,9 @@ final class BaseExpectation /** * Asserts that the value is of type resource. * - * @return BaseExpectation + * @return Expectation */ - public function toBeResource(): BaseExpectation + public function toBeResource(): Expectation { Assert::assertIsResource($this->value); @@ -503,9 +503,9 @@ final class BaseExpectation /** * Asserts that the value is of type scalar. * - * @return BaseExpectation + * @return Expectation */ - public function toBeScalar(): BaseExpectation + public function toBeScalar(): Expectation { Assert::assertIsScalar($this->value); @@ -515,9 +515,9 @@ final class BaseExpectation /** * Asserts that the value is of type string. * - * @return BaseExpectation + * @return Expectation */ - public function toBeString(): BaseExpectation + public function toBeString(): Expectation { Assert::assertIsString($this->value); @@ -527,9 +527,9 @@ final class BaseExpectation /** * Asserts that the value is a JSON string. * - * @return BaseExpectation + * @return Expectation */ - public function toBeJson(): BaseExpectation + public function toBeJson(): Expectation { Assert::assertIsString($this->value); @@ -542,9 +542,9 @@ final class BaseExpectation /** * Asserts that the value is NAN. * - * @return BaseExpectation + * @return Expectation */ - public function toBeNan(): BaseExpectation + public function toBeNan(): Expectation { Assert::assertNan($this->value); @@ -554,9 +554,9 @@ final class BaseExpectation /** * Asserts that the value is null. * - * @return BaseExpectation + * @return Expectation */ - public function toBeNull(): BaseExpectation + public function toBeNull(): Expectation { Assert::assertNull($this->value); @@ -566,9 +566,9 @@ final class BaseExpectation /** * Asserts that the value array has the provided $key. * - * @return BaseExpectation + * @return Expectation */ - public function toHaveKey(string|int $key, mixed $value = null): BaseExpectation + public function toHaveKey(string|int $key, mixed $value = null): Expectation { if (is_object($this->value) && method_exists($this->value, 'toArray')) { $array = $this->value->toArray(); @@ -596,9 +596,9 @@ final class BaseExpectation * * @param array $keys * - * @return BaseExpectation + * @return Expectation */ - public function toHaveKeys(array $keys): BaseExpectation + public function toHaveKeys(array $keys): Expectation { foreach ($keys as $key) { $this->toHaveKey($key); @@ -610,9 +610,9 @@ final class BaseExpectation /** * Asserts that the value is a directory. * - * @return BaseExpectation + * @return Expectation */ - public function toBeDirectory(): BaseExpectation + public function toBeDirectory(): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -626,9 +626,9 @@ final class BaseExpectation /** * Asserts that the value is a directory and is readable. * - * @return BaseExpectation + * @return Expectation */ - public function toBeReadableDirectory(): BaseExpectation + public function toBeReadableDirectory(): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -642,9 +642,9 @@ final class BaseExpectation /** * Asserts that the value is a directory and is writable. * - * @return BaseExpectation + * @return Expectation */ - public function toBeWritableDirectory(): BaseExpectation + public function toBeWritableDirectory(): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -658,9 +658,9 @@ final class BaseExpectation /** * Asserts that the value is a file. * - * @return BaseExpectation + * @return Expectation */ - public function toBeFile(): BaseExpectation + public function toBeFile(): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -674,9 +674,9 @@ final class BaseExpectation /** * Asserts that the value is a file and is readable. * - * @return BaseExpectation + * @return Expectation */ - public function toBeReadableFile(): BaseExpectation + public function toBeReadableFile(): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -690,9 +690,9 @@ final class BaseExpectation /** * Asserts that the value is a file and is writable. * - * @return BaseExpectation + * @return Expectation */ - public function toBeWritableFile(): BaseExpectation + public function toBeWritableFile(): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -707,9 +707,9 @@ final class BaseExpectation * * @param iterable $array * - * @return BaseExpectation + * @return Expectation */ - public function toMatchArray(iterable|object $array): BaseExpectation + public function toMatchArray(iterable|object $array): Expectation { if (is_object($this->value) && method_exists($this->value, 'toArray')) { $valueAsArray = $this->value->toArray(); @@ -740,9 +740,9 @@ final class BaseExpectation * * @param iterable|object $object * - * @return BaseExpectation + * @return Expectation */ - public function toMatchObject(iterable|object $object): BaseExpectation + public function toMatchObject(iterable|object $object): Expectation { foreach ((array) $object as $property => $value) { if (!is_object($this->value) && !is_string($this->value)) { @@ -770,9 +770,9 @@ final class BaseExpectation /** * Asserts that the value matches a regular expression. * - * @return BaseExpectation + * @return Expectation */ - public function toMatch(string $expression): BaseExpectation + public function toMatch(string $expression): Expectation { if (!is_string($this->value)) { InvalidExpectationValue::expected('string'); @@ -785,9 +785,9 @@ final class BaseExpectation /** * Asserts that the value matches a constraint. * - * @return BaseExpectation + * @return Expectation */ - public function toMatchConstraint(Constraint $constraint): BaseExpectation + public function toMatchConstraint(Constraint $constraint): Expectation { Assert::assertThat($this->value, $constraint); @@ -799,9 +799,9 @@ final class BaseExpectation * * @param (Closure(Throwable): mixed)|string $exception * - * @return BaseExpectation + * @return Expectation */ - public function toThrow(callable|string $exception, string $exceptionMessage = null): BaseExpectation + public function toThrow(callable|string $exception, string $exceptionMessage = null): Expectation { $callback = NullClosure::create();