impl. additional unit tests

This commit is contained in:
Dennis Eichhorn 2021-09-29 20:34:41 +02:00
parent 5fb18505dc
commit 4accb7b716
24 changed files with 396 additions and 31 deletions

View File

@ -114,8 +114,9 @@ final class L11nManager
/** @noinspection PhpIncludeInspection */
$lang = include $file;
foreach ($lang as $code => $translation)
$this->loadLanguage($code, $from, $translation);
foreach ($lang as $code => $translation) {
$this->loadLanguage($code, $from, $translation);
}
}
/**

View File

@ -233,7 +233,7 @@ final class HttpHeader extends HeaderAbstract
}
foreach ($this->header as $name => $arr) {
foreach ($arr as $ele => $value) {
foreach ($arr as $value) {
\header($name . ': ' . $value);
}
}

View File

@ -208,7 +208,9 @@ final class HttpResponse extends ResponseAbstract implements RenderableInterface
*/
public function endAllOutputBuffering() : void
{
$this->header->push();
if (!$this->header->isLocked()) {
$this->header->push();
}
$levels = \ob_get_level();
for ($i = 0; $i < $levels; ++$i) {

View File

@ -92,14 +92,14 @@ abstract class RequestAbstract implements MessageInterface
$key = \mb_strtolower($key);
if ($type === null) {
return $this->data[$key] ?? null;
}
if (!isset($this->data[$key])) {
return null;
}
if ($type === null) {
return $this->data[$key] ?? null;
}
switch ($type) {
case 'int':
return (int) $this->data[$key];

View File

@ -186,7 +186,7 @@ final class Argument implements UriInterface
$end = \stripos($uri, ' ', $start + 1);
if ($end === false) {
$end = \strlen($uri);
$end = \strlen($uri); // @codeCoverageIgnore
}
$path = $start < 8 ? \substr($uri, $start + 1, $end - $start - 1) : $uri;

View File

@ -15,6 +15,7 @@ declare(strict_types=1);
namespace phpOMS\Utils\IO\Zip;
use phpOMS\System\File\FileUtils;
use phpOMS\System\File\Local\Directory;
/**
* Zip class for handling zip files.
@ -102,6 +103,10 @@ class Tar implements ArchiveInterface
return false;
}
if (!\is_dir($destination)) {
Directory::create($destination, recursive: true);
}
try {
$destination = \str_replace('\\', '/', $destination);
$destination = \rtrim($destination, '/');

View File

@ -35,7 +35,7 @@ final class Iban extends ValidatorAbstract
$temp = \substr($value, 0, 2);
if ($temp === false) {
return false;
return false; // @codeCoverageIgnore
}
$enumName = 'C_' . \strtoupper($temp);

View File

@ -39,5 +39,7 @@ class CountryTest extends \PHPUnit\Framework\TestCase
self::assertEquals('', $obj->getCode3());
self::assertEquals(0, $obj->getNumeric());
self::assertEquals('', $obj->getSubdevision());
self::assertEquals('', $obj->getRegion());
self::assertFalse($obj->isDeveloped());
}
}

View File

@ -39,5 +39,6 @@ class CurrencyTest extends \PHPUnit\Framework\TestCase
self::assertEquals(0, $obj->getSubunits());
self::assertEquals('', $obj->getDecimals());
self::assertEquals('', $obj->getCountries());
self::assertEquals('', $obj->getCode());
}
}

View File

@ -96,16 +96,31 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase
* @covers phpOMS\Localization\L11nManager
* @group framework
*/
public function testLanguageFile() : void
public function testLanguageFromLanguageFile() : void
{
$this->l11nManager2 = new L11nManager('Api');
$this->l11nManager2->loadLanguageFromFile('en', 'Test', __DIR__ . '/langTestFile.php');
$this->l11nManager2->loadLanguageFromFile('en', 'Test', __DIR__ . '/invalidLangTestFile.php'); // the l11n manager doesn't do anything for invalid lang file paths
self::assertEquals('value', $this->l11nManager2->getHtml('en', 'Test', 'RandomThemeDoesNotMatterAlreadyLoaded', 'key'));
self::assertEquals(['Test' => ['key' => 'value']], $this->l11nManager2->getModuleLanguage('en'));
self::assertEquals(['key' => 'value'], $this->l11nManager2->getModuleLanguage('en', 'Test'));
}
/**
* @testdox Language data can be loaded from a file
* @covers phpOMS\Localization\L11nManager
* @group framework
*/
public function testLanguageMultipleLanguagesFromSingleFile() : void
{
$this->l11nManager2 = new L11nManager('Api');
$this->l11nManager2->loadLanguageFile('Test', __DIR__ . '/multiLangTestFile.php');
$this->l11nManager2->loadLanguageFile('Test', __DIR__ . '/invalidLangTestFile.php'); // the l11n manager doesn't do anything for invalid lang file paths
self::assertEquals('Test_EN', $this->l11nManager2->getHtml('en', 'Test', 'RandomThemeDoesNotMatterAlreadyLoaded', 'key'));
self::assertEquals('Test_DE', $this->l11nManager2->getHtml('de', 'Test', 'RandomThemeDoesNotMatterAlreadyLoaded', 'key'));
}
/**
* @testdox The numeric value can be printed based on the localization
* @covers phpOMS\Localization\L11nManager
@ -163,6 +178,7 @@ class L11nManagerTest extends \PHPUnit\Framework\TestCase
$date = new \DateTime('2020-01-01 13:45:22');
self::assertEquals('2020.01.01', $this->l11nManager->getDateTime($l11n, $date, 'medium'));
self::assertEquals('2020.01.01 01:45', $this->l11nManager->getDateTime($l11n, $date, 'long'));
self::assertEquals('', $this->l11nManager->getDateTime($l11n, null, 'long'));
}
/**

View File

@ -330,6 +330,17 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
self::assertEquals([1], $this->localization->getSpeed());
}
/**
* @testdox Localization data can be loaded from a locale file
* @covers phpOMS\Localization\Localization
* @group framework
*/
public function testLocalizationFromLanguageCode() : void
{
$l11n = Localization::fromLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO4217CharEnum::_EUR, $l11n->getCurrency());
}
/**
* @testdox Localization data can be loaded from a locale file
* @covers phpOMS\Localization\Localization
@ -337,8 +348,8 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
*/
public function testLocalizationLoading() : void
{
$this->localization->loadFromLanguage(ISO639x1Enum::_EN);
self::assertEquals(ISO4217CharEnum::_USD, $this->localization->getCurrency());
$this->localization->loadFromLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO4217CharEnum::_EUR, $this->localization->getCurrency());
}
/**
@ -348,7 +359,7 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
*/
public function testLocalizationSerialize() : void
{
$this->localization->loadFromLanguage(ISO639x1Enum::_EN);
$this->localization->loadFromLanguage(ISO639x1Enum::_DE);
$l11n1 = $this->localization->jsonSerialize();
$l11nObj = Localization::fromJson($l11n1);
@ -364,8 +375,8 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase
*/
public function testInvalidCountryLocalizationLoading() : void
{
$this->localization->loadFromLanguage(ISO639x1Enum::_EN, 'ABC');
self::assertEquals(ISO4217CharEnum::_USD, $this->localization->getCurrency());
$this->localization->loadFromLanguage(ISO639x1Enum::_DE, 'ABC');
self::assertEquals(ISO4217CharEnum::_EUR, $this->localization->getCurrency());
}
/**

View File

@ -0,0 +1,14 @@
<?php declare(strict_types=1);
return [
'en' => [
'Test' => [ // Identifier e.g. Module/application
'key' => 'Test_EN',
],
],
'de' => [
'Test' => [ // Identifier e.g. Module/application
'key' => 'Test_DE',
],
],
];

View File

@ -23,6 +23,7 @@ use phpOMS\Message\Http\Rest;
use phpOMS\Router\RouteVerb;
use phpOMS\System\MimeType;
use phpOMS\Uri\HttpUri;
use phpOMS\Localization\ISO639x1Enum;
/**
* @testdox phpOMS\tests\Message\Http\HttpRequestTest: HttpRequest wrapper for http requests
@ -106,6 +107,18 @@ class HttpRequestTest extends \PHPUnit\Framework\TestCase
self::assertEquals(RouteVerb::PUT, $request->getRouteVerb());
}
/**
* @testdox The request referer can be returned
* @covers phpOMS\Message\Http\HttpRequest<extended>
* @group framework
*/
public function testRequestRefererOutput() : void
{
$request = new HttpRequest(new HttpUri(''), $l11n = new Localization());
self::assertEquals('', $request->getReferer());
}
/**
* @testdox The route verb gets correctly inferred from the request method
* @covers phpOMS\Message\Http\HttpRequest<extended>
@ -137,6 +150,21 @@ class HttpRequestTest extends \PHPUnit\Framework\TestCase
self::assertEquals('http://www.google.com/test/path', $request->__toString());
}
/**
* @testdox The request langauge can be returned
* @covers phpOMS\Message\Http\HttpRequest<extended>
* @group framework
*/
public function testLangaugeOutput() : void
{
$request = new HttpRequest(new HttpUri('http://www.google.com/test/path'), $l11n = new Localization());
$request->header->l11n = new Localization();
$request->header->l11n->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO639x1Enum::_DE, $request->getLanguage());
}
/**
* @testdox The url hashes for the different paths get correctly generated
* @covers phpOMS\Message\Http\HttpRequest<extended>
@ -155,21 +183,6 @@ class HttpRequestTest extends \PHPUnit\Framework\TestCase
self::assertEquals($l11n, $request->header->l11n);
}
/**
* @testdox Request data can be set and returned
* @covers phpOMS\Message\Http\HttpRequest<extended>
* @group framework
*/
public function testDataInputOutput() : void
{
$request = new HttpRequest(new HttpUri('http://www.google.com/test/path'), $l11n = new Localization());
self::assertTrue($request->setData('key', 'value'));
self::assertEquals('value', $request->getData('key'));
self::assertTrue($request->hasData('key'));
self::assertEquals(['key' => 'value'], $request->getData());
}
/**
* @testdox Request data can be forcefully overwritten
* @covers phpOMS\Message\Http\HttpRequest<extended>

View File

@ -16,6 +16,8 @@ namespace phpOMS\tests\Message\Http;
use phpOMS\Message\Http\HttpResponse;
use phpOMS\System\MimeType;
use phpOMS\Localization\Localization;
use phpOMS\Localization\ISO639x1Enum;
/**
* @testdox phpOMS\tests\Message\Http\ResponseTest: HttpResponse wrapper for http responses
@ -83,6 +85,40 @@ class ResponseTest extends \PHPUnit\Framework\TestCase
self::assertFalse($this->response->remove('a'));
}
/**
* @testdox Test disabling output buffering
* @covers phpOMS\Message\Http\HttpResponse<extended>
* @group framework
*/
public function testEndAllOutputBuffering() : void
{
if (\headers_sent()) {
$this->response->header->lock();
}
$start = \ob_get_level();
\ob_start();
\ob_start();
self::assertEquals($start + 2, \ob_get_level());
$this->response->endAllOutputBuffering();
self::assertEquals(0, \ob_get_level());
}
/**
* @testdox The response langauge can be returned
* @covers phpOMS\Message\Http\HttpResponse<extended>
* @group framework
*/
public function testLangaugeOutput() : void
{
$this->response->header->l11n = new Localization();
$this->response->header->l11n->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO639x1Enum::_DE, $this->response->getLanguage());
}
/**
* @testdox Response data can be turned into an array
* @covers phpOMS\Message\Http\HttpResponse<extended>

View File

@ -0,0 +1,95 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Message;
require_once __DIR__ . '/../Autoloader.php';
use phpOMS\Localization\ISO639x1Enum;
use phpOMS\Message\RequestAbstract;
/**
* @testdox phpOMS\tests\Message\RequestAbstractTest: Abstract request
*
* @internal
*/
class RequestAbstractTest extends \PHPUnit\Framework\TestCase
{
protected $request = null;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->request = new class() extends RequestAbstract
{
public function getOrigin() : string
{
return '';
}
public function getBody(bool $optimize = false) : string
{
return '';
}
};
}
/**
* @testdox Request data can be set and returned
* @covers phpOMS\Message\RequestAbstract
* @group framework
*/
public function testDataInputOutput() : void
{
self::assertTrue($this->request->setData('key', 'value'));
self::assertEquals('value', $this->request->getData('key'));
self::assertTrue($this->request->hasData('key'));
self::assertEquals(['key' => 'value'], $this->request->getData());
}
/**
* @testdox A invalid data key returns null
* @covers phpOMS\Message\RequestAbstract
* @group framework
*/
public function testInvalidDataKeyOutput() : void
{
self::assertEquals(null, $this->request->getData('invalid'));
}
/**
* @testdox Request data can be set and returned with correct types
* @covers phpOMS\Message\RequestAbstract
* @group framework
*/
public function testDataTypeInputOutput() : void
{
$this->request->setData('key1', 1);
self::assertEquals('1', $this->request->getData('key1', 'string'));
$this->request->setData('key2', '2');
self::assertEquals(2, $this->request->getData('key2', 'int'));
$this->request->setData('key3', '1');
self::assertEquals(true, $this->request->getData('key3', 'bool'));
$this->request->setData('key4', '1.23');
self::assertEquals(1.23, $this->request->getData('key4', 'float'));
$this->request->setData('key5', 1);
self::assertEquals(1, $this->request->getData('key5', 'invalid'));
}
}

