File: ArrayTest.php

package info (click to toggle)
php-phpdocumentor-type-resolver 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,684 kB
  • sloc: php: 27,998; xml: 102; makefile: 47
file content (92 lines) | stat: -rw-r--r-- 3,230 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
<?php

declare(strict_types=1);

/**
 * This file is part of phpDocumentor.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @link      http://phpdoc.org
 */

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\PseudoTypes\ArrayShape;
use phpDocumentor\Reflection\PseudoTypes\ArrayShapeItem;
use phpDocumentor\Reflection\PseudoTypes\ObjectShape;
use phpDocumentor\Reflection\PseudoTypes\ObjectShapeItem;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class ArrayTest extends TestCase
{
    public function testCreateWithoutParams(): void
    {
        $type = new Array_();

        $this->assertNull($type->getOriginalKeyType());
        $this->assertNull($type->getOriginalValueType());
        $this->assertEquals(new Compound([new String_(), new Integer()]), $type->getKeyType());
        $this->assertEquals(new Mixed_(), $type->getValueType());
    }

    public function testCreateWithParams(): void
    {
        $valueType = new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar'));
        $keyType = new Compound(
            [
                new String_(),
                new Integer(),
            ]
        );

        $type = new Array_($valueType, $keyType);

        $this->assertSame($keyType, $type->getOriginalKeyType());
        $this->assertSame($valueType, $type->getOriginalValueType());
        $this->assertSame($keyType, $type->getKeyType());
        $this->assertSame($valueType, $type->getValueType());
    }

    #[DataProvider('provideToStringData')]
    public function testToString(Array_ $array, string $expectedString): void
    {
        $this->assertSame($expectedString, (string) $array);
    }

    /**
     * @return array<string, array{Array_, string}>
     */
    public static function provideToStringData(): array
    {
        return [
            'simple array' => [new Array_(), 'array'],
            'array of mixed' => [new Array_(new Mixed_()), 'mixed[]'],
            'array of single type' => [new Array_(new String_()), 'string[]'],
            'multidimensional array' => [new Array_(new Array_(new String_())), 'string[][]'],
            'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), 'array<int|string>'],
            'array with key type' => [new Array_(new String_(), new Integer()), 'array<int, string>'],
            'array of array shapes' => [
                new Array_(
                    new ArrayShape(
                        new ArrayShapeItem('foo', new String_(), false),
                        new ArrayShapeItem('bar', new Integer(), false)
                    )
                ),
                'array<array{foo: string, bar: int}>',
            ],
            'array of object shapes' => [
                new Array_(
                    new ObjectShape(
                        new ObjectShapeItem('foo', new String_(), false),
                        new ObjectShapeItem('bar', new Integer(), false)
                    )
                ),
                'array<object{foo: string, bar: int}>',
            ],
        ];
    }
}