Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

Commit

Permalink
Added tests + removed namespaces in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmongeau committed Oct 12, 2016
1 parent d699320 commit 3268508
Show file tree
Hide file tree
Showing 22 changed files with 297 additions and 89 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
},
"autoload": {
"psr-0": {
"Folklore\\GraphQL\\": "src/",
"Folklore\\GraphQL\\Tests": "tests/"
"Folklore\\GraphQL\\": "src/"
}
},
"autoload-dev": {
Expand Down
14 changes: 14 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description>The coding standard for laravel package</description>

<file>src</file>

<exclude-pattern>*.json</exclude-pattern>
<exclude-pattern>*.xml</exclude-pattern>

<rule ref="PSR2"/>
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>
</ruleset>
83 changes: 83 additions & 0 deletions src/Folklore/GraphQL/Console/MutationMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Folklore\GraphQL\Console;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class MutationMakeCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:graphql:mutation {name}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new GraphQL mutation class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Mutation';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/mutation.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\GraphQL\Mutation';
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$stub = parent::buildClass($name);

return $this->replaceType($stub, $name);
}

/**
* Replace the namespace for the given stub.
*
* @param string $stub
* @param string $name
* @return $this
*/
protected function replaceType($stub, $name)
{
preg_match('/([^\\\]+)$/', $name, $matches);
$stub = str_replace(
'DummyMutation',
$matches[1],
$stub
);

return $stub;
}
}
5 changes: 3 additions & 2 deletions src/Folklore/GraphQL/Console/QueryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class QueryMakeCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'make:graphql_query {name}';
protected $signature = 'make:graphql:query {name}';

/**
* The console command description.
Expand Down Expand Up @@ -71,9 +71,10 @@ protected function buildClass($name)
*/
protected function replaceType($stub, $name)
{
preg_match('/([^\\\]+)$/', $name, $matches);
$stub = str_replace(
'DummyQuery',
$name,
$matches[1],
$stub
);

Expand Down
5 changes: 3 additions & 2 deletions src/Folklore/GraphQL/Console/TypeMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TypeMakeCommand extends GeneratorCommand
*
* @var string
*/
protected $signature = 'make:graphql_type {name}';
protected $signature = 'make:graphql:type {name}';

/**
* The console command description.
Expand Down Expand Up @@ -71,9 +71,10 @@ protected function buildClass($name)
*/
protected function replaceType($stub, $name)
{
preg_match('/([^\\\]+)$/', $name, $matches);
$stub = str_replace(
'DummyType',
$name,
$matches[1],
$stub
);

Expand Down
16 changes: 15 additions & 1 deletion src/Folklore/GraphQL/GraphQLServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ protected function bootSchemas()
public function register()
{
$this->registerGraphQL();

$this->registerConsole();
}

/**
* Register panneau
* Register GraphQL facade
*
* @return void
*/
Expand All @@ -105,4 +107,16 @@ public function registerGraphQL()
return new GraphQL($app);
});
}

/**
* Register console commands
*
* @return void
*/
public function registerConsole()
{
$this->commands(\Folklore\GraphQL\Console\TypeMakeCommand::class);
$this->commands(\Folklore\GraphQL\Console\QueryMakeCommand::class);
$this->commands(\Folklore\GraphQL\Console\MutationMakeCommand::class);
}
}
85 changes: 69 additions & 16 deletions tests/ConsoleTest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,98 @@
<?php

namespace Folklore\GraphQL\Tests;

use GraphQL;
use Artisan;
use GraphQL\Schema;
use GraphQL\Type\Definition\ObjectType;

use Folklore\GraphQL\Console\QueryMakeCommand;
use Folklore\GraphQL\Console\TypeMakeCommand;

