From efd9b072b685a37c822316b07dd56ca57eaba174 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Wed, 1 Nov 2017 12:01:23 +0100 Subject: [PATCH 1/3] Introduce dry run mode --- src/Generator/AbstractClassGenerator.php | 19 ++++++-- src/Generator/AbstractTypeGenerator.php | 61 ++++++++++++++++-------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/Generator/AbstractClassGenerator.php b/src/Generator/AbstractClassGenerator.php index 612b7f8..b5a3f0c 100644 --- a/src/Generator/AbstractClassGenerator.php +++ b/src/Generator/AbstractClassGenerator.php @@ -375,13 +375,22 @@ protected function tokenizeUseStatements(array $useStatements, $prefix = '') /** * Generates classes files. * - * @param array $configs raw configs - * @param string $outputDirectory - * @param bool $regenerateIfExists + * @param array $configs raw configs + * @param string $outputDirectory + * @param int|bool $mode * * @return array classes map [[FQCLN => classPath], [FQCLN => classPath], ...] */ - abstract public function generateClasses(array $configs, $outputDirectory, $regenerateIfExists = false); + abstract public function generateClasses(array $configs, $outputDirectory, $mode = false); - abstract public function generateClass(array $config, $outputDirectory, $regenerateIfExists = false); + /** + * Generates a class file. + * + * @param array $config + * @param $outputDirectory + * @param bool $mode + * + * @return array classes map [FQCLN => classPath] + */ + abstract public function generateClass(array $config, $outputDirectory, $mode = false); } diff --git a/src/Generator/AbstractTypeGenerator.php b/src/Generator/AbstractTypeGenerator.php index 1992762..c7bf1b0 100644 --- a/src/Generator/AbstractTypeGenerator.php +++ b/src/Generator/AbstractTypeGenerator.php @@ -19,6 +19,13 @@ abstract class AbstractTypeGenerator extends AbstractClassGenerator { const DEFAULT_CLASS_NAMESPACE = 'Overblog\\CG\\GraphQLGenerator\\__Schema__'; + const MODE_DRY_RUN = 1; + const MODE_MAPPING_ONLY = 2; + const MODE_WRITE = 4; + const MODE_OVERRIDE = 8; + + protected static $deferredPlaceHolders = ['useStatement', 'spaces', 'closureUseStatements']; + protected static $closureTemplate = <<{ return %s; @@ -308,18 +315,19 @@ protected function arrayKeyExistsAndIsNotNull(array $value, $key) * ] * * - * @param array $configs - * @param string $outputDirectory - * @param bool $regenerateIfExists + * @param array $configs + * @param string $outputDirectory + * @param int|bool $mode + * * @return array */ - public function generateClasses(array $configs, $outputDirectory, $regenerateIfExists = false) + public function generateClasses(array $configs, $outputDirectory, $mode = false) { $classesMap = []; foreach ($configs as $name => $config) { $config['config']['name'] = isset($config['config']['name']) ? $config['config']['name'] : $name; - $classMap = $this->generateClass($config, $outputDirectory, $regenerateIfExists); + $classMap = $this->generateClass($config, $outputDirectory, $mode); $classesMap = array_merge($classesMap, $classMap); } @@ -327,26 +335,41 @@ public function generateClasses(array $configs, $outputDirectory, $regenerateIfE return $classesMap; } - public function generateClass(array $config, $outputDirectory, $regenerateIfExists = false) + /** + * @param array $config + * @param string $outputDirectory + * @param int|bool $mode true consider as MODE_WRITE|MODE_OVERRIDE and false as MODE_WRITE + * + * @return array + */ + public function generateClass(array $config, $outputDirectory, $mode = false) { - static $treatLater = ['useStatement', 'spaces', 'closureUseStatements']; - $this->clearInternalUseStatements(); - $code = $this->processTemplatePlaceHoldersReplacements('TypeSystem', $config, $treatLater); - $code = $this->processPlaceHoldersReplacements($treatLater, $code, $config) . "\n"; + if (true === $mode) { + $mode = self::MODE_WRITE|self::MODE_OVERRIDE; + } elseif (false === $mode) { + $mode = self::MODE_WRITE; + } $className = $this->generateClassName($config); - $path = $outputDirectory . '/' . $className . '.php'; - $dir = dirname($path); - if (!is_dir($dir)) { - mkdir($dir, 0775, true); - } - if ($regenerateIfExists || !file_exists($path)) { - file_put_contents($path, $code); - chmod($path, 0664); + if (!($mode & self::MODE_MAPPING_ONLY)) { + $this->clearInternalUseStatements(); + $code = $this->processTemplatePlaceHoldersReplacements('TypeSystem', $config, static::$deferredPlaceHolders); + $code = $this->processPlaceHoldersReplacements(static::$deferredPlaceHolders, $code, $config) . "\n"; + + if ($mode & self::MODE_WRITE) { + $dir = dirname($path); + if (!is_dir($dir)) { + mkdir($dir, 0775, true); + } + if (($mode & self::MODE_OVERRIDE) || !file_exists($path)) { + file_put_contents($path, $code); + chmod($path, 0664); + } + } } - return [$this->getClassNamespace().'\\'.$className => realpath($path)]; + return [$this->getClassNamespace().'\\'.$className => $path]; } } From db32a949d2163d5d05f2eaa42e8758ba3a82afec Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Thu, 2 Nov 2017 15:35:21 +0100 Subject: [PATCH 2/3] Fix tests types sorting when using webonyx/graphql-php^0.11 --- tests/AbstractStarWarsTest.php | 13 +++++++++++++ tests/StarWarsIntrospectionTest.php | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/AbstractStarWarsTest.php b/tests/AbstractStarWarsTest.php index 18aca97..519dd02 100644 --- a/tests/AbstractStarWarsTest.php +++ b/tests/AbstractStarWarsTest.php @@ -49,4 +49,17 @@ protected function assertValidQuery($query, $expected, $variables = null) $expected = ['data' => $expected]; $this->assertEquals($expected, $actual, json_encode($actual)); } + + protected function sortSchemaEntry(array &$entries, $entryKey, $sortBy) + { + if (isset($entries['data']['__schema'][$entryKey])) { + $data = &$entries['data']['__schema'][$entryKey]; + } else { + $data = &$entries['__schema'][$entryKey]; + } + + usort($data, function ($data1, $data2) use ($sortBy) { + return strcmp($data1[$sortBy], $data2[$sortBy]); + }); + } } diff --git a/tests/StarWarsIntrospectionTest.php b/tests/StarWarsIntrospectionTest.php index ebab7e6..a682d2b 100644 --- a/tests/StarWarsIntrospectionTest.php +++ b/tests/StarWarsIntrospectionTest.php @@ -11,6 +11,8 @@ namespace Overblog\GraphQLGenerator\Tests; +use GraphQL\GraphQL; + class StarWarsIntrospectionTest extends AbstractStarWarsTest { // Star Wars Introspection Tests @@ -53,7 +55,12 @@ public function testAllowsQueryingTheSchemaForTypes() ] ] ]; - $this->assertValidQuery($query, $expected); + + $actual = GraphQL::execute($this->schema, $query); + $this->sortSchemaEntry($actual, 'types', 'name'); + $this->sortSchemaEntry($expected, 'types', 'name'); + $expected = ['data' => $expected]; + $this->assertEquals($expected, $actual, json_encode($actual)); } // it('Allows querying the schema for query type') From d6be529c1075280261222631cc6b88067a30c69f Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Thu, 2 Nov 2017 15:47:37 +0100 Subject: [PATCH 3/3] Fix travis build for php version without xdebug --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cccd2e2..42c7c82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ cache: - $HOME/.composer/cache before_install: - - if [[ "$TRAVIS_PHP_VERSION" != "7.1" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi + - if [[ "$TRAVIS_PHP_VERSION" != "7.1" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini || true; fi - composer selfupdate - if [ "$GRAPHQLPHP_VERSION" != "" ]; then composer require "webonyx/graphql-php:${GRAPHQLPHP_VERSION}" --dev --no-update; fi;