Skip to content

Commit c15205f

Browse files
committed
Ensure that setTypes and setDescriptions perform merges
1 parent 091c3e9 commit c15205f

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"friendsofphp/php-cs-fixer": "^2.17",
1818
"infection/infection": "^0.18.0",
1919
"phpstan/phpstan": "~0.12.0",
20-
"phpunit/phpunit": "^9.0"
20+
"phpunit/phpunit": "^9.0",
21+
"symfony/var-dumper": "^6.1"
2122
},
2223
"extra": {
2324
"branch-alias": {
@@ -42,5 +43,11 @@
4243
"./vendor/bin/phpstan analyze",
4344
"./vendor/bin/php-cs-fixer fix --allow-risky=yes"
4445
]
46+
},
47+
"config": {
48+
"allow-plugins": {
49+
"infection/extension-installer": true,
50+
"ergebnis/composer-normalize": true
51+
}
4552
}
4653
}

lib/Resolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,23 @@ public function setDefaults(array $defaults): void
7777
*/
7878
public function setEnums(array $enums): void
7979
{
80-
$this->enums = $enums;
80+
$this->enums = array_merge($this->enums, $enums);
8181
}
8282

8383
/**
84-
* @param array<string,string> $typeMap
84+
* @param array<string,string> $types
8585
*/
86-
public function setTypes(array $typeMap): void
86+
public function setTypes(array $types): void
8787
{
88-
$this->types = $typeMap;
88+
$this->types = array_merge($this->types, $types);
8989
}
9090

9191
/**
9292
* @param array<string,string> $descriptions
9393
*/
9494
public function setDescriptions(array $descriptions): void
9595
{
96-
$this->descriptions = $descriptions;
96+
$this->descriptions = array_merge($this->descriptions, $descriptions);
9797
}
9898

9999
/**

tests/Unit/ResolverTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,62 @@ public function testResolvesDescriptions(): void
144144
'two' => 2,
145145
'three' => null,
146146
'four' => 4,
147+
'five' => 4,
147148
]);
148149
$resolver->setDescriptions([
149150
'two' => 'Two is the number',
150151
'four' => 'Four is also a number',
151152
]);
153+
$resolver->setDescriptions([
154+
'five' => 'Five is the number',
155+
]);
152156
self::assertEquals([
153157
'two' => 'Two is the number',
154158
'four' => 'Four is also a number',
155159
'three' => null,
160+
'five' => 'Five is the number',
156161
], $resolver->resolveDescriptions());
157162
}
158163

164+
public function testResolvesEnums(): void
165+
{
166+
$resolver = new Resolver();
167+
$resolver->setDefaults([
168+
'two' => 2,
169+
]);
170+
$resolver->setEnums([
171+
'two' => [2],
172+
]);
173+
$resolver->setEnums([
174+
'two' => [2, 3],
175+
]);
176+
self::assertEquals([
177+
2,
178+
3,
179+
], $resolver->definitions()->get('two')->enum());
180+
}
181+
182+
public function testExceptionOnInvalidTypeAfterMerge(): void
183+
{
184+
$this->expectException(InvalidMap::class);
185+
$this->expectExceptionMessage('got "NULL"');
186+
$resolver = new Resolver();
187+
$resolver->setDefaults([
188+
'two' => 2,
189+
'three' => null,
190+
]);
191+
$resolver->setTypes([
192+
'two' => 'integer',
193+
]);
194+
$resolver->setTypes([
195+
'three' => 'int',
196+
]);
197+
self::assertEquals([
198+
'two' => 2,
199+
'three' => null,
200+
], $resolver->resolve([]));
201+
}
202+
159203
public function testReturnsDefinition(): void
160204
{
161205
$resolver = new Resolver();

0 commit comments

Comments
 (0)