Skip to content

Commit

Permalink
Merge pull request #18 from mcg-web/tests
Browse files Browse the repository at this point in the history
Add new modes tests
  • Loading branch information
mcg-web authored Nov 6, 2017
2 parents 7ee5852 + 49719c9 commit e6d006a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/AbstractStarWarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function setUp()
*/
protected function assertValidQuery($query, $expected, $variables = null)
{
$actual = GraphQL::execute($this->schema, $query, null, null, $variables);
$actual = GraphQL::executeQuery($this->schema, $query, null, null, $variables)->toArray();
$expected = ['data' => $expected];
$this->assertEquals($expected, $actual, json_encode($actual));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/AbstractTypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function tearDown()
$this->filesystem->remove($this->tmpDir);
}

protected function generateClasses(array $typeConfigs = null, $tmpDir = null, $regenerateIfExists = true)
protected function generateClasses(array $typeConfigs = null, $tmpDir = null, $mode = true)
{
if (null === $typeConfigs) {
$typeConfigs = $this->typeConfigs;
Expand All @@ -55,7 +55,7 @@ protected function generateClasses(array $typeConfigs = null, $tmpDir = null, $r
$tmpDir = $this->tmpDir;
}

$classes = $this->typeGenerator->generateClasses($typeConfigs, $tmpDir, $regenerateIfExists);
$classes = $this->typeGenerator->generateClasses($typeConfigs, $tmpDir, $mode);

$this->classLoader->addClassMap($classes);

Expand Down
77 changes: 77 additions & 0 deletions tests/Generator/TypeGeneratorModeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the OverblogGraphQLPhpGenerator package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLGenerator\Tests\Generator;

use Overblog\GraphQLGenerator\Generator\TypeGenerator;
use Overblog\GraphQLGenerator\Tests\TestCase;

class TypeGeneratorModeTest extends TestCase
{
/** @var string */
private $dir;

/** @var TypeGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $typeGenerator;

private static $configs = [
'Query' => [
'type' => 'object',
'config' => [
'fields' => [
'sayHello' => ['type' => 'String!'],
],
],
]
];

public function setUp()
{
$this->dir = sys_get_temp_dir() . '/mcgweb-graphql-generator-modes';
$this->typeGenerator = $this->getMockBuilder(TypeGenerator::class)
->setMethods(['processPlaceHoldersReplacements', 'processTemplatePlaceHoldersReplacements'])
->getMock()
;
}

public function testDryRunMode()
{
$this->typeGenerator->expects($this->once())->method('processPlaceHoldersReplacements');
$this->typeGenerator->expects($this->once())->method('processTemplatePlaceHoldersReplacements');
$this->assertGenerateClassesMode(TypeGenerator::MODE_DRY_RUN);
}

public function testMappingOnlyMode()
{
$this->typeGenerator->expects($this->never())->method('processPlaceHoldersReplacements');
$this->typeGenerator->expects($this->never())->method('processTemplatePlaceHoldersReplacements');
$this->assertGenerateClassesMode(TypeGenerator::MODE_MAPPING_ONLY);
}

private function assertGenerateClassesMode($mode)
{
$classes = $this->typeGenerator->generateClasses(self::$configs, $this->dir, $mode);
$file = $this->dir.'/QueryType.php';
$this->assertEquals(['Overblog\CG\GraphQLGenerator\__Schema__\QueryType' => $this->dir.'/QueryType.php'], $classes);
if (method_exists($this, 'assertDirectoryNotExists')) {
$this->assertDirectoryNotExists($this->dir);
} else { // for phpunit 4
$this->assertFalse(file_exists($this->dir));
$this->assertFalse(is_dir($this->dir));
}
if (method_exists($this, 'assertFileNotExists')) {
$this->assertFileNotExists($file);
} else { // for phpunit 4
$this->assertFalse(file_exists($file));
$this->assertFalse(is_file($file));
}
}
}
2 changes: 1 addition & 1 deletion tests/StarWarsIntrospectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testAllowsQueryingTheSchemaForTypes()
]
];

$actual = GraphQL::execute($this->schema, $query);
$actual = GraphQL::executeQuery($this->schema, $query)->toArray();
$this->sortSchemaEntry($actual, 'types', 'name');
$this->sortSchemaEntry($expected, 'types', 'name');
$expected = ['data' => $expected];
Expand Down

0 comments on commit e6d006a

Please sign in to comment.