View File

@ -83,6 +83,22 @@ class SocketRouterTest extends \PHPUnit\Framework\TestCase
);
}
/**
* @testdox The routes can be removed from the router
* @covers phpOMS\Router\SocketRouter
* @group framework
*/
public function testRouteClearing() : void
{
self::assertTrue($this->router->importFromFile(__DIR__ . '/socketRouterTestFile.php'));
$this->router->clear();
self::assertEquals(
[],
$this->router->route('backend_admin -settings=general -t 123')
);
}
/**
* @testdox Routes can be added dynamically
* @covers phpOMS\Router\SocketRouter

View File

@ -95,6 +95,26 @@ class WebRouterTest extends \PHPUnit\Framework\TestCase
);
}
/**
* @testdox The routes can be removed from the router
* @covers phpOMS\Router\WebRouter
* @group framework
*/
public function testRouteClearing() : void
{
self::assertTrue($this->router->importFromFile(__DIR__ . '/webRouterTestFile.php'));
$this->router->clear();
self::assertEquals(
[],
$this->router->route(
(new HttpRequest(
new HttpUri('http://test.com/backend/admin/settings/general/something?test')
))->uri->getRoute()
)
);
}
/**
* @testdox Invalid routing verbs don't match even if the route matches
* @covers phpOMS\Router\WebRouter

View File

@ -217,4 +217,27 @@ class LocationTest extends \PHPUnit\Framework\TestCase
self::assertEquals($expected, $this->location->jsonSerialize());
self::assertEquals(\json_encode($this->location->jsonSerialize()), $this->location->serialize());
}
/**
* @testdox The location can unserialized
* @covers phpOMS\Stdlib\Base\Location
* @group framework
*/
public function testUnserialize() : void
{
$expected = [
'postal' => '0123456789',
'city' => 'city',
'country' => 'Country',
'address' => 'Some address here',
'state' => 'This is a state 123',
'geo' => [
'lat' => 12.1,
'long' => 11.2,
],
];
$this->location->unserialize(\json_encode($expected));
self::assertEquals(\json_encode($expected), $this->location->serialize());
}
}

