From 9e2c1dae8777663409b6b2773a1752b4134720b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Tue, 12 Dec 2017 09:53:15 +0100 Subject: [PATCH] Add test --- tests/Type/ObjectIdStub.php | 23 +++++++++++++++++++++++ tests/Type/ScalarSerializationTest.php | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/Type/ObjectIdStub.php diff --git a/tests/Type/ObjectIdStub.php b/tests/Type/ObjectIdStub.php new file mode 100644 index 0000000..5379591 --- /dev/null +++ b/tests/Type/ObjectIdStub.php @@ -0,0 +1,23 @@ +id = $id; + } + + public function __toString() + { + return (string) $this->id; + } +} diff --git a/tests/Type/ScalarSerializationTest.php b/tests/Type/ScalarSerializationTest.php index d554ccf..edebcad 100644 --- a/tests/Type/ScalarSerializationTest.php +++ b/tests/Type/ScalarSerializationTest.php @@ -178,4 +178,25 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase // TODO: how should it behave on '0'? } + + public function testSerializesOutputID() + { + $idType = Type::id(); + + $this->assertSame('string', $idType->serialize('string')); + $this->assertSame('', $idType->serialize('')); + $this->assertSame('1', $idType->serialize('1')); + $this->assertSame('1', $idType->serialize(1)); + $this->assertSame('0', $idType->serialize(0)); + $this->assertSame('true', $idType->serialize(true)); + $this->assertSame('false', $idType->serialize(false)); + $this->assertSame('2', $idType->serialize(new ObjectIdStub(2))); + + try { + $idType->serialize(new \stdClass()); + $this->fail('Expected exception was not thrown'); + } catch (InvariantViolation $e) { + $this->assertEquals('ID type cannot represent non scalar value: instance of stdClass', $e->getMessage()); + } + } }