Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 15, 2021
1 parent b54d01b commit 96ae0c8
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 4 deletions.
13 changes: 12 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false" backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">./src</directory>
Expand Down
5 changes: 5 additions & 0 deletions src/OtherObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function __toString(): string
return $result;
}

public function getContent(): ?string
{
return $this->data;
}

/**
* @return int[]
*/
Expand Down
5 changes: 2 additions & 3 deletions src/OtherObject/SimpleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use CBOR\Utils;
use function chr;
use InvalidArgumentException;
use function ord;

final class SimpleObject extends Base
{
Expand All @@ -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.');
}

Expand Down Expand Up @@ -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.');
}
}
}
195 changes: 195 additions & 0 deletions tests/Type/OtherObject/AllTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

declare(strict_types=1);

/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace CBOR\Test\Type\OtherObject;

use CBOR\CBORObject;
use CBOR\OtherObject\BreakObject;
use CBOR\OtherObject\FalseObject;
use CBOR\OtherObject\NullObject;
use CBOR\OtherObject\SimpleObject;
use CBOR\OtherObject\TrueObject;
use CBOR\OtherObject\UndefinedObject;
use CBOR\StringStream;
use CBOR\Test\Type\BaseTestCase;
use function chr;
use InvalidArgumentException;

/**
* @internal
*/
final class AllTest extends BaseTestCase
{
/**
* @test
*/
public function createValidFalseObject(): void
{
$object = FalseObject::create();

static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->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],
];
}
}

0 comments on commit 96ae0c8

Please sign in to comment.