Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
viniychuk committed Dec 12, 2016
2 parents 75a8b3e + c47745e commit 4406e7c
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 60 deletions.
8 changes: 4 additions & 4 deletions Tests/DataProvider/TestScalarDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ public static function getIdTestData()
public static function getDatetimeTestData()
{
return [
[null, null, false],
[null, null, true],
[new \DateTime('now'), date('Y-m-d H:i:s'), true],
];
}

public static function getDatetimetzTestData()
{
return [
[null, null, false],
[null, null, true],
[new \DateTime('now'), date('r'), true],
];
}

public static function getDateTestData()
{
return [
[null, null, false],
[null, null, true],
[new \DateTime('now'), date('Y-m-d'), true],
];
}
Expand All @@ -130,7 +130,7 @@ public static function getTimestampTestData()
{
return [
[new \DateTime('now'), time(), true],
[null, null, false],
[null, null, true],
];
}

Expand Down
59 changes: 59 additions & 0 deletions Tests/Issues/Issue90/Issue90Schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Youshido\Tests\Issues\Issue90;

use Youshido\GraphQL\Config\Schema\SchemaConfig;
use Youshido\GraphQL\Schema\AbstractSchema;
use Youshido\GraphQL\Type\Object\ObjectType;
use Youshido\GraphQL\Type\Scalar\DateTimeType;

class Issue90Schema extends AbstractSchema
{

public function build(SchemaConfig $config)
{
$config->setQuery(
new ObjectType([
'name' => 'QueryType',
'fields' => [
'echo' => [
'type' => new DateTimeType('Y-m-d H:ia'),
'args' => [
'date' => new DateTimeType('Y-m-d H:ia')
],
'resolve' => function ($value, $args, $info) {

if (isset($args['date'])) {
return $args['date'];
}

return null;
}
]
]
])
);

$config->setMutation(
new ObjectType([
'name' => 'MutationType',
'fields' => [
'echo' => [
'type' => new DateTimeType('Y-m-d H:ia'),
'args' => [
'date' => new DateTimeType('Y-m-d H:ia')
],
'resolve' => function ($value, $args, $info) {

if (isset($args['date'])) {
return $args['date'];
}

return null;
}
]
]
])
);
}

}
76 changes: 76 additions & 0 deletions Tests/Issues/Issue90/Issue90Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Youshido\Tests\Issues\Issue90;

use Youshido\GraphQL\Execution\Processor;
use Youshido\Tests\Issues\Issue90\Issue90Schema;

/**
* User: stefano.corallo
* Date: 25/11/16
* Time: 9.39
*/
class Issue90Test extends \PHPUnit_Framework_TestCase
{

public function testQueryDateTimeTypeWithDateParameter()
{
$schema = new Issue90Schema();
$processor = new Processor($schema);
$processor->processPayload("query{ echo(date: \"2016-11-25 09:53am\") }");
$res = $processor->getResponseData();

self::assertCount(1, $res, "Invalid response array received"); //only data expected

self::assertNotNull($res['data']['echo'], "Invalid echo response received");

self::assertEquals("2016-11-25 09:53am", $res['data']['echo']);
}

public function testQueryDateTimeTypeWithoutParameter()
{
$processor = new Processor(new Issue90Schema());
$processor->processPayload("query{ echo }");
$res = $processor->getResponseData();

self::assertCount(1, $res, "Invalid response array received"); //only data expected

self::assertNull($res['data']['echo']);
}

public function testQueryDateTimeTypeWithNullParameter()
{
$processor = new Processor(new Issue90Schema());
$processor->processPayload("query{ echo(date: null) }");
$res = $processor->getResponseData();

self::assertCount(1, $res, "Invalid response array received"); //only data expected

self::assertNull($res['data']['echo'], "Error Quering with explicit date null parameter ");
}

public function testMutatingDateTimeWithParameter()
{
$schema = new Issue90Schema();
$processor = new Processor($schema);
$processor->processPayload("mutation{ echo(date: \"2016-11-25 09:53am\") }");
$res = $processor->getResponseData();

self::assertCount(1, $res, "Invalid response array received"); //only data expected

self::assertNotNull($res['data']['echo'], "Invalid echo response received during mutation of date parameter");

self::assertEquals("2016-11-25 09:53am", $res['data']['echo']);
}

public function testMutatingDateTimeWithExplicitNullParameter()
{
$schema = new Issue90Schema();
$processor = new Processor($schema);
$processor->processPayload("mutation{ echo(date: null) }");
$res = $processor->getResponseData();

self::assertCount(1, $res, "Invalid response array received"); //only data expected
self::assertNull($res['data']['echo'], "Invalid echo response received during mutation of date parameter with explicit null value");
}
}
2 changes: 1 addition & 1 deletion Tests/Library/Type/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testNormalCreatingParams()
$this->assertEquals($enumType->getNamedType(), $enumType);

