Files
DataEnrichmentKit/tests/Utility/ArrTest.php
2025-11-27 02:10:03 +04:00

323 lines
9.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Diffhead\PHP\DataEnrichmentKit\Tests\Utility;
use Diffhead\PHP\DataEnrichmentKit\Utility\Arr;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;
#[CoversClass(Arr::class)]
#[CoversMethod(Arr::class, 'get')]
#[CoversMethod(Arr::class, 'set')]
class ArrTest extends TestCase
{
public function testGetExistingItems(): void
{
$array = [
'user' => [
'id' => 1,
'name' => 'John Doe',
'address' => [
'city' => 'New York',
'zip' => '10001',
],
],
'posts' => [
['id' => 101, 'title' => 'First Post'],
['id' => 102, 'title' => 'Second Post'],
],
];
$this->assertEquals(1, Arr::get('user.id', $array));
$this->assertEquals('John Doe', Arr::get('user.name', $array));
$this->assertEquals('New York', Arr::get('user.address.city', $array));
$this->assertEquals('10001', Arr::get('user.address.zip', $array));
$this->assertEquals(101, Arr::get('posts.0.id', $array));
$this->assertEquals('First Post', Arr::get('posts.0.title', $array));
$this->assertEquals(102, Arr::get('posts.1.id', $array));
$this->assertEquals('Second Post', Arr::get('posts.1.title', $array));
}
public function testGetExistingItemsFromNestedArray(): void
{
$array = [
[
'orders' => [
['id' => 201, 'amount' => 150],
['id' => 202, 'amount' => 200],
]
],
[
'orders' => [
['id' => 203, 'amount' => 250],
['id' => 204, 'amount' => 300],
]
]
];
$this->assertEquals(201, Arr::get('0.orders.0.id', $array));
$this->assertEquals(200, Arr::get('0.orders.1.amount', $array));
$this->assertEquals(203, Arr::get('1.orders.0.id', $array));
$this->assertEquals(300, Arr::get('1.orders.1.amount', $array));
}
public function testGetMultipleExistingItems(): void
{
$array = [
'users' => [
['name' => 'John Doe'],
['name' => 'Jane Smith'],
['name' => 'Alice Johnson'],
],
'posts' => [
'categories' => [
[
'name' => 'Tech',
'posts' => [
['title' => 'Latest Tech Trends'],
['title' => 'AI Innovations']
]
],
[
'name' => 'Health',
'posts' => [
['title' => 'Wellness Tips'],
['title' => 'Nutrition Advice']
]
]
]
],
];
$this->assertSame(
[
'John Doe',
'Jane Smith',
'Alice Johnson'
],
Arr::get('users.*.name', $array)
);
$this->assertSame(
[
'Latest Tech Trends',
'AI Innovations',
'Wellness Tips',
'Nutrition Advice'
],
Arr::get('posts.categories.*.posts.*.title', $array)
);
}
public function testGetMultipleExistingItemsInsideNestedArray(): void
{
$array = [
[
'orders' => [
['id' => 201, 'amount' => 150],
['id' => 202, 'amount' => 200],
]
],
[
'orders' => [
['id' => 203, 'amount' => 250],
['id' => 204, 'amount' => 300],
]
]
];
$this->assertSame(
[
150,
200,
250,
300
],
Arr::get('*.orders.*.amount', $array)
);
}
public function testGetNonExistingItems(): void
{
$array = [
'user' => [
'id' => 1,
'name' => 'John Doe',
],
];
$this->assertNull(Arr::get('user.age', $array));
$this->assertEquals('Unknown', Arr::get('user.age', $array, 'Unknown'));
$this->assertNull(Arr::get('user.address.city', $array));
$this->assertEquals('N/A', Arr::get('user.address.city', $array, 'N/A'));
$this->assertNull(Arr::get('posts.0.id', $array));
}
public function testGetMultipleNonExistingItems(): void
{
$array = [
'users' => [
['name' => 'John Doe'],
['name' => 'Jane Smith'],
],
];
$this->assertSame([], Arr::get('users.*.age', $array));
$this->assertSame([], Arr::get('users.*.address.*.city', $array));
}
public function testSetValuesAtExistingPaths(): void
{
$origin = [
'user' => [
'id' => 1,
'name' => 'John Doe',
],
];
$updated = Arr::set('user.name', $origin, 'Jane Smith');
$updated = Arr::set('user.id', $updated, 2);
$this->assertEquals('Jane Smith', $updated['user']['name']);
$this->assertEquals(2, $updated['user']['id']);
}
public function testSetMultipleValuesAtExistingPaths(): void
{
$origin = [
'users' => [
['name' => 'John Doe'],
['name' => 'Jane Smith'],
],
];
$updated = Arr::set('users.*.age', $origin, 29);
$updated = Arr::set('users.*.source', $updated, 'facebook');
$this->assertEquals(29, $updated['users'][0]['age']);
$this->assertEquals(29, $updated['users'][1]['age']);
$this->assertEquals('facebook', $updated['users'][0]['source']);
$this->assertEquals('facebook', $updated['users'][1]['source']);
}
public function testSetValuesAtNonExistingPaths(): void
{
$origin = [
'user' => [
'id' => 1,
'name' => 'John Doe',
],
];
$updated = Arr::set('user.address.city', $origin, 'New York');
$updated = Arr::set('user.address.zip', $updated, '10001');
$updated = Arr::set('posts.0.id', $updated, 101);
$updated = Arr::set('posts.0.title', $updated, 'First Post');
$this->assertEquals('New York', $updated['user']['address']['city']);
$this->assertEquals('10001', $updated['user']['address']['zip']);
$this->assertEquals(101, $updated['posts'][0]['id']);
$this->assertEquals('First Post', $updated['posts'][0]['title']);
}
public function testSetMultipleValuesAtNonExistingPaths(): void
{
$origin = [
'users' => [],
];
$updated = Arr::set('users.*.name', $origin, 'John Doe');
$updated = Arr::set('users.*.age', $updated, 30);
$this->assertCount(1, $updated['users']);
$this->assertEquals('John Doe', $updated['users'][0]['name']);
$this->assertEquals(30, $updated['users'][0]['age']);
}
public function testHavingValueAtExistingPath(): void
{
$array = [
'user' => [
'id' => 1,
'name' => 'John Doe',
'address' => [
'city' => 'New York',
'zip' => '10001',
],
],
];
$this->assertTrue(Arr::has('user.id', $array));
$this->assertTrue(Arr::has('user.name', $array));
$this->assertTrue(Arr::has('user.address.city', $array));
$this->assertTrue(Arr::has('user.address.zip', $array));
}
public function testHavingValueAtExistingPathUsingWildcardNonStrictMode(): void
{
$array = [
'users' => [
['age' => 25],
['name' => 'Jane Smith'],
[
'name' => 'Alice Johnson',
'phones' => [
['type' => 'mobile', 'number' => '123-456-7890'],
['type' => 'home', 'number' => '098-765-4321'],
],
],
],
];
$this->assertTrue(Arr::has('users.*.name', $array));
$this->assertTrue(Arr::has('users.*.phones.*.number', $array));
}
public function testHavingValueAtExistingPathUsingWildcardStrictMode(): void
{
$array = [
'users' => [
['age' => 25],
['name' => 'Alice Brandon'],
],
'users_with_phones' => [
[
'name' => 'Bob Brown',
'phones' => [
['type' => 'mobile', 'number' => '555-555-5555'],
],
],
[
'name' => 'Carol White',
'phones' => [
['type' => 'home', 'number' => '444-444-4444'],
],
]
]
];
$this->assertFalse(Arr::has('users.*.name', $array, true));
$this->assertTrue(Arr::has('users_with_phones.*.phones.*.number', $array, true));
}
public function testHavingValueAtNonExistingPathUsingWildcard(): void
{
$array = [
'users' => [
['name' => 'John Doe'],
['name' => 'Jane Smith'],
],
];
$this->assertFalse(Arr::has('users.*.age', $array));
$this->assertFalse(Arr::has('users.*.address.city', $array));
$this->assertFalse(Arr::has('users.*.phones.*.type', $array));
}
}