From 0a5f32ddd89454e53ad6309ab06d800833bc6dbe Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Sun, 25 Feb 2018 16:13:30 +0330 Subject: [PATCH 1/7] Add input-object as an option, to the type make command. --- .../GraphQL/Console/TypeMakeCommand.php | 42 ++++++++++++++----- src/Folklore/GraphQL/Console/stubs/type.stub | 6 ++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/Folklore/GraphQL/Console/TypeMakeCommand.php b/src/Folklore/GraphQL/Console/TypeMakeCommand.php index 3ef26a1d..dbd26485 100644 --- a/src/Folklore/GraphQL/Console/TypeMakeCommand.php +++ b/src/Folklore/GraphQL/Console/TypeMakeCommand.php @@ -3,7 +3,6 @@ namespace Folklore\GraphQL\Console; use Illuminate\Console\GeneratorCommand; -use Symfony\Component\Console\Input\InputOption; class TypeMakeCommand extends GeneratorCommand { @@ -12,7 +11,9 @@ class TypeMakeCommand extends GeneratorCommand * * @var string */ - protected $signature = 'make:graphql:type {name}'; + protected $signature = 'make:graphql:type + {name : The name of the type class} + {--O|object : Create a new input object type}'; /** * The console command description. @@ -41,7 +42,8 @@ protected function getStub() /** * Get the default namespace for the class. * - * @param string $rootNamespace + * @param string $rootNamespace + * * @return string */ protected function getDefaultNamespace($rootNamespace) @@ -52,7 +54,8 @@ protected function getDefaultNamespace($rootNamespace) /** * Build the class with the given name. * - * @param string $name + * @param string $name + * * @return string */ protected function buildClass($name) @@ -65,19 +68,38 @@ protected function buildClass($name) /** * Replace the namespace for the given stub. * - * @param string $stub - * @param string $name + * @param string $stub + * @param string $name + * * @return $this */ protected function replaceType($stub, $name) { preg_match('/([^\\\]+)$/', $name, $matches); - $stub = str_replace( - 'DummyType', - $matches[1], + + $search = ['DummyType', 'DummyInputObject']; + $replace = [$matches[1]]; + + $this->addInputObjectAttribute($replace); + + return str_replace( + $search, + $replace, $stub ); + } - return $stub; + /** + * Add input object attribute to replace type. + * + * @param array $replace + */ + protected function addInputObjectAttribute(array &$replace): void + { + if ($this->option('object')) { + array_push($replace, 'protected $inputObject = true;'); + } else { + array_push($replace, ''); + } } } diff --git a/src/Folklore/GraphQL/Console/stubs/type.stub b/src/Folklore/GraphQL/Console/stubs/type.stub index a32816af..4fb64cfc 100644 --- a/src/Folklore/GraphQL/Console/stubs/type.stub +++ b/src/Folklore/GraphQL/Console/stubs/type.stub @@ -8,6 +8,7 @@ use GraphQL; class DummyClass extends BaseType { + DummyInputObject protected $attributes = [ 'name' => 'DummyType', 'description' => 'A type' @@ -16,7 +17,10 @@ class DummyClass extends BaseType public function fields() { return [ - + 'sampleField' => [ + 'type' => Type::string(), + 'description' => 'Sample field description', + ], ]; } } From f3c2c5aae2fe02d6c6147f27b747f793a7153e9f Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Wed, 5 Sep 2018 17:00:59 +0430 Subject: [PATCH 2/7] remove satooshi/php-coveralls and install php-coveralls/php-coveralls --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a19da2a1..62bcd089 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ "orchestra/testbench": "3.1.*|3.2.*|3.3.*|3.4.*|3.5.*", "fzaninotto/faker": "~1.4", "mockery/mockery": "0.9.*", - "satooshi/php-coveralls": "^1.0", - "phpunit/phpunit": "~4.0|~5.0|~5.7|~6.0" + "phpunit/phpunit": "~4.0|~5.0|~5.7|~6.0", + "php-coveralls/php-coveralls": "^2.1" }, "autoload": { "psr-0": { From 21bcda89eb7ab6068b60117b50bbaf1e0a0af6b7 Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Wed, 5 Sep 2018 17:20:49 +0430 Subject: [PATCH 3/7] fix code for php < 7 --- src/Folklore/GraphQL/Console/TypeMakeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Folklore/GraphQL/Console/TypeMakeCommand.php b/src/Folklore/GraphQL/Console/TypeMakeCommand.php index dbd26485..4f4f4c81 100644 --- a/src/Folklore/GraphQL/Console/TypeMakeCommand.php +++ b/src/Folklore/GraphQL/Console/TypeMakeCommand.php @@ -77,7 +77,7 @@ protected function replaceType($stub, $name) { preg_match('/([^\\\]+)$/', $name, $matches); - $search = ['DummyType', 'DummyInputObject']; + $search = ['DummyType', 'DummyInputObject']; $replace = [$matches[1]]; $this->addInputObjectAttribute($replace); @@ -94,7 +94,7 @@ protected function replaceType($stub, $name) * * @param array $replace */ - protected function addInputObjectAttribute(array &$replace): void + protected function addInputObjectAttribute(array &$replace) { if ($this->option('object')) { array_push($replace, 'protected $inputObject = true;'); From fe9548bb1f390d6afd3e45578eba311de458a1b6 Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Wed, 5 Sep 2018 17:41:32 +0430 Subject: [PATCH 4/7] Fix coveralls command to php-coveralls --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 598ffaa4..f9b286cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -71,7 +71,7 @@ before_script: phpenv config-add ~/xdebug.ini script: vendor/bin/phpunit -after_success: sh -c "if [ ! -z ${COVERAGE+x} ]; then php vendor/bin/coveralls; fi" +after_success: sh -c "if [ ! -z ${COVERAGE+x} ]; then php vendor/bin/php-coveralls; fi" notifications: email: false From a8e9bd1babb3d43f5dfef2aeab29447b8ae27f4d Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Wed, 12 Sep 2018 12:41:49 +0430 Subject: [PATCH 5/7] added test for TypeMakeCommand --- .../GraphQL/Console/TypeMakeCommand.php | 4 +- tests/TypeMakeCommandTest.php | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 tests/TypeMakeCommandTest.php diff --git a/src/Folklore/GraphQL/Console/TypeMakeCommand.php b/src/Folklore/GraphQL/Console/TypeMakeCommand.php index 4f4f4c81..3bc19baa 100644 --- a/src/Folklore/GraphQL/Console/TypeMakeCommand.php +++ b/src/Folklore/GraphQL/Console/TypeMakeCommand.php @@ -73,7 +73,7 @@ protected function buildClass($name) * * @return $this */ - protected function replaceType($stub, $name) + private function replaceType($stub, $name) { preg_match('/([^\\\]+)$/', $name, $matches); @@ -94,7 +94,7 @@ protected function replaceType($stub, $name) * * @param array $replace */ - protected function addInputObjectAttribute(array &$replace) + private function addInputObjectAttribute(array &$replace) { if ($this->option('object')) { array_push($replace, 'protected $inputObject = true;'); diff --git a/tests/TypeMakeCommandTest.php b/tests/TypeMakeCommandTest.php new file mode 100644 index 00000000..de89f489 --- /dev/null +++ b/tests/TypeMakeCommandTest.php @@ -0,0 +1,82 @@ +command = m::mock( + 'Folklore\GraphQL\Console\TypeMakeCommand[error,getPath,rootNamespace]', + [new \Illuminate\Filesystem\Filesystem()] + )->shouldAllowMockingProtectedMethods(); + + $this->command->shouldReceive('rootNamespace')->andReturn('AppTest'); + } + + public function tearDown() + { + parent::tearDown(); + + m::close(); + + exec('rm -rf '.__DIR__.'/appTest'); + } + + public function testItMakeANormalType() + { + $this->mockGetPath('TestNormalType'); + + $this->registerCommand(); + + $this->artisan('make:graphql:type', ['name'=>'TestNormalType']); + + $this->assertTrue(true); + } + + public function testItMakeAInputObjectTypeWithObjectOption() + { + $this->mockGetPath('TestObjectInputType'); + + $this->registerCommand(); + + $this->artisan('make:graphql:type', ['name' => 'TestObjectInputType', '-O' => true]); + + include $this->tmpPath.'TestObjectInputType.php'; + + $inputType = new \AppTest\GraphQL\Type\TestObjectInputType(); + + $this->assertInstanceOf(InputObjectType::class, $inputType->toType()); + } + + public function testItErrorsIfTypeAlradyExists() + { + $this->command->shouldReceive('error')->with('Type already exists!'); + + $this->mockGetPath('TestExistsType'); + + $this->registerCommand(); + + $this->artisan('make:graphql:type', ['name'=>'TestExistsType']); + $this->artisan('make:graphql:type', ['name'=>'TestExistsType']); + + $this->assertTrue(true); + } + + private function registerCommand() + { + $this->app['Illuminate\Contracts\Console\Kernel']->registerCommand($this->command); + } + + private function mockGetPath($name) + { + $this->command->shouldReceive('getPath')->andReturn($this->tmpPath.$name.'.php'); + } +} From e50226f343d4bd07a2608c39be85029e28b9e08e Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Wed, 12 Sep 2018 13:12:05 +0430 Subject: [PATCH 6/7] Fixed for ILLUMINATE_VERSION < 4 --- tests/TypeMakeCommandTest.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/TypeMakeCommandTest.php b/tests/TypeMakeCommandTest.php index de89f489..f8aff3c1 100644 --- a/tests/TypeMakeCommandTest.php +++ b/tests/TypeMakeCommandTest.php @@ -7,12 +7,16 @@ class TypeMakeCommandTest extends TestCase { protected $command; - protected $tmpPath = __DIR__.'/appTest/GraphQL/Type/'; + protected $tmpPath; public function setUp() { parent::setUp(); + $this->app->singleton('Illuminate\Contracts\Console\Kernel', TestKernel::class); + + $this->tmpPath = __DIR__.'/appTest/GraphQL/Type/'; + $this->command = m::mock( 'Folklore\GraphQL\Console\TypeMakeCommand[error,getPath,rootNamespace]', [new \Illuminate\Filesystem\Filesystem()] @@ -80,3 +84,11 @@ private function mockGetPath($name) $this->command->shouldReceive('getPath')->andReturn($this->tmpPath.$name.'.php'); } } + +class TestKernel extends \Illuminate\Foundation\Console\Kernel +{ + public function registerCommand($command) + { + $this->getArtisan()->add($command); + } +} From 87c8b14ed5b60a22c62d06c5226896d05c71488f Mon Sep 17 00:00:00 2001 From: fouladgardev Date: Wed, 12 Sep 2018 16:09:54 +0430 Subject: [PATCH 7/7] changed assert to file exists --- tests/TypeMakeCommandTest.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/TypeMakeCommandTest.php b/tests/TypeMakeCommandTest.php index f8aff3c1..5bb6ce8f 100644 --- a/tests/TypeMakeCommandTest.php +++ b/tests/TypeMakeCommandTest.php @@ -1,6 +1,5 @@ artisan('make:graphql:type', ['name'=>'TestNormalType']); - $this->assertTrue(true); + $this->assertFileExists($this->tmpPath.'TestNormalType.php'); } public function testItMakeAInputObjectTypeWithObjectOption() @@ -53,11 +52,7 @@ public function testItMakeAInputObjectTypeWithObjectOption() $this->artisan('make:graphql:type', ['name' => 'TestObjectInputType', '-O' => true]); - include $this->tmpPath.'TestObjectInputType.php'; - - $inputType = new \AppTest\GraphQL\Type\TestObjectInputType(); - - $this->assertInstanceOf(InputObjectType::class, $inputType->toType()); + $this->assertFileExists($this->tmpPath.'TestObjectInputType.php'); } public function testItErrorsIfTypeAlradyExists() @@ -71,7 +66,7 @@ public function testItErrorsIfTypeAlradyExists() $this->artisan('make:graphql:type', ['name'=>'TestExistsType']); $this->artisan('make:graphql:type', ['name'=>'TestExistsType']); - $this->assertTrue(true); + $this->assertFileExists($this->tmpPath.'TestExistsType.php'); } private function registerCommand()