From a34bb68d65833491b958e3026ac5dd064210bc0a Mon Sep 17 00:00:00 2001 From: spawnia Date: Sun, 23 Jun 2019 21:03:17 +0200 Subject: [PATCH] Resolve todo in Boolean coercion, add explanation, update test names to match reference implementation --- src/Type/Definition/BooleanType.php | 9 ++++--- tests/Type/ScalarSerializationTest.php | 34 ++++++++++++++------------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Type/Definition/BooleanType.php b/src/Type/Definition/BooleanType.php index 56b5f0f..f9c91bb 100644 --- a/src/Type/Definition/BooleanType.php +++ b/src/Type/Definition/BooleanType.php @@ -20,11 +20,14 @@ class BooleanType extends ScalarType 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; } diff --git a/tests/Type/ScalarSerializationTest.php b/tests/Type/ScalarSerializationTest.php index 17f2368..f7b3c6d 100644 --- a/tests/Type/ScalarSerializationTest.php +++ b/tests/Type/ScalarSerializationTest.php @@ -13,9 +13,9 @@ class ScalarSerializationTest extends TestCase { // 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(); @@ -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(); @@ -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(); @@ -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(); - 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(1)); + self::assertTrue($boolType->serialize('1')); + self::assertTrue($boolType->serialize('string')); + 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();