-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add array_first() and array_last() in compat.php
#9556
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
932c7ca
feat: add array_first() and array_last() in compat.php
USERSATOSHI 6cd475c
refactor: address all feedbacks
USERSATOSHI c90619a
test: add missing ticket annotation for array_first() and array_last(…
USERSATOSHI aa81889
Add test for when the array pointer is not at the start
aaronjorbin 414fb76
test array_first, not array_key_first
aaronjorbin 14c6abe
Update tests/phpunit/tests/compat/arrayKeyFirst.php
aaronjorbin acdc9fb
Update tests/phpunit/tests/compat/arrayFirst.php
aaronjorbin 9fecb7b
Update variable name
aaronjorbin 3f8cad1
fix phpcs
aaronjorbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @group compat | ||
| * | ||
| * @covers ::array_first | ||
| */ | ||
| class Tests_Compat_arrayFirst extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * @ticket 63853 | ||
| * | ||
| * Test that array_first() is always available (either from PHP or WP). | ||
| */ | ||
| public function test_array_first_availability(): void { | ||
| $this->assertTrue( function_exists( 'array_first' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 63853 | ||
| * | ||
| * @dataProvider data_array_first | ||
| * | ||
| * @param mixed $expected The value extracted from the given array. | ||
| * @param array $arr The array to get the first value from. | ||
| */ | ||
| public function test_array_first( $expected, $arr ): void { | ||
| $this->assertSame( $expected, array_first( $arr ) ); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Data provider. | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public function data_array_first(): array { | ||
| $obj = new \stdClass(); | ||
| return array( | ||
| 'string values' => array( | ||
| 'expected' => 'a', | ||
| 'arr' => array( 'a', 'b', 'c' ), | ||
| ), | ||
| 'associative array' => array( | ||
| 'expected' => 10, | ||
| 'arr' => array( | ||
| 'foo' => 10, | ||
| 'bar' => 20, | ||
| ), | ||
| ), | ||
| 'empty array' => array( | ||
| 'expected' => null, | ||
| 'arr' => array(), | ||
| ), | ||
| 'single element array' => array( | ||
| 'expected' => 42, | ||
| 'arr' => array( 42 ), | ||
| ), | ||
| 'null values' => array( | ||
| 'expected' => null, | ||
| 'arr' => array( null, 'b', 'c' ), | ||
| ), | ||
| 'objects' => array( | ||
| 'expected' => $obj, | ||
| 'arr' => array( | ||
| $obj, | ||
| 1, | ||
| 2, | ||
| ), | ||
| ), | ||
| 'boolean values' => array( | ||
| 'expected' => false, | ||
| 'arr' => array( false, true, 1, 2, 3 ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that array_first() returns the pointer is not the first element. | ||
| * | ||
| * @ticket 63853 | ||
| */ | ||
| public function test_array_first_with_end_pointer() { | ||
| $arr = array( | ||
| 'key1' => 'val1', | ||
| 'key2' => 'val2', | ||
| ); | ||
| // change the pointer to the last element | ||
| end( $arr ); | ||
|
|
||
| $val = array_first( $arr ); | ||
| $this->assertSame( 'val2', current( $arr ) ); | ||
| $this->assertSame( 'val1', $val ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @group compat | ||
| * | ||
| * @covers ::array_last | ||
| */ | ||
| class Tests_Compat_arrayLast extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * @ticket 63853 | ||
| * | ||
| * Test that array_last() is always available (either from PHP or WP). | ||
| */ | ||
| public function test_array_last_availability(): void { | ||
| $this->assertTrue( function_exists( 'array_last' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 63853 | ||
| * | ||
| * @dataProvider data_array_last | ||
| * | ||
| * @param mixed $expected The expected last value. | ||
| * @param array $arr The array to get the last value from. | ||
| */ | ||
| public function test_array_last( $expected, $arr ): void { | ||
| $this->assertSame( $expected, array_last( $arr ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Data provider for array_last(). | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public function data_array_last(): array { | ||
| $obj = new \stdClass(); | ||
| return array( | ||
| 'string values' => array( | ||
| 'expected' => 'c', | ||
| 'arr' => array( 'a', 'b', 'c' ), | ||
| ), | ||
| 'associative array' => array( | ||
| 'expected' => 20, | ||
| 'arr' => array( | ||
| 'foo' => 10, | ||
| 'bar' => 20, | ||
| ), | ||
| ), | ||
| 'empty array' => array( | ||
| 'expected' => null, | ||
| 'arr' => array(), | ||
| ), | ||
| 'single element array' => array( | ||
| 'expected' => 42, | ||
| 'arr' => array( 42 ), | ||
| ), | ||
| 'null values' => array( | ||
| 'expected' => null, | ||
| 'arr' => array( 'a', 'b', null ), | ||
| ), | ||
| 'objects' => array( | ||
| 'expected' => $obj, | ||
| 'arr' => array( | ||
| 1, | ||
| 2, | ||
| $obj, | ||
| ), | ||
| ), | ||
| 'boolean values' => array( | ||
| 'expected' => false, | ||
| 'arr' => array( true, false ), | ||
| ), | ||
| 'null values in between' => array( | ||
| 'expected' => 'c', | ||
| 'arr' => array( 'a', null, 'b', 'c' ), | ||
| ), | ||
| 'empty string values' => array( | ||
| 'expected' => '', | ||
| 'arr' => array( 'a', 'b', '' ), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.