File: TypedMapTest.php

package info (click to toggle)
php-ramsey-collection 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: php: 3,239; xml: 31; makefile: 22
file content (101 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

declare(strict_types=1);

namespace Ramsey\Collection\Test\Map;

use Ramsey\Collection\Exception\InvalidArgumentException;
use Ramsey\Collection\Map\TypedMap;
use Ramsey\Collection\Map\TypedMapInterface;
use Ramsey\Collection\Test\TestCase;

use function array_keys;

/**
 * Tests for TypedMap
 */
class TypedMapTest extends TestCase
{
    public function testConstructor(): void
    {
        /** @var TypedMap<int, string> $typed */
        $typed = new TypedMap('int', 'string');

        $this->assertInstanceOf(TypedMapInterface::class, $typed);
        $this->assertSame('int', $typed->getKeyType());
        $this->assertSame('string', $typed->getValueType());

        /** @psalm-suppress TypeDoesNotContainType */
        $this->assertEmpty($typed);

        /** @psalm-suppress NoValue */
        $this->assertCount(0, $typed);
    }

    public function testConstructorWithValues(): void
    {
        $content = [0 => '0', 1 => '1', 2 => '4', 3 => '8', 4 => '16'];
        $keys = array_keys($content);

        /** @var TypedMap<int, string> $map */
        $map = new TypedMap('int', 'string', $content);

        $this->assertSame($keys, $map->keys());
        foreach ($keys as $key) {
            $this->assertSame($content[$key], $map->get($key));
        }
    }

    public function testConstructorAddWrongKeyType(): void
    {
        /** @var TypedMap<string, mixed> $map */
        $map = new TypedMap('string', 'mixed');

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Key must be of type string; key is');

        /**
         * @phpstan-ignore-next-line
         * @psalm-suppress InvalidArgument
         */
        $map[9] = 'foo';
    }

    public function testConstructorAllowAddEmptyKey(): void
    {
        /** @var TypedMap<string, mixed> $map */
        $map = new TypedMap('string', 'mixed');
        $map[''] = 'foo';
        $this->assertSame([''], $map->keys());
    }

    public function testConstructorAddWrongValueType(): void
    {
        /** @var TypedMap<string, string> $map */
        $map = new TypedMap('string', 'string');

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Value must be of type string; value is');

        /**
         * @phpstan-ignore-next-line
         * @psalm-suppress InvalidArgument
         */
        $map['foo'] = 9;
    }

    public function testNullKeyRaisesException(): void
    {
        /** @var TypedMap<string, mixed> $map */
        $map = new TypedMap('string', 'mixed');

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Key must be of type string; key is NULL');

        /**
         * @phpstan-ignore-next-line
         * @psalm-suppress NullArgument
         */
        $map[] = 'foo';
    }
}