iflrandevu/tests/Unit/Helper/ArrayHelperTest.php

56 lines
1.3 KiB
PHP
Raw Normal View History

2023-03-23 09:49:11 +03:00
<?php
namespace Unit\Helper;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../../application/helpers/array_helper.php';
2023-03-23 10:05:14 +03:00
class ArrayHelperTest extends TestCase {
public function testIsAssocReturnsTrueOnAssociativeArray()
2023-03-23 09:49:11 +03:00
{
$this->assertTrue(is_assoc(['test' => 'value']));
}
2023-03-23 10:05:14 +03:00
public function testIsAssocReturnsFalseOnIndexedArray()
2023-03-23 09:49:11 +03:00
{
$this->assertFalse(is_assoc(['one', 'two', 'three']));
}
2023-03-23 10:05:14 +03:00
public function testIsAssocReturnsTrueOnMixedArray()
2023-03-23 09:49:11 +03:00
{
$this->assertTrue(is_assoc(['one', 'two', 'three' => 'value']));
}
2023-03-23 10:05:14 +03:00
public function testArrayFindReturnsCorrectElement()
2023-03-23 09:49:11 +03:00
{
$arr = [
[
'id' => 1
],
[
'id' => 2
],
[
'id' => 3
],
];
$this->assertSame($arr[0], array_find($arr, fn($element) => $element['id'] === 1));
}
2023-03-23 10:05:14 +03:00
public function testArrayFieldsReturnsStrippedArray()
2023-03-23 09:49:11 +03:00
{
$arr = [
'name' => 'John',
'email' => 'john@example.org',
];
$stripped = array_fields($arr, ['name']);
$this->assertArrayHasKey('name', $stripped);
2023-03-23 10:05:14 +03:00
2023-03-23 09:49:11 +03:00
$this->assertArrayNotHasKey('email', $stripped);
}
}