class ConsoleTest extends TestCase
{

protected function getEnvironmentSetUp($app)
{
Artisan::registerCommand($app->make(QueryMakeCommand::class));
Artisan::registerCommand($app->make(TypeMakeCommand::class));
$app->setBasePath(__DIR__.'/fixture');
}

public function tearDown()
{
unlink(app_path('GraphQL/Query/TestQuery.php'));
rmdir(app_path('GraphQL/Query'));
rmdir(app_path('GraphQL'));
if (file_exists(app_path('GraphQL/Query/TestQuery.php'))) {
unlink(app_path('GraphQL/Query/TestQuery.php'));
}
if (file_exists(app_path('GraphQL/Type/TestType.php'))) {
unlink(app_path('GraphQL/Type/TestType.php'));
}
if (file_exists(app_path('GraphQL/Mutation/TestMutation.php'))) {
unlink(app_path('GraphQL/Mutation/TestMutation.php'));
}

if (file_exists(app_path('GraphQL/Type'))) {
rmdir(app_path('GraphQL/Type'));
}
if (file_exists(app_path('GraphQL/Query'))) {
rmdir(app_path('GraphQL/Query'));
}
if (file_exists(app_path('GraphQL/Mutation'))) {
rmdir(app_path('GraphQL/Mutation'));
}
if (file_exists(app_path('GraphQL'))) {
rmdir(app_path('GraphQL'));
}

parent::tearDown();
}

/**
* Test
* Test make query
*
* @test
*/
public function testMakeQuery()
{
Artisan::call('make:graphql_query', [
Artisan::call('make:graphql:query', [
'name' => 'TestQuery'
]);

$queryPath = app_path('GraphQL/Query/TestQuery.php');
$this->assertTrue(file_exists($queryPath));
$path = app_path('GraphQL/Query/TestQuery.php');
$this->assertTrue(file_exists($path));

$content = file_get_contents($path);
$this->assertRegExp('/class TestQuery extends Query/', $content);
$this->assertRegExp('/'.preg_quote('\'name\' => \'TestQuery\'').'/', $content);
}

/**
* Test make type
*
* @test
*/
public function testMakeType()
{
Artisan::call('make:graphql:type', [
'name' => 'TestType'
]);

$path = app_path('GraphQL/Type/TestType.php');
$this->assertTrue(file_exists($path));

$content = file_get_contents($path);
$this->assertRegExp('/class TestType extends Type/', $content);
$this->assertRegExp('/'.preg_quote('\'name\' => \'TestType\'').'/', $content);
}

/**
* Test make mutation
*
* @test
*/
public function testMakeMutation()
{
Artisan::call('make:graphql:mutation', [
'name' => 'TestMutation'
]);

$path = app_path('GraphQL/Mutation/TestMutation.php');
$this->assertTrue(file_exists($path));

$content = file_get_contents($path);
$this->assertRegExp('/class TestMutation extends Mutation/', $content);
$this->assertRegExp('/'.preg_quote('\'name\' => \'TestMutation\'').'/', $content);
}
}
15 changes: 6 additions & 9 deletions tests/EndpointTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

namespace Folklore\GraphQL\Tests;

use GraphQL;
use GraphQL\Schema;
use GraphQL\Type\Definition\ObjectType;

Expand All @@ -27,25 +24,25 @@ protected function getEnvironmentSetUp($app)
{
$app['config']->set('graphql.schemas.default', [
'query' => [
'examples' => \Folklore\GraphQL\Tests\Objects\ExamplesQuery::class,
'examplesContext' => \Folklore\GraphQL\Tests\Objects\ExamplesContextQuery::class
'examples' => ExamplesQuery::class,
'examplesContext' => ExamplesContextQuery::class
],
'mutation' => [
'updateExample' => \Folklore\GraphQL\Tests\Objects\UpdateExampleMutation::class
'updateExample' => UpdateExampleMutation::class
]
]);

$app['config']->set('graphql.schemas.custom', [
'query' => [
'examplesCustom' => \Folklore\GraphQL\Tests\Objects\ExamplesQuery::class
'examplesCustom' => ExamplesQuery::class
],
'mutation' => [
'updateExampleCustom' => \Folklore\GraphQL\Tests\Objects\UpdateExampleMutation::class
'updateExampleCustom' => UpdateExampleMutation::class
]
]);

$app['config']->set('graphql.types', [
'Example' => \Folklore\GraphQL\Tests\Objects\ExampleType::class
'Example' => ExampleType::class
]);
}

Expand Down
5 changes: 0 additions & 5 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<?php

namespace Folklore\GraphQL\Tests;

use GraphQL;
use Closure;
use Folklore\Support\Field;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
use Folklore\GraphQL\Tests\Objects\ExampleField;

class FieldTest extends TestCase
{
Expand Down
Loading

0 comments on commit 3268508

Please sign in to comment.