From 4de64d7f2c68470adc6ed9432722757de0d62542 Mon Sep 17 00:00:00 2001 From: Uncle Cheese Date: Sun, 18 Dec 2016 21:38:48 +1300 Subject: [PATCH] Cast default value for args --- src/Scaffolding/Util/TypeParser.php | 8 +- tests/ScaffoldingTest.php | 173 +++++++++++++++------------- 2 files changed, 100 insertions(+), 81 deletions(-) diff --git a/src/Scaffolding/Util/TypeParser.php b/src/Scaffolding/Util/TypeParser.php index c0dd98f2a..7f599fcb7 100644 --- a/src/Scaffolding/Util/TypeParser.php +++ b/src/Scaffolding/Util/TypeParser.php @@ -82,21 +82,27 @@ public function getDefaultValue() public function toArray() { $type = null; + $defaultValue = null; switch ($this->argType) { case Type::ID: $type = Type::id(); + $defaultValue = (int) $this->defaultValue; break; case Type::STRING: $type = Type::string(); + $defaultValue = (string) $this->defaultValue; break; case Type::BOOLEAN: $type = Type::boolean(); + $defaultValue = (boolean) $this->defaultValue; break; case Type::INT: $type = Type::int(); + $defaultValue = (int) $this->defaultValue; break; case Type::FLOAT: $type = Type::float(); + $defaultValue = (float) $this->defaultValue; break; } @@ -111,7 +117,7 @@ public function toArray() return [ 'type' => $type, - 'defaultValue' => $this->defaultValue + 'defaultValue' => $defaultValue ]; } } \ No newline at end of file diff --git a/tests/ScaffoldingTest.php b/tests/ScaffoldingTest.php index 785da4537..d4e35459a 100644 --- a/tests/ScaffoldingTest.php +++ b/tests/ScaffoldingTest.php @@ -681,86 +681,99 @@ public function testInputTypesAreCreated() $this->assertInstanceOf(InputObjectType::class, $type->getWrappedType()); } - // public function testTypeParser() - // { - // $parser = new TypeParser('String!=Test'); - // $this->assertTrue($parser->isRequired()); - // $this->assertEquals('String', $parser->getArgTypeName()); - // $this->assertEquals('Test', $parser->getDefaultValue()); - - // $parser = new TypeParser('String! = Test'); - // $this->assertTrue($parser->isRequired()); - // $this->assertEquals('String', $parser->getArgTypeName()); - // $this->assertEquals('Test', $parser->getDefaultValue()); - - // $parser = new TypeParser('Int!'); - // $this->assertTrue($parser->isRequired()); - // $this->assertEquals('Int', $parser->getArgTypeName()); - // $this->assertNull($parser->getDefaultValue()); - - // $parser = new TypeParser('Boolean'); - // $this->assertFalse($parser->isRequired()); - // $this->assertEquals('Boolean', $parser->getArgTypeName()); - // $this->assertNull($parser->getDefaultValue()); - - // $parser = new TypeParser('String!=Test'); - // $arr = $parser->toArray(); - // $this->assertInstanceOf(NonNull::class, $arr['type']); - // $this->assertInstanceOf(StringType::class, $arr['type']->getWrappedType()); - // $this->assertEquals('Test', $arr['defaultValue']); - - // $this->setExpectedException(InvalidArgumentException::class); - // $parser = new TypeParser(' ... Nothing'); - - // $this->setExpectedException(InvalidArgumentException::class); - // $parser = (new TypeParser('Nothing'))->toArray(); - // } - - // public function testArgsParser() - // { - // $parsers = [ - // new ArgsParser([ - // 'Test' => 'String' - // ]), - // new ArgsParser([ - // 'Test' => Type::string() - // ]), - // new ArgsParser([ - // 'Test' => ['type' => Type::string()] - // ]) - // ]; - - // foreach ($parsers as $parser) { - // $arr = $parser->toArray(); - // $this->assertArrayHasKey('Test', $arr); - // $this->assertArrayHasKey('type', $arr['Test']); - // $this->assertInstanceOf(StringType::class, $arr['Test']['type']); - // } - // } - - - // public function testOperationList() - // { - // $list = new OperationList(); - - // $list->push(new MutationScaffolder('myMutation1', 'test1')); - // $list->push(new MutationScaffolder('myMutation2', 'test2')); - - // $this->assertInstanceOf( - // MutationScaffolder::class, - // $list->findByName('myMutation1') - // ); - // $this->assertFalse($list->findByName('myMutation3')); - - // $list->removeByName('myMutation2'); - // $this->assertEquals(1, $list->count()); - - // $list->removeByName('nothing'); - // $this->assertEquals(1, $list->count()); - - // $this->setExpectedException(InvalidArgumentException::class); - // $list->push(new OperationList()); - // } + public function testTypeParser() + { + $parser = new TypeParser('String!=Test'); + $this->assertTrue($parser->isRequired()); + $this->assertEquals('String', $parser->getArgTypeName()); + $this->assertEquals('Test', $parser->getDefaultValue()); + $this->assertTrue(is_string($parser->toArray()['defaultValue'])); + + $parser = new TypeParser('String! = Test'); + $this->assertTrue($parser->isRequired()); + $this->assertEquals('String', $parser->getArgTypeName()); + $this->assertEquals('Test', $parser->getDefaultValue()); + + $parser = new TypeParser('Int!'); + $this->assertTrue($parser->isRequired()); + $this->assertEquals('Int', $parser->getArgTypeName()); + $this->assertNull($parser->getDefaultValue()); + + $parser = new TypeParser('Int!=23'); + $this->assertTrue($parser->isRequired()); + $this->assertEquals('Int', $parser->getArgTypeName()); + $this->assertEquals('23', $parser->getDefaultValue()); + $this->assertTrue(is_int($parser->toArray()['defaultValue'])); + + $parser = new TypeParser('Boolean'); + $this->assertFalse($parser->isRequired()); + $this->assertEquals('Boolean', $parser->getArgTypeName()); + $this->assertNull($parser->getDefaultValue()); + + $parser = new TypeParser('Boolean=1'); + $this->assertFalse($parser->isRequired()); + $this->assertEquals('Boolean', $parser->getArgTypeName()); + $this->assertEquals('1', $parser->getDefaultValue()); + $this->assertTrue(is_bool($parser->toArray()['defaultValue'])); + + $parser = new TypeParser('String!=Test'); + $arr = $parser->toArray(); + $this->assertInstanceOf(NonNull::class, $arr['type']); + $this->assertInstanceOf(StringType::class, $arr['type']->getWrappedType()); + $this->assertEquals('Test', $arr['defaultValue']); + + $this->setExpectedException(InvalidArgumentException::class); + $parser = new TypeParser(' ... Nothing'); + + $this->setExpectedException(InvalidArgumentException::class); + $parser = (new TypeParser('Nothing'))->toArray(); + } + + public function testArgsParser() + { + $parsers = [ + new ArgsParser([ + 'Test' => 'String' + ]), + new ArgsParser([ + 'Test' => Type::string() + ]), + new ArgsParser([ + 'Test' => ['type' => Type::string()] + ]) + ]; + + foreach ($parsers as $parser) { + $arr = $parser->toArray(); + $this->assertArrayHasKey('Test', $arr); + $this->assertArrayHasKey('type', $arr['Test']); + $this->assertInstanceOf(StringType::class, $arr['Test']['type']); + } + } + + + public function testOperationList() + { + $list = new OperationList(); + + $list->push(new MutationScaffolder('myMutation1', 'test1')); + $list->push(new MutationScaffolder('myMutation2', 'test2')); + + $this->assertInstanceOf( + MutationScaffolder::class, + $list->findByName('myMutation1') + ); + $this->assertFalse($list->findByName('myMutation3')); + + $list->removeByName('myMutation2'); + $this->assertEquals(1, $list->count()); + + $list->removeByName('nothing'); + $this->assertEquals(1, $list->count()); + + $this->setExpectedException(InvalidArgumentException::class); + $list->push(new OperationList()); + } protected function createOperationCreator($resolver = null) {