graphql-php/tests/Executor/MutationsTest.php

157 lines
5.3 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Executor;
2015-07-15 20:05:46 +03:00
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\Executor;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
2018-09-01 18:07:06 +03:00
use GraphQL\Tests\Executor\TestClasses\Root;
2015-07-15 20:05:46 +03:00
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-09-01 18:07:06 +03:00
use GraphQL\Type\Schema;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2015-07-15 20:05:46 +03:00
2018-07-29 18:43:10 +03:00
class MutationsTest extends TestCase
2015-07-15 20:05:46 +03:00
{
// Execute: Handles mutation execution ordering
/**
* @see it('evaluates mutations serially')
*/
public function testEvaluatesMutationsSerially() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = 'mutation M {
2015-07-15 20:05:46 +03:00
first: immediatelyChangeTheNumber(newNumber: 1) {
theNumber
},
second: promiseToChangeTheNumber(newNumber: 2) {
theNumber
},
third: immediatelyChangeTheNumber(newNumber: 3) {
theNumber
}
fourth: promiseToChangeTheNumber(newNumber: 4) {
theNumber
},
fifth: immediatelyChangeTheNumber(newNumber: 5) {
theNumber
}
}';
2018-09-01 18:07:06 +03:00
$ast = Parser::parse($doc);
$mutationResult = Executor::execute($this->schema(), $ast, new Root(6));
2018-09-01 18:07:06 +03:00
$expected = [
2015-07-15 20:05:46 +03:00
'data' => [
2018-09-01 18:07:06 +03:00
'first' => ['theNumber' => 1],
'second' => ['theNumber' => 2],
'third' => ['theNumber' => 3],
'fourth' => ['theNumber' => 4],
'fifth' => ['theNumber' => 5],
],
2015-07-15 20:05:46 +03:00
];
2016-12-03 00:23:21 +03:00
$this->assertEquals($expected, $mutationResult->toArray());
2015-07-15 20:05:46 +03:00
}
2018-09-01 18:07:06 +03:00
private function schema() : Schema
{
$numberHolderType = new ObjectType([
'fields' => [
'theNumber' => ['type' => Type::int()],
],
'name' => 'NumberHolder',
]);
$schema = new Schema([
'query' => new ObjectType([
'fields' => [
'numberHolder' => ['type' => $numberHolderType],
],
'name' => 'Query',
]),
'mutation' => new ObjectType([
'fields' => [
'immediatelyChangeTheNumber' => [
'type' => $numberHolderType,
'args' => ['newNumber' => ['type' => Type::int()]],
'resolve' => function (Root $obj, $args) {
return $obj->immediatelyChangeTheNumber($args['newNumber']);
},
],
'promiseToChangeTheNumber' => [
'type' => $numberHolderType,
'args' => ['newNumber' => ['type' => Type::int()]],
'resolve' => function (Root $obj, $args) {
return $obj->promiseToChangeTheNumber($args['newNumber']);
},
],
'failToChangeTheNumber' => [
'type' => $numberHolderType,
'args' => ['newNumber' => ['type' => Type::int()]],
'resolve' => function (Root $obj, $args) {
$obj->failToChangeTheNumber();
},
],
'promiseAndFailToChangeTheNumber' => [
'type' => $numberHolderType,
'args' => ['newNumber' => ['type' => Type::int()]],
'resolve' => function (Root $obj, $args) {
return $obj->promiseAndFailToChangeTheNumber();
},
],
],
'name' => 'Mutation',
]),
]);
return $schema;
}
/**
* @see it('evaluates mutations correctly in the presense of a failed mutation')
*/
public function testEvaluatesMutationsCorrectlyInThePresenseOfAFailedMutation() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = 'mutation M {
2015-07-15 20:05:46 +03:00
first: immediatelyChangeTheNumber(newNumber: 1) {
theNumber
},
second: promiseToChangeTheNumber(newNumber: 2) {
theNumber
},
third: failToChangeTheNumber(newNumber: 3) {
theNumber
}
fourth: promiseToChangeTheNumber(newNumber: 4) {
theNumber
},
fifth: immediatelyChangeTheNumber(newNumber: 5) {
theNumber
}
sixth: promiseAndFailToChangeTheNumber(newNumber: 6) {
theNumber
}
}';
2018-09-01 18:07:06 +03:00
$ast = Parser::parse($doc);
$mutationResult = Executor::execute($this->schema(), $ast, new Root(6));
2018-09-01 18:07:06 +03:00
$expected = [
'data' => [
'first' => ['theNumber' => 1],
'second' => ['theNumber' => 2],
'third' => null,
'fourth' => ['theNumber' => 4],
'fifth' => ['theNumber' => 5],
'sixth' => null,
2015-07-15 20:05:46 +03:00
],
'errors' => [
[
'debugMessage' => 'Cannot change the number',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 8, 'column' => 7]],
],
[
'debugMessage' => 'Cannot change the number',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 17, 'column' => 7]],
],
],
2015-07-15 20:05:46 +03:00
];
$this->assertArraySubset($expected, $mutationResult->toArray(true));
2015-07-15 20:05:46 +03:00
}
}