diff --git a/src/Concerns/Extendable.php b/src/Concerns/Extendable.php index 8713de72..1a8dc379 100644 --- a/src/Concerns/Extendable.php +++ b/src/Concerns/Extendable.php @@ -19,7 +19,7 @@ trait Extendable */ private static array $extends = []; - /** @var array> */ + /** @var array> */ private static array $pipes = []; /** @@ -50,7 +50,7 @@ trait Extendable } self::pipe($name, function ($next, ...$arguments) use ($handler, $filter) { - /** @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ if ($filter($this->value)) { //@phpstan-ignore-next-line $handler->bindTo($this, get_class($this))(...$arguments); @@ -75,7 +75,7 @@ trait Extendable */ private function pipes(string $name, object $context, string $scope): array { - return array_map(fn(Closure $pipe) => $pipe->bindTo($context, $scope), self::$pipes[$name] ?? []); + return array_map(fn (Closure $pipe) => $pipe->bindTo($context, $scope), self::$pipes[$name] ?? []); } /** diff --git a/tests/Features/Expect/pipe.php b/tests/Features/Expect/pipe.php deleted file mode 100644 index e8574acf..00000000 --- a/tests/Features/Expect/pipe.php +++ /dev/null @@ -1,240 +0,0 @@ -value = $value; - } -} - -class Character -{ - public $value; - - public function __construct($value) - { - $this->value = $value; - } -} - -class Symbol -{ - public $value; - - public function __construct($value) - { - $this->value = $value; - } -} - -class State -{ - public $runCount = []; - public $appliedCount = []; - - public function __construct() - { - $this->reset(); - } - - public function reset(): void - { - $this->runCount = [ - 'character' => 0, - 'number' => 0, - 'wildcard' => 0, - 'symbol' => 0, - ]; - - $this->appliedCount = [ - 'character' => 0, - 'number' => 0, - 'wildcard' => 0, - 'symbol' => 0, - ]; - } -} - -$state = new State(); - -/* - * Overrides toBe to assert two Characters are the same - */ -expect()->pipe('toBe', function ($next, $expected) use ($state) { - $state->runCount['character']++; - - if ($this->value instanceof Character) { - $state->appliedCount['character']++; - assertInstanceOf(Character::class, $expected); - assertEquals($this->value->value, $expected->value); - - return; - } - - $next(); -}); - -/* - * Overrides toBe to assert two Numbers are the same - */ -expect()->intercept('toBe', Number::class, function ($expected) use ($state) { - $state->runCount['number']++; - $state->appliedCount['number']++; - assertEquals($this->value->value, $expected->value); -}); - -/* - * Overrides toBe to assert all integers are allowed if value is an '*' - */ -expect()->intercept('toBe', function ($value) { - return $value === '*'; -}, function ($expected) use ($state) { - $state->runCount['wildcard']++; - $state->appliedCount['wildcard']++; - assertIsNumeric($expected); -}); - -/* - * Overrides toBe to assert two Symbols are the same - */ -expect()->pipe('toBe', function ($next, $expected) use ($state) { - $state->runCount['symbol']++; - - if ($this->value instanceof Symbol) { - $state->appliedCount['symbol']++; - assertInstanceOf(Symbol::class, $expected); - assertEquals($this->value->value, $expected->value); - - return; - } - - $next(); -}); - -/* - * Overrides toBe check strings ignoring case - */ -expect()->intercept('toBe', function ($value) { - return is_string($value); -}, function ($expected, $ignoreCase = false) { - if ($ignoreCase) { - assertEqualsIgnoringCase($expected, $this->value); - } else { - assertSame($expected, $this->value); - } -}); - -test('pipe is applied and can stop pipeline', function () use ($state) { - $letter = new Character('A'); - - $state->reset(); - - expect($letter)->toBe(new Character('A')) - ->and($state) - ->runCount->toMatchArray([ - 'character' => 1, - 'number' => 0, - 'wildcard' => 0, - 'symbol' => 0, - ]) - ->appliedCount->toMatchArray([ - 'character' => 1, - 'number' => 0, - 'wildcard' => 0, - 'symbol' => 0, - ]); -}); - -test('interceptor works with negated expectation', function () { - $letter = new Number(1); - - expect($letter)->not->toBe(new Character('B')); -}); - -test('pipe works with negated expectation', function () { - $letter = new Character('A'); - - expect($letter)->not->toBe(new Character('B')); -}); - -test('pipe is run and can let the pipeline keep going', function () use ($state) { - $state->reset(); - - expect(3)->toBe(3) - ->and($state) - ->runCount->toMatchArray([ - 'character' => 1, - 'number' => 0, - 'wildcard' => 0, - 'symbol' => 1, - ]) - ->appliedCount->toMatchArray([ - 'character' => 0, - 'number' => 0, - 'wildcard' => 0, - 'symbol' => 0, - ]); -}); - -test('intercept is applied', function () use ($state) { - $number = new Number(1); - - $state->reset(); - - expect($number)->toBe(new Number(1)) - ->and($state) - ->runCount->toHaveKey('number', 1) - ->appliedCount->toHaveKey('number', 1); -}); - -test('intercept stops the pipeline', function () use ($state) { - $number = new Number(1); - - $state->reset(); - - expect($number)->toBe(new Number(1)) - ->and($state) - ->runCount->toMatchArray([ - 'character' => 1, - 'number' => 1, - 'wildcard' => 0, - 'symbol' => 0, - ]) - ->appliedCount->toMatchArray([ - 'character' => 0, - 'number' => 1, - 'wildcard' => 0, - 'symbol' => 0, - ]); -}); - -test('interception is called only when filter is met', function () use ($state) { - $state->reset(); - - expect(1)->toBe(1) - ->and($state) - ->runCount->toHaveKey('number', 0) - ->appliedCount->toHaveKey('number', 0); -}); - -test('intercept can be filtered with a closure', function () use ($state) { - $state->reset(); - - expect('*')->toBe(1) - ->and($state) - ->runCount->toHaveKey('wildcard', 1) - ->appliedCount->toHaveKey('wildcard', 1); -}); - -test('intercept can add new parameters to the expectation', function () { - expect('Foo')->toBe('foo', true); -}); diff --git a/tests/Features/Expect/pipes.php b/tests/Features/Expect/pipes.php index c98d25fe..5e311e07 100644 --- a/tests/Features/Expect/pipes.php +++ b/tests/Features/Expect/pipes.php @@ -1,5 +1,7 @@ reset(); expect(3)->toBe(3)