From c84a5d2bf8e8f302a48c97b5daef16a29f0749e4 Mon Sep 17 00:00:00 2001 From: et-nik Date: Thu, 15 Nov 2018 14:21:12 +0300 Subject: [PATCH] Add new tests --- tests/BinnAbstractTest.php | 18 ++++++++++++++++++ tests/BinnObjectTest.php | 38 ++++++++++++++++++++++++++++++++++++++ tests/BinnTest.php | 8 ++++++++ 3 files changed, 64 insertions(+) diff --git a/tests/BinnAbstractTest.php b/tests/BinnAbstractTest.php index e202ff5..d27faec 100644 --- a/tests/BinnAbstractTest.php +++ b/tests/BinnAbstractTest.php @@ -89,6 +89,19 @@ public function testPack() $this->assertNull($binn->pack('Unknown', 'Unknown')); } + + public function testGetTypeSize() + { + $binn = new BinnOver(); + + $this->assertEquals(['meta' => 1, 'data' => 0], $binn->getTypeSize($binn::BINN_TRUE)); + $this->assertEquals(['meta' => 1, 'data' => 0], $binn->getTypeSize($binn::BINN_FALSE)); + $this->assertEquals(['meta' => 3, 'data' => 1], $binn->getTypeSize($binn::BINN_STRING, 'a')); + $this->assertEquals(['meta' => 3, 'data' => 3], $binn->getTypeSize($binn::BINN_STRING, 'abc')); + $this->assertEquals(['meta' => 6, 'data' => 256], $binn->getTypeSize($binn::BINN_STRING, str_repeat('a', 256))); + $this->assertEquals(['meta' => 2, 'data' => 1], $binn->getTypeSize($binn::BINN_STORAGE_BLOB, 'a')); + $this->assertEquals(['meta' => 2, 'data' => 2], $binn->getTypeSize($binn::BINN_STORAGE_BLOB, 'ab')); + } } // Make protected methods public @@ -128,4 +141,9 @@ public function unpack($varType, $value = null) { return parent::unpack($varType, $value); } + + public function getTypeSize($varType, $value = '') + { + return parent::getTypeSize($varType, $value); + } } \ No newline at end of file diff --git a/tests/BinnObjectTest.php b/tests/BinnObjectTest.php index 3d7aa27..61ba7a9 100644 --- a/tests/BinnObjectTest.php +++ b/tests/BinnObjectTest.php @@ -84,4 +84,42 @@ public function testValidArray() $this->assertFalse(BinnObject::validArray([1 => 0, 2 => 2])); $this->assertTrue(BinnObject::validArray(['key' => 'val'])); } + + public function testObjectOpen() + { + $binnString = "\xE2\x11\x01\x05hello\xA0\x05world\x00"; + + $binn = new BinnObject(); + $binn->binnOpen($binnString); + $this->assertEquals(['hello' => 'world'], $binn->unserialize()); + } + + public function testSerialize() + { + $array = ['hello' => 'world']; + $binn = new BinnObject(); + $serialized = $binn->serialize($array); + + $this->assertEquals("\xE2\x11\x01\x05hello\xA0\x05world\x00", $serialized); + } + + /** + * @expectedException Knik\Binn\Exceptions\InvalidArrayException + */ + public function testSerializeInvalid() + { + $binn = new BinnObject(); + $binn->serialize(['list', 'array']); + } + + public function testSerializeContainers() + { + $binn = new BinnObject(); + + $array = ['test' => ['list', 'array'], 'test2' => [1 => 'map', 5 => 'array']]; + $serialized = $binn->serialize($array); + $unserizlized = $binn->unserialize($serialized); + + $this->assertEquals($array, $unserizlized); + } } \ No newline at end of file diff --git a/tests/BinnTest.php b/tests/BinnTest.php index 1b92a56..fbab5d1 100644 --- a/tests/BinnTest.php +++ b/tests/BinnTest.php @@ -90,6 +90,14 @@ public function testSerializeTypes() $this->assertEquals($array, $unserialized, '', 0.000001); } + public function testSerializeNullContainers() + { + $binn = new Binn; + $binn->setContainersClasses([]); + $serialized = $binn->serialize(['array']); + $this->assertNull($serialized); + } + public function testObject() { $array = ['name' => 'knik/binn', 'description' => 'Serialize to binary string.', 'keywords' => ["serialize", "bin"]];