Merge pull request #511 from spawnia/scalar-coercion

Resolve todo in Boolean coercion, add explanation, update test names to match reference implementation
This commit is contained in:
Vladimir Razuvaev 2019-07-01 13:03:03 +07:00 committed by GitHub
commit 54064b37b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 18 deletions

View File

@ -20,11 +20,14 @@ class BooleanType extends ScalarType
public $description = 'The `Boolean` scalar type represents `true` or `false`.'; public $description = 'The `Boolean` scalar type represents `true` or `false`.';
/** /**
* @param mixed $value * Coerce the given value to a boolean.
* *
* @return bool * The GraphQL spec leaves this up to the implementations, so we just do what
* PHP does natively to make this intuitive for developers.
*
* @param mixed $value
*/ */
public function serialize($value) public function serialize($value) : bool
{ {
return (bool) $value; return (bool) $value;
} }

View File

@ -13,9 +13,9 @@ class ScalarSerializationTest extends TestCase
{ {
// Type System: Scalar coercion // Type System: Scalar coercion
/** /**
* @see it('serializes output int') * @see it('serializes output as Int')
*/ */
public function testSerializesOutputInt() : void public function testSerializesOutputAsInt() : void
{ {
$intType = Type::int(); $intType = Type::int();
@ -114,9 +114,9 @@ class ScalarSerializationTest extends TestCase
} }
/** /**
* @see it('serializes output float') * @see it('serializes output as Float')
*/ */
public function testSerializesOutputFloat() : void public function testSerializesOutputAsFloat() : void
{ {
$floatType = Type::float(); $floatType = Type::float();
@ -149,9 +149,9 @@ class ScalarSerializationTest extends TestCase
} }
/** /**
* @see it('serializes output strings') * @see it('serializes output as String')
*/ */
public function testSerializesOutputStrings() : void public function testSerializesOutputAsString() : void
{ {
$stringType = Type::string(); $stringType = Type::string();
@ -181,23 +181,27 @@ class ScalarSerializationTest extends TestCase
} }
/** /**
* @see it('serializes output boolean') * @see it('serializes output as Boolean')
*/ */
public function testSerializesOutputBoolean() : void public function testSerializesOutputAsBoolean() : void
{ {
$boolType = Type::boolean(); $boolType = Type::boolean();
self::assertTrue($boolType->serialize('string'));
self::assertFalse($boolType->serialize(''));
self::assertTrue($boolType->serialize('1'));
self::assertTrue($boolType->serialize(1));
self::assertFalse($boolType->serialize(0));
self::assertTrue($boolType->serialize(true)); self::assertTrue($boolType->serialize(true));
self::assertTrue($boolType->serialize(1));
self::assertTrue($boolType->serialize('1'));
self::assertTrue($boolType->serialize('string'));
self::assertFalse($boolType->serialize(false)); self::assertFalse($boolType->serialize(false));
// TODO: how should it behave on '0'? self::assertFalse($boolType->serialize(0));
self::assertFalse($boolType->serialize('0'));
self::assertFalse($boolType->serialize(''));
} }
public function testSerializesOutputID() : void /**
* @see it('serializes output as ID')
*/
public function testSerializesOutputAsID() : void
{ {
$idType = Type::id(); $idType = Type::id();