Commit 80360d79 authored by Dainis Abols's avatar Dainis Abols
Browse files

Type class tests

parent 60bd0eb5
<?php
declare(strict_types=1);
namespace Lu\LuApi\Tests\Type;
use Lu\LuApi\Types\RequestType;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Test for type class `RequestType`
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 21.02.2022
*/
class RequestTypeTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* Initialize global settings
*/
private function createGlobals()
{
global $GLOBALS;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['RequestType']['className'] = \Lu\LuApi\Types\RequestType::class;
$GLOBALS['TYPO3_CONF_VARS']['LOG'] = null;
}
/**
* Check type and methods
*
* @test
*/
public function typeRequestTypeTest()
{
$this->createGlobals();
// Assert we get correct type
$val = new RequestType(RequestType::BE);
$this->assertEquals(\Lu\LuApi\Types\RequestType::class, get_class($val), 'Wrong instance for `requesttype`');
// Assert `getValue`
$this->assertTrue(method_exists($val, 'getValue'), 'Method not found [getValue]');
$this->assertEquals('be', $val->getValue(), 'Value fetch failed');
// Assert `validate`
$this->assertTrue(method_exists($val, 'validate'), 'Method not found [validate]');
$this->assertNull($val->validate(''), 'Validation passed unsuccesfully');
$this->assertNull($val->validate('AA'), 'Validation passed unsuccesfully');
$this->assertEquals('be', $val->validate(RequestType::BE), 'Validation failed');
// Assert `equals`
$this->assertTrue(method_exists($val, 'equals'), 'Method not found [equals]');
$this->assertTrue($val->equals(RequestType::BE), 'Results do not match for [equals]');
// Assert `toString`
$this->assertTrue(method_exists($val, 'toString'), 'Method not found [toString]');
$this->assertIsString($val->toString(), 'Unexpected type received [toString]');
$this->assertEquals('be', $val->toString(), 'Results do not match [toString]');
// Assert `toInt`
$this->assertTrue(method_exists($val, 'toInt'), 'Method not found [toInt]');
$this->assertIsInt($val->toInt(), 'Unexpected type received [toInt]');
$this->assertEquals(0, $val->toInt(), 'Results do not match [toInt]');
// Assert `toList`
$this->assertTrue(method_exists($val, 'toList'), 'Method not found [toList]');
$this->assertIsArray($val->toList(), 'Unexpected type received [toList]');
$this->assertEquals(['be'], $val->toList(), 'Results do not match [toList]');
// Assert `toJson`
$this->assertTrue(method_exists($val, 'toJson'), 'Method not found [toJson]');
$this->assertJson($val->toJson(), 'Unexpected type received [toJson]');
$this->assertEquals('["be"]', $val->toJson(), 'Results do not match [toJson]');
// Assert `toXml`
$this->assertTrue(method_exists($val, 'toXml'), 'Method not found [toXml]');
$this->assertJson($val->toJson(), 'Unexpected type received [toXml]');
$this->assertEquals('<?xml version="1.0"?><RequestType><value>be</value></RequestType>', $val->toXml(), 'Results do not match [toXml]');
}
}
<?php
declare(strict_types=1);
namespace Lu\LuApi\Tests\Type;
use Lu\LuApi\Types\Table;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Test for type class `Table`
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 21.02.2022
*/
class TableTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* Initialize global settings
*/
private function createGlobals()
{
global $GLOBALS;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['Table']['className'] = \Lu\LuApi\Types\Table::class;
$GLOBALS['TYPO3_CONF_VARS']['LOG'] = null;
}
/**
* Check type and methods
*
* @test
*/
public function typeTableTest()
{
$this->createGlobals();
// Assert we get correct type
$val = new Table('777table');
$this->assertEquals(\Lu\LuApi\Types\Table::class, get_class($val), 'Wrong instance for `table`');
// Assert `getValue`
$this->assertTrue(method_exists($val, 'getValue'), 'Method not found [getValue]');
$this->assertEquals('777table', $val->getValue(), 'Value fetch failed');
// Assert `validate`
$this->assertTrue(method_exists($val, 'validate'), 'Method not found [validate]');
$this->assertNull($val->validate(''), 'Validation passed unsuccesfully');
$this->assertEquals('777table', $val->validate('777table'), 'Validation failed');
// Assert `equals`
$this->assertTrue(method_exists($val, 'equals'), 'Method not found [equals]');
$this->assertTrue($val->equals('777table'), 'Results do not match for [equals]');
// Assert `toString`
$this->assertTrue(method_exists($val, 'toString'), 'Method not found [toString]');
$this->assertIsString($val->toString(), 'Unexpected type received [toString]');
$this->assertEquals('777table', $val->toString(), 'Results do not match [toString]');
// Assert `toInt`
$this->assertTrue(method_exists($val, 'toInt'), 'Method not found [toInt]');
$this->assertIsInt($val->toInt(), 'Unexpected type received [toInt]');
$this->assertEquals(777, $val->toInt(), 'Results do not match [toInt]');
// Assert `toList`
$this->assertTrue(method_exists($val, 'toList'), 'Method not found [toList]');
$this->assertIsArray($val->toList(), 'Unexpected type received [toList]');
$this->assertEquals(['777table'], $val->toList(), 'Results do not match [toList]');
// Assert `toJson`
$this->assertTrue(method_exists($val, 'toJson'), 'Method not found [toJson]');
$this->assertJson($val->toJson(), 'Unexpected type received [toJson]');
$this->assertEquals('["777table"]', $val->toJson(), 'Results do not match [toJson]');
// Assert `toXml`
$this->assertTrue(method_exists($val, 'toXml'), 'Method not found [toXml]');
$this->assertJson($val->toJson(), 'Unexpected type received [toXml]');
$this->assertEquals('<?xml version="1.0"?><Table><value>777table</value></Table>', $val->toXml(), 'Results do not match [toXml]');
}
}
<?php
declare(strict_types=1);
namespace Lu\LuApi\Tests\Type;
use Lu\LuApi\Types\Username;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Test for type class `Username`
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 21.02.2022
*/
class UsernameTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* Initialize global settings
*/
private function createGlobals()
{
global $GLOBALS;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['Username']['className'] = \Lu\LuApi\Types\Username::class;
$GLOBALS['TYPO3_CONF_VARS']['LOG'] = null;
}
/**
* Check type and methods
*
* @test
*/
public function typeUsernameTest()
{
$this->createGlobals();
// Assert we get correct type
$val = new Username('777name');
$this->assertEquals(\Lu\LuApi\Types\Username::class, get_class($val), 'Wrong instance for `username`');
// Assert `getValue`
$this->assertTrue(method_exists($val, 'getValue'), 'Method not found [getValue]');
$this->assertEquals('777name', $val->getValue(), 'Value fetch failed');
// Assert `validate`
$this->assertTrue(method_exists($val, 'validate'), 'Method not found [validate]');
$this->assertNull($val->validate(''), 'Validation passed unsuccesfully');
$this->assertEquals('777name', $val->validate('777name'), 'Validation failed');
// Assert `equals`
$this->assertTrue(method_exists($val, 'equals'), 'Method not found [equals]');
$this->assertTrue($val->equals('777name'), 'Results do not match for [equals]');
// Assert `toString`
$this->assertTrue(method_exists($val, 'toString'), 'Method not found [toString]');
$this->assertIsString($val->toString(), 'Unexpected type received [toString]');
$this->assertEquals('777name', $val->toString(), 'Results do not match [toString]');
// Assert `toInt`
$this->assertTrue(method_exists($val, 'toInt'), 'Method not found [toInt]');
$this->assertIsInt($val->toInt(), 'Unexpected type received [toInt]');
$this->assertEquals(777, $val->toInt(), 'Results do not match [toInt]');
// Assert `toList`
$this->assertTrue(method_exists($val, 'toList'), 'Method not found [toList]');
$this->assertIsArray($val->toList(), 'Unexpected type received [toList]');
$this->assertEquals(['777name'], $val->toList(), 'Results do not match [toList]');
// Assert `toJson`
$this->assertTrue(method_exists($val, 'toJson'), 'Method not found [toJson]');
$this->assertJson($val->toJson(), 'Unexpected type received [toJson]');
$this->assertEquals('["777name"]', $val->toJson(), 'Results do not match [toJson]');
// Assert `toXml`
$this->assertTrue(method_exists($val, 'toXml'), 'Method not found [toXml]');
$this->assertJson($val->toJson(), 'Unexpected type received [toXml]');
$this->assertEquals('<?xml version="1.0"?><Username><value>777name</value></Username>', $val->toXml(), 'Results do not match [toXml]');
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment