Add helper to get last element of array

This commit is contained in:
jshayes
2024-10-13 09:47:54 -04:00
parent 7c639cdbbd
commit cd2eb3504b
6 changed files with 74 additions and 6 deletions

View File

@ -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)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 21 todos, 32 skipped, 1138 passed (2719 assertions)

View File

@ -0,0 +1,49 @@
<?php
use Pest\Support\Arr;
describe('last', function () {
it('should return false for an empty arary', function () {
expect(Arr::last([]))->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);
});
});

View File

@ -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();