Skip to content

Commit e2b7cf7

Browse files
committed
Fixing aggregate query provider and adding tests
1 parent 106e305 commit e2b7cf7

File tree

4 files changed

+126
-58
lines changed

4 files changed

+126
-58
lines changed

src/AggregateControllerQueryProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function getMutations(): array
9797

9898
foreach ($this->controllers as $controllerName) {
9999
$controller = $this->container->get($controllerName);
100-
$queryProvider = new ControllerQueryProvider($controller, $this->annotationReader, $this->typeMapper, $this->hydrator);
100+
$queryProvider = new ControllerQueryProvider($controller, $this->annotationReader, $this->typeMapper, $this->hydrator, $this->authenticationService, $this->authorizationService);
101101
$mutationList = array_merge($mutationList, $queryProvider->getMutations());
102102
}
103103

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\GraphQL\Controllers;
5+
6+
7+
use PHPUnit\Framework\TestCase;
8+
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
9+
use Youshido\GraphQL\Type\Object\ObjectType;
10+
use Youshido\GraphQL\Type\Scalar\StringType;
11+
use Youshido\GraphQL\Type\TypeInterface;
12+
13+
abstract class AbstractQueryProviderTest extends TestCase
14+
{
15+
private $testObjectType;
16+
private $typeMapper;
17+
private $hydrator;
18+
19+
protected function getTestObjectType()
20+
{
21+
if ($this->testObjectType === null) {
22+
$this->testObjectType = new ObjectType([
23+
'name' => 'TestObject',
24+
'fields' => [
25+
'test' => new StringType(),
26+
],
27+
]);
28+
}
29+
return $this->testObjectType;
30+
}
31+
32+
protected function getTypeMapper()
33+
{
34+
if ($this->typeMapper === null) {
35+
$this->typeMapper = new class($this->getTestObjectType()) implements TypeMapperInterface {
36+
/**
37+
* @var ObjectType
38+
*/
39+
private $testObjectType;
40+
41+
public function __construct(ObjectType $testObjectType)
42+
{
43+
$this->testObjectType = $testObjectType;
44+
}
45+
46+
public function mapClassToType(string $className): TypeInterface
47+
{
48+
if ($className === TestObject::class) {
49+
return $this->testObjectType;
50+
} else {
51+
throw new \RuntimeException('Unexpected type');
52+
}
53+
}
54+
};
55+
}
56+
return $this->typeMapper;
57+
}
58+
59+
protected function getHydrator()
60+
{
61+
if ($this->hydrator === null) {
62+
$this->hydrator = new class implements HydratorInterface {
63+
public function hydrate(array $data, TypeInterface $type)
64+
{
65+
return new TestObject($data['test']);
66+
}
67+
};
68+
}
69+
return $this->hydrator;
70+
}
71+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQL\Controllers;
4+
5+
6+
use Doctrine\Common\Annotations\AnnotationReader;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Container\ContainerExceptionInterface;
9+
use Psr\Container\ContainerInterface;
10+
use Psr\Container\NotFoundExceptionInterface;
11+
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestController;
12+
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
13+
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
14+
15+
class AggregateControllerQueryProviderTest extends AbstractQueryProviderTest
16+
{
17+
public function testAggregate()
18+
{
19+
$controller = new TestController();
20+
$reader = new AnnotationReader();
21+
22+
$container = new class([ 'controller' => $controller ]) implements ContainerInterface {
23+
24+
/**
25+
* @var array
26+
*/
27+
private $controllers;
28+
29+
public function __construct(array $controllers)
30+
{
31+
$this->controllers = $controllers;
32+
}
33+
34+
public function get($id)
35+
{
36+
return $this->controllers[$id];
37+
}
38+
39+
public function has($id)
40+
{
41+
return isset($this->controllers[$id]);
42+
}
43+
};
44+
45+
$aggregateQueryProvider = new AggregateControllerQueryProvider([ 'controller' ], $container, $reader, $this->getTypeMapper(), $this->getHydrator(), new VoidAuthenticationService(), new VoidAuthorizationService());
46+
47+
$queries = $aggregateQueryProvider->getQueries();
48+
$this->assertCount(1, $queries);
49+
50+
$mutations = $aggregateQueryProvider->getMutations();
51+
$this->assertCount(1, $mutations);
52+
}
53+
}

tests/ControllerQueryProviderTest.php

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,8 @@
1616
use Youshido\GraphQL\Type\Scalar\StringType;
1717
use Youshido\GraphQL\Type\TypeInterface;
1818

19-
class ControllerQueryProviderTest extends TestCase
19+
class ControllerQueryProviderTest extends AbstractQueryProviderTest
2020
{
21-
private $testObjectType;
22-
private $typeMapper;
23-
private $hydrator;
24-
25-
private function getTestObjectType()
26-
{
27-
if ($this->testObjectType === null) {
28-
$this->testObjectType = new ObjectType([
29-
'name' => 'TestObject',
30-
'fields' => [
31-
'test' => new StringType(),
32-
],
33-
]);
34-
}
35-
return $this->testObjectType;
36-
}
37-
38-
private function getTypeMapper()
39-
{
40-
if ($this->typeMapper === null) {
41-
$this->typeMapper = new class($this->getTestObjectType()) implements TypeMapperInterface {
42-
/**
43-
* @var ObjectType
44-
*/
45-
private $testObjectType;
46-
47-
public function __construct(ObjectType $testObjectType)
48-
{
49-
$this->testObjectType = $testObjectType;
50-
}
51-
52-
public function mapClassToType(string $className): TypeInterface
53-
{
54-
if ($className === TestObject::class) {
55-
return $this->testObjectType;
56-
} else {
57-
throw new \RuntimeException('Unexpected type');
58-
}
59-
}
60-
};
61-
}
62-
return $this->typeMapper;
63-
}
64-
65-
private function getHydrator()
66-
{
67-
if ($this->hydrator === null) {
68-
$this->hydrator = new class implements HydratorInterface {
69-
public function hydrate(array $data, TypeInterface $type)
70-
{
71-
return new TestObject($data['test']);
72-
}
73-
};
74-
}
75-
return $this->hydrator;
76-
}
7721

7822
public function testQueryProvider()
7923
{

0 commit comments

Comments
 (0)