Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add tests for accessible and take method #51724

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class SupportArrTest extends TestCase
{
public function testAccessible()
public function testAccessible(): void
{
$this->assertTrue(Arr::accessible([]));
$this->assertTrue(Arr::accessible([1, 2]));
Expand All @@ -23,6 +23,11 @@ public function testAccessible()
$this->assertFalse(Arr::accessible('abc'));
$this->assertFalse(Arr::accessible(new stdClass));
$this->assertFalse(Arr::accessible((object) ['a' => 1, 'b' => 2]));
$this->assertFalse(Arr::accessible(123));
$this->assertFalse(Arr::accessible(12.34));
$this->assertFalse(Arr::accessible(true));
$this->assertFalse(Arr::accessible(new \DateTime));
$this->assertFalse(Arr::accessible(static fn () => null));
}

public function testAdd()
Expand Down Expand Up @@ -1387,17 +1392,24 @@ public function testPrependKeysWith()
], Arr::prependKeysWith($array, 'test.'));
}

public function testTake()
public function testTake(): void
{
$array = [1, 2, 3, 4, 5, 6];

$this->assertEquals([
1, 2, 3,
], Arr::take($array, 3));
// Test with a positive limit, should return the first 'limit' elements.
$this->assertEquals([1, 2, 3], Arr::take($array, 3));

$this->assertEquals([
4, 5, 6,
], Arr::take($array, -3));
// Test with a negative limit, should return the last 'abs(limit)' elements.
$this->assertEquals([4, 5, 6], Arr::take($array, -3));

// Test with zero limit, should return an empty array.
$this->assertEquals([], Arr::take($array, 0));

// Test with a limit greater than the array size, should return the entire array.
$this->assertEquals([1, 2, 3, 4, 5, 6], Arr::take($array, 10));

// Test with a negative limit greater than the array size, should return the entire array.
$this->assertEquals([1, 2, 3, 4, 5, 6], Arr::take($array, -10));
}

public function testSelect()
Expand Down