-
-
Notifications
You must be signed in to change notification settings - Fork 899
Expand file tree
/
Copy pathArrayDescriberTest.php
More file actions
93 lines (79 loc) · 3.21 KB
/
ArrayDescriberTest.php
File metadata and controls
93 lines (79 loc) · 3.21 KB
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
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\TypeDescriber;
use Nelmio\ApiDocBundle\TypeDescriber\ArrayDescriber;
use Nelmio\ApiDocBundle\TypeDescriber\TypeDescriberInterface;
use OpenApi\Annotations\Schema;
use PHPUnit\Framework\TestCase;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\UnionType;
class ArrayDescriberTest extends TestCase
{
private ArrayDescriber $describer;
protected function setUp(): void
{
$this->describer = new ArrayDescriber();
}
/**
* @dataProvider provideInvalidCollectionTypes
*
* @param CollectionType $type
*/
public function testDescribeHandlesInvalidKeyType($type): void
{
self::expectException(\LogicException::class);
self::expectExceptionMessage('This describer only supports '.CollectionType::class.' with '.UnionType::class.' as key type.');
$this->describer->describe($type, new Schema([]));
}
public static function provideInvalidCollectionTypes(): \Generator
{
yield [Type::array(Type::int(), Type::int())];
yield [Type::array(Type::int(), Type::string())];
yield [Type::list()];
yield [Type::dict()];
}
/**
* When the key type is arrayKey() (int|string union), the describer should
* treat the collection as a list rather than splitting into anyOf [array, object].
*/
public function testArrayKeyUnionIsTreatedAsList(): void
{
$innerDescriber = $this->createMock(TypeDescriberInterface::class);
$innerDescriber->expects(self::once())
->method('describe')
->with(self::callback(static function (Type $type): bool {
// Should delegate a list(string) — i.e. CollectionType with int key and string value
return $type instanceof CollectionType
&& $type->isList()
&& 'string' === (string) $type->getCollectionValueType();
}));
$this->describer->setDescriber($innerDescriber);
// array<string> is resolved by TypeInfo as CollectionType with arrayKey() union key
$type = Type::array(Type::string());
$this->describer->describe($type, new Schema([]));
}
/**
* When a Traversable object is auto-wrapped in CollectionType(ObjectType),
* the describer should unwrap it and delegate the ObjectType directly.
*/
public function testTraversableObjectIsUnwrapped(): void
{
$objectType = Type::object(\ArrayObject::class);
// Simulate what StringTypeResolver does: wrap in CollectionType(ObjectType)
$collectionType = new CollectionType($objectType);
$innerDescriber = $this->createMock(TypeDescriberInterface::class);
$innerDescriber->expects(self::once())
->method('describe')
->with(self::identicalTo($objectType));
$this->describer->setDescriber($innerDescriber);
$this->describer->describe($collectionType, new Schema([]));
}
}