From cd2eb3504b7dce1d3de14e5af8c3c2c04d72be8f Mon Sep 17 00:00:00 2001 From: jshayes Date: Sun, 13 Oct 2024 09:47:54 -0400 Subject: [PATCH] Add helper to get last element of array --- src/PendingCalls/AfterEachCall.php | 3 +- src/PendingCalls/BeforeEachCall.php | 7 +++-- src/Support/Arr.php | 10 ++++++ tests/.snapshots/success.txt | 9 +++++- tests/Unit/Support/Arr.php | 49 +++++++++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/Support/Arr.php diff --git a/src/PendingCalls/AfterEachCall.php b/src/PendingCalls/AfterEachCall.php index 643ab924..2fdc6679 100644 --- a/src/PendingCalls/AfterEachCall.php +++ b/src/PendingCalls/AfterEachCall.php @@ -6,6 +6,7 @@ namespace Pest\PendingCalls; use Closure; use Pest\PendingCalls\Concerns\Describable; +use Pest\Support\Arr; use Pest\Support\Backtrace; use Pest\Support\ChainableClosure; use Pest\Support\HigherOrderMessageCollection; @@ -54,7 +55,7 @@ final class AfterEachCall $proxies = $this->proxies; $afterEachTestCase = ChainableClosure::boundWhen( - fn (): bool => $describing === [] || in_array(end($describing), $this->__describing, true), + fn (): bool => $describing === [] || in_array(Arr::last($describing), $this->__describing, true), ChainableClosure::bound(fn () => $proxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line )->bindTo($this, self::class); diff --git a/src/PendingCalls/BeforeEachCall.php b/src/PendingCalls/BeforeEachCall.php index 4582cbbc..3170d8ea 100644 --- a/src/PendingCalls/BeforeEachCall.php +++ b/src/PendingCalls/BeforeEachCall.php @@ -7,6 +7,7 @@ namespace Pest\PendingCalls; use Closure; use Pest\Exceptions\AfterBeforeTestFunction; use Pest\PendingCalls\Concerns\Describable; +use Pest\Support\Arr; use Pest\Support\Backtrace; use Pest\Support\ChainableClosure; use Pest\Support\HigherOrderMessageCollection; @@ -64,11 +65,11 @@ final class BeforeEachCall $beforeEachTestCall = function (TestCall $testCall) use ($describing): void { if ($this->describing !== []) { - if (end($describing) !== end($this->describing)) { + if (Arr::last($describing) !== Arr::last($this->describing)) { return; } - if (! in_array(end($describing), $testCall->describing, true)) { + if (! in_array(Arr::last($describing), $testCall->describing, true)) { return; } } @@ -77,7 +78,7 @@ final class BeforeEachCall }; $beforeEachTestCase = ChainableClosure::boundWhen( - fn (): bool => $describing === [] || in_array(end($describing), $this->__describing, true), + fn (): bool => $describing === [] || in_array(Arr::last($describing), $this->__describing, true), ChainableClosure::bound(fn () => $testCaseProxies->chain($this), $this->closure)->bindTo($this, self::class), // @phpstan-ignore-line )->bindTo($this, self::class); diff --git a/src/Support/Arr.php b/src/Support/Arr.php index aed6c5a7..daf7e3a4 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -81,4 +81,14 @@ final class Arr return $results; } + + /** + * Returns the value of the last element or false for empty array + * + * @param array $array + */ + public static function last(array $array): mixed + { + return end($array); + } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index fbb39a1b..acf5bedf 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1491,6 +1491,13 @@ ✓ preset invalid name ✓ preset → myFramework + PASS Tests\Unit\Support\Arr + ✓ last → it should return false for an empty arary + ✓ last → it should return the last element for an array with a single element + ✓ last → it should return the last element for an array without changing the internal pointer + ✓ last → it should return the last element for an associative array without changing the internal pointer + ✓ last → it should return the last element for an mixed key array without changing the internal pointer + PASS Tests\Unit\Support\Backtrace ✓ it gets file name from called file @@ -1642,4 +1649,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 21 todos, 32 skipped, 1133 passed (2702 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 21 todos, 32 skipped, 1138 passed (2719 assertions) \ No newline at end of file diff --git a/tests/Unit/Support/Arr.php b/tests/Unit/Support/Arr.php new file mode 100644 index 00000000..6dc6f3bb --- /dev/null +++ b/tests/Unit/Support/Arr.php @@ -0,0 +1,49 @@ +toBeFalse(); + }); + + it('should return the last element for an array with a single element', function () { + expect(Arr::last([1]))->toBe(1); + }); + + it('should return the last element for an array without changing the internal pointer', function () { + $array = [1, 2, 3]; + + expect(Arr::last($array))->toBe(3); + expect(current($array))->toBe(1); + + next($array); + expect(current($array))->toBe(2); + expect(Arr::last($array))->toBe(3); + expect(current($array))->toBe(2); + }); + + it('should return the last element for an associative array without changing the internal pointer', function () { + $array = ['first' => 1, 'second' => 2, 'third' => 3]; + + expect(Arr::last($array))->toBe(3); + expect(current($array))->toBe(1); + + next($array); + expect(current($array))->toBe(2); + expect(Arr::last($array))->toBe(3); + expect(current($array))->toBe(2); + }); + + it('should return the last element for an mixed key array without changing the internal pointer', function () { + $array = ['first' => 1, 2, 'third' => 3]; + + expect(Arr::last($array))->toBe(3); + expect(current($array))->toBe(1); + + next($array); + expect(current($array))->toBe(2); + expect(Arr::last($array))->toBe(3); + expect(current($array))->toBe(2); + }); +}); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index eebfbec3..ae7e86f2 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ $run = function () { test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 21 todos, 23 skipped, 1123 passed (2678 assertions)') + ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 21 todos, 23 skipped, 1128 passed (2695 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();