$this->assertFalse($enumType->isValidValue($enumType));
$this->assertFalse($enumType->isValidValue(null));
$this->assertTrue($enumType->isValidValue(null));

$this->assertTrue($enumType->isValidValue(true));
$this->assertTrue($enumType->isValidValue('disable'));
Expand Down
10 changes: 7 additions & 3 deletions Tests/Library/Type/ScalarTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testScalarPrimitives()
foreach (call_user_func(['Youshido\Tests\DataProvider\TestScalarDataProvider', $testDataMethod]) as list($data, $serialized, $isValid)) {

$this->assertSerialization($scalarType, $data, $serialized);
$this->assertParse($scalarType, $data, $serialized);
$this->assertParse($scalarType, $data, $serialized, $typeName);

if ($isValid) {
$this->assertTrue($scalarType->isValidValue($data), $typeName . ' validation for :' . serialize($data));
Expand Down Expand Up @@ -65,9 +65,13 @@ private function assertSerialization(AbstractScalarType $object, $input, $expect
$this->assertEquals($expected, $object->serialize($input), $object->getName() . ' serialize for: ' . serialize($input));
}

private function assertParse(AbstractScalarType $object, $input, $expected)
private function assertParse(AbstractScalarType $object, $input, $expected, $typeName)
{
$this->assertEquals($expected, $object->parseValue($input), $object->getName() . ' serialize for: ' . serialize($input));
$parsed = $object->parseValue($input);
if ($parsed instanceof \DateTime) {
$expected = \DateTime::createFromFormat($typeName == 'datetime' ? 'Y-m-d H:i:s' : 'D, d M Y H:i:s O', $expected);
}
$this->assertEquals($expected, $parsed, $object->getName() . ' parse for: ' . serialize($input));
}

}
45 changes: 37 additions & 8 deletions Tests/Schema/InputObjectDefaultValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function testDefaultEnum()
'name' => 'InternalStatus',
'values' => [
[
'name' => 1,
'value' => 'ACTIVE'
'name' => 'ACTIVE',
'value' => 1,
],
[
'name' => 0,
'value' => 'DISABLED'
'name' => 'DISABLED',
'value' => 0,
],
]
]);
Expand All @@ -36,8 +36,8 @@ public function testDefaultEnum()
'name' => 'RootQuery',
'fields' => [
'stringQuery' => [
'type' => new StringType(),
'args' => [
'type' => new StringType(),
'args' => [
'statObject' => new InputObjectType([
'name' => 'StatObjectType',
'fields' => [
Expand All @@ -49,22 +49,51 @@ public function testDefaultEnum()
]
])
],
'resolve' => function ($source, $args) {
'resolve' => function ($source, $args) {
return sprintf('Result with level %s and status %s',
$args['statObject']['level'], $args['statObject']['status']
);
},
],
'enumObject' => [
'type' => new ObjectType([
'name' => 'EnumObject',
'fields' => [
'status' => $enumType
]
]),
'resolve' => function() {
return [
'status' => null
];
}
],

]
])
]);

