Skip to content

Commit

Permalink
Cast default value for args
Browse files Browse the repository at this point in the history
  • Loading branch information
Uncle Cheese committed Jan 20, 2017
1 parent d92d3ad commit 4de64d7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 81 deletions.
8 changes: 7 additions & 1 deletion src/Scaffolding/Util/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Expand All @@ -111,7 +117,7 @@ public function toArray()

return [
'type' => $type,
'defaultValue' => $this->defaultValue
'defaultValue' => $defaultValue
];
}
}
173 changes: 93 additions & 80 deletions tests/ScaffoldingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 4de64d7

Please sign in to comment.