Completed
Push — master ( 106e30...f04cc8 )
by David
11s
created

AbstractQueryProviderTest.php$1 ➔ hydrate()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
}