View File

@ -39,4 +39,22 @@ class MimeTypeTest extends \PHPUnit\Framework\TestCase
self::assertTrue(true);
}
/**
* @covers phpOMS\System\MimeType
* @group framework
*/
public function testExtensionToMime() : void
{
self::assertEquals('application/pdf', MimeType::extensionToMime('pdf'));
}
/**
* @covers phpOMS\System\MimeType
* @group framework
*/
public function testInvalidExtensionToMime() : void
{
self::assertEquals('application/octet-stream', MimeType::extensionToMime('INVALID'));
}
}

View File

@ -202,6 +202,20 @@ class HttpUriTest extends \PHPUnit\Framework\TestCase
self::assertEquals('new', $obj->getPath());
}
/**
* @covers phpOMS\Uri\HttpUri
* @group framework
*/
public function testPathElementInputOutput() : void
{
$obj = new HttpUri('https://www.google.com/test/second/path.php?para1=abc&para2=2#frag');
self::assertEquals(['test', 'second', 'path'], $obj->getPathElements());
$obj->setPath('new/test');
self::assertEquals(['new', 'test'], $obj->getPathElements());
}
/**
* @testdox The path offset can be set and returned
* @covers phpOMS\Uri\HttpUri

View File

@ -82,6 +82,27 @@ class TarTest extends \PHPUnit\Framework\TestCase
self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt'));
\unlink(__DIR__ . '/test.tar');
/* @todo: fix this, this is not working "cannot open test.tar"
// second test
self::assertTrue(Tar::pack(
[__DIR__ . '/test' => 'test'],
__DIR__ . '/test.tar'
));
self::assertTrue(Tar::unpack(__DIR__ . '/test.tar', __DIR__ . '/new_dir'));
self::assertFileExists(__DIR__ . '/new_dir/test');
self::assertEquals($c, \file_get_contents(__DIR__ . '/new_dir/test/test c.txt'));
\unlink(__DIR__ . '/new_dir/test/test c.txt');
\unlink(__DIR__ . '/new_dir/test/test d.txt');
\unlink(__DIR__ . '/new_dir/test/sub/test e.txt');
\rmdir(__DIR__ . '/new_dir/test/sub');
\rmdir(__DIR__ . '/new_dir/test');
\rmdir(__DIR__ . '/new_dir');
\unlink(__DIR__ . '/test.tar');
*/
}
/**

View File

@ -83,6 +83,25 @@ class ZipTest extends \PHPUnit\Framework\TestCase
self::assertEquals($e, \file_get_contents(__DIR__ . '/test/sub/test e.txt'));
\unlink(__DIR__ . '/test.zip');
// second test
self::assertTrue(Zip::pack(
__DIR__ . '/test',
__DIR__ . '/test.zip'
));
self::assertTrue(Zip::unpack(__DIR__ . '/test.zip', __DIR__ . '/new_dir'));
self::assertFileExists(__DIR__ . '/new_dir/test');
self::assertEquals($c, \file_get_contents(__DIR__ . '/new_dir/test/test c.txt'));
\unlink(__DIR__ . '/new_dir/test/test c.txt');
\unlink(__DIR__ . '/new_dir/test/test d.txt');
\unlink(__DIR__ . '/new_dir/test/sub/test e.txt');
\rmdir(__DIR__ . '/new_dir/test/sub');
\rmdir(__DIR__ . '/new_dir/test');
\rmdir(__DIR__ . '/new_dir');
\unlink(__DIR__ . '/test.zip');
}
/**

View File

@ -228,4 +228,16 @@ class StringUtilsTest extends \PHPUnit\Framework\TestCase
StringUtils::createDiffMarkup($original, $new)
);
}
/**
* @testdox A string can be validated for shell safety
* @covers phpOMS\Utils\StringUtils
* @group framework
*/
public function testIsShellSafe() : void
{
self::assertTrue(StringUtils::isShellSafe('asdf'));
self::assertFalse(StringUtils::isShellSafe('&#;`|*?~<>^()[]{}$\\'));
self::assertFalse(StringUtils::isShellSafe('™'));
}
}

View File

@ -389,6 +389,19 @@ class ViewTest extends \PHPUnit\Framework\TestCase
self::assertEquals('<strong>Test</strong>', $view->render());
}
/**
* @testdox A view template can be build
* @covers phpOMS\Views\View<extended>
* @group framework
*/
public function testBuild() : void
{
$view = new View();
$view->setTemplate('/phpOMS/tests/Views/testTemplate');
self::assertEquals('<strong>Test</strong>', $view->build());
}
/**
* @testdox A view template can be serialized
* @covers phpOMS\Views\View<extended>
@ -440,6 +453,19 @@ class ViewTest extends \PHPUnit\Framework\TestCase
self::assertEquals('', $view->render());
}
/**
* @testdox Building a invalid template throws a PathException
* @covers phpOMS\Views\View<extended>
* @group framework
*/
public function testBuildException() : void
{
$view = new View($this->app->l11nManager);
$view->setTemplate('something.txt');
self::assertEquals('', $view->build());
}
/**
* @testdox Serializing a invalid template throws a PathException
* @covers phpOMS\Views\View<extended>