8000 [12.x] Typed getters for Arr helper by tibbsa · Pull Request #55567 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Typed getters for Arr helper #55567

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

Merged
merged 6 commits into from
Apr 29, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests for Arr typed array value getters
  • Loading branch information
tibbsa committed Apr 26, 2025
commit 9f59480436f86d162d719d2b2bdab8ac93d2e7d6
100 changes: 100 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,106 @@ public function testGet()
$this->assertSame('bar', Arr::get(['' => ['' => 'bar']], '.'));
}

public function testItGetsAString()
{
$test_array = ['string' => 'foo bar','integer' => 1234];

// Test string values are returned as strings
$this->assertSame(
'foo bar', Arr::string($test_array, 'string')
);

// Test that default string values are returned for missing keys
$this->assertSame(
'default', Arr::string($test_array, 'missing_key', 'default')
);

// Test that an exception is raised if the value is not a string
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Array value for key \[integer\] must be a string, (.*) given.#');
Arr::string($test_array, 'integer');
}

public function testItGetsAnInteger()
{
$test_array = ['string' => 'foo bar', 'integer' => 1234];

// Test integer values are returned as integers
$this->assertSame(
1234, Arr::integer($test_array, 'integer')
);

// Test that default integer values are returned for missing keys
$this->assertSame(
999, Arr::integer($test_array, 'missing_key', 999)
);

// Test that an exception is raised if the value is not an integer
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be an integer, (.*) given.#');
Arr::integer($test_array, 'string');
}

public function testItGetsAFloat()
{
$test_array = ['string' => 'foo bar', 'float' => 12.34];

// Test float values are returned as floats
$this->assertSame(
12.34, Arr::float($test_array, 'float')
);

// Test that default float values are returned for missing keys
$this->assertSame(
56.78, Arr::float($test_array, 'missing_key', 56.78)
);

// Test that an exception is raised if the value is not a float
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be a float, (.*) given.#');
Arr::float($test_array, 'string');
}

public function testItGetsABoolean()
{
$test_array = ['string' => 'foo bar', 'boolean' => true];

// Test boolean values are returned as booleans
$this->assertSame(
true, Arr::boolean($test_array, 'boolean')
);

// Test that default boolean values are returned for missing keys
$this->assertSame(
true, Arr::boolean($test_array, 'missing_key', true)
);

// Test that an exception is raised if the value is not a boolean
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be a boolean, (.*) given.#');
Arr::boolean($test_array, 'string');
}

public function testItGetsAnArray()
{
$test_array = ['string' => 'foo bar', 'array' => ['foo', 'bar']];

// Test array values are returned as arrays
$this->assertSame(
['foo', 'bar'], Arr::array($test_array, 'array')
);

// Test that default array values are returned for missing keys
$this->assertSame(
[1, 'two'], Arr::array($test_array, 'missing_key', [1, 'two'])
);

// Test that an exception is raised if the value is not an array
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be an array, (.*) given.#');
Arr::array($test_array, 'string');
}

public function testHas()
{
$array = ['products.desk' => ['price' => 100]];
Expand Down
0