mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 04:46:04 +03:00
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:
commit
54064b37b3
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user