Skip to content

Commit 1946bb3

Browse files
Andriy KhomychKlaus Purer
Andriy Khomych
authored and
Klaus Purer
committed
fix(schema): Preserve extension keys after sorting (#3515245 by andriy khomych)
1 parent 6b6d79e commit 1946bb3

File tree

4 files changed

+16
-34
lines changed

4 files changed

+16
-34
lines changed

src/Plugin/SchemaExtensionPluginManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getExtensions($id) {
9393
* The sorted schema extension plugins.
9494
*/
9595
public static function sortByPriority(array $extensions): array {
96-
usort($extensions, function (SchemaExtensionPluginInterface $a, SchemaExtensionPluginInterface $b) {
96+
uasort($extensions, function (SchemaExtensionPluginInterface $a, SchemaExtensionPluginInterface $b) {
9797
$priority_a = $a->getPluginDefinition()['priority'] ?? 0;
9898
$priority_b = $b->getPluginDefinition()['priority'] ?? 0;
9999
return $priority_b <=> $priority_a;

tests/src/Kernel/AlterableSchemaTest.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Drupal\Tests\graphql\Kernel;
44

5-
use Drupal\Tests\graphql\Kernel\Schema\AlterableComposableTestSchema;
65
use Drupal\graphql\GraphQL\ResolverRegistry;
6+
use Drupal\graphql\Plugin\GraphQL\Schema\AlterableComposableSchema;
77
use Drupal\graphql\Plugin\SchemaExtensionPluginInterface;
88
use Drupal\graphql\Plugin\SchemaExtensionPluginManager;
99

@@ -144,7 +144,7 @@ protected function mockSchema($id, $schema, array $extensions = []): void {
144144
/** @var \PHPUnit\Framework\MockObject\MockObject $extensionManager */
145145
$extensionManager = $this->getMockBuilder(SchemaExtensionPluginManager::class)
146146
->disableOriginalConstructor()
147-
->onlyMethods(['getExtensions'])
147+
->onlyMethods(['getExtensions', 'createInstance'])
148148
->getMock();
149149

150150
// Adds extra extension in order to test alter extension data event.
@@ -179,9 +179,12 @@ protected function mockSchema($id, $schema, array $extensions = []): void {
179179
$extensionManager->expects(static::any())
180180
->method('getExtensions')
181181
->willReturn($extensions);
182+
$extensionManager->expects(static::any())
183+
->method('createInstance')
184+
->willReturn($extensions['graphql_alterable_schema_test']);
182185

183186
// Replace mock schema with our own implementation.
184-
$this->schema = $this->getMockBuilder(AlterableComposableTestSchema::class)
187+
$this->schema = $this->getMockBuilder(AlterableComposableSchema::class)
185188
->setConstructorArgs([
186189
[],
187190
$id,
@@ -192,7 +195,7 @@ protected function mockSchema($id, $schema, array $extensions = []): void {
192195
['development' => FALSE],
193196
$this->container->get('event_dispatcher'),
194197
])
195-
->onlyMethods(['getSchemaDefinition', 'getResolverRegistry'])
198+
->onlyMethods(['getSchemaDefinition', 'getResolverRegistry', 'getConfiguration'])
196199
->getMock();
197200

198201
$this->schema->expects(static::any())
@@ -203,6 +206,9 @@ protected function mockSchema($id, $schema, array $extensions = []): void {
203206
$this->schema->expects($this->any())
204207
->method('getResolverRegistry')
205208
->willReturn($this->registry);
209+
$this->schema->expects($this->any())
210+
->method('getConfiguration')
211+
->willReturn(['extensions' => ['graphql_alterable_schema_test' => 'graphql_alterable_schema_test']]);
206212
}
207213

208214
}

tests/src/Kernel/Schema/AlterableComposableTestSchema.php

-28
This file was deleted.

tests/src/Kernel/SchemaExtensionPluginPriorityTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function testSchemaExtensionPluginPriority(): void {
3434
$extensions = $schemaExtensionPluginManager->getExtensions('composable');
3535

3636
// Check that the returned extensions are ordered by priority.
37-
$expected_order = ['high_priority_test', 'test', 'low_priority_test'];
37+
$expected_order = [
38+
'high_priority_test' => 'high_priority_test',
39+
'test' => 'test',
40+
'low_priority_test' => 'low_priority_test',
41+
];
3842
$actual_order = array_map(static fn ($extension) => $extension->getPluginId(), $extensions);
3943
static::assertEquals($expected_order, $actual_order);
4044
}

0 commit comments

Comments
 (0)