phpOMS/tests/Message/ResponseAbstractTest.php
Dennis Eichhorn 941bdac44e test fixes
2023-10-22 20:05:14 +00:00

164 lines
4.7 KiB
PHP
Executable File

<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\tests\Message;
require_once __DIR__ . '/../Autoloader.php';
use phpOMS\Message\ResponseAbstract;
/**
* @testdox phpOMS\tests\Message\ResponseAbstractTest: Abstract response
*
* @internal
*/
final class ResponseAbstractTest extends \PHPUnit\Framework\TestCase
{
protected $response = null;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->response = new class() extends ResponseAbstract
{
public function toArray() : array
{
return [1];
}
public function getBody(bool $optimize = false) : string
{
return '';
}
};
}
/**
* @testdox The response has the expected default values after initialization
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testDefault() : void
{
self::assertNull($this->response->getData('asdf'));
self::assertEquals('', $this->response->getBody());
}
/**
* @testdox The response can be json serialized
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testJsonSerialize() : void
{
self::assertEquals([1], $this->response->jsonSerialize());
}
public function testDataAllInputOutput() : void
{
$this->response->set('asdf', false);
self::assertEquals(['asdf' => false], $this->response->getData());
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testDataInputOutput() : void
{
$this->response->set('asdf', false);
self::assertFalse($this->response->getData('asdf'));
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testDataStringInputOutput() : void
{
$this->response->set('asdf', 1);
self::assertEquals('1', $this->response->getDataString('asdf'));
self::assertEquals('1', $this->response->getData('asdf', 'string'));
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testDataBoolInputOutput() : void
{
$this->response->set('asdf', 1);
self::assertEquals(true, $this->response->getDataBool('asdf'));
self::assertEquals(true, $this->response->getData('asdf', 'bool'));
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testDataFloatInputOutput() : void
{
$this->response->set('asdf', 1);
self::assertEquals(1.0, $this->response->getDataFloat('asdf'));
self::assertEquals(1.0, $this->response->getData('asdf', 'float'));
}
/**
* @group framework
*/
public function testDataJsonInputOutput() : void
{
$this->response->set('asdf', '[1,2,3]');
self::assertEquals([1,2,3], $this->response->getDataJson('asdf'));
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testDataDateTimeInputOutput() : void
{
$this->response->set('asdf', '2023-01-01');
self::assertEquals((new \DateTime('2023-01-01'))->format('Y-m-d'), $this->response->getDataDateTime('asdf')->format('Y-m-d'));
self::assertEquals((new \DateTime('2023-01-01'))->format('Y-m-d'), $this->response->getData('asdf', 'DateTime')->format('Y-m-d'));
}
public function testDataInvalidTypeInputOutput() : void
{
$this->response->set('asdf', 1);
self::assertEquals(1, $this->response->getData('asdf', 'invalid'));
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
* @group framework
*/
public function testInvalidDataTypeInputOutput() : void
{
self::assertEquals(null, $this->response->getDataString('a'));
self::assertEquals(null, $this->response->getDataBool('a'));
self::assertEquals(null, $this->response->getDataInt('a'));
self::assertEquals(null, $this->response->getDataFloat('a'));
self::assertEquals(null, $this->response->getDataDateTime('a'));
}
}