Add tests e.g. preloader

This commit is contained in:
Dennis Eichhorn 2020-10-05 19:28:57 +02:00
parent 14fcafc9a5
commit 3c69c2c209
25 changed files with 549 additions and 83 deletions

View File

@ -90,18 +90,18 @@ final class ApplicationManager
* @param string $destination Destination of the application
* @param string $theme Theme
*
* @return void
* @return bool
*
* @todo Orange-Management/phpOMS#245
* [ApplicationManager] Implement test for invalid source and invalid destination
*
* @since 1.0.0
*/
public function install(string $source, string $destination, string $theme = 'Default') : void
public function install(string $source, string $destination, string $theme = 'Default') : bool
{
$destination = \rtrim($destination, '\\/');
if (!\file_exists($source) || \file_exists($destination)) {
return;
return false;
}
$app = $this->loadInfo(\rtrim($source, '/\\') . '/info.json');
@ -111,20 +111,21 @@ final class ApplicationManager
$this->installTheme($destination, $theme);
$this->installFromModules($app);
$files = Directory::list($destination);
$files = Directory::list($destination, '*', true);
foreach ($files as $file) {
if (!\is_file($destination . '/' . $file)) {
continue;
}
$content = \file_get_contents($destination . '/' . $file);
if ($content === false) {
continue;
continue; // @codeCoverageIgnore
}
\file_put_contents($destination . '/' . $file, \str_replace('{APPNAME}', \basename($destination), $content));
}
return true;
}
/**

View File

@ -541,14 +541,14 @@ final class EigenvalueDecomposition
--$l;
}
if ($l == $n) {
if ($l === $n) {
$this->H[$n][$n] = $this->H[$n][$n] + $exshift;
$this->D[$n] = $this->H[$n][$n];
$this->E[$n] = 0.0;
$iter = 0;
--$n;
} elseif ($l == $n - 1) {
} elseif ($l === $n - 1) {
$w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n];
$p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0;
$q = $p * $p + $w;
@ -691,7 +691,10 @@ final class EigenvalueDecomposition
$s = $p < 0 ? -\sqrt($p * $p + $q * $q + $r * $r) : \sqrt($p * $p + $q * $q + $r * $r);
if ($s != 0) {
if ($s == 0) {
continue;
}
if ($k !== $m) {
$this->H[$k][$k - 1] = -$s * $x;
} elseif ($l !== $m) {
@ -742,7 +745,6 @@ final class EigenvalueDecomposition
}
}
}
}
if ($norm == 0) {
return;

View File

@ -111,7 +111,7 @@ final class LUDecomposition
}
}
if ($p != $j) {
if ($p !== $j) {
for ($k = 0; $k < $this->n; ++$k) {
$t = $this->LU[$p][$k];
$this->LU[$p][$k] = $this->LU[$j][$k];

View File

@ -152,7 +152,7 @@ final class PackageManager
*/
private function hashFiles() : string
{
$files = Directory::list($this->extractPath);
$files = Directory::list($this->extractPath, '*', true);
$state = \sodium_crypto_generichash_init();
foreach ($files as $file) {

View File

@ -108,7 +108,7 @@ final class Preloader
$fh = \opendir($path);
if ($fh === false) {
return;
return; // @codeCoverageIgnore
}
while ($file = \readdir($fh)) {

View File

@ -77,14 +77,20 @@ final class SocketRouter implements RouterInterface
*
* @since 1.0.0
*/
public function add(string $route, $destination) : void
{
public function add(
string $route,
$destination,
array $validation = [],
string $dataPattern = ''
) : void {
if (!isset($this->routes[$route])) {
$this->routes[$route] = [];
}
$this->routes[$route][] = [
'dest' => $destination,
'validation' => empty($validation) ? null : $validation,
'pattern' => empty($dataPattern) ? null : $dataPattern,
];
}
@ -136,7 +142,16 @@ final class SocketRouter implements RouterInterface
}
}
$bound[] = ['dest' => $d['dest']];
$temp = ['dest' => $d['dest']];
// fill data
if (isset($d['pattern'])) {
\preg_match($d['pattern'], $uri, $matches);
$temp['data'] = $matches;
}
$bound[] = $temp;
}
}

View File

