diff --git a/.gitignore b/.gitignore index ced3610..c7820e3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ schema_object/* !schema_object/.gitkeep .phpunit.result.cache /build/ -composer.lock \ No newline at end of file +composer.lock +/.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..dcd64b5 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,17 @@ +in(__DIR__ . '/src') + ->in(__DIR__ . '/tests') +; + +return (new PhpCsFixer\Config()) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, // should be last of rules set to avoid override its rules + // here we override some rules + 'yoda_style' => false, + 'declare_strict_types' => true, + ]) + ->setFinder($finder) +; diff --git a/bin/generate_schema_objects b/bin/generate_schema_objects index cad2ae9..6e327bc 100755 --- a/bin/generate_schema_objects +++ b/bin/generate_schema_objects @@ -1,45 +1,80 @@ #!/usr/bin/env php addArgument('graphql-endpoint', InputArgument::REQUIRED, 'GraphQL endpoint URL') + ->addOption('auth-header', null, InputOption::VALUE_REQUIRED, 'Authorization Header Name') + ->addOption('auth-header-value', null, InputOption::VALUE_REQUIRED, 'Authorization Header Name') + ->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Custom classes writing dir', '') + ->setCode(function (InputInterface $input, OutputInterface $output) { + $io = new SymfonyStyle($input, $output); + $endpoint = $input->getArgument('graphql-endpoint'); + $dir = ''; + $authHeaders = []; -$authHeaders = []; -$authHeaderName = readline('Authorization header name: '); -if (!empty($authHeaderName)) { - $authHeaderValue = readline('Authorization header value: '); - $authHeaders = [$authHeaderName => $authHeaderValue]; -} + if ($input->getOption('dir')) { + $dir = $input->getOption('dir'); + } + + if ($input->getOption('auth-header')) { + $authHeaders = [$input->getOption('auth-header') => $input->getOption('auth-header-value')]; + } + + // Create default HandlerStack + $stack = HandlerStack::create(); + + // Add this middleware to the top with `push` + $cacheStorage = new Psr6CacheStorage(new FilesystemAdapter(directory: __DIR__.'/../cache')); + $cacheStrategy = new class($cacheStorage) extends PrivateCacheStrategy { + protected function getCacheKey(RequestInterface $request, KeyValueHttpHeader $varyHeaders = null) + { + return hash('sha256', $request->getMethod().$request->getUri().$request->getBody()); + } + }; + $cacheMiddleware = new CacheMiddleware($cacheStrategy); + $cacheMiddleware->setHttpMethods(['GET' => true, 'POST' => true]); + $stack->push($cacheMiddleware, 'cache'); + + // Initialize the client with the handler option + $guzzle = new HttpClient(['handler' => $stack]); -$customWriteDir = readline('Custom classes writing dir (optional): '); -if (empty($customWriteDir)) $customWriteDir = ''; + $client = new GraphQLClient( + endpointUrl: $endpoint, + authorizationHeaders: $authHeaders, + httpClient: new GuzzleAdapter($guzzle) + ); -$client = new Client($endpointUrl, $authHeaders); -$scanner = new SchemaClassGenerator($client, $customWriteDir); + $scanner = new SchemaClassGenerator($client, $dir); -print "-------------------------------------------\n"; -print "Generating schema objects from schema types\n"; -print "-------------------------------------------\n"; + $io->block('Generating schema objects from schema types'); -$scanner->generateRootQueryObject(); + $scanner->generateRootQueryObject(); -print "-------------------------------------------\n"; -print "Schema objects generation complete\n"; -print "-------------------------------------------\n"; + $io->block('Schema objects generation complete'); + }) + ->run(); diff --git a/composer.json b/composer.json index 303c0ee..392ba3b 100644 --- a/composer.json +++ b/composer.json @@ -34,13 +34,19 @@ }, "bin": ["bin/generate_schema_objects"], "require": { - "php": "^7.1 || ^8.0", - "gmostafa/php-graphql-client": "^1.12" + "php": "^8.1", + "gmostafa/php-graphql-client": "^1.12", + "nette/php-generator": "^4.0", + "guzzlehttp/guzzle": "^7.4", + "kevinrob/guzzle-cache-middleware": "^4.0", + "symfony/cache": "^6.0" }, "require-dev": { "phpunit/phpunit": "^7.5|^8.0", "codacy/coverage": "^1.4", - "ext-json": "*" + "ext-readline": "*", + "ext-json": "*", + "friendsofphp/php-cs-fixer": "^3.7" }, "scripts": { "test": "phpunit tests/ --whitelist src/ --coverage-clover build/coverage/xml" diff --git a/schema_object/.gitkeep b/schema_object/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Enumeration/FieldTypeKindEnum.php b/src/Enumeration/FieldTypeKindEnum.php index 28dfa7b..c80d2ed 100644 --- a/src/Enumeration/FieldTypeKindEnum.php +++ b/src/Enumeration/FieldTypeKindEnum.php @@ -1,19 +1,20 @@ classFile->writeFile(); + } +} diff --git a/src/SchemaGenerator/CodeGenerator/ArgumentsObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/ArgumentsObjectClassBuilder.php index 90d7ca6..7319fa3 100644 --- a/src/SchemaGenerator/CodeGenerator/ArgumentsObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/ArgumentsObjectClassBuilder.php @@ -1,41 +1,52 @@ classFile = new ClassFile($writeDir, $objectName); - $this->classFile->setNamespace($namespace); - if ($namespace !== self::DEFAULT_NAMESPACE) { - $this->classFile->addImport('GraphQL\\SchemaObject\\ArgumentsObject'); - } - $this->classFile->extendsClass('ArgumentsObject'); + $this->classFile = new ClassFile($writeDir, $objectName, $namespace); + $this->classFile->extendsClass(ArgumentsObject::class); } - /** - * @param string $argumentName - */ - public function addScalarArgument(string $argumentName) + public function addScalarArgument(string $argumentName, ?string $typeName = ''): void { + $lowerTypeName = strtolower($typeName); + if ($lowerTypeName === 'boolean') { + $lowerTypeName = 'bool'; + } + + if ($lowerTypeName === 'id') { + $lowerTypeName = 'string'; + } + + if ($lowerTypeName === 'money') { + $lowerTypeName = 'string'; + } + + if ($lowerTypeName === 'url') { + $lowerTypeName = 'string'; + } + + if ($lowerTypeName) { + assert(in_array($lowerTypeName, ['bool', 'int', 'float', 'string']), $lowerTypeName); + } + $upperCamelCaseArg = StringLiteralFormatter::formatUpperCamelCase($argumentName); - $this->addProperty($argumentName); + $this->addProperty($argumentName, null, $lowerTypeName); $this->addScalarSetter($argumentName, $upperCamelCaseArg); } @@ -43,41 +54,26 @@ public function addScalarArgument(string $argumentName) * @param string string $argumentName * @param string string $typeName */ - public function addListArgument(string $argumentName, string $typeName) + public function addListArgument(string $argumentName, string $typeName): void { $upperCamelCaseArg = StringLiteralFormatter::formatUpperCamelCase($argumentName); - $this->addProperty($argumentName); + $this->addProperty($argumentName, null, 'array'); $this->addListSetter($argumentName, $upperCamelCaseArg, $typeName); } - /** - * @param string $argumentName - * @param string $typeName - */ - public function addInputEnumArgument(string $argumentName, string $typeName) + public function addInputEnumArgument(string $argumentName, string $typeName): void { + $typeName .= 'EnumObject'; $upperCamelCaseArg = StringLiteralFormatter::formatUpperCamelCase($argumentName); - $this->addProperty($argumentName); + $this->addProperty($argumentName, null, $typeName); $this->addEnumSetter($argumentName, $upperCamelCaseArg, $typeName); } - /** - * @param string $argumentName - * @param string $typeName - */ - public function addInputObjectArgument(string $argumentName, string $typeName) + public function addInputObjectArgument(string $argumentName, string $typeName): void { $typeName .= 'InputObject'; $upperCamelCaseArg = StringLiteralFormatter::formatUpperCamelCase($argumentName); - $this->addProperty($argumentName); + $this->addProperty($argumentName, null, $typeName); $this->addObjectSetter($argumentName, $upperCamelCaseArg, $typeName); } - - /** - * @return void - */ - public function build(): void - { - $this->classFile->writeFile(); - } -} \ No newline at end of file +} diff --git a/src/SchemaGenerator/CodeGenerator/CodeFile/AbstractCodeFile.php b/src/SchemaGenerator/CodeGenerator/CodeFile/AbstractCodeFile.php index 9df6494..9b1c7f8 100644 --- a/src/SchemaGenerator/CodeGenerator/CodeFile/AbstractCodeFile.php +++ b/src/SchemaGenerator/CodeGenerator/CodeFile/AbstractCodeFile.php @@ -1,55 +1,66 @@ validateDirectory($writeDir); - $this->writeDir = $writeDir; - $this->fileName = $fileName; + + $this->classLike = $this->createClassLikeClass($className, $namespace); } - /** - * @param string $dirName - * - * @return bool - */ - private function validateDirectory(string $dirName): bool + abstract protected function createClassLikeClass(string $className, ?string $namespace = ''): ClassLike; + + #[Pure] + protected function generateFileContents(): string + { + $file = new PhpFile(); + $printer = new PsrPrinter(); + $namespace = $this->classLike->getNamespace(); + $namespace?->add($this->classLike); + $file->addNamespace($namespace); + + return $printer->printFile($file); + } + + public function extendsClass(string $className): void + { + $this->classLike->setExtends($className); + } + + public function addConstant(string $name, $value): void + { + $this->classLike->addConstant($name, $value); + } + + public function addImport(string $className): void + { + $namespace = $this->classLike->getNamespace(); + $namespace?->addUse($className); + } + + private function validateDirectory(string $dirName): void { if (!is_dir($dirName)) { throw new RuntimeException("$dirName is not a valid directory"); @@ -57,39 +68,24 @@ private function validateDirectory(string $dirName): bool if (!is_writable($dirName)) { throw new RuntimeException("$dirName is not writable"); } - - return true; } /** - * @inheritdoc + * {@inheritdoc} */ - public final function writeFile(): bool + final public function writeFile(): bool { $fileContents = $this->generateFileContents(); $filePath = $this->writeDir; - if (substr($filePath, -1) !== '/') { + if (!str_ends_with($filePath, '/')) { $filePath .= '/'; } - $filePath .= $this->fileName . '.php'; + $filePath .= $this->classLike->getName().'.php'; return $this->writeFileToPath($fileContents, $filePath); } - /** - * This method generates and returns the file contents from class properties - * - * @return string - */ - protected abstract function generateFileContents(): string; - - /** - * @param string $fileContents - * @param string $filePath - * - * @return bool - */ private function writeFileToPath(string $fileContents, string $filePath): bool { return file_put_contents($filePath, $fileContents) !== false; @@ -97,35 +93,20 @@ private function writeFileToPath(string $fileContents, string $filePath): bool /** * @codeCoverageIgnore - * - * @return string - */ - public function getFileName(): string - { - return $this->fileName; - } - - /** - * @param string $fileName */ - public function changeFileName(string $fileName) + public function getClassName(): string { - $this->fileName = $fileName; + return $this->classLike->getName(); } /** * @codeCoverageIgnore - * - * @return string */ public function getWriteDir(): string { return $this->writeDir; } - /** - * @param string $writeDir - */ public function changeWriteDir(string $writeDir) { $this->validateDirectory($writeDir); @@ -133,11 +114,9 @@ public function changeWriteDir(string $writeDir) $this->writeDir = $writeDir; } - /** - * @return string - */ + #[Pure] public function getWritePath(): string { - return $this->writeDir . "/$this->fileName.php"; + return $this->writeDir."/{$this->classLike->getName()}.php"; } -} \ No newline at end of file +} diff --git a/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php b/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php index a6525ec..4bf8c9a 100644 --- a/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php +++ b/src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php @@ -1,197 +1,28 @@ value - * - * @var array - */ - protected $constants; - - /** - * ClassFile constructor. - * - * @param string $writeDir - * @param string $fileName - */ - public function __construct(string $writeDir, string $fileName) - { - parent::__construct($writeDir, $fileName); - $this->baseClass = ''; - $this->interfaces = []; - $this->traits = []; - $this->constants = []; - } - - /** - * @param string $className - */ - public function extendsClass(string $className) - { - if (!empty($className)) { - $this->baseClass = $className; - } - } - - /** - * @param string $interfaceName - */ - public function implementsInterface(string $interfaceName) - { - if (!empty($interfaceName)) { - $this->interfaces[$interfaceName] = null; - } - } - - /** - * @param string $traitName - */ - public function addTrait(string $traitName) - { - if (!empty($traitName)) { - $this->traits[$traitName] = null; - } - } + /** @var ClassType */ + protected ClassLike $classLike; - /** - * @param string $name - * @param string|int|bool $value - */ - public function addConstant(string $name, $value) + protected function createClassLikeClass(string $className, ?string $namespace = ''): ClassLike { - if (!empty($name)) { - $this->constants[$name] = $value; - } + return new ClassType($className, new PhpNamespace($namespace)); } - /** - * @inheritdoc - */ - protected function generateFileContents(): string + public function implementsInterface(string $name): void { - $className = $this->generateClassName(); - - // Generate class headers - $namespace = $this->generateNamespace(); - if (!empty($namespace)) $namespace = PHP_EOL . $namespace; - $imports = $this->generateImports(); - if (!empty($imports)) $imports = PHP_EOL . $imports; - - // Generate class body - $traits = $this->generateTraits(); - if (!empty($traits)) $traits = PHP_EOL . $traits; - $constants = $this->generateConstants(); - if (!empty($constants)) $constants = PHP_EOL . $constants; - $properties = $this->generateProperties(); - if (!empty($properties)) $properties = PHP_EOL . $properties; - $methods = $this->generateMethods(); - - $contents = sprintf( - static::FILE_FORMAT, - $namespace, - $imports, - $className, - $traits, - $constants, - $properties, - $methods - ); - return $this->normalizeLineEndings($contents); - } - - /** - * @return string - */ - protected function generateClassName(): string - { - $string = $this->fileName; - if (!empty($this->baseClass)) { - $string .= " extends $this->baseClass"; - } - - // Append interfaces list - if (!empty($this->interfaces)) { - $string .= ' implements '; - $first = true; - foreach ($this->interfaces as $interfaceName => $nothing) { - if (!$first) { - $string .= ', '; - } - $string .= $interfaceName; - $first = false; - } - } - - return $string; - } - - /** - * @return string - */ - protected function generateTraits(): string - { - $string = ''; - if (!empty($this->traits)) { - foreach ($this->traits as $traitName => $nothing) { - $string .= " use $traitName;" . PHP_EOL; - } - } - - return $string; - } - - /** - * @return string - */ - protected function generateConstants(): string - { - $string = ''; - if (!empty($this->constants)) { - foreach ($this->constants as $name => $value) { - $value = $this->serializeParameterValue($value); - $string .= " const $name = $value;" . PHP_EOL; - } - } - - return $string; + $this->classLike->addImplement($name); } } diff --git a/src/SchemaGenerator/CodeGenerator/CodeFile/CodeFileInterface.php b/src/SchemaGenerator/CodeGenerator/CodeFile/CodeFileInterface.php index a8a59e5..330311f 100644 --- a/src/SchemaGenerator/CodeGenerator/CodeFile/CodeFileInterface.php +++ b/src/SchemaGenerator/CodeGenerator/CodeFile/CodeFileInterface.php @@ -1,18 +1,18 @@ classLike->addCase($name, $value); + } +} diff --git a/src/SchemaGenerator/CodeGenerator/CodeFile/InterfaceFile.php b/src/SchemaGenerator/CodeGenerator/CodeFile/InterfaceFile.php new file mode 100644 index 0000000..3bc97d8 --- /dev/null +++ b/src/SchemaGenerator/CodeGenerator/CodeFile/InterfaceFile.php @@ -0,0 +1,20 @@ + value - * - * @var array - */ - protected $properties; - - /** - * This array is a list that stores the string representations of methods in the file - * - * @var array - */ - protected $methods; - - /** - * TraitFile constructor. - * - * @param $writeDir - * @param $fileName - */ - public function __construct(string $writeDir, string $fileName) - { - parent::__construct($writeDir, $fileName); - $this->namespace = ''; - $this->imports = []; - $this->properties = []; - $this->methods = []; - } - - /** - * @param $namespaceName - */ - public function setNamespace(string $namespaceName) - { - if (!empty($namespaceName)) { - $this->namespace = $namespaceName; - } - } - - /** - * @param string $fullyQualifiedName - */ - public function addImport(string $fullyQualifiedName) - { - if (!empty($fullyQualifiedName)) { - $this->imports[$fullyQualifiedName] = null; - } - } - - /** - * @param string $name - * @param null|string|int|bool $value - */ - public function addProperty(string $name, $value = null) - { - if (is_string($name) && !empty($name)) { - $this->properties[$name] = $value; - } - } - - /** - * @param string $methodString - * @param bool $isDeprecated - * @param string|null $deprecationReason - */ - public function addMethod(string $methodString, bool $isDeprecated = false, ?string $deprecationReason = null) - { - if (!empty($methodString)) { - $methodString = $this->prependDeprecationComment($methodString, $isDeprecated, $deprecationReason); - - // Normalize line endings here to make replacements in generateMethods() work. - $methodString = $this->normalizeLineEndings($methodString); - $this->methods[] = $methodString; - } - } + /** @var TraitType */ + protected ClassLike $classLike; - /** - * @inheritdoc - */ - protected function generateFileContents(): string + protected function createClassLikeClass(string $className, ?string $namespace = ''): ClassLike { - $className = $this->fileName; - - // Generate class headers - $namespace = $this->generateNamespace(); - if (!empty($namespace)) $namespace = PHP_EOL . $namespace; - $imports = $this->generateImports(); - if (!empty($imports)) $imports = PHP_EOL . $imports; - - // Generate class body - $properties = $this->generateProperties(); - if (!empty($properties)) $properties = PHP_EOL . $properties; - $methods = $this->generateMethods(); - - $contents = sprintf(static::FILE_FORMAT, $namespace, $imports, $className, $properties, $methods); - return $this->normalizeLineEndings($contents); + return new TraitType($className, new PhpNamespace($namespace)); } - /** - * @return string - */ - protected function generateNamespace(): string + public function addTrait(string $name) { - $string = ''; - if (!empty($this->namespace)) { - $string = "namespace $this->namespace;" . PHP_EOL; - } - - return $string; + $this->classLike->addTrait($name); } - /** - * @return string - */ - protected function generateImports(): string + public function addProperty(string $propertyName, mixed $defaultValue = null, ?string $propertyType = null): void { - $string = ''; - if (!empty($this->imports)) { - foreach ($this->imports as $import => $nothing) { - $string .= "use $import;" . PHP_EOL; - } + $property = $this->classLike->addProperty($propertyName)->setType($propertyType)->setVisibility(ClassLike::VisibilityProtected); + if ($defaultValue !== null) { + $property->setValue($defaultValue); } - - return $string; } - /** - * @return string - */ - protected function generateProperties(): string - { - $string = ''; - if (!empty($this->properties)) { - foreach ($this->properties as $name => $value) { - if ($value === null) { - $string .= " protected $$name;" . PHP_EOL; - } else { - $value = $this->serializeParameterValue($value); - $string .= " protected $$name = $value;" . PHP_EOL; - } - } - } - - return $string; - } - - /** - * @return string - */ - protected function generateMethods(): string - { - $string = ''; - if (!empty($this->methods)) { - foreach ($this->methods as $method) { - // Indent method with 4 space characters - $method = str_replace(PHP_EOL, PHP_EOL . ' ', $method); - // Make lines that were previously empty, empty again. - $method = str_replace(PHP_EOL . ' ' . PHP_EOL, PHP_EOL . PHP_EOL, $method); - $string .= PHP_EOL . ' ' . $method . PHP_EOL; - } - } - - return $string; - } - - /** - * @param $value - * - * @return string - */ - protected function serializeParameterValue($value): string - { - return StringLiteralFormatter::formatValueForRHS($value); - } - - /** - * @param string $code - * @param bool $isDeprecated - * @param string|null $deprecationReason - * - * @return string - */ - protected function prependDeprecationComment(string $code, bool $isDeprecated, ?string $deprecationReason): string + public function addMethod(string $methodName, bool $isDeprecated = false, ?string $deprecationReason = ''): Method { + $method = $this->classLike->addMethod($methodName); if ($isDeprecated) { - $code = "/** - * @deprecated".($deprecationReason ? " $deprecationReason" : "")." - */ -".$code; - } - - return $code; - } - - protected function normalizeLineEndings(string $code) - { - $code = str_replace("\r", '', $code); - if (PHP_EOL !== "\n") { - $code = str_replace("\n", PHP_EOL, $code); + $method->addComment('@deprecated '.$deprecationReason); } - return $code; + return $method; } } diff --git a/src/SchemaGenerator/CodeGenerator/EnumObjectBuilder.php b/src/SchemaGenerator/CodeGenerator/EnumObjectBuilder.php index 7032054..14ae6f6 100644 --- a/src/SchemaGenerator/CodeGenerator/EnumObjectBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/EnumObjectBuilder.php @@ -1,54 +1,26 @@ classFile = new ClassFile($writeDir, $className); - $this->classFile->setNamespace($namespace); - if ($namespace !== self::DEFAULT_NAMESPACE) { - $this->classFile->addImport('GraphQL\\SchemaObject\\EnumObject'); - } - $this->classFile->extendsClass('EnumObject'); + $this->classFile = new EnumFile($writeDir, $className, $namespace); } - /** - * @param string $valueName - */ public function addEnumValue(string $valueName) { $constantName = strtoupper($valueName); $this->classFile->addConstant($constantName, $valueName); } - - /** - * @return void - */ - public function build(): void - { - $this->classFile->writeFile(); - } -} \ No newline at end of file +} diff --git a/src/SchemaGenerator/CodeGenerator/InputObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/InputObjectClassBuilder.php index 1eb4ad9..2e186ec 100644 --- a/src/SchemaGenerator/CodeGenerator/InputObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/InputObjectClassBuilder.php @@ -1,39 +1,35 @@ classFile = new ClassFile($writeDir, $className, $namespace); + $this->classFile->extendsClass(InputObject::class); - $this->classFile = new ClassFile($writeDir, $className); - $this->classFile->setNamespace($namespace); - if ($namespace !== self::DEFAULT_NAMESPACE) { - $this->classFile->addImport('GraphQL\\SchemaObject\\InputObject'); + if ($interfaces) { + foreach ($interfaces as $interface) { + $this->classFile->implementsInterface($interface); + } } - $this->classFile->extendsClass('InputObject'); } - /** - * @param string $argumentName - */ public function addScalarValue(string $argumentName) { $upperCamelCaseArg = StringLiteralFormatter::formatUpperCamelCase($argumentName); @@ -41,10 +37,6 @@ public function addScalarValue(string $argumentName) $this->addScalarSetter($argumentName, $upperCamelCaseArg); } - /** - * @param string $argumentName - * @param string $typeName - */ public function addListValue(string $argumentName, string $typeName) { $upperCamelCaseArg = StringLiteralFormatter::formatUpperCamelCase($argumentName); @@ -52,10 +44,6 @@ public function addListValue(string $argumentName, string $typeName) $this->addListSetter($argumentName, $upperCamelCaseArg, $typeName); } - /** - * @param string $argumentName - * @param string $typeName - */ public function addInputObjectValue(string $argumentName, string $typeName) { $typeName .= 'InputObject'; @@ -63,12 +51,4 @@ public function addInputObjectValue(string $argumentName, string $typeName) $this->addProperty($argumentName); $this->addObjectSetter($argumentName, $upperCamelCaseArg, $typeName); } - - /** - * @return void - */ - public function build(): void - { - $this->classFile->writeFile(); - } -} \ No newline at end of file +} diff --git a/src/SchemaGenerator/CodeGenerator/InterfaceObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/InterfaceObjectClassBuilder.php new file mode 100644 index 0000000..7c272b4 --- /dev/null +++ b/src/SchemaGenerator/CodeGenerator/InterfaceObjectClassBuilder.php @@ -0,0 +1,66 @@ +classFile = new InterfaceFile($writeDir, $className, $namespace); + + // Special case for handling root query object + if ($objectName === QueryObject::ROOT_QUERY_OBJECT_NAME) { + $objectName = ''; + } + + $this->classFile->addConstant('OBJECT_NAME', $objectName); + } + + protected function addScalarSetter($propertyName, $upperCamelName) + { + $lowerCamelName = lcfirst($upperCamelName); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($lowerCamelName); + } + + protected function addListSetter(string $propertyName, string $upperCamelName, string $propertyType) + { + $lowerCamelName = lcfirst($upperCamelName); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($lowerCamelName)->setType($propertyType); + } + + protected function addObjectSetter(string $propertyName, string $upperCamelName, string $objectClass) + { + $lowerCamelName = lcfirst(str_replace('_', '', $objectClass)); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($lowerCamelName)->setType($objectClass); + } + + protected function addEnumSetter(string $propertyName, string $upperCamelName, string $objectClass) + { + $lowerCamelName = lcfirst(str_replace('_', '', $objectClass)); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($lowerCamelName)->setType($objectClass); + } + + protected function addSimpleSelector(string $propertyName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) + { + $this->classFile->addMethod("select$upperCamelName", $isDeprecated, $deprecationReason); + } + + protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, FieldTypeKindEnum $fieldTypeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) + { + $method = $this->classFile->addMethod("select$upperCamelName", $isDeprecated, $deprecationReason); + $method->addParameter('argsObject')->setType($argsObjectName)->setDefaultValue(null); + } +} diff --git a/src/SchemaGenerator/CodeGenerator/ObjectBuilderInterface.php b/src/SchemaGenerator/CodeGenerator/ObjectBuilderInterface.php index f6e8d57..0be59c3 100644 --- a/src/SchemaGenerator/CodeGenerator/ObjectBuilderInterface.php +++ b/src/SchemaGenerator/CodeGenerator/ObjectBuilderInterface.php @@ -1,18 +1,15 @@ classFile->addProperty($propertyName); + $this->classFile->addProperty($propertyName, $defaultValue, $propertyType); } /** @@ -31,64 +21,31 @@ protected function addProperty($propertyName) protected function addScalarSetter($propertyName, $upperCamelName) { $lowerCamelName = lcfirst($upperCamelName); - $method = "public function set$upperCamelName($$lowerCamelName) -{ - \$this->$propertyName = $$lowerCamelName; - - return \$this; -}"; - $this->classFile->addMethod($method); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($lowerCamelName); + $method->addBody('$this->? = $?;', [$propertyName, $lowerCamelName]); + $method->addBody('return $this;'); } - /** - * @param string $propertyName - * @param string $upperCamelName - * @param string $propertyType - */ protected function addListSetter(string $propertyName, string $upperCamelName, string $propertyType) { $lowerCamelName = lcfirst($upperCamelName); - $method = "public function set$upperCamelName(array $$lowerCamelName) -{ - \$this->$propertyName = $$lowerCamelName; - - return \$this; -}"; - $this->classFile->addMethod($method); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($lowerCamelName)->setType('array'); + $method->addBody('$this->? = $?;', [$propertyName, $lowerCamelName]); + $method->addBody('return $this;'); } - /** - * @param string $propertyName - * @param string $upperCamelName - * @param string $objectClass - */ protected function addEnumSetter(string $propertyName, string $upperCamelName, string $objectClass) { - $lowerCamelName = lcfirst(str_replace('_', '', $objectClass)); - $method = "public function set$upperCamelName($$lowerCamelName) -{ - \$this->$propertyName = new RawObject($$lowerCamelName); - - return \$this; -}"; - $this->classFile->addMethod($method); - $this->classFile->addImport('GraphQL\\RawObject'); + $this->addObjectSetter($propertyName, $upperCamelName, $objectClass); } - /** - * @param string $propertyName - * @param string $upperCamelName - * @param string $objectClass - */ protected function addObjectSetter(string $propertyName, string $upperCamelName, string $objectClass) { - $lowerCamelName = lcfirst(str_replace('_', '', $objectClass)); - $method = "public function set$upperCamelName($objectClass $$lowerCamelName) -{ - \$this->$propertyName = $$lowerCamelName; - - return \$this; -}"; - $this->classFile->addMethod($method); + $method = $this->classFile->addMethod("set$upperCamelName"); + $method->addParameter($propertyName)->setType($objectClass); + $method->addBody('$this->? = $?;', [$propertyName, $propertyName]); + $method->addBody('return $this;'); } -} \ No newline at end of file +} diff --git a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php index a8f4d03..c5cab1b 100644 --- a/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php @@ -1,36 +1,35 @@ classFile = new ClassFile($writeDir, $className, $namespace); + $this->classFile->extendsClass(QueryObject::class); - $this->classFile = new ClassFile($writeDir, $className); - $this->classFile->setNamespace($namespace); - if ($namespace !== self::DEFAULT_NAMESPACE) { - $this->classFile->addImport('GraphQL\\SchemaObject\\QueryObject'); + if ($interfaces) { + foreach ($interfaces as $interface) { + $this->classFile->implementsInterface($interface); + } } - $this->classFile->extendsClass('QueryObject'); // Special case for handling root query object if ($objectName === QueryObject::ROOT_QUERY_OBJECT_NAME) { @@ -39,76 +38,38 @@ public function __construct(string $writeDir, string $objectName, string $namesp $this->classFile->addConstant('OBJECT_NAME', $objectName); } - /** - * @param string $fieldName - */ public function addScalarField(string $fieldName, bool $isDeprecated, ?string $deprecationReason) { $upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); $this->addSimpleSelector($fieldName, $upperCamelCaseProp, $isDeprecated, $deprecationReason); } - /** - * @param string $fieldName - * @param string $typeName - * @param string $typeKind - * @param string $argsObjectName - * @param bool $isDeprecated - * @param string|null $deprecationReason - */ - public function addObjectField(string $fieldName, string $typeName, string $typeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) + public function addObjectField(string $fieldName, string $typeName, FieldTypeKindEnum $typeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) { $upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName); $this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $typeKind, $argsObjectName, $isDeprecated, $deprecationReason); } - /** - * @param string $propertyName - * @param string $upperCamelName - * @param bool $isDeprecated - * @param string|null $deprecationReason - */ protected function addSimpleSelector(string $propertyName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason) { - $method = "public function select$upperCamelName() -{ - \$this->selectField(\"$propertyName\"); - - return \$this; -}"; - $this->classFile->addMethod($method, $isDeprecated, $deprecationReason); + $method = $this->classFile->addMethod("select$upperCamelName", $isDeprecated, $deprecationReason); + $method->addBody('$this->selectField(?);', [$propertyName]); + $method->addBody('return $this;'); } - /** - * @param string $fieldName - * @param string $upperCamelName - * @param string $fieldTypeName - * @param string $fieldTypeKind - * @param string $argsObjectName - * @param bool $isDeprecated - * @param string|null $deprecationReason - */ - protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $fieldTypeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) + protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, FieldTypeKindEnum $fieldTypeKind, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason) { - $objectClass = $fieldTypeName . ($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); - $method = "public function select$upperCamelName($argsObjectName \$argsObject = null) -{ - \$object = new $objectClass(\"$fieldName\"); - if (\$argsObject !== null) { - \$object->appendArguments(\$argsObject->toArray()); - } - \$this->selectField(\$object); + $objectClass = $fieldTypeName.($fieldTypeKind === FieldTypeKindEnum::UNION_OBJECT ? 'UnionObject' : 'QueryObject'); - return \$object; -}"; - $this->classFile->addMethod($method, $isDeprecated, $deprecationReason); - } - - /** - * This method builds the class and writes it to the file system - */ - public function build(): void - { - $this->classFile->writeFile(); + $method = $this->classFile->addMethod("select$upperCamelName", $isDeprecated, $deprecationReason); + $method->setParameters([ + (new Parameter('argsObject'))->setType($argsObjectName)->setDefaultValue(null), + ]); + $method->addBody('$object = new '.$objectClass.'(?);', [$fieldName]); + $method->addBody('if ($argsObject !== null) {'); + $method->addBody(' $object->appendArguments($argsObject->toArray());'); + $method->addBody('}'); + $method->addBody('$this->selectField($object);'); + $method->addBody('return $object;'); } } diff --git a/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php b/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php index 750a9d7..d22f7b4 100644 --- a/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php +++ b/src/SchemaGenerator/CodeGenerator/UnionObjectBuilder.php @@ -1,63 +1,36 @@ classFile = new ClassFile($writeDir, $className); - $this->classFile->setNamespace($namespace); - if ($namespace !== self::DEFAULT_NAMESPACE) { - $this->classFile->addImport('GraphQL\\SchemaObject\\UnionObject'); - } - $this->classFile->extendsClass('UnionObject'); + $this->classFile = new ClassFile($writeDir, $className, $namespace); + $this->classFile->extendsClass(UnionObject::class); } - /** - * @param string $typeName - */ - public function addPossibleType(string $typeName) + public function addPossibleType(string $typeName): void { $upperCamelCaseTypeName = StringLiteralFormatter::formatUpperCamelCase($typeName); - $objectClassName = $typeName . 'QueryObject'; - $method = "public function on$upperCamelCaseTypeName() -{ - \$object = new $objectClassName(); - \$this->addPossibleType(\$object); - - return \$object; -}"; - $this->classFile->addMethod($method); - } - - /** - * @return void - */ - public function build(): void - { - $this->classFile->writeFile(); + $objectClassName = $typeName.'QueryObject'; + $method = $this->classFile->addMethod("on$upperCamelCaseTypeName"); + $method->addBody('$object = new '.$objectClassName.'();'); + $method->addBody('$this->addPossibleType($object);'); + $method->addBody('return $object;'); } } diff --git a/src/SchemaGenerator/SchemaClassGenerator.php b/src/SchemaGenerator/SchemaClassGenerator.php index e4920ad..38e57ca 100644 --- a/src/SchemaGenerator/SchemaClassGenerator.php +++ b/src/SchemaGenerator/SchemaClassGenerator.php @@ -1,5 +1,7 @@ true - *AND complete covering the schema scanner class + *AND complete covering the schema scanner class. + * * @var array */ - private $generatedObjects; + private $generatedObjects; /** * SchemaClassGenerator constructor. - * - * @param Client $client - * @param string $writeDir - * @param string $namespace */ - public function __construct(Client $client, string $writeDir = '', string $namespace = ObjectBuilderInterface::DEFAULT_NAMESPACE) + public function __construct(Client $client, string $writeDir = '', string $namespace = ObjectBuilderInterface::DEFAULT_NAMESPACE) { - $this->schemaInspector = new SchemaInspector($client); - $this->generatedObjects = []; - $this->writeDir = $writeDir; + $this->schemaInspector = new SchemaInspector($client); + $this->generatedObjects = []; + $this->writeDir = $writeDir; $this->generationNamespace = $namespace; $this->setWriteDir(); } - /** - * @return bool - */ - public function generateRootQueryObject(): bool - { - $objectArray = $this->schemaInspector->getQueryTypeSchema(); + public function generateRootQueryObject(): bool + { + $objectArray = $this->schemaInspector->getQueryTypeSchema(); $rootObjectName = QueryObject::ROOT_QUERY_OBJECT_NAME; - $queryTypeName = $objectArray['name']; - //$rootObjectDescr = $objectArray['description']; + $queryTypeName = $objectArray['name']; + // $rootObjectDescr = $objectArray['description']; if (array_key_exists($queryTypeName, $this->generatedObjects)) { return true; @@ -86,21 +81,20 @@ public function generateRootQueryObject(): bool } /** - * This method receives the array of object fields as an input and adds the fields to the query object building - * - * @param QueryObjectClassBuilder $queryObjectBuilder - * @param string $currentTypeName - * @param array $fieldsArray + * This method receives the array of object fields as an input and adds the fields to the query object building. */ - private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBuilder, string $currentTypeName, array $fieldsArray) + private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBuilder, string $currentTypeName, array $fieldsArray): void { foreach ($fieldsArray as $fieldArray) { $name = $fieldArray['name']; // Skip fields with name "query" - if ($name === 'query') continue; + if ($name === 'query') { + continue; + } - //$description = $fieldArray['description']; + // $description = $fieldArray['description']; [$typeName, $typeKind] = $this->getTypeInfo($fieldArray); + $typeKind = FieldTypeKindEnum::from($typeKind); if ($typeKind === FieldTypeKindEnum::SCALAR) { $queryObjectBuilder->addScalarField($name, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']); @@ -108,52 +102,33 @@ private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBui $this->generateEnumObject($typeName); $queryObjectBuilder->addScalarField($name, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']); } else { - // Generate nested type object if it wasn't generated $objectGenerated = $this->generateObject($typeName, $typeKind); if ($objectGenerated) { - // Generate nested type arguments object if it wasn't generated - $argsObjectName = $currentTypeName . StringLiteralFormatter::formatUpperCamelCase($name) . 'ArgumentsObject'; + $argsObjectName = $currentTypeName.StringLiteralFormatter::formatUpperCamelCase($name).'ArgumentsObject'; $argsObjectGenerated = $this->generateArgumentsObject($argsObjectName, $fieldArray['args'] ?? []); if ($argsObjectGenerated) { - // Add sub type as a field to the query object if all generation happened successfully - $queryObjectBuilder->addObjectField($name, $typeName, $typeKind, $argsObjectName, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']); + $queryObjectBuilder->addObjectField($name, $typeName, $typeKind, '\\'.$this->generationNamespace.'\\'.$argsObjectName, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']); } } } } } - /** - * @param string $objectName - * @param string $objectKind - * - * @return bool - */ - protected function generateObject(string $objectName, string $objectKind): bool + protected function generateObject(string $objectName, FieldTypeKindEnum $objectKind): bool { - switch ($objectKind) { - case FieldTypeKindEnum::OBJECT: - return $this->generateQueryObject($objectName); - case FieldTypeKindEnum::INPUT_OBJECT: - return $this->generateInputObject($objectName); - case FieldTypeKindEnum::ENUM_OBJECT: - return $this->generateEnumObject($objectName); - case FieldTypeKindEnum::UNION_OBJECT: - return $this->generateUnionObject($objectName); - default: - print "Couldn't generate type $objectName: generating $objectKind kind is not supported yet" . PHP_EOL; - return false; - } + return match ($objectKind) { + FieldTypeKindEnum::OBJECT => $this->generateQueryObject($objectName), + FieldTypeKindEnum::INPUT_OBJECT => $this->generateInputObject($objectName), + FieldTypeKindEnum::ENUM_OBJECT => $this->generateEnumObject($objectName), + FieldTypeKindEnum::UNION_OBJECT => $this->generateUnionObject($objectName), + FieldTypeKindEnum::INTERFACE_OBJECT => $this->generateInterfaceObject($objectName), + default => false, + }; } - /** - * @param string $objectName - * - * @return bool - */ protected function generateQueryObject(string $objectName): bool { if (array_key_exists($objectName, $this->generatedObjects)) { @@ -161,9 +136,27 @@ protected function generateQueryObject(string $objectName): bool } $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getObjectSchema($objectName); - $objectName = $objectArray['name']; - $objectBuilder = new QueryObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); + $objectArray = $this->schemaInspector->getObjectSchema($objectName); + $objectName = $objectArray['name']; + $implements = $objectArray['implements'] ?? []; + $objectBuilder = new QueryObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace, $implements); + + $this->appendQueryObjectFields($objectBuilder, $objectName, $objectArray['fields']); + $objectBuilder->build(); + + return true; + } + + protected function generateInterfaceObject(string $objectName) + { + if (array_key_exists($objectName, $this->generatedObjects)) { + return true; + } + + $this->generatedObjects[$objectName] = true; + $objectArray = $this->schemaInspector->getObjectSchema($objectName); + $objectName = $objectArray['name']; + $objectBuilder = new InterfaceObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); $this->appendQueryObjectFields($objectBuilder, $objectName, $objectArray['fields']); $objectBuilder->build(); @@ -171,11 +164,6 @@ protected function generateQueryObject(string $objectName): bool return true; } - /** - * @param string $objectName - * - * @return bool - */ protected function generateInputObject(string $objectName): bool { if (array_key_exists($objectName, $this->generatedObjects)) { @@ -183,30 +171,35 @@ protected function generateInputObject(string $objectName): bool } $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getInputObjectSchema($objectName); - $objectName = $objectArray['name']; - $objectBuilder = new InputObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace); + $objectArray = $this->schemaInspector->getInputObjectSchema($objectName); + $objectName = $objectArray['name']; + $implements = $objectArray['implements'] ?? []; + $objectBuilder = new InputObjectClassBuilder($this->writeDir, $objectName, $this->generationNamespace, $implements); foreach ($objectArray['inputFields'] as $inputFieldArray) { $name = $inputFieldArray['name']; - //$description = $inputFieldArray['description']; - //$defaultValue = $inputFieldArray['defaultValue']; + // $description = $inputFieldArray['description']; + // $defaultValue = $inputFieldArray['defaultValue']; [$typeName, $typeKind, $typeKindWrappers] = $this->getTypeInfo($inputFieldArray); + $typeKind = FieldTypeKindEnum::from($typeKind); + $objectGenerated = true; if ($typeKind !== FieldTypeKindEnum::SCALAR) { $objectGenerated = $this->generateObject($typeName, $typeKind); } if ($objectGenerated) { - if (in_array(FieldTypeKindEnum::LIST, $typeKindWrappers)) { + if (in_array(FieldTypeKindEnum::LIST, $typeKindWrappers, true)) { $objectBuilder->addListValue($name, $typeName); + } elseif ($typeKind === FieldTypeKindEnum::SCALAR) { + $objectBuilder->addScalarValue($name); + } elseif ($typeKind === FieldTypeKindEnum::ENUM_OBJECT) { + $typeName = $this->generationNamespace.'\\'.$typeName.'EnumObject'; + $objectBuilder->addInputObjectValue($name, $typeName); } else { - if ($typeKind === FieldTypeKindEnum::SCALAR || $typeKind === FieldTypeKindEnum::ENUM_OBJECT) { - $objectBuilder->addScalarValue($name); - } else { - $objectBuilder->addInputObjectValue($name, $typeName); - } + $typeName = $this->generationNamespace.'\\'.$typeName; + $objectBuilder->addInputObjectValue($name, $typeName); } } } @@ -216,11 +209,6 @@ protected function generateInputObject(string $objectName): bool return true; } - /** - * @param string $objectName - * - * @return bool - */ protected function generateEnumObject(string $objectName): bool { if (array_key_exists($objectName, $this->generatedObjects)) { @@ -229,13 +217,13 @@ protected function generateEnumObject(string $objectName): bool $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getEnumObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getEnumObjectSchema($objectName); + $objectName = $objectArray['name']; $objectBuilder = new EnumObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); foreach ($objectArray['enumValues'] as $enumValue) { - $name = $enumValue['name']; - //$description = $enumValue['description']; + $name = $enumValue['name']; + // $description = $enumValue['description']; $objectBuilder->addEnumValue($name); } $objectBuilder->build(); @@ -243,11 +231,6 @@ protected function generateEnumObject(string $objectName): bool return true; } - /** - * @param string $objectName - * - * @return bool - */ protected function generateUnionObject(string $objectName): bool { if (array_key_exists($objectName, $this->generatedObjects)) { @@ -256,12 +239,15 @@ protected function generateUnionObject(string $objectName): bool $this->generatedObjects[$objectName] = true; - $objectArray = $this->schemaInspector->getUnionObjectSchema($objectName); - $objectName = $objectArray['name']; + $objectArray = $this->schemaInspector->getUnionObjectSchema($objectName); + $objectName = $objectArray['name']; $objectBuilder = new UnionObjectBuilder($this->writeDir, $objectName, $this->generationNamespace); foreach ($objectArray['possibleTypes'] as $possibleType) { - $this->generateObject($possibleType['name'], $possibleType['kind']); + $objectKind = $possibleType['kind']; + $objectKind = FieldTypeKindEnum::from($objectKind); + + $this->generateObject($possibleType['name'], $objectKind); $objectBuilder->addPossibleType($possibleType['name']); } $objectBuilder->build(); @@ -269,12 +255,6 @@ protected function generateUnionObject(string $objectName): bool return true; } - /** - * @param string $argsObjectName - * @param array $arguments - * - * @return bool - */ protected function generateArgumentsObject(string $argsObjectName, array $arguments): bool { if (array_key_exists($argsObjectName, $this->generatedObjects)) { @@ -287,9 +267,10 @@ protected function generateArgumentsObject(string $argsObjectName, array $argume foreach ($arguments as $argumentArray) { $name = $argumentArray['name']; - //$description = $inputFieldArray['description']; - //$defaultValue = $inputFieldArray['defaultValue']; + // $description = $inputFieldArray['description']; + // $defaultValue = $inputFieldArray['defaultValue']; [$typeName, $typeKind, $typeKindWrappers] = $this->getTypeInfo($argumentArray); + $typeKind = $typeKind instanceof FieldTypeKindEnum ? $typeKind : FieldTypeKindEnum::from($typeKind); $objectGenerated = true; if ($typeKind !== FieldTypeKindEnum::SCALAR) { @@ -297,16 +278,16 @@ protected function generateArgumentsObject(string $argsObjectName, array $argume } if ($objectGenerated) { - if (in_array(FieldTypeKindEnum::LIST, $typeKindWrappers)) { + if (in_array(FieldTypeKindEnum::LIST, $typeKindWrappers, true)) { $objectBuilder->addListArgument($name, $typeName); + } elseif ($typeKind === FieldTypeKindEnum::SCALAR) { + $objectBuilder->addScalarArgument($name, $typeName); + } elseif ($typeKind === FieldTypeKindEnum::ENUM_OBJECT) { + $typeName = '\\'.$this->generationNamespace.'\\'.$typeName; + $objectBuilder->addInputEnumArgument($name, $typeName); } else { - if ($typeKind === FieldTypeKindEnum::SCALAR) { - $objectBuilder->addScalarArgument($name); - } elseif ($typeKind === FieldTypeKindEnum::ENUM_OBJECT) { - $objectBuilder->addInputEnumArgument($name, $typeName); - } else { - $objectBuilder->addInputObjectArgument($name, $typeName); - } + $typeName = '\\'.$this->generationNamespace.'\\'.$typeName; + $objectBuilder->addInputObjectArgument($name, $typeName); } } } @@ -325,7 +306,7 @@ protected function getTypeInfo(array $dataArray): array $typeArray = $dataArray['type']; $typeWrappers = []; while ($typeArray['ofType'] !== null) { - $typeWrappers[] = $typeArray['kind']; + $typeWrappers[] = $typeArray['kind'] instanceof FieldTypeKindEnum ? $typeArray['kind'] : FieldTypeKindEnum::from($typeArray['kind']); $typeArray = $typeArray['ofType']; // Throw exception if next array doesn't have ofType key @@ -333,29 +314,27 @@ protected function getTypeInfo(array $dataArray): array throw new RuntimeException('Reached the limit of nesting in type info'); } } - $typeInfo = [$typeArray['name'], $typeArray['kind'], $typeWrappers]; - return $typeInfo; + return [$typeArray['name'], $typeArray['kind'], $typeWrappers]; } /** - * Sets the write directory if it's not set for the class + * Sets the write directory if it's not set for the class. */ - private function setWriteDir(): void + private function setWriteDir(): void { - if ($this->writeDir !== '') return; + if ($this->writeDir !== '') { + return; + } $currentDir = dirname(__FILE__); while (basename($currentDir) !== 'php-graphql-oqm') { $currentDir = dirname($currentDir); } - $this->writeDir = $currentDir . '/schema_object'; + $this->writeDir = $currentDir.'/schema_object'; } - /** - * @return string - */ public function getWriteDir(): string { $this->setWriteDir(); diff --git a/src/SchemaGenerator/SchemaInspector.php b/src/SchemaGenerator/SchemaInspector.php index 424ccc7..7546b17 100644 --- a/src/SchemaGenerator/SchemaInspector.php +++ b/src/SchemaGenerator/SchemaInspector.php @@ -1,15 +1,15 @@ client = $client; } - /** - * @return array - */ public function getQueryTypeSchema(): array { - $schemaQuery = "{ - __schema{ - queryType{ + $schemaQuery = ' +query IntrospectionQuery { + __schema { + queryType { + ...FullType + } + mutationType { + ...FullType + } + subscriptionType { + ...FullType + } + types { + ...FullType + } + directives { name - kind description - fields(includeDeprecated: true){ + locations + args { + ...InputValue + } + } + } +} +fragment FullType on __Type { + kind + name + description + fields(includeDeprecated: true) { + name + description + args { + ...InputValue + } + type { + ...TypeRef + } + isDeprecated + deprecationReason + } + inputFields { + ...InputValue + } + interfaces { + ...TypeRef + } + enumValues(includeDeprecated: true) { + name + description + isDeprecated + deprecationReason + } + possibleTypes { + ...TypeRef + } +} + +fragment InputValue on __InputValue { + name + description + type { + ...TypeRef + } + defaultValue +} + +fragment TypeRef on __Type { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind name - description - isDeprecated - deprecationReason - " . static::TYPE_SUB_QUERY . " - args{ + ofType { + kind name - description - defaultValue - " . static::TYPE_SUB_QUERY . " + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + } + } + } } } } } -}"; +}'; $response = $this->client->runRawQuery($schemaQuery, true); return $response->getData()['__schema']['queryType']; } - /** - * @param string $objectName - * - * @return array - */ public function getObjectSchema(string $objectName): array { $schemaQuery = "{ @@ -101,26 +167,21 @@ public function getObjectSchema(string $objectName): array description isDeprecated deprecationReason - " . static::TYPE_SUB_QUERY . " + ".static::TYPE_SUB_QUERY.' args{ name description defaultValue - " . static::TYPE_SUB_QUERY . " + '.static::TYPE_SUB_QUERY.' } } } -}"; +}'; $response = $this->client->runRawQuery($schemaQuery, true); return $response->getData()['__type']; } - /** - * @param string $objectName - * - * @return array - */ public function getInputObjectSchema(string $objectName): array { $schemaQuery = "{ @@ -131,20 +192,15 @@ public function getInputObjectSchema(string $objectName): array name description defaultValue - " . static::TYPE_SUB_QUERY . " + ".static::TYPE_SUB_QUERY.' } } -}"; +}'; $response = $this->client->runRawQuery($schemaQuery, true); return $response->getData()['__type']; } - /** - * @param string $objectName - * - * @return array - */ public function getEnumObjectSchema(string $objectName): array { $schemaQuery = "{ @@ -162,11 +218,6 @@ enumValues { return $response->getData()['__type']; } - /** - * @param string $objectName - * - * @return array - */ public function getUnionObjectSchema(string $objectName): array { $schemaQuery = "{ diff --git a/src/SchemaObject/ArgumentsObject.php b/src/SchemaObject/ArgumentsObject.php index 553d8db..fc64f11 100644 --- a/src/SchemaObject/ArgumentsObject.php +++ b/src/SchemaObject/ArgumentsObject.php @@ -1,17 +1,14 @@ $value) { - if (empty($value)) continue; + if (empty($value)) { + continue; + } // Append space at the beginning if it's not the first item on the list if ($first) { @@ -40,18 +42,15 @@ public function __toString() $value = StringLiteralFormatter::formatArrayForGQLQuery($value); } // TODO: Handle cases where a non-string-convertible object is added to the arguments - $objectString .= $name . ': ' . $value; + $objectString .= $name.': '.$value; } $objectString .= '}'; return $objectString; } - /** - * @return RawObject - */ public function toRawObject(): RawObject { return new RawObject((string) $this); } -} \ No newline at end of file +} diff --git a/src/SchemaObject/QueryObject.php b/src/SchemaObject/QueryObject.php index 42b54b4..391d582 100644 --- a/src/SchemaObject/QueryObject.php +++ b/src/SchemaObject/QueryObject.php @@ -1,28 +1,28 @@ fileName = 'EmptyCodeFile'; @@ -39,7 +36,7 @@ protected function setUp(): void AbstractCodeFile::class, [static::getGeneratedFilesDir(), $this->fileName] ); - $this->codeFile->method('generateFileContents')->willReturn("codeFile->method('generateFileContents')->willReturn('expectException(\Exception::class); $mock = $this->getMockForAbstractClass( AbstractCodeFile::class, - [static::getGeneratedFilesDir() . '/invalid', $this->fileName] + [static::getGeneratedFilesDir().'/invalid', $this->fileName] ); $mock->method('generateFileContents')->willReturn("expectException(\Exception::class); - $this->codeFile->changeWriteDir(static::getGeneratedFilesDir() . '/invalid'); + $this->codeFile->changeWriteDir(static::getGeneratedFilesDir().'/invalid'); } /** @@ -98,7 +95,7 @@ public function testInvalidWriteDir() */ public function testUnwritableDir() { - $testDir = static::getGeneratedFilesDir() . '/unwritable'; + $testDir = static::getGeneratedFilesDir().'/unwritable'; mkdir($testDir); chmod($testDir, 0444); @@ -111,7 +108,7 @@ public function testUnwritableDir() */ public function testWritePathGetter() { - $this->assertEquals(static::getGeneratedFilesDir() . "/$this->fileName.php", $this->codeFile->getWritePath()); + $this->assertEquals(static::getGeneratedFilesDir()."/$this->fileName.php", $this->codeFile->getWritePath()); } /** @@ -124,7 +121,7 @@ public function testWritePathGetter() public function testFileWritingWorks() { $this->codeFile->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$this->fileName.php", $this->codeFile->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$this->fileName.php", $this->codeFile->getWritePath()); } /** @@ -137,10 +134,10 @@ public function testFileWritingWorks() public function testFileWritingWorksWithTrailingSlash() { $this->fileName = 'EmptyCodeFileWithSlash'; - $this->codeFile->changeWriteDir($this->codeFile->getWriteDir() . '/'); + $this->codeFile->changeWriteDir($this->codeFile->getWriteDir().'/'); $this->codeFile->changeFileName($this->fileName); $this->codeFile->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$this->fileName.php", $this->codeFile->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$this->fileName.php", $this->codeFile->getWritePath()); } } diff --git a/tests/ArgumentsObjectClassBuilderTest.php b/tests/ArgumentsObjectClassBuilderTest.php index 99edc3d..f3eb8e5 100644 --- a/tests/ArgumentsObjectClassBuilderTest.php +++ b/tests/ArgumentsObjectClassBuilderTest.php @@ -1,14 +1,13 @@ addScalarArgument('scalarProperty'); + $classBuilder->addScalarArgument('scalarProperty', 'string'); $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -55,13 +54,13 @@ public function testAddMultipleScalarArguments() { $objectName = 'WithMultipleScalarArgsArgumentsObject'; $classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE); - $classBuilder->addScalarArgument('scalarProperty'); - $classBuilder->addScalarArgument('another_scalar_property'); + $classBuilder->addScalarArgument('scalarProperty', 'string'); + $classBuilder->addScalarArgument('another_scalar_property', 'string'); $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -81,8 +80,8 @@ public function testAddListArgument() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -103,8 +102,8 @@ public function testAddMultipleListArguments() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -124,8 +123,8 @@ public function testAddInputObjectArgument() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -141,13 +140,13 @@ public function testAddMultipleInputObjectArguments() { $objectName = 'WithMultipleInputObjectArgsArgumentsObject'; $classBuilder = new ArgumentsObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE); - $classBuilder->addInputObjectArgument('objectProperty', 'Some'); - $classBuilder->addInputObjectArgument('another_object_property', 'Another'); + $classBuilder->addInputObjectArgument('objectProperty', static::TESTING_NAMESPACE . '\\Some'); + $classBuilder->addInputObjectArgument('another_object_property', static::TESTING_NAMESPACE . '\\Another'); $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } -} \ No newline at end of file +} diff --git a/tests/ArgumentsObjectTest.php b/tests/ArgumentsObjectTest.php index 5856935..beec26e 100644 --- a/tests/ArgumentsObjectTest.php +++ b/tests/ArgumentsObjectTest.php @@ -1,5 +1,7 @@ toArray(); + $array = (new class() extends ArgumentsObject {})->toArray(); $this->assertIsArray($array); $this->assertEmpty($array); } @@ -31,7 +31,7 @@ public function testGetArray() $argumentsObj = (new SimpleArgumentsObject())->setStringProperty('string'); $this->assertEquals( [ - 'stringProperty' => 'string', + 'stringProperty' => 'string', ], $argumentsObj->toArray() ); @@ -39,7 +39,7 @@ public function testGetArray() $argumentsObj->setIntegerProperty(5); $this->assertEquals( [ - 'stringProperty' => 'string', + 'stringProperty' => 'string', 'integerProperty' => 5, ], $argumentsObj->toArray() @@ -48,9 +48,9 @@ public function testGetArray() $argumentsObj->setArrayProperty([1, 2, 3]); $this->assertEquals( [ - 'stringProperty' => 'string', + 'stringProperty' => 'string', 'integerProperty' => 5, - 'arrayProperty' => [1, 2, 3] + 'arrayProperty' => [1, 2, 3], ], $argumentsObj->toArray() ); @@ -59,10 +59,10 @@ public function testGetArray() $argumentsObj->setObjectProperty($filter); $this->assertEquals( [ - 'stringProperty' => 'string', + 'stringProperty' => 'string', 'integerProperty' => 5, - 'arrayProperty' => [1, 2, 3], - 'objectProperty' => $filter + 'arrayProperty' => [1, 2, 3], + 'objectProperty' => $filter, ], $argumentsObj->toArray() ); @@ -79,24 +79,28 @@ class SimpleArgumentsObject extends ArgumentsObject public function setStringProperty(string $stringValue) { $this->stringProperty = $stringValue; + return $this; } public function setIntegerProperty(int $integerValue) { $this->integerProperty = $integerValue; + return $this; } public function setArrayProperty(array $arrayValue) { $this->arrayProperty = $arrayValue; + return $this; } public function setObjectProperty(FilterInputObject $filterInputObject) { $this->objectProperty = $filterInputObject; + return $this; } } @@ -108,6 +112,7 @@ class FilterInputObject extends InputObject public function setIds($ids) { $this->ids = $ids; + return $this; } -} \ No newline at end of file +} diff --git a/tests/ClassFileTest.php b/tests/ClassFileTest.php index da624dc..5bfcf07 100644 --- a/tests/ClassFileTest.php +++ b/tests/ClassFileTest.php @@ -1,17 +1,21 @@ writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); return $class; } @@ -37,7 +41,7 @@ public function testEmptyClass() * @depends testEmptyClass * * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::extendsClass - * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::generateClassName + * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::getClassName */ public function testExtendsClass() { @@ -46,12 +50,10 @@ public function testExtendsClass() $class->extendsClass('Base'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); } /** - * @param ClassFile $class - * * @depends clone testEmptyClass */ public function testExtendsEmptyClassName(ClassFile $class) @@ -59,8 +61,8 @@ public function testExtendsEmptyClassName(ClassFile $class) $class->extendsClass(''); $class->writeFile(); - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $fileName = $class->getClassName(); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); } /** @@ -69,7 +71,7 @@ public function testExtendsEmptyClassName(ClassFile $class) * @depends testEmptyClass * * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::implementsInterface - * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::generateClassName + * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::getClassName */ public function testImplementsInterfaces() { @@ -78,46 +80,29 @@ public function testImplementsInterfaces() $class->implementsInterface('InterfaceOne'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); $fileName = 'ClassImplementsMultipleInterfaces'; - $class->changeFileName($fileName); + $class = new ClassFile(static::getGeneratedFilesDir(), $fileName); + $class->implementsInterface('InterfaceOne'); $class->implementsInterface('InterfaceTwo'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); return $class; } /** - * @param ClassFile $class - * - * @depends clone testImplementsInterfaces - * - * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::generateClassName - */ - public function testImplementDuplicateInterfaces(ClassFile $class) - { - $class->implementsInterface('InterfaceOne'); - $class->writeFile(); - - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); - } - - /** - * @param ClassFile $class - * * @depends clone testEmptyClass */ public function testImplementEmptyInterfaceName(ClassFile $class) { + $this->expectException(\Nette\InvalidArgumentException::class); + $this->expectExceptionMessage("Value '' is not valid class name"); + $class->implementsInterface(''); $class->writeFile(); - - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); } /** @@ -135,47 +120,43 @@ public function testUseTraits() $class->addTrait('TraitOne'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); $fileName = 'ClassWithMultipleTraits'; - $class->changeFileName($fileName); + $class = new ClassFile(static::getGeneratedFilesDir(), $fileName); + $class->addTrait('TraitOne'); $class->addTrait('TraitTwo'); $class->addTrait('TraitThree'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); return $class; } /** - * @param ClassFile $class - * * @depends clone testUseTraits * * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::generateTraits */ public function testUseDuplicateTraits(ClassFile $class) { + $this->expectException(\Nette\InvalidStateException::class); + $this->expectExceptionMessage("Cannot add trait 'TraitThree', because it already exists."); $class->addTrait('TraitThree'); $class->writeFile(); - - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); } /** - * @param ClassFile $class - * * @depends clone testEmptyClass */ public function testUseEmptyTraitName(ClassFile $class) { + $this->expectException(\Nette\InvalidArgumentException::class); + $this->expectExceptionMessage("Value '' is not valid trait name"); + $class->addTrait(''); $class->writeFile(); - - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); } /** @@ -194,10 +175,11 @@ public function testClassWithConstants() $class->addConstant('CONST_ONE', 'ONE'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); $fileName = 'ClassWithMultipleConstants'; - $class->changeFileName($fileName); + $class = new ClassFile(static::getGeneratedFilesDir(), $fileName); + $class->addConstant('CONST_ONE', 'ONE'); $class->addConstant('CONST_TWO', 2); $class->addConstant('CONST_THEE', true); $class->addConstant('CONST_FOUR', false); @@ -205,39 +187,34 @@ public function testClassWithConstants() $class->addConstant('CONST_SIX', 6.6); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); return $class; } /** - * @param ClassFile $class - * * @depends clone testClassWithConstants * * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\ClassFile::generateConstants */ public function testClassWithDuplicateConstants(ClassFile $class) { + $this->expectException(\Nette\InvalidStateException::class); + $this->expectExceptionMessage("Cannot add constant 'CONST_TWO', because it already exists."); $class->addConstant('CONST_TWO', 2); $class->writeFile(); - - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); } /** - * @param ClassFile $class - * * @depends clone testEmptyClass */ public function testConstantWithEmptyName(ClassFile $class) { + $this->expectException(\Nette\InvalidArgumentException::class); + $this->expectExceptionMessage("Value '' is not valid name."); + $class->addConstant('', null); $class->writeFile(); - - $fileName = $class->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); } /** @@ -261,9 +238,8 @@ public function testConstantWithEmptyName(ClassFile $class) public function testFullClass() { $fileName = 'ClassWithEverything'; - $class = new ClassFile(static::getGeneratedFilesDir(), $fileName); + $class = new ClassFile(static::getGeneratedFilesDir(), $fileName, 'GraphQl\\Test'); - $class->setNamespace('GraphQl\\Test'); $class->addImport('GraphQl\\Base\\Base'); $class->addImport('GraphQl\\Interfaces\\Intr1'); $class->addImport('GraphQl\\Interfaces\\Intr2'); @@ -281,14 +257,10 @@ public function testFullClass() $class->addProperty('propertyOne'); $class->addProperty('propertyTwo', ''); - $class->addMethod('public function dumpAll() { - print \'dumping\'; -}'); - $class->addMethod('protected function internalStuff($i) { - return ++$i; -}'); + $class->addMethod('dumpAll')->setVisibility(ClassLike::VisibilityPublic)->setBody("print 'dumping';"); + $class->addMethod('internalStuff')->setVisibility(ClassLike::VisibilityProtected)->setParameters([ new Parameter('i') ])->setBody('return ++$i;'); $class->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $class->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $class->getWritePath()); } -} \ No newline at end of file +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2c2c5b8..07b0235 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -1,5 +1,7 @@ mockHandler = new MockHandler(); $handler = HandlerStack::create($this->mockHandler); - $this->client = new Client('', [], ['handler' => $handler]); + $this->client = new Client('', [], ['handler' => $handler]); } /** @@ -46,10 +43,12 @@ protected function setUp(): void */ public function testConstructClient() { + $this->markTestSkipped('Does not belong to this lib'); + $mockHandler = new MockHandler(); - $handler = HandlerStack::create($mockHandler); - $container = []; - $history = Middleware::history($container); + $handler = HandlerStack::create($mockHandler); + $container = []; + $history = Middleware::history($container); $handler->push($history); $mockHandler->append(new Response(200)); @@ -86,9 +85,9 @@ public function testValidQueryResponse() 'data' => 'value', ], [ 'data' => 'value', - ] - ] - ] + ], + ], + ], ]))); $objectResults = $this->client->runRawQuery(''); @@ -107,9 +106,9 @@ public function testValidQueryResponseToArray() 'data' => 'value', ], [ 'data' => 'value', - ] - ] - ] + ], + ], + ], ]))); $arrayResults = $this->client->runRawQuery('', true); @@ -129,10 +128,10 @@ public function testInvalidQueryResponseWith200() [ 'line' => 1, 'column' => 3, - ] + ], ], - ] - ] + ], + ], ]))); $this->expectException(QueryError::class); @@ -153,10 +152,10 @@ public function testInvalidQueryResponseWith400() [ 'line' => 1, 'column' => 3, - ] + ], ], - ] - ] + ], + ], ])))); $this->expectException(QueryError::class); diff --git a/tests/CodeFileTestCase.php b/tests/CodeFileTestCase.php index d09cbdb..3848db8 100644 --- a/tests/CodeFileTestCase.php +++ b/tests/CodeFileTestCase.php @@ -1,5 +1,7 @@ build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -54,8 +55,8 @@ public function testAddValue() $enumBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -75,8 +76,8 @@ public function testAddMultipleValues() $enumBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } -} \ No newline at end of file +} diff --git a/tests/InputObjectClassBuilderTest.php b/tests/InputObjectClassBuilderTest.php index a9dc712..8688d07 100644 --- a/tests/InputObjectClassBuilderTest.php +++ b/tests/InputObjectClassBuilderTest.php @@ -1,11 +1,13 @@ build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -59,8 +61,8 @@ public function testAddMultipleScalarValues() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -81,8 +83,8 @@ public function testAddListValue() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -104,8 +106,8 @@ public function testAddMultipleListValues() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -126,8 +128,8 @@ public function testAddInputObjectValue() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -144,13 +146,13 @@ public function testAddMultipleInputObjectValues() $objectName = 'WithMultipleInputObjectValues'; $classBuilder = new InputObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE); $objectName .= 'InputObject'; - $classBuilder->addInputObjectValue('inputObject', 'WithListValue'); - $classBuilder->addInputObjectValue('inputObjectTwo', '_TestFilter'); + $classBuilder->addInputObjectValue('inputObject', static::TESTING_NAMESPACE . '\\WithListValue'); + $classBuilder->addInputObjectValue('inputObjectTwo', static::TESTING_NAMESPACE . '\\_TestFilter'); $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -170,8 +172,8 @@ public function testInputObjectIntegration() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } -} \ No newline at end of file +} diff --git a/tests/InputObjectTest.php b/tests/InputObjectTest.php index 72d6c62..7784124 100644 --- a/tests/InputObjectTest.php +++ b/tests/InputObjectTest.php @@ -1,5 +1,7 @@ inputObject = new _TestFilterInputObject(); @@ -59,4 +58,4 @@ public function testSetInputValues() $this->assertEquals('{first_name: "Mostafa", lastName: "Ghoneimy", ids: [2, 34, 567]}', (string) $this->inputObject); $this->assertEquals('{first_name: "Mostafa", lastName: "Ghoneimy", ids: [2, 34, 567]}', (string) $this->inputObject->toRawObject()); } -} \ No newline at end of file +} diff --git a/tests/QueryObjectClassBuilderTest.php b/tests/QueryObjectClassBuilderTest.php index b3eda79..0911aa5 100644 --- a/tests/QueryObjectClassBuilderTest.php +++ b/tests/QueryObjectClassBuilderTest.php @@ -1,5 +1,7 @@ build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -47,8 +49,8 @@ public function testBuildRootQueryObject() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -65,8 +67,8 @@ public function testAddSimpleSelector() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -87,8 +89,8 @@ public function testAddMultipleSimpleSelectors() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -105,8 +107,8 @@ public function testAddObjectSelector() $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -121,13 +123,13 @@ public function testAddMultipleObjectSelectors() $objectName = 'MultipleObjectSelectors'; $classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE); $objectName .= 'QueryObject'; - $classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', FieldTypeKindEnum::OBJECT, 'MultipleObjectSelectorsRightArgumentsObject', false, null); - $classBuilder->addObjectField('left_objects', 'Left', FieldTypeKindEnum::OBJECT, 'MultipleObjectSelectorsLeftObjectsArgumentsObject', true, null); + $classBuilder->addObjectField('right', 'MultipleObjectSelectorsRight', FieldTypeKindEnum::OBJECT, static::TESTING_NAMESPACE . '\\MultipleObjectSelectorsRightArgumentsObject', false, null); + $classBuilder->addObjectField('left_objects', 'Left', FieldTypeKindEnum::OBJECT, static::TESTING_NAMESPACE . '\\MultipleObjectSelectorsLeftObjectsArgumentsObject', true, null); $classBuilder->build(); $this->assertFileEquals( - static::getExpectedFilesDir() . "/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } } diff --git a/tests/QueryObjectTest.php b/tests/QueryObjectTest.php index a85f8df..05294ff 100644 --- a/tests/QueryObjectTest.php +++ b/tests/QueryObjectTest.php @@ -1,5 +1,7 @@ queryObject = new SimpleQueryObject('simples'); @@ -102,7 +99,7 @@ public function testSelectFields() */ public function testSelectSubFieldsWithArguments() { - $this->queryObject->selectSiblings((new SimpleSiblingsArgumentObject())->setFirst(5)->setIds([1,2]))->selectScalar(); + $this->queryObject->selectSiblings((new SimpleSiblingsArgumentObject())->setFirst(5)->setIds([1, 2]))->selectScalar(); $this->assertEquals( 'query { simples { @@ -119,11 +116,13 @@ public function testSelectSubFieldsWithArguments() ->selectSiblings( (new SimpleSiblingsArgumentObject()) ->setObject( - (new class extends InputObject { + (new class() extends InputObject { protected $field; - public function setField($field) { + public function setField($field) + { $this->field = $field; + return $this; } })->setField('something') @@ -145,7 +144,7 @@ public function setField($field) { class SimpleQueryObject extends QueryObject { - const OBJECT_NAME = 'Simple'; + public const OBJECT_NAME = 'Simple'; public function selectScalar() { @@ -182,18 +181,21 @@ class SimpleSiblingsArgumentObject extends ArgumentsObject public function setFirst($first) { $this->first = $first; + return $this; } public function setIds(array $ids) { $this->ids = $ids; + return $this; } public function setObject($obj) { $this->obj = $obj; + return $this; } } diff --git a/tests/SchemaClassGeneratorTest.php b/tests/SchemaClassGeneratorTest.php index bdd95bf..6947f63 100644 --- a/tests/SchemaClassGeneratorTest.php +++ b/tests/SchemaClassGeneratorTest.php @@ -1,5 +1,7 @@ mockHandler = new MockHandler(); @@ -63,7 +62,7 @@ public function testGetTypeInfo() 'name' => 'String', 'kind' => FieldTypeKindEnum::SCALAR, 'ofType' => null, - ] + ], ]; $typeInfo = $this->classGenerator->getTypeInfo($dataArray); @@ -71,7 +70,7 @@ public function testGetTypeInfo() [ 'String', FieldTypeKindEnum::SCALAR, - [] + [], ], $typeInfo ); @@ -92,10 +91,10 @@ public function testGetTypeInfoForMultiLevels() 'ofType' => [ 'name' => 'WrappedObject', 'kind' => FieldTypeKindEnum::OBJECT, - 'ofType' => null - ] - ] - ] + 'ofType' => null, + ], + ], + ], ]; $typeInfo = $this->classGenerator->getTypeInfo($dataArray); @@ -103,7 +102,7 @@ public function testGetTypeInfoForMultiLevels() [ 'WrappedObject', FieldTypeKindEnum::OBJECT, - [FieldTypeKindEnum::LIST, FieldTypeKindEnum::NON_NULL] + [FieldTypeKindEnum::LIST, FieldTypeKindEnum::NON_NULL], ], $typeInfo ); @@ -126,11 +125,11 @@ public function testCrossNestingLimitForGetTypeInfo() 'kind' => FieldTypeKindEnum::NON_NULL, 'ofType' => [ 'name' => 'WrappedObject', - 'kind' => 'OBJECT' - ] - ] - ] - ] + 'kind' => 'OBJECT', + ], + ], + ], + ], ]; $this->expectExceptionMessage('Reached the limit of nesting in type info'); @@ -160,16 +159,16 @@ public function testGenerateEnumObject() 'name' => 'oneMoreValue', 'description' => null, ], - ] - ] - ] + ], + ], + ], ]))); $this->classGenerator->generateEnumObject($objectName); $objectName .= 'EnumObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/enum_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/enum_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -208,16 +207,16 @@ public function testGenerateInputObjectWithScalarValues() 'ofType' => null, ], ], - ] - ] - ] + ], + ], + ], ]))); $this->classGenerator->generateInputObject($objectName); $objectName .= 'InputObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/input_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/input_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -248,12 +247,12 @@ public function testGenerateInputObjectWithEnumValue() 'kind' => FieldTypeKindEnum::ENUM_OBJECT, 'description' => null, 'ofType' => null, - ] - ] + ], + ], ], - ] - ] - ] + ], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ @@ -264,17 +263,17 @@ public function testGenerateInputObjectWithEnumValue() [ 'name' => 'some_value', 'description' => null, - ] - ] - ] - ] + ], + ], + ], + ], ]))); $this->classGenerator->generateInputObject($objectName); $objectName .= 'InputObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/input_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/input_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -328,16 +327,16 @@ public function testGenerateInputObjectWithListValues() ], ], ], - ] - ] - ] + ], + ], + ], ]))); $this->classGenerator->generateInputObject($objectName); $objectName .= 'InputObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/input_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/input_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -376,34 +375,34 @@ public function testGenerateInputObjectWithNestedObjectValues() 'ofType' => null, ], ], - ] - ] - ] + ], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ '__type' => [ 'name' => 'WithListValue', 'kind' => FieldTypeKindEnum::INPUT_OBJECT, - 'inputFields' => [] - ] - ] + 'inputFields' => [], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ '__type' => [ 'name' => '_TestFilter', 'kind' => FieldTypeKindEnum::INPUT_OBJECT, - 'inputFields' => [] - ] - ] + 'inputFields' => [], + ], + ], ]))); $this->classGenerator->generateInputObject($objectName); $objectName .= 'InputObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/input_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/input_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -424,7 +423,7 @@ public function testGenerateArgumentsObjectWithScalarArgs() 'kind' => FieldTypeKindEnum::SCALAR, 'description' => null, 'ofType' => null, - ] + ], ], [ 'name' => 'another_scalar_property', 'description' => null, @@ -434,14 +433,14 @@ public function testGenerateArgumentsObjectWithScalarArgs() 'kind' => FieldTypeKindEnum::SCALAR, 'description' => null, 'ofType' => null, - ] - ] + ], + ], ]; $this->classGenerator->generateArgumentsObject($objectName, $argsArray); $this->assertFileEquals( - static::getExpectedFilesDir() . "/arguments_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/arguments_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -462,12 +461,12 @@ public function testGenerateArgumentsObjectWithEnumArg() [ 'name' => 'some_value', 'description' => null, - ] - ] - ] - ] + ], + ], + ], + ], ]))); - $argsArray = [ + $argsArray = [ [ 'name' => 'enumProperty', 'description' => null, @@ -477,14 +476,14 @@ public function testGenerateArgumentsObjectWithEnumArg() 'kind' => FieldTypeKindEnum::ENUM_OBJECT, 'description' => null, 'ofType' => null, - ] - ] + ], + ], ]; $this->classGenerator->generateArgumentsObject($objectName, $argsArray); $this->assertFileEquals( - static::getExpectedFilesDir() . "/arguments_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/arguments_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -505,10 +504,10 @@ public function testGenerateArgumentsObjectWithListArgs() [ 'name' => 'some_value', 'description' => null, - ] - ] - ] - ] + ], + ], + ], + ], ]))); $argsArray = [ [ @@ -524,8 +523,8 @@ public function testGenerateArgumentsObjectWithListArgs() 'kind' => FieldTypeKindEnum::ENUM_OBJECT, 'description' => null, 'ofType' => null, - ] - ] + ], + ], ], [ 'name' => 'another_list_property', 'description' => null, @@ -543,16 +542,16 @@ public function testGenerateArgumentsObjectWithListArgs() 'kind' => FieldTypeKindEnum::SCALAR, 'description' => null, 'ofType' => null, - ] - ] - ] - ] + ], + ], + ], + ], ]; $this->classGenerator->generateArgumentsObject($objectName, $argsArray); $this->assertFileEquals( - static::getExpectedFilesDir() . "/arguments_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/arguments_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -568,18 +567,18 @@ public function testGenerateArgumentsObjectWithInputObjectArgs() '__type' => [ 'name' => 'Some', 'kind' => FieldTypeKindEnum::INPUT_OBJECT, - 'inputFields' => [] - ] - ] + 'inputFields' => [], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ '__type' => [ 'name' => 'Another', 'kind' => FieldTypeKindEnum::INPUT_OBJECT, - 'inputFields' => [] - ] - ] + 'inputFields' => [], + ], + ], ]))); $objectName = 'WithMultipleInputObjectArgsArgumentsObject'; @@ -609,8 +608,8 @@ public function testGenerateArgumentsObjectWithInputObjectArgs() $this->classGenerator->generateArgumentsObject($objectName, $argsArray); $this->assertFileEquals( - static::getExpectedFilesDir() . "/arguments_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/arguments_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -665,10 +664,10 @@ public function testGenerateQueryObjectWithScalarFields() 'ofType' => null, ], 'args' => null, - ] - ] - ] - ] + ], + ], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ @@ -677,16 +676,16 @@ public function testGenerateQueryObjectWithScalarFields() 'kind' => FieldTypeKindEnum::ENUM_OBJECT, 'enumValues' => [ [ - "name" => 'UNKNOWN', - 'description' => null + 'name' => 'UNKNOWN', + 'description' => null, ], [ 'name' => 'FEMALE', - 'description' => null + 'description' => null, ], [ 'name' => 'MALE', - 'description' => null + 'description' => null, ], ], ], @@ -696,8 +695,8 @@ public function testGenerateQueryObjectWithScalarFields() $objectName .= 'QueryObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/query_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/query_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -730,7 +729,7 @@ public function testGenerateQueryObjectWithObjectFields() 'kind' => FieldTypeKindEnum::OBJECT, 'description' => null, 'ofType' => null, - ] + ], ], 'args' => null, ], [ @@ -747,46 +746,46 @@ public function testGenerateQueryObjectWithObjectFields() 'kind' => FieldTypeKindEnum::OBJECT, 'description' => null, 'ofType' => null, - ] + ], ], 'args' => null, ], - ] - ] - ] + ], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ '__type' => [ 'name' => 'MultipleObjectSelectorsRight', 'kind' => FieldTypeKindEnum::OBJECT, - 'fields' => [] - ] - ] + 'fields' => [], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ '__type' => [ 'name' => 'Left', 'kind' => FieldTypeKindEnum::OBJECT, - 'fields' => [] - ] - ] + 'fields' => [], + ], + ], ]))); $objectName .= 'QueryObject'; $this->classGenerator->generateQueryObject($objectName); $this->assertFileEquals( - static::getExpectedFilesDir() . "/query_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/query_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); // Test if the right classes are generated. - $this->assertFileExists(static::getGeneratedFilesDir() . "/LeftQueryObject.php", "The query object name for the left field should consist of the type name Left plus QueryObject"); - $this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsLeftObjectsArgumentsObject.php", "The argument object name for the left field should consist of the parent type name MultipleObjectSelectors plus the field name LeftObjects plus ArgumentsObject"); + $this->assertFileExists(static::getGeneratedFilesDir().'/LeftQueryObject.php', 'The query object name for the left field should consist of the type name Left plus QueryObject'); + $this->assertFileExists(static::getGeneratedFilesDir().'/MultipleObjectSelectorsLeftObjectsArgumentsObject.php', 'The argument object name for the left field should consist of the parent type name MultipleObjectSelectors plus the field name LeftObjects plus ArgumentsObject'); - $this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsRightQueryObject.php", "The query object name for the right field should consist of the type name MultipleObjectSelectorsRight plus QueryObject"); - $this->assertFileExists(static::getGeneratedFilesDir() . "/MultipleObjectSelectorsRightArgumentsObject.php", "The argument object name for the right field should consist of the parent type name MultipleObjectSelectors plus the field name Right plus ArgumentsObject"); + $this->assertFileExists(static::getGeneratedFilesDir().'/MultipleObjectSelectorsRightQueryObject.php', 'The query object name for the right field should consist of the type name MultipleObjectSelectorsRight plus QueryObject'); + $this->assertFileExists(static::getGeneratedFilesDir().'/MultipleObjectSelectorsRightArgumentsObject.php', 'The argument object name for the right field should consist of the parent type name MultipleObjectSelectors plus the field name Right plus ArgumentsObject'); } /** @@ -801,17 +800,17 @@ public function testGenerateRootObject() 'name' => 'Query', 'kind' => FieldTypeKindEnum::OBJECT, 'description' => null, - 'fields' => [] - ] - ] - ] + 'fields' => [], + ], + ], + ], ]))); $this->classGenerator->generateRootQueryObject(); $objectName = 'RootQueryObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/query_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/query_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } @@ -835,9 +834,9 @@ public function testGenerateUnionObject() 'kind' => FieldTypeKindEnum::OBJECT, 'name' => 'UnionObject2', ], - ] - ] - ] + ], + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ @@ -859,8 +858,8 @@ public function testGenerateUnionObject() 'args' => null, ], ], - ] - ] + ], + ], ]))); $this->mockHandler->append(new Response(200, [], json_encode([ 'data' => [ @@ -868,35 +867,35 @@ public function testGenerateUnionObject() 'name' => 'UnionObject2', 'kind' => FieldTypeKindEnum::OBJECT, 'fields' => [], - ] - ] + ], + ], ]))); $this->classGenerator->generateUnionObject($objectName); $objectName .= 'UnionObject'; $this->assertFileEquals( - static::getExpectedFilesDir() . "/union_objects/UnionObject1QueryObject.php", - static::getGeneratedFilesDir() . "/UnionObject1QueryObject.php" + static::getExpectedFilesDir().'/union_objects/UnionObject1QueryObject.php', + static::getGeneratedFilesDir().'/UnionObject1QueryObject.php' ); $this->assertFileEquals( - static::getExpectedFilesDir() . "/union_objects/UnionObject2QueryObject.php", - static::getGeneratedFilesDir() . "/UnionObject2QueryObject.php" + static::getExpectedFilesDir().'/union_objects/UnionObject2QueryObject.php', + static::getGeneratedFilesDir().'/UnionObject2QueryObject.php' ); $this->assertFileEquals( - static::getExpectedFilesDir() . "/union_objects/$objectName.php", - static::getGeneratedFilesDir() . "/$objectName.php" + static::getExpectedFilesDir()."/union_objects/$objectName.php", + static::getGeneratedFilesDir()."/$objectName.php" ); } - ///** + // /** // * @covers \GraphQL\SchemaGenerator\SchemaClassGenerator::generateObject // */ - //public function testGenerateObjectWithUnregisteredKind() - //{ + // public function testGenerateObjectWithUnregisteredKind() + // { // $this->expectExceptionMessage('Unsupported object type'); // $this->classGenerator->generateObject('someNae', 'someKind'); - //} + // } } class TransparentSchemaClassGenerator extends SchemaClassGenerator @@ -904,8 +903,7 @@ class TransparentSchemaClassGenerator extends SchemaClassGenerator public function __construct( Client $client, string $writeDir = '' - ) - { + ) { parent::__construct($client, $writeDir, 'GraphQL\\Tests\\SchemaObject'); } @@ -929,7 +927,7 @@ public function generateInputObject(string $objectName): bool return parent::generateInputObject($objectName); } - public function generateObject(string $objectName, string $objectKind): bool + public function generateObject(string $objectName, FieldTypeKindEnum $objectKind): bool { return parent::generateObject($objectName, $objectKind); } diff --git a/tests/TraitFileTest.php b/tests/TraitFileTest.php index 3bb9ae1..030330c 100644 --- a/tests/TraitFileTest.php +++ b/tests/TraitFileTest.php @@ -1,25 +1,28 @@ writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -42,11 +45,10 @@ public function testEmptyTrait() public function testTraitWithNamespace() { $fileName = 'TraitWithNamespace'; - $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->setNamespace("GraphQL\Test"); + $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName, "GraphQL\Test"); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -61,10 +63,9 @@ public function testTraitWithEmptyNamespace() { $fileName = 'EmptyTrait'; $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->setNamespace(''); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -83,7 +84,7 @@ public function testTraitWithImports() $trait->addImport("GraphQL\Client"); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -98,10 +99,14 @@ public function testTraitWithEmptyImport() { $fileName = 'EmptyTrait'; $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->addImport(""); + + $this->expectException(\Nette\InvalidArgumentException::class); + $this->expectExceptionMessage("Value '' is not valid class/function/constant name."); + + $trait->addImport(''); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -117,13 +122,12 @@ public function testTraitWithEmptyImport() public function testTraitWithNamespaceAndImports() { $fileName = 'TraitWithNamespaceAndImports'; - $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->setNamespace("GraphQL\\Test"); - $trait->addImport("GraphQL\\Query"); - $trait->addImport("GraphQL\\Client"); + $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName, 'GraphQL\\Test'); + $trait->addImport('GraphQL\\Query'); + $trait->addImport('GraphQL\\Client'); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -143,7 +147,7 @@ public function testTraitWithProperties() $trait->addProperty('property_three'); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); return $trait; } @@ -160,15 +164,17 @@ public function testTraitWithEmptyProperty() { $fileName = 'EmptyTrait'; $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); + + $this->expectException(\Nette\InvalidArgumentException::class); + $this->expectExceptionMessage("Value '' is not valid name."); + $trait->addProperty(''); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** - * @param TraitFile $trait - * * @depends clone testTraitWithProperties * * @covers \GraphQL\SchemaGenerator\CodeGenerator\CodeFile\TraitFile::addProperty @@ -176,11 +182,11 @@ public function testTraitWithEmptyProperty() */ public function testTraitWithDuplicateProperties(TraitFile $trait) { + $this->expectException(\Nette\InvalidStateException::class); + $this->expectExceptionMessage("Cannot add property 'property1', because it already exists."); + // Adding the same property again $trait->addProperty('property1'); - - $fileName = $trait->getFileName(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); } /** @@ -205,7 +211,7 @@ public function testTraitWithPropertiesAndValues() $trait->addProperty('propertySeven', 7.7); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -220,14 +226,10 @@ public function testTraitWithOneMethod() { $fileName = 'TraitWithOneMethod'; $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->addMethod('public function testTheTrait() { - print "test!"; - die(); -}' - ); + $trait->addMethod('testTheTrait')->setVisibility(ClassLike::VisibilityPublic)->addBody('print "test!";')->addBody('die();'); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -242,19 +244,11 @@ public function testTraitWithMultipleMethods() { $fileName = 'TraitWithMultipleMethods'; $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->addMethod('public function testTheTrait() { - $this->innerTest(); - die(); -}' - ); - $trait->addMethod('private function innerTest() { - print "test!"; - return 0; -}' - , true, 'is deprecated'); + $trait->addMethod('testTheTrait')->setVisibility(ClassLike::VisibilityPublic)->addBody('$this->innerTest();')->addBody('die();'); + $trait->addMethod('innerTest', true, 'is deprecated')->setVisibility(ClassLike::VisibilityPrivate)->addBody('print "test!";')->addBody('die();'); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -267,12 +261,12 @@ public function testTraitWithMultipleMethods() */ public function testTraitWithEmptyMethod() { + $this->expectException(\Nette\InvalidArgumentException::class); + $this->expectExceptionMessage("Value '' is not valid name."); $fileName = 'EmptyTrait'; $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); $trait->addMethod(''); $trait->writeFile(); - - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); } /** @@ -289,18 +283,11 @@ public function testTraitWithPropertiesAndMethods() $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); $trait->addProperty('propOne'); $trait->addProperty('propTwo', true); - $trait->addMethod('public function getProperties() { - return [$this->propOne, $this->propTwo]; -}' - ); - $trait->addMethod('public function clearProperties() { - $this->propOne = 1; - $this->propTwo = 2; -}' - ); + $trait->addMethod('getProperties')->setVisibility(ClassLike::VisibilityPublic)->addBody('return [$this->propOne, $this->propTwo];'); + $trait->addMethod('clearProperties')->setVisibility(ClassLike::VisibilityPublic)->addBody('$this->propOne = 1;')->addBody('$this->propTwo = 2;'); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } /** @@ -314,23 +301,15 @@ public function testTraitWithPropertiesAndMethods() public function testTraitWithEverything() { $fileName = 'TraitWithEverything'; - $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName); - $trait->setNamespace("GraphQL\\Test"); - $trait->addImport("GraphQL\\Query"); - $trait->addImport("GraphQL\\Client"); + $trait = new TraitFile(static::getGeneratedFilesDir(), $fileName, 'GraphQL\\Test'); + $trait->addImport('GraphQL\\Query'); + $trait->addImport('GraphQL\\Client'); $trait->addProperty('propOne'); - $trait->addProperty('propTwo', true); - $trait->addMethod('public function getProperties() { - return [$this->propOne, $this->propTwo]; -}' - ); - $trait->addMethod('public function clearProperties() { - $this->propOne = 1; - $this->propTwo = 2; -}' - ); + $trait->addProperty('propTwo', 'bool'); + $trait->addMethod('getProperties')->setVisibility(ClassLike::VisibilityPublic)->addBody('return [$this->propOne, $this->propTwo];'); + $trait->addMethod('clearProperties')->setVisibility(ClassLike::VisibilityPublic)->addBody('$this->propOne = 1;')->addBody('$this->propTwo = 2;'); $trait->writeFile(); - $this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php", $trait->getWritePath()); + $this->assertFileEquals(static::getExpectedFilesDir()."/$fileName.php", $trait->getWritePath()); } } diff --git a/tests/UnionObjectTest.php b/tests/UnionObjectTest.php index ae1a5de..e321329 100644 --- a/tests/UnionObjectTest.php +++ b/tests/UnionObjectTest.php @@ -1,16 +1,15 @@ objectProperty = $someInputObject; - + $this->objectProperty = $objectProperty; return $this; } } diff --git a/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php index d77ffa8..e089152 100644 --- a/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithListArgArgumentsObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\ArgumentsObject; - -class WithListArgArgumentsObject extends ArgumentsObject +class WithListArgArgumentsObject extends \GraphQL\SchemaObject\ArgumentsObject { - protected $listProperty; + protected array $listProperty; public function setListProperty(array $listProperty) { $this->listProperty = $listProperty; - return $this; } } diff --git a/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php index 30ce55f..38eea31 100644 --- a/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleEnumArgArgumentsObject.php @@ -2,17 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\ArgumentsObject; -use GraphQL\RawObject; - -class WithMultipleEnumArgArgumentsObject extends ArgumentsObject +class WithMultipleEnumArgArgumentsObject extends \GraphQL\SchemaObject\ArgumentsObject { - protected $enumProperty; + protected SomeEnumObject $enumProperty; - public function setEnumProperty($some) + public function setEnumProperty(SomeEnumObject $enumProperty) { - $this->enumProperty = new RawObject($some); - + $this->enumProperty = $enumProperty; return $this; } } diff --git a/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php index bcbccc2..4bc93f7 100644 --- a/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleInputObjectArgsArgumentsObject.php @@ -2,24 +2,20 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\ArgumentsObject; - -class WithMultipleInputObjectArgsArgumentsObject extends ArgumentsObject +class WithMultipleInputObjectArgsArgumentsObject extends \GraphQL\SchemaObject\ArgumentsObject { - protected $objectProperty; - protected $another_object_property; + protected SomeInputObject $objectProperty; + protected AnotherInputObject $another_object_property; - public function setObjectProperty(SomeInputObject $someInputObject) + public function setObjectProperty(SomeInputObject $objectProperty) { - $this->objectProperty = $someInputObject; - + $this->objectProperty = $objectProperty; return $this; } - public function setAnotherObjectProperty(AnotherInputObject $anotherInputObject) + public function setAnotherObjectProperty(AnotherInputObject $another_object_property) { - $this->another_object_property = $anotherInputObject; - + $this->another_object_property = $another_object_property; return $this; } } diff --git a/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php index 50f2b2d..6332b88 100644 --- a/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleListArgsArgumentsObject.php @@ -2,24 +2,20 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\ArgumentsObject; - -class WithMultipleListArgsArgumentsObject extends ArgumentsObject +class WithMultipleListArgsArgumentsObject extends \GraphQL\SchemaObject\ArgumentsObject { - protected $listProperty; - protected $another_list_property; + protected array $listProperty; + protected array $another_list_property; public function setListProperty(array $listProperty) { $this->listProperty = $listProperty; - return $this; } public function setAnotherListProperty(array $anotherListProperty) { $this->another_list_property = $anotherListProperty; - return $this; } } diff --git a/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php b/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php index 33001b8..3512641 100644 --- a/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithMultipleScalarArgsArgumentsObject.php @@ -2,24 +2,20 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\ArgumentsObject; - -class WithMultipleScalarArgsArgumentsObject extends ArgumentsObject +class WithMultipleScalarArgsArgumentsObject extends \GraphQL\SchemaObject\ArgumentsObject { - protected $scalarProperty; - protected $another_scalar_property; + protected string $scalarProperty; + protected string $another_scalar_property; public function setScalarProperty($scalarProperty) { $this->scalarProperty = $scalarProperty; - return $this; } public function setAnotherScalarProperty($anotherScalarProperty) { $this->another_scalar_property = $anotherScalarProperty; - return $this; } } diff --git a/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php b/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php index 34825ef..1fb1f2a 100644 --- a/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php +++ b/tests/files_expected/arguments_objects/WithScalarArgArgumentsObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\ArgumentsObject; - -class WithScalarArgArgumentsObject extends ArgumentsObject +class WithScalarArgArgumentsObject extends \GraphQL\SchemaObject\ArgumentsObject { - protected $scalarProperty; + protected string $scalarProperty; public function setScalarProperty($scalarProperty) { $this->scalarProperty = $scalarProperty; - return $this; } } diff --git a/tests/files_expected/classes/ClassExtendsBase.php b/tests/files_expected/classes/ClassExtendsBase.php index 0191b84..3c46fa2 100644 --- a/tests/files_expected/classes/ClassExtendsBase.php +++ b/tests/files_expected/classes/ClassExtendsBase.php @@ -1,4 +1,5 @@ enumVal = $enumVal; - return $this; } } diff --git a/tests/files_expected/input_objects/WithInputObjectValueInputObject.php b/tests/files_expected/input_objects/WithInputObjectValueInputObject.php index bec87cd..8c8764d 100644 --- a/tests/files_expected/input_objects/WithInputObjectValueInputObject.php +++ b/tests/files_expected/input_objects/WithInputObjectValueInputObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class WithInputObjectValueInputObject extends InputObject +class WithInputObjectValueInputObject extends \GraphQL\SchemaObject\InputObject { protected $inputObject; - public function setInputObject(WithListValueInputObject $withListValueInputObject) + public function setInputObject(\WithListValueInputObject $inputObject) { - $this->inputObject = $withListValueInputObject; - + $this->inputObject = $inputObject; return $this; } } diff --git a/tests/files_expected/input_objects/WithListValueInputObject.php b/tests/files_expected/input_objects/WithListValueInputObject.php index adfa7c1..b2cb1bf 100644 --- a/tests/files_expected/input_objects/WithListValueInputObject.php +++ b/tests/files_expected/input_objects/WithListValueInputObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class WithListValueInputObject extends InputObject +class WithListValueInputObject extends \GraphQL\SchemaObject\InputObject { protected $listOne; public function setListOne(array $listOne) { $this->listOne = $listOne; - return $this; } } diff --git a/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php b/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php index 1b9f4dc..2f7e644 100644 --- a/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php +++ b/tests/files_expected/input_objects/WithMultipleInputObjectValuesInputObject.php @@ -2,24 +2,20 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class WithMultipleInputObjectValuesInputObject extends InputObject +class WithMultipleInputObjectValuesInputObject extends \GraphQL\SchemaObject\InputObject { protected $inputObject; protected $inputObjectTwo; - public function setInputObject(WithListValueInputObject $withListValueInputObject) + public function setInputObject(WithListValueInputObject $inputObject) { - $this->inputObject = $withListValueInputObject; - + $this->inputObject = $inputObject; return $this; } - public function setInputObjectTwo(_TestFilterInputObject $testFilterInputObject) + public function setInputObjectTwo(_TestFilterInputObject $inputObjectTwo) { - $this->inputObjectTwo = $testFilterInputObject; - + $this->inputObjectTwo = $inputObjectTwo; return $this; } } diff --git a/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php b/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php index be4176f..a0ed05f 100644 --- a/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php +++ b/tests/files_expected/input_objects/WithMultipleListValuesInputObject.php @@ -2,9 +2,7 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class WithMultipleListValuesInputObject extends InputObject +class WithMultipleListValuesInputObject extends \GraphQL\SchemaObject\InputObject { protected $listOne; protected $list_two; @@ -12,14 +10,12 @@ class WithMultipleListValuesInputObject extends InputObject public function setListOne(array $listOne) { $this->listOne = $listOne; - return $this; } public function setListTwo(array $listTwo) { $this->list_two = $listTwo; - return $this; } } diff --git a/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php b/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php index b3b116e..a8cc468 100644 --- a/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php +++ b/tests/files_expected/input_objects/WithMultipleScalarValuesInputObject.php @@ -2,9 +2,7 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class WithMultipleScalarValuesInputObject extends InputObject +class WithMultipleScalarValuesInputObject extends \GraphQL\SchemaObject\InputObject { protected $valOne; protected $val_two; @@ -12,14 +10,12 @@ class WithMultipleScalarValuesInputObject extends InputObject public function setValOne($valOne) { $this->valOne = $valOne; - return $this; } public function setValTwo($valTwo) { $this->val_two = $valTwo; - return $this; } } diff --git a/tests/files_expected/input_objects/WithScalarValueInputObject.php b/tests/files_expected/input_objects/WithScalarValueInputObject.php index 09a3afa..f57d608 100644 --- a/tests/files_expected/input_objects/WithScalarValueInputObject.php +++ b/tests/files_expected/input_objects/WithScalarValueInputObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class WithScalarValueInputObject extends InputObject +class WithScalarValueInputObject extends \GraphQL\SchemaObject\InputObject { protected $valOne; public function setValOne($valOne) { $this->valOne = $valOne; - return $this; } } diff --git a/tests/files_expected/input_objects/_TestFilterInputObject.php b/tests/files_expected/input_objects/_TestFilterInputObject.php index 48c1086..abbcfc7 100644 --- a/tests/files_expected/input_objects/_TestFilterInputObject.php +++ b/tests/files_expected/input_objects/_TestFilterInputObject.php @@ -2,9 +2,7 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\InputObject; - -class _TestFilterInputObject extends InputObject +class _TestFilterInputObject extends \GraphQL\SchemaObject\InputObject { protected $first_name; protected $lastName; @@ -14,28 +12,24 @@ class _TestFilterInputObject extends InputObject public function setFirstName($firstName) { $this->first_name = $firstName; - return $this; } public function setLastName($lastName) { $this->lastName = $lastName; - return $this; } public function setIds(array $ids) { $this->ids = $ids; - return $this; } - public function setTestFilter(_TestFilterInputObject $testFilterInputObject) + public function setTestFilter(\_TestFilterInputObject $testFilter) { - $this->testFilter = $testFilterInputObject; - + $this->testFilter = $testFilter; return $this; } } diff --git a/tests/files_expected/query_objects/EmptyQueryObject.php b/tests/files_expected/query_objects/EmptyQueryObject.php index ba1d616..bbfda62 100644 --- a/tests/files_expected/query_objects/EmptyQueryObject.php +++ b/tests/files_expected/query_objects/EmptyQueryObject.php @@ -2,9 +2,7 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class EmptyQueryObject extends QueryObject +class EmptyQueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "Empty"; + public const OBJECT_NAME = 'Empty'; } diff --git a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php index eecce4b..0b4294a 100644 --- a/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php @@ -2,20 +2,17 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class MultipleObjectSelectorsQueryObject extends QueryObject +class MultipleObjectSelectorsQueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "MultipleObjectSelectors"; + public const OBJECT_NAME = 'MultipleObjectSelectors'; public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObject = null) { - $object = new MultipleObjectSelectorsRightQueryObject("right"); + $object = new MultipleObjectSelectorsRightQueryObject('right'); if ($argsObject !== null) { $object->appendArguments($argsObject->toArray()); } $this->selectField($object); - return $object; } @@ -24,12 +21,11 @@ public function selectRight(MultipleObjectSelectorsRightArgumentsObject $argsObj */ public function selectLeftObjects(MultipleObjectSelectorsLeftObjectsArgumentsObject $argsObject = null) { - $object = new LeftQueryObject("left_objects"); + $object = new LeftQueryObject('left_objects'); if ($argsObject !== null) { $object->appendArguments($argsObject->toArray()); } $this->selectField($object); - return $object; } } diff --git a/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php b/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php index 01b778d..52ee069 100644 --- a/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php +++ b/tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class MultipleSimpleSelectorsQueryObject extends QueryObject +class MultipleSimpleSelectorsQueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "MultipleSimpleSelectors"; + public const OBJECT_NAME = 'MultipleSimpleSelectors'; public function selectFirstName() { - $this->selectField("first_name"); - + $this->selectField('first_name'); return $this; } @@ -20,15 +17,13 @@ public function selectFirstName() */ public function selectLastName() { - $this->selectField("last_name"); - + $this->selectField('last_name'); return $this; } public function selectGender() { - $this->selectField("gender"); - + $this->selectField('gender'); return $this; } } diff --git a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php index 0c42077..cddc628 100644 --- a/tests/files_expected/query_objects/ObjectSelectorQueryObject.php +++ b/tests/files_expected/query_objects/ObjectSelectorQueryObject.php @@ -2,20 +2,17 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class ObjectSelectorQueryObject extends QueryObject +class ObjectSelectorQueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "ObjectSelector"; + public const OBJECT_NAME = 'ObjectSelector'; - public function selectOthers(RootOthersArgumentsObject $argsObject = null) + public function selectOthers(\RootOthersArgumentsObject $argsObject = null) { - $object = new OtherQueryObject("others"); + $object = new OtherQueryObject('others'); if ($argsObject !== null) { $object->appendArguments($argsObject->toArray()); } $this->selectField($object); - return $object; } } diff --git a/tests/files_expected/query_objects/RootQueryObject.php b/tests/files_expected/query_objects/RootQueryObject.php index b1abbdd..3cfa8eb 100644 --- a/tests/files_expected/query_objects/RootQueryObject.php +++ b/tests/files_expected/query_objects/RootQueryObject.php @@ -2,9 +2,7 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class RootQueryObject extends QueryObject +class RootQueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = ""; + public const OBJECT_NAME = ''; } diff --git a/tests/files_expected/query_objects/SimpleSelectorQueryObject.php b/tests/files_expected/query_objects/SimpleSelectorQueryObject.php index c568068..0f53412 100644 --- a/tests/files_expected/query_objects/SimpleSelectorQueryObject.php +++ b/tests/files_expected/query_objects/SimpleSelectorQueryObject.php @@ -2,16 +2,13 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class SimpleSelectorQueryObject extends QueryObject +class SimpleSelectorQueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "SimpleSelector"; + public const OBJECT_NAME = 'SimpleSelector'; public function selectName() { - $this->selectField("name"); - + $this->selectField('name'); return $this; } } diff --git a/tests/files_expected/traits/EmptyTrait.php b/tests/files_expected/traits/EmptyTrait.php index 03ee8bc..ee38859 100644 --- a/tests/files_expected/traits/EmptyTrait.php +++ b/tests/files_expected/traits/EmptyTrait.php @@ -1,4 +1,5 @@ propOne, $this->propTwo]; } - public function clearProperties() { + public function clearProperties() + { $this->propOne = 1; $this->propTwo = 2; } diff --git a/tests/files_expected/traits/TraitWithImports.php b/tests/files_expected/traits/TraitWithImports.php index 161eb5e..c4aee29 100644 --- a/tests/files_expected/traits/TraitWithImports.php +++ b/tests/files_expected/traits/TraitWithImports.php @@ -1,7 +1,8 @@ innerTest(); die(); } @@ -10,8 +11,9 @@ public function testTheTrait() { /** * @deprecated is deprecated */ - private function innerTest() { + private function innerTest() + { print "test!"; - return 0; + die(); } } diff --git a/tests/files_expected/traits/TraitWithNamespace.php b/tests/files_expected/traits/TraitWithNamespace.php index 52687d5..3d24256 100644 --- a/tests/files_expected/traits/TraitWithNamespace.php +++ b/tests/files_expected/traits/TraitWithNamespace.php @@ -3,4 +3,5 @@ namespace GraphQL\Test; trait TraitWithNamespace -{} +{ +} diff --git a/tests/files_expected/traits/TraitWithNamespaceAndImports.php b/tests/files_expected/traits/TraitWithNamespaceAndImports.php index fb9ca9e..ff0edc3 100644 --- a/tests/files_expected/traits/TraitWithNamespaceAndImports.php +++ b/tests/files_expected/traits/TraitWithNamespaceAndImports.php @@ -2,8 +2,9 @@ namespace GraphQL\Test; -use GraphQL\Query; use GraphQL\Client; +use GraphQL\Query; trait TraitWithNamespaceAndImports -{} +{ +} diff --git a/tests/files_expected/traits/TraitWithOneMethod.php b/tests/files_expected/traits/TraitWithOneMethod.php index f2bb306..3e03db0 100644 --- a/tests/files_expected/traits/TraitWithOneMethod.php +++ b/tests/files_expected/traits/TraitWithOneMethod.php @@ -2,7 +2,8 @@ trait TraitWithOneMethod { - public function testTheTrait() { + public function testTheTrait() + { print "test!"; die(); } diff --git a/tests/files_expected/traits/TraitWithPropertiesAndMethods.php b/tests/files_expected/traits/TraitWithPropertiesAndMethods.php index 3bd4888..6a8053d 100644 --- a/tests/files_expected/traits/TraitWithPropertiesAndMethods.php +++ b/tests/files_expected/traits/TraitWithPropertiesAndMethods.php @@ -5,11 +5,13 @@ trait TraitWithPropertiesAndMethods protected $propOne; protected $propTwo = true; - public function getProperties() { + public function getProperties() + { return [$this->propOne, $this->propTwo]; } - public function clearProperties() { + public function clearProperties() + { $this->propOne = 1; $this->propTwo = 2; } diff --git a/tests/files_expected/traits/TraitWithPropertiesAndValues.php b/tests/files_expected/traits/TraitWithPropertiesAndValues.php index 9a622aa..82ddf31 100644 --- a/tests/files_expected/traits/TraitWithPropertiesAndValues.php +++ b/tests/files_expected/traits/TraitWithPropertiesAndValues.php @@ -4,9 +4,9 @@ trait TraitWithPropertiesAndValues { protected $propertyOne; protected $propertyTwo = 2; - protected $propertyThree = "three"; + protected $propertyThree = 'three'; protected $propertyFour = false; protected $propertyFive = true; - protected $propertySix = ""; + protected $propertySix = ''; protected $propertySeven = 7.7; } diff --git a/tests/files_expected/union_objects/UnionObject1QueryObject.php b/tests/files_expected/union_objects/UnionObject1QueryObject.php index 22a8708..6dff5fe 100644 --- a/tests/files_expected/union_objects/UnionObject1QueryObject.php +++ b/tests/files_expected/union_objects/UnionObject1QueryObject.php @@ -2,20 +2,17 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class UnionObject1QueryObject extends QueryObject +class UnionObject1QueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "UnionObject1"; + public const OBJECT_NAME = 'UnionObject1'; public function selectUnion(UnionObject1UnionArgumentsObject $argsObject = null) { - $object = new UnionTestObjectUnionObject("union"); + $object = new UnionTestObjectUnionObject('union'); if ($argsObject !== null) { $object->appendArguments($argsObject->toArray()); } $this->selectField($object); - return $object; } } diff --git a/tests/files_expected/union_objects/UnionObject2QueryObject.php b/tests/files_expected/union_objects/UnionObject2QueryObject.php index 53aa127..781abc6 100644 --- a/tests/files_expected/union_objects/UnionObject2QueryObject.php +++ b/tests/files_expected/union_objects/UnionObject2QueryObject.php @@ -2,9 +2,7 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\QueryObject; - -class UnionObject2QueryObject extends QueryObject +class UnionObject2QueryObject extends \GraphQL\SchemaObject\QueryObject { - const OBJECT_NAME = "UnionObject2"; + public const OBJECT_NAME = 'UnionObject2'; } diff --git a/tests/files_expected/union_objects/UnionTestObjectUnionObject.php b/tests/files_expected/union_objects/UnionTestObjectUnionObject.php index 5b20cdd..027e679 100644 --- a/tests/files_expected/union_objects/UnionTestObjectUnionObject.php +++ b/tests/files_expected/union_objects/UnionTestObjectUnionObject.php @@ -2,15 +2,12 @@ namespace GraphQL\Tests\SchemaObject; -use GraphQL\SchemaObject\UnionObject; - -class UnionTestObjectUnionObject extends UnionObject +class UnionTestObjectUnionObject extends \GraphQL\SchemaObject\UnionObject { public function onUnionObject1() { $object = new UnionObject1QueryObject(); $this->addPossibleType($object); - return $object; } @@ -18,7 +15,6 @@ public function onUnionObject2() { $object = new UnionObject2QueryObject(); $this->addPossibleType($object); - return $object; } }