From 96ae0c80ddce2a9a20ceca666688cc5873e300f6 Mon Sep 17 00:00:00 2001 From: Spomky Date: Thu, 16 Sep 2021 00:08:09 +0200 Subject: [PATCH] Tests --- phpunit.xml.dist | 13 +- src/OtherObject.php | 5 + src/OtherObject/SimpleObject.php | 5 +- tests/Type/OtherObject/AllTest.php | 195 +++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+), 4 deletions(-) create mode 100644 tests/Type/OtherObject/AllTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ff31d5b..d360275 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,16 @@ - + ./src diff --git a/src/OtherObject.php b/src/OtherObject.php index f78b001..cc86a74 100644 --- a/src/OtherObject.php +++ b/src/OtherObject.php @@ -38,6 +38,11 @@ public function __toString(): string return $result; } + public function getContent(): ?string + { + return $this->data; + } + /** * @return int[] */ diff --git a/src/OtherObject/SimpleObject.php b/src/OtherObject/SimpleObject.php index d00ce7f..75b18a2 100644 --- a/src/OtherObject/SimpleObject.php +++ b/src/OtherObject/SimpleObject.php @@ -17,7 +17,6 @@ use CBOR\Utils; use function chr; use InvalidArgumentException; -use function ord; final class SimpleObject extends Base { @@ -28,7 +27,7 @@ public static function supportedAdditionalInformation(): array public static function createFromLoadedData(int $additionalInformation, ?string $data): Base { - if (null !== $data && ord($data) < 32) { + if (null !== $data && $additionalInformation < 32) { throw new InvalidArgumentException('Invalid simple value. Content data should not be present.'); } @@ -58,7 +57,7 @@ public static function create(int $value): self case $value < 256: return new self(24, chr($value)); default: - throw new InvalidArgumentException('The value is not a valid simple value'); + throw new InvalidArgumentException('The value is not a valid simple value.'); } } } diff --git a/tests/Type/OtherObject/AllTest.php b/tests/Type/OtherObject/AllTest.php new file mode 100644 index 0000000..26555c0 --- /dev/null +++ b/tests/Type/OtherObject/AllTest.php @@ -0,0 +1,195 @@ +getMajorType()); + static::assertEquals(CBORObject::OBJECT_FALSE, $object->getAdditionalInformation()); + static::assertNull($object->getContent()); + + $stream = new StringStream($object->__toString()); + $decoded = $this->getDecoder()->decode($stream); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertEquals(CBORObject::OBJECT_FALSE, $decoded->getAdditionalInformation()); + static::assertNull($decoded->getContent()); + } + + /** + * @test + */ + public function createValidTrueObject(): void + { + $object = TrueObject::create(); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertEquals(CBORObject::OBJECT_TRUE, $object->getAdditionalInformation()); + + $stream = new StringStream($object->__toString()); + $decoded = $this->getDecoder()->decode($stream); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertEquals(CBORObject::OBJECT_TRUE, $decoded->getAdditionalInformation()); + static::assertNull($decoded->getContent()); + } + + /** + * @test + */ + public function createValidNullObject(): void + { + $object = NullObject::create(); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertEquals(CBORObject::OBJECT_NULL, $object->getAdditionalInformation()); + + $stream = new StringStream($object->__toString()); + $decoded = $this->getDecoder()->decode($stream); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertEquals(CBORObject::OBJECT_NULL, $decoded->getAdditionalInformation()); + static::assertNull($decoded->getContent()); + } + + /** + * @test + */ + public function createValidUndefinedObject(): void + { + $object = UndefinedObject::create(); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertEquals(CBORObject::OBJECT_UNDEFINED, $object->getAdditionalInformation()); + + $stream = new StringStream($object->__toString()); + $decoded = $this->getDecoder()->decode($stream); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertEquals(CBORObject::OBJECT_UNDEFINED, $decoded->getAdditionalInformation()); + static::assertNull($decoded->getContent()); + } + + /** + * @test + */ + public function createValidBreakObject(): void + { + $object = BreakObject::create(); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertEquals(CBORObject::OBJECT_BREAK, $object->getAdditionalInformation()); + } + + /** + * @test + * @dataProvider getSimpleObjectWithoutContent + */ + public function createValidSimpleObjectWithoutContent(int $value): void + { + $object = SimpleObject::create($value); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertEquals($value, $object->getAdditionalInformation()); + static::assertNull($object->getContent()); + + $stream = new StringStream($object->__toString()); + $decoded = $this->getDecoder()->decode($stream); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertEquals($value, $decoded->getAdditionalInformation()); + static::assertNull($decoded->getContent()); + } + + /** + * @test + * @dataProvider getSimpleObjectWithContent + */ + public function createValidSimpleObjectWithContent(int $value): void + { + $object = SimpleObject::create($value); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertEquals(CBORObject::OBJECT_SIMPLE_VALUE, $object->getAdditionalInformation()); + static::assertEquals(chr($value), $object->getContent()); + + $stream = new StringStream($object->__toString()); + $decoded = $this->getDecoder()->decode($stream); + + static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertEquals(CBORObject::OBJECT_SIMPLE_VALUE, $decoded->getAdditionalInformation()); + static::assertEquals(chr($value), $decoded->getContent()); + } + + /** + * @test + */ + public function createInvalidSimpleObjectWithContent(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid simple value. Content data should not be present.'); + + SimpleObject::createFromLoadedData(0, ' '); + } + + /** + * @test + */ + public function createInvalidSimpleObjectOutOfRange(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The value is not a valid simple value.'); + + SimpleObject::create(256); + } + + public function getSimpleObjectWithoutContent(): array + { + return [ + [0], + [18], + [19], + ]; + } + + public function getSimpleObjectWithContent(): array + { + return [ + [32], + [255], + ]; + } +}