Skip to content

Commit 2d9b835

Browse files
committed
Adding integration tests for new StaticTypeMapper
1 parent cca5f0f commit 2d9b835

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

src/Mappers/Proxys/MutableAdapterTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __toString()
7878
*
7979
* @throws Exception
8080
*/
81-
public function getField($name)
81+
public function getField($name): FieldDefinition
8282
{
8383
if ($this->status === MutableInterface::STATUS_PENDING) {
8484
throw new RuntimeException('You must freeze() a '.get_class($this).' before fetching its fields.');
@@ -92,7 +92,7 @@ public function getField($name)
9292
*
9393
* @return bool
9494
*/
95-
public function hasField($name)
95+
public function hasField($name): bool
9696
{
9797
if ($this->status === MutableInterface::STATUS_PENDING) {
9898
throw new RuntimeException('You must freeze() a '.get_class($this).' before fetching its fields.');

src/Mappers/Proxys/MutableInterfaceTypeAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use GraphQL\Utils\Utils;
1313
use RuntimeException;
1414
use TheCodingMachine\GraphQLite\Types\MutableInterface;
15+
use TheCodingMachine\GraphQLite\Types\MutableInterfaceType;
1516
use TheCodingMachine\GraphQLite\Types\NoFieldsException;
1617
use function call_user_func;
1718
use function is_array;
@@ -24,7 +25,7 @@
2425
*
2526
* @internal
2627
*/
27-
class MutableInterfaceTypeAdapter extends InterfaceType implements MutableInterface
28+
class MutableInterfaceTypeAdapter extends MutableInterfaceType implements MutableInterface
2829
{
2930
/** @use MutableAdapterTrait<InterfaceType> */
3031
use MutableAdapterTrait;

src/Mappers/Proxys/MutableObjectTypeAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use GraphQL\Utils\Utils;
1313
use RuntimeException;
1414
use TheCodingMachine\GraphQLite\Types\MutableInterface;
15+
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
1516
use TheCodingMachine\GraphQLite\Types\NoFieldsException;
1617
use function assert;
1718
use function call_user_func;
@@ -25,7 +26,7 @@
2526
*
2627
* @internal
2728
*/
28-
class MutableObjectTypeAdapter extends ObjectType implements MutableInterface
29+
class MutableObjectTypeAdapter extends MutableObjectType implements MutableInterface
2930
{
3031
/** @use MutableAdapterTrait<ObjectType> */
3132
use MutableAdapterTrait;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQLite\Fixtures\StaticTypeMapper\Controllers;
5+
6+
7+
use TheCodingMachine\GraphQLite\Annotations\Query;
8+
use TheCodingMachine\GraphQLite\Fixtures\StaticTypeMapper\Types\TestLegacyObject;
9+
10+
class TestLegacyController
11+
{
12+
/**
13+
* @Query()
14+
*/
15+
public function getLegacyObject(): TestLegacyObject
16+
{
17+
return new TestLegacyObject();
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQLite\Fixtures\StaticTypeMapper\Types;
5+
6+
7+
class TestLegacyObject
8+
{
9+
public function getFoo() {
10+
return 42;
11+
}
12+
}

tests/Mappers/StaticTypeMapperTest.php

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
namespace TheCodingMachine\GraphQLite\Mappers;
44

5+
use GraphQL\Error\Debug;
6+
use GraphQL\GraphQL;
57
use GraphQL\Type\Definition\StringType;
68
use GraphQL\Type\Definition\Type;
7-
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10+
use Symfony\Component\Cache\Psr16Cache;
811
use TheCodingMachine\GraphQLite\AbstractQueryProviderTest;
12+
use TheCodingMachine\GraphQLite\Containers\BasicAutoWiringContainer;
13+
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
914
use TheCodingMachine\GraphQLite\Fixtures\Mocks\MockResolvableInputObjectType;
15+
use TheCodingMachine\GraphQLite\Fixtures\StaticTypeMapper\Types\TestLegacyObject;
1016
use TheCodingMachine\GraphQLite\Fixtures\TestObject2;
11-
use TheCodingMachine\GraphQLite\Mappers\CannotMapTypeException;
17+
use TheCodingMachine\GraphQLite\Loggers\ExceptionLogger;
1218
use TheCodingMachine\GraphQLite\Fixtures\TestObject;
1319
use GraphQL\Type\Definition\InputObjectType;
1420
use GraphQL\Type\Definition\ObjectType;
21+
use TheCodingMachine\GraphQLite\SchemaFactory;
1522
use TheCodingMachine\GraphQLite\Types\MutableObjectType;
16-
use TheCodingMachine\GraphQLite\Types\ResolvableMutableInputObjectType;
1723

1824
class StaticTypeMapperTest extends AbstractQueryProviderTest
1925
{
@@ -123,4 +129,54 @@ public function testUnsupportedSubtypes(): void
123129
$this->expectException(CannotMapTypeException::class);
124130
$this->typeMapper->mapClassToType(TestObject::class, new StringType());
125131
}
132+
133+
public function testEndToEnd(): void
134+
{
135+
$arrayAdapter = new ArrayAdapter();
136+
$arrayAdapter->setLogger(new ExceptionLogger());
137+
$schemaFactory = new SchemaFactory(new Psr16Cache($arrayAdapter), new BasicAutoWiringContainer(new EmptyContainer()));
138+
$schemaFactory->addControllerNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\StaticTypeMapper\\Controllers');
139+
140+
$staticTypeMapper = new StaticTypeMapper();
141+
// Let's register a type that maps by default to the "MyClass" PHP class
142+
$staticTypeMapper->setTypes([
143+
TestLegacyObject::class => new ObjectType([
144+
'name' => 'TestLegacyObject',
145+
'fields' => [
146+
'foo' => [
147+
'type' =>Type::int(),
148+
'resolve' => function(TestLegacyObject $source) {
149+
return $source->getFoo();
150+
}
151+
]
152+
]
153+
])
154+
]);
155+
156+
// Register the static type mapper in your application using the SchemaFactory instance
157+
$schemaFactory->addTypeMapper($staticTypeMapper);
158+
159+
$schema = $schemaFactory->createSchema();
160+
161+
$schema->validate();
162+
163+
$queryString = '
164+
query {
165+
legacyObject {
166+
foo
167+
}
168+
}
169+
';
170+
171+
$result = GraphQL::executeQuery(
172+
$schema,
173+
$queryString
174+
);
175+
176+
$this->assertSame([
177+
'legacyObject' => [
178+
'foo' => 42
179+
]
180+
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
181+
}
126182
}

0 commit comments

Comments
 (0)