Skip to content

Commit

Permalink
Merge pull request #23 from moufmouf/refactoring_underlying_graphqlco…
Browse files Browse the repository at this point in the history
…ntrollers

Refactoring lib
  • Loading branch information
moufmouf committed Dec 5, 2018
2 parents dd1a4a3 + 14519f0 commit 029c6e7
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 263 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"require-dev": {
"phpunit/phpunit": "^6.1",
"satooshi/php-coveralls": "^1.0"
"satooshi/php-coveralls": "^1.0",
"mouf/picotainer": "^1.1",
"symfony/cache": "^4.2"
},
"autoload": {
"psr-4": {
Expand Down
153 changes: 0 additions & 153 deletions src/AbstractTdbmGraphQLTypeMapper.php

This file was deleted.

46 changes: 0 additions & 46 deletions src/GraphQLTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function __construct(string $namespace, ?string $generatedNamespace = nul
public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void
{
$this->generateTypes($beanDescriptors);
$this->generateTypeMapper($beanDescriptors);
}

/**
Expand Down Expand Up @@ -289,49 +288,4 @@ protected function {$getterName}Field() : Field

return $code;
}

/**
* @param BeanDescriptorInterface[] $beanDescriptors
*/
private function generateTypeMapper(array $beanDescriptors)
{
$mapCode = '';

foreach ($beanDescriptors as $beanDescriptor) {
$fqcn = $beanDescriptor->getBeanNamespace().'\\'.$beanDescriptor->getBeanClassName();
$graphqlType = $this->namespace.'\\'.$this->namingStrategy->getClassName($beanDescriptor->getBeanClassName());

$beanToGraphQLMap[$fqcn] = $graphqlType;
$mapCode .= ' '.var_export($fqcn, true).' => '.var_export($graphqlType, true).",\n";
}


$str = <<<EOF
<?php
namespace {$this->namespace};
use TheCodingMachine\Tdbm\GraphQL\AbstractTdbmGraphQLTypeMapper;
class TdbmGraphQLTypeMapper extends AbstractTdbmGraphQLTypeMapper
{
protected function getMap(): array
{
return [
$mapCode
];
}
}
EOF;

$classMapperFqcn = $this->namespace.'\\TdbmGraphQLTypeMapper';

$fileSystem = new Filesystem();
$filePaths = $this->classNameMapper->getPossibleFileNames($classMapperFqcn);
if (empty($filePaths)) {
throw new GraphQLGeneratorNamespaceException('Unable to find a suitable autoload path for class '.$fqcn);
}
$filePath = $filePaths[0];
$fileSystem->dumpFile($filePath, $str);
}
}
100 changes: 87 additions & 13 deletions tests/GraphQLTypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@

namespace TheCodingMachine\Tdbm\GraphQL;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\SchemaConfig;
use Mouf\Picotainer\Picotainer;
use Psr\Container\ContainerInterface;
use Symfony\Component\Cache\Simple\NullCache;
use TheCodingMachine\GraphQL\Controllers\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer;
use TheCodingMachine\GraphQL\Controllers\ControllerQueryProviderFactory;
use TheCodingMachine\GraphQL\Controllers\HydratorInterface;
use TheCodingMachine\GraphQL\Controllers\Mappers\GlobTypeMapper;
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper;
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface;
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface;
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface;
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
use TheCodingMachine\TDBM\Configuration;
use TheCodingMachine\Tdbm\GraphQL\Registry\EmptyContainer;
use TheCodingMachine\Tdbm\GraphQL\Registry\Registry;
use TheCodingMachine\Tdbm\GraphQL\Tests\Beans\Country;
use TheCodingMachine\Tdbm\GraphQL\Tests\Beans\User;
use TheCodingMachine\Tdbm\GraphQL\Tests\DAOs\CountryDao;
use TheCodingMachine\Tdbm\GraphQL\Tests\DAOs\UserDao;
use TheCodingMachine\Tdbm\GraphQL\Tests\GraphQL\CountryType;
Expand All @@ -32,6 +51,66 @@

class GraphQLTypeGeneratorTest extends TestCase
{
/**
* @var ContainerInterface
*/
private $mainContainer;

public function setUp()
{
$this->mainContainer = new Picotainer([
ControllerQueryProviderFactory::class => function (ContainerInterface $container) {
return new ControllerQueryProviderFactory(
$container->get(AnnotationReader::class),
$container->get(HydratorInterface::class),
$container->get(AuthenticationServiceInterface::class),
$container->get(AuthorizationServiceInterface::class),
$container->get(BasicAutoWiringContainer::class)
);
},
BasicAutoWiringContainer::class => function (ContainerInterface $container) {
return new BasicAutoWiringContainer(new EmptyContainer());
},
AuthorizationServiceInterface::class => function (ContainerInterface $container) {
return new VoidAuthorizationService();
},
AuthenticationServiceInterface::class => function (ContainerInterface $container) {
return new VoidAuthenticationService();
},
RecursiveTypeMapperInterface::class => function (ContainerInterface $container) {
return new RecursiveTypeMapper($container->get(TypeMapperInterface::class));
},
TypeMapperInterface::class => function (ContainerInterface $container) {
return new GlobTypeMapper(
'TheCodingMachine\\Tdbm\\GraphQL\\Tests\\GraphQL',
$container->get(TypeGenerator::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
new NullCache()
);
},
TypeGenerator::class => function (ContainerInterface $container) {
return new TypeGenerator(
$container->get(AnnotationReader::class),
$container->get(ControllerQueryProviderFactory::class)
);
},
AnnotationReader::class => function (ContainerInterface $container) {
return new AnnotationReader(new DoctrineAnnotationReader());
},
HydratorInterface::class => function (ContainerInterface $container) {
return new class implements HydratorInterface {
public function hydrate(array $data, InputType $type)
{
throw new \RuntimeException('Not implemented');
//return new Contact($data['name']);
}
};
}
]);
}


private static function getAdminConnectionParams(): array
{
return array(
Expand All @@ -52,6 +131,9 @@ private static function getConnectionParams(): array

public static function setUpBeforeClass()
{
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

$config = new \Doctrine\DBAL\Configuration();

$adminConn = DriverManager::getConnection(self::getAdminConnectionParams(), $config);
Expand Down Expand Up @@ -90,16 +172,12 @@ public function testGenerate()

$this->assertFileExists(__DIR__.'/../src/Tests/GraphQL/UserType.php');
$this->assertFileExists(__DIR__.'/../src/Tests/GraphQL/Generated/AbstractUserType.php');
$this->assertFileExists(__DIR__.'/../src/Tests/GraphQL/TdbmGraphQLTypeMapper.php');

$this->assertTrue(class_exists(AbstractCountryType::class));
$abstractCountryType = new \ReflectionClass(AbstractCountryType::class);
$this->assertNotNull($abstractCountryType->getMethod('getUsersField'));
$abstractUserType = new \ReflectionClass(AbstractUserType::class);
$this->assertNotNull($abstractUserType->getMethod('getRolesField'));

$tdbmGraphQLTypeMapper = new \ReflectionClass(TdbmGraphQLTypeMapper::class);
$this->assertNotNull($tdbmGraphQLTypeMapper->getMethod('mapClassToType'));
}

/**
Expand All @@ -109,17 +187,17 @@ public function testQuery()
{
$tdbmService = self::getTDBMService();
$userDao = new UserDao($tdbmService);
$container = new EmptyContainer();
$typeMapper = new TdbmGraphQLTypeMapper();
//$container = new EmptyContainer();
/*$typeMapper = new TdbmGraphQLTypeMapper();
$registry = TestRegistryFactory::build($container, null, null, null, $typeMapper);
$typeMapper->setContainer($registry);
$typeMapper->setContainer($registry);*/


$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'users' => [
'type' => Type::listOf($registry->get(UserType::class)),
'type' => Type::listOf($this->mainContainer->get(RecursiveTypeMapperInterface::class)->mapClassToType(User::class)),
'resolve' => function () use ($userDao) {
return $userDao->findAll();
}
Expand Down Expand Up @@ -215,11 +293,7 @@ private function recursiveDelete(string $str) : bool

public function testResultIteratorType()
{
$typeMapper = new TdbmGraphQLTypeMapper();
$container = new EmptyContainer();
$registry = TestRegistryFactory::build($container, null, null, null, $typeMapper);

$type = new ResultIteratorType($registry->get(CountryType::class));
$type = new ResultIteratorType($this->mainContainer->get(RecursiveTypeMapperInterface::class)->mapClassToType(Country::class));

$tdbmService = self::getTDBMService();
$countryDao = new CountryDao($tdbmService);
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 029c6e7

Please sign in to comment.