|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of ILIAS, a powerful learning management system |
| 5 | + * published by ILIAS open source e-Learning e.V. |
| 6 | + * |
| 7 | + * ILIAS is licensed with the GPL-3.0, |
| 8 | + * see https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | + * You should have received a copy of said license along with the |
| 10 | + * source code, too. |
| 11 | + * |
| 12 | + * If this is not the case or you just want to try ILIAS, you'll find |
| 13 | + * us at: |
| 14 | + * https://www.ilias.de |
| 15 | + * https://github.com/ILIAS-eLearning |
| 16 | + * |
| 17 | + *********************************************************************/ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace ILIAS\Tests\KeyValueStorage\Internal; |
| 22 | + |
| 23 | +use ILIAS\KeyValueStorage\Internal\StorageNamespace; |
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | + |
| 27 | +class StorageNamespaceTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @param list<string> $segments |
| 31 | + */ |
| 32 | + #[DataProvider('validNamespaces')] |
| 33 | + public function testValidNamespacesAreAccepted(array $segments, string $value): void |
| 34 | + { |
| 35 | + $namespace = new StorageNamespace($segments); |
| 36 | + |
| 37 | + $this->assertSame($value, $namespace->value()); |
| 38 | + $this->assertSame($value, (string) $namespace); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return array<string, array{list<string>, string}> |
| 43 | + */ |
| 44 | + public static function validNamespaces(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + 'single segment' => [['ui'], 'ui'], |
| 48 | + 'two segments' => [['ui', 'storage'], 'ui.storage'], |
| 49 | + 'many segments' => [['my_component', 'view_state', 'table'], 'my_component.view_state.table'], |
| 50 | + 'uppercase' => [['Ui', 'Storage'], 'Ui.Storage'], |
| 51 | + 'leading digit' => [['1ui'], '1ui'], |
| 52 | + 'hyphen' => [['ui-storage'], 'ui-storage'], |
| 53 | + 'backslash' => [['ILIAS\\UI'], 'ILIAS\\UI'], |
| 54 | + 'maximum length' => [[str_repeat('a', StorageNamespace::MAX_LENGTH)], str_repeat('a', StorageNamespace::MAX_LENGTH)], |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param list<mixed> $segments |
| 60 | + */ |
| 61 | + #[DataProvider('invalidNamespaces')] |
| 62 | + public function testInvalidNamespacesAreRejected(array $segments): void |
| 63 | + { |
| 64 | + $this->expectException(\InvalidArgumentException::class); |
| 65 | + |
| 66 | + new StorageNamespace($segments); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return array<string, array{list<mixed>}> |
| 71 | + */ |
| 72 | + public static function invalidNamespaces(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'empty list' => [[]], |
| 76 | + 'empty segment' => [['ui', '']], |
| 77 | + 'dot in segment' => [['ui.storage']], |
| 78 | + 'colon in segment' => [['ui:storage']], |
| 79 | + 'control character' => [["ui\nstorage"]], |
| 80 | + 'non-string segment' => [['ui', 1]], |
| 81 | + 'too long' => [[str_repeat('a', StorageNamespace::MAX_LENGTH + 1)]], |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments