|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Helpers\Array; |
| 13 | + |
| 14 | +use CodeIgniter\Test\CIUnitTestCase; |
| 15 | +use InvalidArgumentException; |
| 16 | + |
| 17 | +/** |
| 18 | + * @group Others |
| 19 | + * |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +final class ArrayHelperDotKeyExistsTest extends CIUnitTestCase |
| 23 | +{ |
| 24 | + private array $array = [ |
| 25 | + 'contacts' => [ |
| 26 | + 'friends' => [ |
| 27 | + ['name' => 'Fred Flinstone', 'age' => 20], |
| 28 | + ['age' => 21], // 'name' key does not exist |
| 29 | + ], |
| 30 | + ], |
| 31 | + ]; |
| 32 | + |
| 33 | + public function testDotKeyExists(): void |
| 34 | + { |
| 35 | + $this->assertFalse(ArrayHelper::dotKeyExists('', $this->array)); |
| 36 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts', $this->array)); |
| 37 | + $this->assertFalse(ArrayHelper::dotKeyExists('not', $this->array)); |
| 38 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends', $this->array)); |
| 39 | + $this->assertFalse(ArrayHelper::dotKeyExists('not.friends', $this->array)); |
| 40 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends.0.name', $this->array)); |
| 41 | + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.name', $this->array)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testDotKeyExistsWithEndingWildCard(): void |
| 45 | + { |
| 46 | + $this->expectException(InvalidArgumentException::class); |
| 47 | + $this->expectExceptionMessage('You must set key right after "*". Invalid index: "contacts.*"'); |
| 48 | + |
| 49 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*', $this->array)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testDotKeyExistsWithDoubleWildCard(): void |
| 53 | + { |
| 54 | + $this->expectException(InvalidArgumentException::class); |
| 55 | + $this->expectExceptionMessage('You must set key right after "*". Invalid index: "contacts.*.*.age"'); |
| 56 | + |
| 57 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.*.age', $this->array)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testDotKeyExistsWithWildCard(): void |
| 61 | + { |
| 62 | + $this->assertTrue(ArrayHelper::dotKeyExists('*.friends', $this->array)); |
| 63 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.friends.*.age', $this->array)); |
| 64 | + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.name', $this->array)); |
| 65 | + $this->assertTrue(ArrayHelper::dotKeyExists('*.friends.*.age', $this->array)); |
| 66 | + $this->assertFalse(ArrayHelper::dotKeyExists('*.friends.*.name', $this->array)); |
| 67 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.age', $this->array)); |
| 68 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.1.age', $this->array)); |
| 69 | + $this->assertTrue(ArrayHelper::dotKeyExists('contacts.*.0.name', $this->array)); |
| 70 | + $this->assertFalse(ArrayHelper::dotKeyExists('contacts.*.1.name', $this->array)); |
| 71 | + } |
| 72 | +} |
0 commit comments