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

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

View File

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

View File

@ -81,4 +81,14 @@ final class Arr
return $results;
}
/**
* Returns the value of the last element or false for empty array
*
* @param array<array-key, mixed> $array
*/
public static function last(array $array): mixed
{
return end($array);
}
}