$processor = new Processor($schema);
$processor->processPayload('{ stringQuery(statObject: { level: 1 }) }');
$result = $processor->getResponseData();
$this->assertEquals(['data' => [
'stringQuery' => 'Result with level 1 and status 1'
]], $result);

$processor->processPayload('{ stringQuery(statObject: { level: 1, status: DISABLED }) }');
$result = $processor->getResponseData();

$this->assertEquals(['data' => [
'stringQuery' => 'Result with level 1 and status ACTIVE'
'stringQuery' => 'Result with level 1 and status 0'
]], $result);

$processor->processPayload('{ enumObject { status } }');
$result = $processor->getResponseData();

$this->assertEquals(['data' => [
'enumObject' => [
'status' => null
]
]], $result);
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/Schema/InputParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public function testDateInput($query, $expected)
'stringQuery' => [
'type' => new StringType(),
'args' => [
'from' => new DateTimeType(),
'from' => new DateTimeType('Y-m-d H:i:s'),
'fromtz' => new DateTimeTzType(),
],
'resolve' => function ($source, $args) {
return sprintf('Result with %s date and %s tz',
empty($args['from']) ? 'default' : $args['from'],
empty($args['fromtz']) ? 'default' : $args['fromtz']
empty($args['from']) ? 'default' : $args['from']->format('Y-m-d H:i:s'),
empty($args['fromtz']) ? 'default' : $args['fromtz']->format('r')
);
},
],
Expand Down
8 changes: 7 additions & 1 deletion examples/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ const blogSchema = new GraphQLSchema({
return [DataProvider.getPost(2), DataProvider.getBanner(3)];
}
},
enumNull: {
type: postStatus,
resolve: () => {
return null;
}
},
scalarList: {
type: new GraphQLList(new GraphQLObjectType({
name: 'scalarObject',
Expand Down Expand Up @@ -224,7 +230,7 @@ const blogSchema = new GraphQLSchema({
})
});

var query = '{ scalarList(count: 12) { id, cost } }';
var query = '{ enumNull }';
graphql(blogSchema, query).then(result => {
console.log(JSON.stringify(result, null, 5));
process.exit(0);
Expand Down
2 changes: 1 addition & 1 deletion src/Execution/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private function prepareArgumentValue($argumentValue, AbstractType $argumentType
foreach($argumentType->getFields() as $field) {
/** @var $field Field */
if ($field->getConfig()->has('default')) {
$result[$field->getName()] = $field->getConfig()->get('default');
$result[$field->getName()] = $field->getType()->getNullableType()->parseInputValue($field->getConfig()->get('default'));
}
}
foreach ($argumentValue->getValue() as $key => $item) {
Expand Down
1 change: 0 additions & 1 deletion src/Field/AbstractField.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ abstract class AbstractField implements FieldInterface
}
protected $isFinal = false;

private $resolveFunctionCache = null;
private $nameCache = null;

public function __construct(array $config = [])
Expand Down
5 changes: 5 additions & 0 deletions src/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function parseValue($value)
return $value;
}

public function parseInputValue($value)
{
return $this->parseValue($value);
}

public function serialize($value)
{
return $value;
Expand Down
12 changes: 12 additions & 0 deletions src/Type/Enum/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getKind()
*/
public function isValidValue($value)
{
if (is_null($value)) return true;
foreach ($this->getConfig()->get('values') as $item) {
if ($value === $item['name'] || $value === $item['value']) {
return true;
Expand Down Expand Up @@ -84,4 +85,15 @@ public function parseValue($value)
return null;
}

public function parseInputValue($value)
{
foreach ($this->getConfig()->get('values') as $valueItem) {
if ($value === $valueItem['value']) {
return $valueItem['name'];
}
}

return null;
}

}
Loading

0 comments on commit 4406e7c

Please sign in to comment.