Skip to content

Commit

Permalink
Merge branch '0.12' into 0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Jan 13, 2021
2 parents 161fda8 + d0b9710 commit ab14d1e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Transformer/ArgumentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info
{
$isRequired = '!' === $argType[\strlen($argType) - 1];
$isMultiple = '[' === $argType[0];
$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
$isStrictMultiple = false;
if ($isMultiple) {
$isStrictMultiple = '!' === $argType[\strpos($argType, ']') - 1];
}

$endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0);
$type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType));

$result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
Expand Down
74 changes: 74 additions & 0 deletions tests/Transformer/ArgumentsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Overblog\GraphQLBundle\Tests\Transformer;

use Generator;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
Expand Down Expand Up @@ -294,4 +297,75 @@ public function testRaisedErrorsForMultipleInputs(): void
$this->assertEquals($e->toState(), $expected);
}
}

public function getWrappedInputObject(): Generator
{
$inputObject = new InputObjectType([
'name' => 'InputType1',
'fields' => [
'field1' => Type::string(),
'field2' => Type::int(),
'field3' => Type::boolean(),
],
]);
yield [$inputObject, false];
yield [new NonNull($inputObject), false];
}

/** @dataProvider getWrappedInputObject */
public function testInputObjectWithWrappingType(Type $type): void
{
$transformer = $this->getTransformer([
'InputType1' => ['type' => 'input', 'class' => InputType1::class],
], new ConstraintViolationList()
);
$info = $this->getResolveInfo(self::getTypes());

$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];

$inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1');

/* @var InputType1 $inputValue */
$this->assertInstanceOf(InputType1::class, $inputValue);
$this->assertEquals($inputValue->field1, $data['field1']);
$this->assertEquals($inputValue->field2, $data['field2']);
$this->assertEquals($inputValue->field3, $data['field3']);
}

public function getWrappedInputObjectList(): Generator
{
$inputObject = new InputObjectType([
'name' => 'InputType1',
'fields' => [
'field1' => Type::string(),
'field2' => Type::int(),
'field3' => Type::boolean(),
],
]);
yield [new ListOfType($inputObject)];
yield [new ListOfType(new NonNull($inputObject))];
yield [new NonNull(new ListOfType($inputObject))];
yield [new NonNull(new ListOfType(new NonNull($inputObject)))];
}

/** @dataProvider getWrappedInputObjectList */
public function testInputObjectWithWrappingTypeList(Type $type): void
{
$transformer = $this->getTransformer(
['InputType1' => ['type' => 'input', 'class' => InputType1::class]],
new ConstraintViolationList()
);
$info = $this->getResolveInfo(self::getTypes());

$data = ['field1' => 'hello', 'field2' => 12, 'field3' => true];

$inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1');
$inputValue = \reset($inputValue);

/* @var InputType1 $inputValue */
$this->assertInstanceOf(InputType1::class, $inputValue);
$this->assertEquals($inputValue->field1, $data['field1']);
$this->assertEquals($inputValue->field2, $data['field2']);
$this->assertEquals($inputValue->field3, $data['field3']);
}
}

0 comments on commit ab14d1e

Please sign in to comment.