@ -83,8 +83,13 @@ final class WebRouter implements RouterInterface
*
* @since 1.0.0
*/
public function add(string $route, $destination, int $verb = RouteVerb::GET, bool $csrf = false, array $validation = [], string $dataPattern = '') : void
{
public function add(
string $route,
$destination,
int $verb = RouteVerb::GET,
bool $csrf = false, array $validation = [],
string $dataPattern = ''
) : void {
if (!isset($this->routes[$route])) {
$this->routes[$route] = [];
}

View File

@ -42,10 +42,13 @@ class Tar implements ArchiveInterface
$tar = new \PharData($destination);
/**
* @var string $source
* @var string $relative
*/
foreach ($sources as $source => $relative) {
if (\is_int($source)) {
$source = $relative;
}
if (($source = \realpath($source)) === false
|| ($source = \str_replace('\\', '/', $source)) === false
|| !\file_exists($source)

View File

@ -45,10 +45,13 @@ class Zip implements ArchiveInterface
}
/**
* @var string $source
* @var string $relative
*/
foreach ($sources as $source => $relative) {
if (\is_int($source)) {
$source = $relative;
}
if (($source = \realpath($source)) === false
|| ($source = \str_replace('\\', '/', $source)) === false
|| !\file_exists($source)

View File

@ -12,6 +12,7 @@
*/
declare(strict_types=1);
// @codeCoverageIgnoreStart
require_once __DIR__ . '/Preloader.php';
$preloader = new \phpOMS\Preloader();
@ -35,3 +36,4 @@ $preloader->includePath(__DIR__ . '/Account')
->includePath(__DIR__ . '/Uri')
->includePath(__DIR__ . '/Views')
->load();
// @codeCoverageIgnoreEnd

View File

@ -0,0 +1,74 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @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\Application;
require_once __DIR__ . '/../Autoloader.php';
use phpOMS\Application\ApplicationManager;
use phpOMS\Application\ApplicationAbstract;
use phpOMS\Router\WebRouter;
use phpOMS\Dispatcher\Dispatcher;
use Model\CoreSettings;
use Modules\CMS\Models\Application;
use phpOMS\Module\ModuleManager;
/**
* @testdox phpOMS\tests\Application\ApplicationManagerTest: Application manager
*
* @internal
*/
class ApplicationManagerTest extends \PHPUnit\Framework\TestCase
{
protected ApplicationManager $appManager;
protected function setUp() : void
{
$app = new class() extends ApplicationAbstract {
protected string $appName = 'Api';
};
$app->appName = 'Api';
$app->dbPool = $GLOBALS['dbpool'];
$app->router = new WebRouter();
$app->dispatcher = new Dispatcher($app);
$app->appSettings = new CoreSettings($app->dbPool->get('admin'));
$app->moduleManager = new ModuleManager($app, __DIR__ . '/../../../Modules');
$this->appManager = new ApplicationManager($app->moduleManager);
}
public function testInstall() : void
{
self::markTestIncomplete();
}
public function testInvalidSourceDestinationInstallPath() : void
{
self::assertFalse($this->appManager->install(__DIR__ . '/invalid', __DIR__));
self::assertFalse($this->appManager->install(__DIR__, __DIR__));
}
public function testMissingApplicationInfoFile() : void
{
$this->expectException(\phpOMS\System\File\PathException::class);
self::assertFalse($this->appManager->install(__DIR__, __DIR__ . '/newapp'));
}
public function testInstallFromModules() : void
{
self::markTestIncomplete();
}
}

View File

@ -33,4 +33,41 @@ class AutoloaderTest extends \PHPUnit\Framework\TestCase
self::assertTrue(Autoloader::exists('\phpOMS\Autoloader'));
self::assertFalse(Autoloader::exists('\Does\Not\Exist'));
}
public function testLoading() : void
{
Autoloader::defaultAutoloader('\phpOMS\tests\TestLoad');
$includes = \get_included_files();
self::assertTrue(\in_array(\realpath(__DIR__ . '/TestLoad.php'), $includes));
}
public function testManualPathLoading() : void
{
Autoloader::addPath(__DIR__ . '/../');
Autoloader::defaultAutoloader('\tests\TestLoad2');
Autoloader::defaultAutoloader('\tests\Invalid');
$includes = \get_included_files();
self::assertTrue(\in_array(\realpath(__DIR__ . '/TestLoad2.php'), $includes));
}
public function testOpcodeCacheInvalidation() : void
{
if (!\extension_loaded('opcache')) {
$this->markTestSkipped(
'The opcache extension is not available.'
);
}
Autoloader::defaultAutoloader('\phpOMS\tests\TestLoad3');
Autoloader::invalidate(__DIR__ . '/TestLoad3.php');
self::assertTrue(\opcache_is_script_cached(__DIR__ . '/TestLoad3.php'));
}
public function testUncachedInvalidation() : void
{
self::assertFalse(\opcache_is_script_cached(__DIR__ . '/TestLoad4.php'));
self::assertFalse(Autoloader::invalidate(__DIR__ . '/TestLoad4.php'));
}
}

View File

@ -70,7 +70,8 @@ class EigenvalueDecompositionTest extends \PHPUnit\Framework\TestCase
$eig = new EigenvalueDecomposition($A);
self::assertEqualsWithDelta([0, 2, 5], $eig->getRealEigenvalues()->toArray(), 0.2);
self::assertEqualsWithDelta([0, 2, 5], $eig->getRealEigenvalues()->toArray(), 0.1);
self::assertEqualsWithDelta([0, 0, 0], $eig->getImagEigenvalues()->toArray(), 0.1);
}
/**
@ -135,7 +136,8 @@ class EigenvalueDecompositionTest extends \PHPUnit\Framework\TestCase
$eig = new EigenvalueDecomposition($A);
self::assertEqualsWithDelta([-5, 3, 6], $eig->getRealEigenvalues()->toArray(), 0.2);
self::assertEqualsWithDelta([-5, 3, 6], $eig->getRealEigenvalues()->toArray(), 0.1);
self::assertEqualsWithDelta([0, 0, 0], $eig->getImagEigenvalues()->toArray(), 0.1);;
}
/**
@ -234,4 +236,22 @@ class EigenvalueDecompositionTest extends \PHPUnit\Framework\TestCase
0.2
);
}
public function testComplexEigenvalueDecomposition() : void
{
$A = new Matrix();
$A->setMatrix([
[3, -2],
[4, -1],
]);
$eig = new EigenvalueDecomposition($A);
self::assertEqualsWithDelta([
[1, 2],
[-2, 1],
], $eig->getD()->toArray(), 0.1);
self::assertEqualsWithDelta([1, 1], $eig->getRealEigenvalues()->toArray(), 0.1);
self::assertEqualsWithDelta([2, -2], $eig->getImagEigenvalues()->toArray(), 0.1);
}
}

View File

@ -50,7 +50,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase
$alice_sign_publickey = \sodium_crypto_sign_publickey($alice_sign_kp);
// create signature
$files = Directory::list(__DIR__ . '/testPackage');
$files = Directory::list(__DIR__ . '/testPackage', '*', true);
$state = \sodium_crypto_generichash_init();
foreach ($files as $file) {

27
tests/Preload0.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests;
/**
* Preload class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Preload0
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\PreloadTest;
/**
* Preload class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Preload1
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\PreloadTest\Sub;
/**
* Preload class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Preload2
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\PreloadTest\Sub;
/**
* Preload class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Preload3
{
}

45
tests/PreloaderTest.php Normal file
View File

@ -0,0 +1,45 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @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;
use phpOMS\Preloader;
/**
* @testdox phpOMS\tests\PreloaderTest: Class preloader
*
* @internal
*/
class PreloaderTest extends \PHPUnit\Framework\TestCase
{
public function testPreloading() : void
{
$includes = \get_included_files();
self::assertFalse(\in_array(\realpath(__DIR__ . '/PreloadTest/Preload1.php'), $includes));
self::assertFalse(\in_array(\realpath(__DIR__ . '/PreloadTest/Sub/Preload2.php'), $includes));
self::assertFalse(\in_array(\realpath(__DIR__ . '/PreloadTest/Sub/Preload3.php'), $includes));
$preloader = new Preloader();
$preloader->ignore(__DIR__ . '/PreloadTest/Sub/Preload3.php')
->includePath(__DIR__ . '/PreloadTest')
->includePath(__DIR__ . '/Preload0.php')
->includePath(__DIR__ . '/PreloadTest/Sub/Preload3.php')
->load();
$includes = \get_included_files();
self::assertTrue(\in_array(\realpath(__DIR__ . '/PreloadTest/Preload1.php'), $includes));
self::assertTrue(\in_array(\realpath(__DIR__ . '/PreloadTest/Sub/Preload2.php'), $includes));
self::assertFalse(\in_array(\realpath(__DIR__ . '/PreloadTest/Sub/Preload3.php'), $includes));
}
}

View File

@ -90,11 +90,6 @@ class SocketRouterTest extends \PHPUnit\Framework\TestCase
*/
public function testDynamicRouteAdding() : void
{
self::assertNotEquals(
[['dest' => '\Modules\Admin\Controller:viewSettingsGeneral']],
$this->router->route('backends_admin -settings=general -t 123')
);
$this->router->add('^.*backends_admin -settings=general.*$', 'Controller:test');
self::assertEquals(
[['dest' => 'Controller:test']],
@ -109,10 +104,6 @@ class SocketRouterTest extends \PHPUnit\Framework\TestCase
*/
public function testWithValidPermissions() : void
{
if (!Autoloader::exists('\Modules\Admin\Controller')) {
self::markTestSkipped();
}
self::assertTrue($this->router->importFromFile(__DIR__ . '/socketRouterTestFilePermission.php'));
$perm = new class(
@ -146,10 +137,6 @@ class SocketRouterTest extends \PHPUnit\Framework\TestCase
*/
public function testWithInvalidPermissions() : void
{
if (!Autoloader::exists('\Modules\Admin\Controller')) {
self::markTestSkipped();
}
self::assertTrue($this->router->importFromFile(__DIR__ . '/socketRouterTestFilePermission.php'));
$perm2 = new class(
@ -199,4 +186,69 @@ class SocketRouterTest extends \PHPUnit\Framework\TestCase
)
);
}
/**
* @testdox A data validation pattern validates matches correctly
* @covers phpOMS\Router\SocketRouter
* @group framework
*/
public function testDataValidation() : void
{
$this->router->add(
'^.*backends_admin -settings=general.*$',
'Controller:test',
['test_pattern' => '/^[a-z]*$/']
);
self::assertEquals(
[['dest' => 'Controller:test']],
$this->router->route('backends_admin -settings=general -t 123', null, null, null, ['test_pattern' => 'abcdef'])
);
}
/**
* @testdox A data validation pattern invalidates missmatches
* @covers phpOMS\Router\SocketRouter
* @group framework
*/
public function testInvalidDataValidation() : void
{
$this->router->add(
'^.*backends_admin -settings=general.*$',
'Controller:test',
['test_pattern' => '/^[a-z]*$/']
);
self::assertNotEquals(
[['dest' => 'Controller:test']],
$this->router->route('backends_admin -settings=general -t 123', null, null, null, ['test_pattern' => '123'])
);
}
/**
* @testdox A uri can be used for data population
* @covers phpOMS\Router\SocketRouter
* @group framework
*/
public function testDataFromPattern() : void
{
$this->router->add(
'^.*-settings=general.*$',
'Controller:test',
[],
'/^.*?(settings)=([a-z]*).*?$/'
);
self::assertEquals(
[[
'dest' => 'Controller:test',
'data' => [
'backends_admin -settings=general -t 123',
'settings',
'general',
],
]],
$this->router->route('backends_admin -settings=general -t 123')
);
}
}

View File

@ -119,15 +119,6 @@ class WebRouterTest extends \PHPUnit\Framework\TestCase
*/
public function testDynamicRouteAdding() : void
{
self::assertNotEquals(
[['dest' => '\Modules\Admin\Controller:viewSettingsGeneral']],
$this->router->route(
(new HttpRequest(
new HttpUri('http://test.com/backends/admin/settings/general/something?test')
))->getUri()->getRoute()
)
);
$this->router->add('^.*/backends/admin/settings/general.*$', 'Controller:test', RouteVerb::GET | RouteVerb::SET);
self::assertEquals(
[['dest' => 'Controller:test']],

27
tests/TestLoad.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests;
/**
* Autoloader class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class TestLoad
{
}

27
tests/TestLoad2.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace tests;
/**
* Autoloader class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class TestLoad2
{
}

27
tests/TestLoad3.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests;
/**
* Autoloader class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class TestLoad3
{
}

27
tests/TestLoad4.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package Tests\PHPUnit
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests;
/**
* Autoloader class.
*
* @package Tests\PHPUnit
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class TestLoad4
{
}