impl. more tests

This commit is contained in:
Dennis Eichhorn 2023-10-22 02:59:46 +00:00
parent 4a208b8cd0
commit 663cf6b11e
72 changed files with 1791 additions and 127 deletions

View File

@ -4,7 +4,7 @@
*
* PHP Version 8.1
*
* @package phpOMS\Algorithm\CoinMatching
* @package phpOMS\Algorithm\Frequency
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
@ -12,14 +12,14 @@
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\CoinMatching;
namespace phpOMS\Algorithm\Frequency;
/**
* Apriori algorithm.
*
* The algorithm cheks how often a set exists in a given set of sets.
*
* @package phpOMS\Algorithm\CoinMatching
* @package phpOMS\Algorithm\Frequency
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
@ -70,13 +70,14 @@ final class Apriori
*
* The algorithm cheks how often a set exists in a given set of sets.
*
* @param array<array> $sets Sets of a set (e.g. [[1,2,3,4], [1,2], [1]])
* @param array<array> $sets Sets of a set (e.g. [[1,2,3,4], [1,2], [1]])
* @param array $subset Subset to check for (empty array -> all subsets are checked)
*
* @return array
*
* @since 1.0.0
*/
public static function apriori(array $sets) : array
public static function apriori(array $sets, array $subset = []) : array
{
// Unique single items
$totalSet = [];
@ -102,6 +103,10 @@ final class Apriori
}
foreach ($combinations as $combination) {
if (!empty($subset) && $combination !== $subset) {
continue;
}
foreach ($sets as $set) {
foreach ($combination as $item) {
if (!\in_array($item, $set)) {

View File

@ -34,13 +34,16 @@ final class Nominatim
/**
* {@inheritdoc}
*/
public static function geocoding(string $country, string $city, string $address = '') : array
public static function geocoding(string $country, string $city, string $address = '', string $postal = '') : array
{
$URL = 'https://nominatim.openstreetmap.org/search.php?format=jsonv2';
$request = new HttpRequest(
new HttpUri(
$URL . '&country=' . \urlencode($country) . '&city=' . \urlencode($city) . ($address === '' ? '' : '&street=' . \urlencode($address))
$URL . '&country=' . \urlencode($country)
. '&city=' . \urlencode($city)
. ($address === '' ? '' : '&street=' . \urlencode($address))
. ($postal === '' ? '' : '&postalcode=' . \urlencode($postal))
)
);
$request->setMethod(RequestMethod::GET);

View File

@ -891,6 +891,7 @@ final class ReadMapper extends DataMapperAbstract
}
}
// @todo: How is this allowed? at the bottom we set $obj->hasMany = value. A has many should be always an array?!
foreach ($this->mapper::HAS_MANY as $member => $def) {
$column = $def['mapper']::getColumnByMember($def['column'] ?? $member);
$alias = $column . '_d' . ($this->depth + 1);
@ -926,33 +927,39 @@ final class ReadMapper extends DataMapperAbstract
$refProp = $refClass->getProperty($member);
}
if (\in_array($def['mapper']::COLUMNS[$column]['type'], ['string', 'int', 'float', 'bool'])) {
$type = $def['mapper']::COLUMNS[$column]['type'];
if (\in_array($type, ['string', 'compress', 'int', 'float', 'bool'])) {
if ($value !== null && $type === 'compress') {
$type = 'string';
$value = \gzinflate($value);
}
if ($value !== null
|| ($isPrivate ? $refProp->getValue($obj) !== null : $obj->{$member} !== null)
) {
\settype($value, $def['mapper']::COLUMNS[$column]['type']);
\settype($value, $type);
}
if ($hasPath) {
$value = ArrayUtils::setArray($arrayPath, $aValue, $value, '/', true);
}
} elseif ($def['mapper']::COLUMNS[$column]['type'] === 'DateTime') {
} elseif ($type === 'DateTime') {
$value ??= new \DateTime($value);
if ($hasPath) {
$value = ArrayUtils::setArray($arrayPath, $aValue, $value, '/', true);
}
} elseif ($def['mapper']::COLUMNS[$column]['type'] === 'DateTimeImmutable') {
} elseif ($type === 'DateTimeImmutable') {
$value ??= new \DateTimeImmutable($value);
if ($hasPath) {
$value = ArrayUtils::setArray($arrayPath, $aValue, $value, '/', true);
}
} elseif ($def['mapper']::COLUMNS[$column]['type'] === 'Json') {
} elseif ($type === 'Json') {
if ($hasPath) {
$value = ArrayUtils::setArray($arrayPath, $aValue, $value, '/', true);
}
$value = \json_decode($value, true);
} elseif ($def['mapper']::COLUMNS[$column]['type'] === 'Serializable') {
} elseif ($type === 'Serializable') {
$mObj = $isPrivate ? $refProp->getValue($obj) : $obj->{$member};
if ($mObj !== null && $value !== null) {

View File

@ -391,7 +391,7 @@ class Builder extends BuilderAbstract
}
// add from to existing dependencies
foreach ($this->from as $table => $from) {
foreach ($this->from as $table => $_) {
$dependencies[$table] = [];
}

114
Math/Functions/Algebra.php Normal file
View File

@ -0,0 +1,114 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package phpOMS\Math\Functions
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\Math\Functions;
use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/**
* Algebra functions
*
* @package phpOMS\Math\Functions
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class Algebra
{
/**
* Get the dot product of two arrays
*
* @param array $value1 Value 1 is a matrix or a vector
* @param array $value2 Value 2 is a matrix or vector (cannot be a matrix if value1 is a vector)
*
* @return array
*
* @throws InvalidDimensionException
* @throws \InvalidArgumentException
*
* @since 1.0.0
*/
public static function dot(array $value1, array $value2) : int|float|array
{
$m1 = \count($value1);
$n1 = ($isMatrix1 = \is_array($value1[0])) ? \count($value1[0]) : 1;
$m2 = \count($value2);
$n2 = ($isMatrix2 = \is_array($value2[0])) ? \count($value2[0]) : 1;
$result = null;
if ($isMatrix1 && $isMatrix2) {
if ($m2 !== $n1) {
throw new InvalidDimensionException($m2 . 'x' . $n2 . ' not compatible with ' . $m1 . 'x' . $n1);
}
$result = [[]];
for ($i = 0; $i < $m1; ++$i) { // Row of 1
for ($c = 0; $c < $n2; ++$c) { // Column of 2
$temp = 0;
for ($j = 0; $j < $m2; ++$j) { // Row of 2
$temp += $value1[$i][$j] * $value2[$j][$c];
}
$result[$i][$c] = $temp;
}
}
} elseif (!$isMatrix1 && !$isMatrix2) {
if ($m1 !== $m2) {
throw new InvalidDimensionException($m1 . ' vs. ' . $m2);
}
$result = 0;
for ($i = 0; $i < $m1; ++$i) {
$result += $value1[$i] * $value2[$i];
}
} elseif ($isMatrix1 && !$isMatrix2) {
$result = [];
for ($i = 0; $i < $m1; ++$i) { // Row of 1
$temp = 0;
for ($c = 0; $c < $m2; ++$c) { // Row of 2
$temp += $value1[$i][$c] * $value2[$c];
}
$result[$i] = $temp;
}
} else {
throw new \InvalidArgumentException();
}
return $result;
}
/**
* Calculate the vector corss product
*
* @param array $vector1 First 3 vector
* @param array $vector2 Second 3 vector
*
* @return array<int, int|float>
*
* @since 1.0.0
*/
public static function cross3(array $vector1, array $vector2) : array
{
return [
$vector1[1] * $vector2[2] - $vector1[2] * $vector2[1],
$vector1[2] * $vector2[0] - $vector1[0] * $vector2[2],
$vector1[0] * $vector2[1] - $vector1[1] * $vector2[0],
];
}
}

View File

@ -22,7 +22,7 @@ namespace phpOMS\Math\Topology;
* @link https://jingga.app
* @since 1.0.0
*/
final class Kernels2D
final class Kernel2D
{
/**
* Constructor

View File

@ -290,4 +290,9 @@ class BinarySearchTree
$this->delete($temp);
}
}
public function toArray() : array
{
return $this->root?->toArray() ?? ['key' => null, 0 => null, 1 => null];
}
}

View File

@ -99,4 +99,13 @@ class Node
{
return $this->data <=> $data;
}
public function toArray() : array
{
return [
'key' => $this->key,
0 => $this->left?->toArray(),
1 => $this->right?->toArray(),
];
}
}

View File

@ -45,12 +45,12 @@ final class SystemUtils
{
$mem = 0;
if (\stristr(\PHP_OS, 'WIN')) {
if (\stripos(\PHP_OS, 'WIN')) {
$memArr = [];
\exec('wmic memorychip get capacity', $memArr);
$mem = \array_sum($memArr) / 1024;
} elseif (\stristr(\PHP_OS, 'LINUX')) {
} elseif (\stripos(\PHP_OS, 'LINUX')) {
$fh = \fopen('/proc/meminfo', 'r');
if ($fh === false) {
@ -82,7 +82,7 @@ final class SystemUtils
{
$memUsage = 0;
if (\stristr(\PHP_OS, 'LINUX')) {
if (\stripos(\PHP_OS, 'LINUX')) {
$free = \shell_exec('free');
if ($free === null || $free === false) {
@ -110,11 +110,11 @@ final class SystemUtils
{
$cpuUsage = 0;
if (\stristr(\PHP_OS, 'WIN') !== false) {
if (\stripos(\PHP_OS, 'WIN') !== false) {
$cpuUsage = null;
\exec('wmic cpu get LoadPercentage', $cpuUsage);
$cpuUsage = (int) ($cpuUsage[1] ?? -1);
} elseif (\stristr(\PHP_OS, 'LINUX') !== false) {
} elseif (\stripos(\PHP_OS, 'LINUX') !== false) {
$loadavg = \sys_getloadavg();
if ($loadavg === false) {
@ -167,7 +167,7 @@ final class SystemUtils
*/
public static function runProc(string $executable, string $cmd, bool $async = false) : array
{
if (\strtolower((string) \substr(\PHP_OS, 0, 3)) === 'win') {
if (\stripos(\PHP_OS, 'WIN') !== false) {
$cmd = 'cd ' . \escapeshellarg(\dirname($executable))
. ' && ' . \basename($executable)
. ' '
@ -193,7 +193,6 @@ final class SystemUtils
];
$resource = \proc_open($cmd, $desc, $pipes, null, null);
if ($resource === false) {
throw new \Exception();
}

View File

@ -504,90 +504,4 @@ final class ArrayUtils
return $diff;
}
/**
* Get the dot product of two arrays
*
* @param array $value1 Value 1 is a matrix or a vector
* @param array $value2 Value 2 is a matrix or vector (cannot be a matrix if value1 is a vector)
*
* @return array
*
* @throws InvalidDimensionException
* @throws \InvalidArgumentException
*
* @since 1.0.0
*/
public static function dot(array $value1, array $value2) : int|float|array
{
$m1 = \count($value1);
$n1 = ($isMatrix1 = \is_array($value1[0])) ? \count($value1[0]) : 1;
$m2 = \count($value2);
$n2 = ($isMatrix2 = \is_array($value2[0])) ? \count($value2[0]) : 1;
$result = null;
if ($isMatrix1 && $isMatrix2) {
if ($m2 !== $n1) {
throw new InvalidDimensionException($m2 . 'x' . $n2 . ' not compatible with ' . $m1 . 'x' . $n1);
}
$result = [[]];
for ($i = 0; $i < $m1; ++$i) { // Row of 1
for ($c = 0; $c < $n2; ++$c) { // Column of 2
$temp = 0;
for ($j = 0; $j < $m2; ++$j) { // Row of 2
$temp += $value1[$i][$j] * $value2[$j][$c];
}
$result[$i][$c] = $temp;
}
}
} elseif (!$isMatrix1 && !$isMatrix2) {
if ($m1 !== $m2) {
throw new InvalidDimensionException($m1 . ' vs. ' . $m2);
}
$result = 0;
for ($i = 0; $i < $m1; ++$i) {
$result += $value1[$i] * $value2[$i];
}
} elseif ($isMatrix1 && !$isMatrix2) {
$result = [];
for ($i = 0; $i < $m1; ++$i) { // Row of 1
$temp = 0;
for ($c = 0; $c < $m2; ++$c) { // Row of 2
$temp += $value1[$i][$c] * $value2[$c];
}
$result[$i] = $temp;
}
} else {
throw new \InvalidArgumentException();
}
return $result;
}
/**
* Calculate the vector corss product
*
* @param array $vector1 First 3 vector
* @param array $vector2 Second 3 vector
*
* @return array<int, int|float>
*
* @since 1.0.0
*/
public function cross3(array $vector1, array $vector2) : array
{
return [
$vector1[1] * $vector2[2] - $vector1[2] * $vector2[1],
$vector1[2] * $vector2[0] - $vector1[0] * $vector2[2],
$vector1[0] * $vector2[1] - $vector1[1] * $vector2[0],
];
}
}

View File

@ -66,6 +66,16 @@ class ICalParser
\preg_match('/LOCATION:(.*?)\r\n/', $match[1], $locationMatch);
$event['location'] = $locationMatch[1];
\preg_match('/GEO:(.*?)\r\n/', $match[1], $geo);
$temp = \explode(';', $geo[1]);
$event['geo'] = [
'lat' => (float) $temp[0],
'lon' => (float) $temp[1],
];
\preg_match('/URL:(.*?)\r\n/', $match[1], $url);
$event['url'] = $url[1];
// Check if this event is recurring
if (\preg_match('/RRULE:(.*?)\r\n/', $match[1], $rruleMatch)) {
$rrule = self::parseRRule($rruleMatch[1]);

View File

@ -38,15 +38,13 @@ class DocumentParser
*/
public static function parseDocument(string $path, string $output = 'html') : string
{
if ($output === 'html') {
$doc = IOFactory::load($path);
$doc = IOFactory::load($path);
if ($output === 'html') {
$writer = new HTML($doc);
return $writer->getContent();
} elseif ($output === 'pdf') {
$doc = IOFactory::load($path);
$writer = new DocumentWriter($doc);
return $writer->toPdfString();

View File

@ -190,7 +190,7 @@ class Markdown
// method.
$html = $this->body($text);
if (isset($this->options['toc']) && $this->options['toc'] == false) {
if (isset($this->options['toc']) && $this->options['toc'] === false) {
return $html;
}

View File

@ -38,9 +38,9 @@ class SpreadsheetParser
*/
public static function parseSpreadsheet(string $path, string $output = 'json') : string
{
if ($output === 'json') {
$spreadsheet = IOFactory::load($path);
$spreadsheet = IOFactory::load($path);
if ($output === 'json') {
$sheetCount = $spreadsheet->getSheetCount();
$csv = [];
@ -52,8 +52,6 @@ class SpreadsheetParser
return $json === false ? '' : $json;
} elseif ($output === 'pdf') {
$spreadsheet = IOFactory::load($path);
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
@ -64,8 +62,6 @@ class SpreadsheetParser
return $writer->toPdfString();
} elseif ($output === 'html') {
$spreadsheet = IOFactory::load($path);
IOFactory::registerWriter('custom', \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter::class);
/** @var \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter $writer */
$writer = IOFactory::createWriter($spreadsheet, 'custom');

View File

@ -0,0 +1,83 @@
<?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\Algorithm\Frequency;
use phpOMS\Algorithm\Frequency\Apriori;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\tests\Algorithm\Frequency\AprioriTest:
*
* @internal
*/
final class AprioriTest extends \PHPUnit\Framework\TestCase
{
public function testApriori() : void
{
self::assertEquals(
[],
Apriori::apriori([
['alpha', 'beta', 'epsilon'],
['alpha', 'beta', 'theta'],
['alpha', 'beta', 'epsilon'],
['alpha', 'beta', 'theta'],
])
);
self::assertEquals(
[],
Apriori::apriori([
[1, 2, 3, 4],
[1, 2, 4],
[1, 2],
[2, 3, 4],
[3, 4],
[2, 4],
])
);
}
public function testAprioriSubset() : void
{
self::assertEquals(
['beta:theta' => 2],
Apriori::apriori(
[
['alpha', 'beta', 'epsilon'],
['alpha', 'beta', 'theta'],
['alpha', 'beta', 'epsilon'],
['alpha', 'beta', 'theta'],
],
['beta', 'theta']
)
);
self::assertEquals(
['2:3' => 2],
Apriori::apriori(
[
[1, 2, 3, 4],
[1, 2, 4],
[1, 2],
[2, 3, 4],
[3, 4],
[2, 4],
],
[2, 3]
)
);
}
}

View File

@ -0,0 +1,45 @@
<?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\Algorithm\Graph;
use phpOMS\Algorithm\Graph\DependencyResolver;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\tests\Algorithm\Graph\DependencyResolverTest:
*
* @internal
*/
final class DependencyResolverTest extends \PHPUnit\Framework\TestCase
{
/**
* @testdox A value is matched with the minimum quantity of available coins.
* @covers phpOMS\Algorithm\Graph\DependencyResolver
* @group framework
*/
public function testResolve() : void
{
self::assertEquals(
null,
DependencyResolver::resolve([0 => [1, 2], 1 => [0, 2]])
);
self::assertEquals(
[0, 1, 2, 3],
DependencyResolver::resolve([0 => [1, 2], 1 => [2, 3]])
);
}
}

View File

@ -0,0 +1,105 @@
<?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\Algorithm\Graph;
use phpOMS\Algorithm\Graph\MarkovChain;
require_once __DIR__ . '/../../Autoloader.php';
/**
* @testdox phpOMS\tests\Algorithm\Graph\MarkovChainTest:
*
* @internal
*/
final class MarkovChainTest extends \PHPUnit\Framework\TestCase
{
/**
* @covers phpOMS\Algorithm\Graph\MarkovChain
* @group framework
*/
public function testGenerate() : void
{
$markov = new MarkovChain();
$markov->setTraining(
[
'A' => ['A' => 0.1, 'C' => 0.6, 'E' => 0.3],
'C' => ['A' => 0.25, 'C' => 0.05, 'E' => 0.7],
'E' => ['A' => 0.7, 'C' => 0.3, 'E' => 0.0],
]
);
self::assertEquals(3, \count($markov->generate(3, ['A'])));
}
/**
* @covers phpOMS\Algorithm\Graph\MarkovChain
* @group framework
*/
public function testTrainingGenerate() : void
{
$markov = new MarkovChain();
$markov->train(['A', 'C', 'E', 'A', 'C', 'E', 'E', 'C', 'A', 'A', 'E', 'A']);
self::assertEquals(5, \count($markov->generate(5, ['A'])));
}
/**
* @covers phpOMS\Algorithm\Graph\MarkovChain
* @group framework
*/
public function testStepProbability() : void
{
$markov = new MarkovChain();
$markov->setTraining(
[
'A A' => ['A' => 0.18, 'D' => 0.6, 'G' => 0.22],
'A D' => ['A' => 0.5, 'D' => 0.5, 'G' => 0.0],
'A G' => ['A' => 0.15, 'D' => 0.75, 'G' => 0.1],
'D D' => ['A' => 0.0, 'D' => 0.0, 'G' => 1.0],
'D A' => ['A' => 0.25, 'D' => 0.0, 'G' => 0.75],
'D G' => ['A' => 0.9, 'D' => 0.1, 'G' => 0.0],
'G G' => ['A' => 0.4, 'D' => 0.4, 'G' => 0.2],
'G A' => ['A' => 0.5, 'D' => 0.25, 'G' => 0.25],
'G D' => ['A' => 1.0, 'D' => 0.0, 'G' => 0.0],
]
);
self::assertEquals(0.1, $markov->stepProbability(['D', 'G'], 'D'));
}
/**
* @covers phpOMS\Algorithm\Graph\MarkovChain
* @group framework
*/
public function testPathProbability() : void
{
$markov = new MarkovChain();
$markov->setTraining(
[
'A A' => ['A' => 0.18, 'D' => 0.6, 'G' => 0.22],
'A D' => ['A' => 0.5, 'D' => 0.5, 'G' => 0.0],
'A G' => ['A' => 0.15, 'D' => 0.75, 'G' => 0.1],
'D D' => ['A' => 0.0, 'D' => 0.0, 'G' => 1.0],
'D A' => ['A' => 0.25, 'D' => 0.0, 'G' => 0.75],
'D G' => ['A' => 0.9, 'D' => 0.1, 'G' => 0.0],
'G G' => ['A' => 0.4, 'D' => 0.4, 'G' => 0.2],
'G A' => ['A' => 0.5, 'D' => 0.25, 'G' => 0.25],
'G D' => ['A' => 1.0, 'D' => 0.0, 'G' => 0.0],
]
);
self::assertEquals(0.9 * 0.5 * 0.6, $markov->pathProbability(['D', 'G', 'A', 'A', 'D']));
}
}

View File

@ -0,0 +1,46 @@
<?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\Api\Geocoding;
use phpOMS\Api\Geocoding\Nominatim;
/**
* @testdox phpOMS\tests\Api\Geocoding\NominatimTest: EU VAT Vies validation
*
* @internal
*/
final class NominatimTest extends \PHPUnit\Framework\TestCase
{
public function testGeocoding() : void
{
self::assertEqualsWithDelta(
[
'lat' => 50.3050738,
'lon' => 8.688465172531158,
],
Nominatim::geocoding('de', 'Rosbach', 'Kirchstraße 33'),
0.01
);
self::assertEqualsWithDelta(
[
'lat' => 50.3050738,
'lon' => 8.688465172531158,
],
Nominatim::geocoding('de', 'Rosbach', 'Kirchstraße 33', '61191'),
0.01
);
}
}

View File

@ -46,6 +46,7 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
'CREATE TABLE `test_base` (
`test_base_id` int(11) NOT NULL AUTO_INCREMENT,
`test_base_string` varchar(254) NOT NULL,
`test_base_pstring` varchar(254) NOT NULL,
`test_base_int` int(11) NOT NULL,
`test_base_bool` tinyint(1) DEFAULT NULL,
`test_base_null` int(11) DEFAULT NULL,
@ -56,7 +57,7 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
`test_base_json_serializable` varchar(254) DEFAULT NULL,
`test_base_serializable` varchar(254) DEFAULT NULL,
`test_base_datetime` datetime DEFAULT NULL,
`test_base_datetime_null` datetime DEFAULT NULL, /* There was a bug where it returned the current date because new \DateTime(null) === current date which is wrong, we want null as value! */
`test_base_datetime_null` datetime DEFAULT NULL,
PRIMARY KEY (`test_base_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;'
)->execute();
@ -199,6 +200,8 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
self::assertEquals($this->model->getId(), $modelR->getId());
self::assertEquals($this->model->string, $modelR->string);
self::assertEquals($this->model->compress, $modelR->compress);
self::assertEquals($this->model->getPString(), $modelR->getPString());
self::assertEquals($this->model->int, $modelR->int);
self::assertEquals($this->model->bool, $modelR->bool);
self::assertEquals($this->model->float, $modelR->float);
@ -218,12 +221,35 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
self::assertEquals($this->model->belongsToOne->string, $modelR->belongsToOne->string);
}
public function testGetRaw() : void
{
$id = BaseModelMapper::create()->execute($this->model);
/** @var BaseModel $modelR */
$modelR = BaseModelMapper::getRaw()
->with('belongsToOne')
->with('ownsOneSelf')
->with('hasManyDirect')
->with('hasMnayRelations')
->with('conditional')
->where('id', $id)
->execute();
self::assertTrue(\is_array($modelR));
}
public function testGetAll() : void
{
BaseModelMapper::create()->execute($this->model);
self::assertCount(1, BaseModelMapper::getAll()->execute());
}
public function testGetYield() : void
{
BaseModelMapper::create()->execute($this->model);
self::assertCount(1, BaseModelMapper::yield()->execute());
}
public function testGetFor() : void
{
$id = BaseModelMapper::create()->execute($this->model);
@ -269,6 +295,38 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
self::assertEquals(NullBaseModel::class, \get_class(BaseModelMapper::get()->where('id', 99)->execute()));
}
public function testCount() : void
{
BaseModelMapper::create()->execute($this->model);
self::assertEquals(1, BaseModelMapper::count()->execute());
}
public function testSum() : void
{
BaseModelMapper::create()->execute($this->model);
self::assertEquals(11, BaseModelMapper::sum()->columns(['test_base_int'])->execute());
}
public function testExists() : void
{
$id = BaseModelMapper::create()->execute($this->model);
self::assertTrue(BaseModelMApper::exists()->where('id', $id)->execute());
self::assertFalse(BaseModelMApper::exists()->where('id', $id + 1)->execute());
}
public function testHas() : void
{
$id = BaseModelMapper::create()->execute($this->model);
self::assertTrue(BaseModelMApper::has()->with('hasManyRelations')->where('id', $id)->execute());
self::assertTrue(BaseModelMApper::has()->with('hasManyDirect')->where('id', $id)->execute());
}
public function testRandom() : void
{
$id = BaseModelMapper::create()->execute($this->model);
self::assertEquals($id, BaseModelMApper::getRandom()->execute()->id);
}
/**
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory

View File

@ -22,6 +22,10 @@ class BaseModel
public string $string = 'Base';
public string $compress = 'Uncompressed';
private string $pstring = 'Private';
public string $conditional = '';
public int $int = 11;
@ -40,10 +44,18 @@ class BaseModel
public array $hasManyRelations = [];
private array $hasManyDirectPrivate = [];
private array $hasManyRelationsPrivate = [];
public $ownsOneSelf = 0;
public $belongsToOne = 0;
private $ownsOneSelfPrivate = 0;
private $belongsToOnePrivate = 0;
public ?object $serializable = null;
public array $json = [1, 2, 3];
@ -64,6 +76,16 @@ class BaseModel
new ManyToManyRelModel(),
];
$this->hasManyDirectPrivate = [
new ManyToManyDirectModel(),
new ManyToManyDirectModel(),
];
$this->hasManyRelationsPrivate = [
new ManyToManyRelModel(),
new ManyToManyRelModel(),
];
$this->ownsOneSelf = new OwnsOneModel();
$this->belongsToOne = new BelongsToModel();
@ -89,6 +111,11 @@ class BaseModel
};
}
public function getPString() : string
{
return $this->pstring;
}
public function getId() : int
{
return $this->id;

View File

@ -27,6 +27,8 @@ class BaseModelMapper extends DataMapperFactory
public const COLUMNS = [
'test_base_id' => ['name' => 'test_base_id', 'type' => 'int', 'internal' => 'id'],
'test_base_string' => ['name' => 'test_base_string', 'type' => 'string', 'internal' => 'string', 'autocomplete' => true],
'test_base_compress' => ['name' => 'test_base_compress', 'type' => 'compress', 'internal' => 'compress',],
'test_base_pstring' => ['name' => 'test_base_pstring', 'type' => 'pstring', 'internal' => 'pstring', 'private' => true],
'test_base_int' => ['name' => 'test_base_int', 'type' => 'int', 'internal' => 'int'],
'test_base_bool' => ['name' => 'test_base_bool', 'type' => 'bool', 'internal' => 'bool'],
'test_base_null' => ['name' => 'test_base_null', 'type' => 'int', 'internal' => 'null'],
@ -51,6 +53,11 @@ class BaseModelMapper extends DataMapperFactory
'mapper' => BelongsToModelMapper::class,
'external' => 'test_base_belongs_to_one',
],
'belongsToOnePrivate' => [
'mapper' => BelongsToModelMapper::class,
'external' => 'test_base_belongs_to_one',
'private' => true,
],
];
public const OWNS_ONE = [
@ -58,6 +65,11 @@ class BaseModelMapper extends DataMapperFactory
'mapper' => OwnsOneModelMapper::class,
'external' => 'test_base_owns_one_self',
],
'ownsOneSelfPrivate' => [
'mapper' => OwnsOneModelMapper::class,
'external' => 'test_base_owns_one_self',
'private' => true
],
];
/**
@ -86,6 +98,20 @@ class BaseModelMapper extends DataMapperFactory
'column' => 'title',
'external' => null,
],
'hasManyDirectPrivate' => [
'mapper' => ManyToManyDirectModelMapper::class,
'table' => 'test_has_many_direct',
'self' => 'test_has_many_direct_to',
'external' => null,
'private' => true,
],
'hasManyRelationsPrivate' => [
'mapper' => ManyToManyRelModelMapper::class,
'table' => 'test_has_many_rel_relations',
'external' => 'test_has_many_rel_relations_src',
'self' => 'test_has_many_rel_relations_dest',
'private' => true
],
];
/**

View File

@ -0,0 +1,91 @@
<?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\Math\Functions;
use phpOMS\Math\Functions\Algebra;
/**
* @testdox phpOMS\tests\Math\Functions\AlgebraTest: Various math functions
*
* @internal
*/
final class AlgebraTest extends \PHPUnit\Framework\TestCase
{
public function testDotVectors() : void
{
self::assertEquals(
3,
Algebra::dot([1, 3, -5], [4, -2, -1])
);
}
public function testDotMatrices() : void
{
self::assertEquals(
[
[58, 64],
[139, 154],
],
Algebra::dot(
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8],
[9, 10],
[11, 12],
]
)
);
}
public function testDotVectorMatrix() : void
{
self::assertEquals(
[11, 39, 53],
Algebra::dot(
[3, 4],
[
[1, 5, 7],
[2, 6, 8],
]
)
);
}
public function testDotMatrixVector() : void
{
self::assertEquals(
[11, 39, 53],
Algebra::dot(
[
[1, 2],
[5, 6],
[7, 8],
],
[3, 4]
)
);
}
public function testCross3() : void
{
self::assertEquals(
[-15, -2, 39],
Algebra::cross3([3, -3, 1], [4, 9, 2])
);
}
}

View File

@ -28,7 +28,7 @@ final class BetaTest extends \PHPUnit\Framework\TestCase
* @covers phpOMS\Math\Functions\Beta
* @group framework
*/
public function beta() : void
public function testBeta() : void
{
self::assertEqualsWithDelta(1.0, Beta::beta(0, 3), 0.001);
self::assertEqualsWithDelta(4.4776093, Beta::beta(1.5, 0.2), 0.001);

View File

@ -0,0 +1,51 @@
<?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\Math\Geometry\ConvexHull;
use phpOMS\Math\Geometry\ConvexHull\GrahamScan;
/**
* @testdox phpOMS\tests\Math\Geometry\ConvexHull\GrahamScanTest: Monotone chain
*
* @internal
*/
final class GrahamScanTest extends \PHPUnit\Framework\TestCase
{
/**
* @testdox A convex hull can be formed from multiple points on a plane
* @covers phpOMS\Math\Geometry\ConvexHull\GrahamScan
* @group framework
*/
public function testGrahamScan() : void
{
self::assertEquals([['x' => 9, 'y' => 0]], GrahamScan::createConvexHull([['x' => 9, 'y' => 0]]));
$points = [];
for ($i = 0; $i < 10; ++$i) {
for ($j = 0; $j < 10; ++$j) {
$points[] = ['x' => $i, 'y' => $j];
}
}
self::assertEquals([
['x' => 0, 'y' => 0],
['x' => 9, 'y' => 0],
['x' => 9, 'y' => 9],
['x' => 0, 'y' => 9],
],
GrahamScan::createConvexHull($points)
);
}
}

View File

@ -492,4 +492,181 @@ final class MatrixTest extends \PHPUnit\Framework\TestCase
$A->mult($B);
}
public function testDotVectors() : void
{
$v1 = Vector::fromArray([1, 3, -5]);
self::assertEquals(
3,
$v1->dot(Vector::fromArray([4, -2, -1]))
);
}
public function testDotMatrices() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
]);
self::assertEquals(
[
[58, 64],
[139, 154],
],
$m->dot(
Matrix::fromArray([
[7, 8],
[9, 10],
[11, 12],
])
)->toArray()
);
}
public function testDotVectorMatrix() : void
{
$v = Vector::fromArray([3, 4]);
self::assertEquals(
[11, 39, 53],
$v->dot(
MAtrix::fromArray([
[1, 5, 7],
[2, 6, 8],
])
)->toArray()
);
}
public function testDotMatrixVector() : void
{
$m = Matrix::fromArray([
[1, 2],
[5, 6],
[7, 8],
]);
self::assertEquals(
[11, 39, 53],
$m->dot(
Vector::fromArray([3, 4])
)->toArray()
);
}
public function testSumAll() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
self::assertEquals(
45,
$m->sum(-1)
);
}
public function testSumColumns() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
self::assertEquals(
[12, 15, 18],
$m->sum(0)
);
}
public function testSumRows() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
self::assertEquals(
[6, 15, 24],
$m->sum(1)
);
}
public function testDiaglonal() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
self::assertFalse($m->isDiagonal());
$m = Matrix::fromArray([
[1, 0, 0],
[0, 5, 0],
[0, 0, -8],
]);
self::assertFalse($m->isDiagonal());
}
public function testPow() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
self::assertEquals(
[
[30, 36, 42],
[66, 81, 96],
[102, 126, 150],
],
$m->pow(2)->toArray()
);
$m = Matrix::fromArray([
[1.5, 2.5, 3.5],
[4.5, 5.5, 6.5],
[7.5, 8.5, 9.5],
]);
self::assertEqualsWithDelta(
[
[39.75, 47.25, 54.75],
[80.25, 96.75, 113.25],
[120.75, 146.25, 171.75],
],
$m->pow(2.5)->toArray(),
0.1
);
}
public function testExp() : void
{
$m = Matrix::fromArray([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
self::assertEqualsWithDelta(
[
[1118906.6994131860386, 1374815.062935806540981, 1630724.426458427043361],
[2533881.041898971697907, 3113415.03138055427637, 3692947.020862136854833],
[3948856.384384757357213, 4852012.999825302011759, 5755170.615265846666304],
],
$m->exp()->toArray(),
0.1
);
}
}

View File

@ -65,4 +65,20 @@ final class VectorTest extends \PHPUnit\Framework\TestCase
self::assertEquals(5, $vec->getM());
}
public function testCosine() : void
{
$v1 = Vector::fromArray([3, 4, 0]);
$v2 = Vector::fromArray([4, 4, 2]);
self::assertEqualsWithDelta(14 / 15, $v1->cosine($v2), 0.1);
}
public function testCross3() : void
{
self::assertEquals(
[-15, -2, 39],
Vector::fromArray([3, -3, 1])->cross3(Vector::fromArray([4, 9, 2]))->toArray()
);
}
}

View File

@ -0,0 +1,110 @@
<?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\Math\Optimization;
use phpOMS\Math\Optimization\Simplex;
/**
* @testdox phpOMS\tests\Math\Optimization\SimplexTest: Numeric integration
*
* @internal
*
* Commented out assertions which take a long time with xdebug. without xdebug these are fine!
*/
final class SimplexTest extends \PHPUnit\Framework\TestCase
{
public function testSimplex() : void
{
$simplex = new Simplex();
self::assertEquals(
[],
$simplex->solve(
[
[-1, 1],
[1, 1],
[1, -4],
],
[8, -3, 2],
[1, 3]
)
);
}
public function testSimplexBasicFeasible() : void
{
$simplex = new Simplex();
self::assertEquals(
[],
$simplex->solve(
[
[-1, 1],
[-2, -1],
],
[1, 2],
[5, -3]
)
);
}
public function testSimplexBasicInfeasible() : void
{
$simplex = new Simplex();
self::assertEquals(
[],
$simplex->solve(
[
[-1, 1],
[1, 1],
[1, -4],
],
[8, -3, 2],
[1, 3]
)
);
}
public function testSimplexLPInfeasible() : void
{
$simplex = new Simplex();
self::assertEquals(
[],
$simplex->solve(
[
[-1, -1],
[2, 2],
],
[2, -10],
[3, -2]
)
);
}
public function testSimplexLPUnbound() : void
{
$simplex = new Simplex();
self::assertEquals(
[],
$simplex->solve(
[
[2, -1],
[1, 2],
],
[-1, -2],
[1, -1]
)
);
}
}

View File

@ -0,0 +1,34 @@
<?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\Math\Solver\Root;
use phpOMS\Math\Solver\Root\Bisection;
/**
* @testdox phpOMS\tests\Math\Solver\Root\BisectionTest: Various math functions
*
* @internal
*/
final class BisectionTest extends \PHPUnit\Framework\TestCase
{
public function testRoot() : void
{
self::assertEqualsWithDelta(
1.521,
Bisection::root(function($x) { return $x * $x * $x - $x - 2; }, 1, 2),
0.1
);
}
}

View File

@ -0,0 +1,34 @@
<?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\Math\Solver\Root;
use phpOMS\Math\Solver\Root\Illinois;
/**
* @testdox phpOMS\tests\Math\Solver\Root\IllinoisTest: Various math functions
*
* @internal
*/
final class IllinoisTest extends \PHPUnit\Framework\TestCase
{
public function testRoot() : void
{
self::assertEqualsWithDelta(
1.521,
Illinois::root(function($x) { return $x * $x * $x - $x - 2; }, 1, 2),
0.1
);
}
}

View File

@ -0,0 +1,34 @@
<?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\Math\Solver\Root;
use phpOMS\Math\Solver\Root\RegulaFalsi;
/**
* @testdox phpOMS\tests\Math\Solver\Root\RegulaFalsiTest: Various math functions
*
* @internal
*/
final class RegulaFalsiTest extends \PHPUnit\Framework\TestCase
{
public function testRoot() : void
{
self::assertEqualsWithDelta(
1.521,
RegulaFalsi::root(function($x) { return $x * $x * $x - $x - 2; }, 1, 2),
0.1
);
}
}

View File

@ -0,0 +1,115 @@
<?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\Math\Topology;
use phpOMS\Math\Topology\Kernel2D;
/**
* @testdox phpOMS\tests\Math\Topology\Kernel2DTest: Metric/distance calculations
*
* @internal
*/
final class Kernel2DTest extends \PHPUnit\Framework\TestCase
{
public function testUniform() : void
{
self::assertEquals(0.5, Kernel2D::uniformKernel(1, 0));
self::assertEquals(0.5, Kernel2D::uniformKernel(1, -1));
self::assertEquals(0.5, Kernel2D::uniformKernel(1, 1));
self::assertEquals(0.0, Kernel2D::uniformKernel(1, 2));
self::assertEquals(0.0, Kernel2D::uniformKernel(1, -2));
}
public function testTriangle() : void
{
self::assertEquals(1.0, Kernel2D::triangularKernel(1, 0));
self::assertEquals(0.0, Kernel2D::triangularKernel(1, -1));
self::assertEquals(0.0, Kernel2D::triangularKernel(1, 1));
self::assertEquals(0.0, Kernel2D::triangularKernel(1, 2));
self::assertEquals(0.0, Kernel2D::triangularKernel(1, -2));
}
public function testEpanechnikov() : void
{
self::assertEquals(3 / 4, Kernel2D::epanechnikovKernel(1, 0));
self::assertEquals(0.0, Kernel2D::epanechnikovKernel(1, -1));
self::assertEquals(0.0, Kernel2D::epanechnikovKernel(1, 1));
self::assertEquals(0.0, Kernel2D::epanechnikovKernel(1, 2));
self::assertEquals(0.0, Kernel2D::epanechnikovKernel(1, -2));
}
public function testQuartic() : void
{
self::assertEquals(15 / 6, Kernel2D::quarticKernel(1, 0));
self::assertEquals(0.0, Kernel2D::quarticKernel(1, -1));
self::assertEquals(0.0, Kernel2D::quarticKernel(1, 1));
self::assertEquals(0.0, Kernel2D::quarticKernel(1, 2));
self::assertEquals(0.0, Kernel2D::quarticKernel(1, -2));
}
public function testTriweight() : void
{
self::assertEquals(35 / 32, Kernel2D::triweightKernel(1, 0));
self::assertEquals(0.0, Kernel2D::triweightKernel(1, -1));
self::assertEquals(0.0, Kernel2D::triweightKernel(1, 1));
self::assertEquals(0.0, Kernel2D::triweightKernel(1, 2));
self::assertEquals(0.0, Kernel2D::triweightKernel(1, -2));
}
public function testTricube() : void
{
self::assertEquals(70 / 81, Kernel2D::tricubeKernel(1, 0));
self::assertEquals(0.0, Kernel2D::tricubeKernel(1, -1));
self::assertEquals(0.0, Kernel2D::tricubeKernel(1, 1));
self::assertEquals(0.0, Kernel2D::tricubeKernel(1, 2));
self::assertEquals(0.0, Kernel2D::tricubeKernel(1, -2));
}
public function testGaussian() : void
{
self::assertEquals(1 / \sqrt(2 * \M_PI), Kernel2D::gaussianKernel(1, 0));
self::assertEquals(0.0, Kernel2D::gaussianKernel(1, -1));
self::assertEquals(0.0, Kernel2D::gaussianKernel(1, 1));
self::assertEquals(0.0, Kernel2D::gaussianKernel(1, 2));
self::assertEquals(0.0, Kernel2D::gaussianKernel(1, -2));
}
public function testCosine() : void
{
self::assertEquals(\M_PI / 4, Kernel2D::cosineKernel(1, 0));
self::assertEquals(0.0, Kernel2D::cosineKernel(1, -1));
self::assertEquals(0.0, Kernel2D::cosineKernel(1, 1));
self::assertEquals(0.0, Kernel2D::cosineKernel(1, 2));
self::assertEquals(0.0, Kernel2D::cosineKernel(1, -2));
}
public function testLogistic() : void
{
self::assertEquals(0.5, Kernel2D::logisticKernel(1, 0));
self::assertEquals(0.0, Kernel2D::logisticKernel(1, -1));
self::assertEquals(0.0, Kernel2D::logisticKernel(1, 1));
self::assertEquals(0.0, Kernel2D::logisticKernel(1, 2));
self::assertEquals(0.0, Kernel2D::logisticKernel(1, -2));
}
}

View File

@ -92,6 +92,20 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase
);
}
/**
* @testdox The cosine distance can be calculated
* @covers phpOMS\Math\Topology\MetricsND
* @group framework
*/
public function testCosine() : void
{
self::assertEqualsWithDelta(
14 / 15,
MetricsND::cosine(['x' => 3, 'y' => 4, 'z' => 0], ['x' => 4, 'y' => 4, 'z' => 2]),
0.1
);
}
/**
* @testdox The bray-curtis distance can be calculated
* @covers phpOMS\Math\Topology\MetricsND
@ -193,6 +207,18 @@ final class MetricsNDTest extends \PHPUnit\Framework\TestCase
MetricsND::canberra([3, 6, 4], [4, 6, 8, 3]);
}
/**
* @testdox Different dimension sizes for the coordinates in the cosine metric throw a InvalidDimensionException
* @covers phpOMS\Math\Topology\MetricsND
* @group framework
*/
public function testInvalidCosineDimension() : void
{
$this->expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class);
MetricsND::cosine([3, 6, 4], [4, 6, 8, 3]);
}
/**
* @testdox Different dimension sizes for the coordinates in the Bray Curtis metric throw a InvalidDimensionException
* @covers phpOMS\Math\Topology\MetricsND

View File

@ -96,4 +96,78 @@ final class RequestAbstractTest extends \PHPUnit\Framework\TestCase
$this->request->setData('key5', 1);
self::assertEquals(1, $this->request->getData('key5', 'invalid'));
}
public function testDataAllInputOutput() : void
{
$this->request->set('asdf', false);
self::assertFalse(['asdf' => false], $this->request->getData());
}
/**
* @group framework
*/
public function testDataJsonInputOutput() : void
{
$this->request->set('asdf', '[1,2,3]');
self::assertEquals([1,2,3], $this->request->getDataJson('asdf'));
self::assertEquals([1,2,3], $this->request->getData('asdf', 'json'));
}
/**
* @group framework
*/
public function testDataStringInputOutput() : void
{
$this->request->set('asdf', 1);
self::assertEquals('1', $this->request->getDataString('asdf'));
self::assertEquals('1', $this->request->getData('asdf', 'string'));
}
/**
* @group framework
*/
public function testDataBoolInputOutput() : void
{
$this->request->set('asdf', 1);
self::assertEquals(true, $this->request->getDataBool('asdf'));
self::assertEquals(true, $this->request->getData('asdf', 'bool'));
}
/**
* @group framework
*/
public function testDataFloatInputOutput() : void
{
$this->request->set('asdf', 1);
self::assertEquals(1.0, $this->request->getDataFloat('asdf'));
self::assertEquals(1.0, $this->request->getData('asdf', 'float'));
}
/**
* @group framework
*/
public function testDataDateTimeInputOutput() : void
{
$this->request->set('asdf', '2023-01-01');
self::assertEquals((new \DateTime('2023-01-01'))->format('Y-m-d'), $this->request->getDataDateTime('asdf'));
self::assertEquals((new \DateTime('2023-01-01'))->format('Y-m-d'), $this->request->getData('asdf', 'float'));
}
public function testDataInvalidTypeInputOutput() : void
{
$this->request->set('asdf', 1);
self::assertEquals(1, $this->request->getData('asdf', 'invalid'));
}
/**
* @group framework
*/
public function testInvalidDataTypeInputOutput() : void
{
self::assertEquals(null, $this->request->getDataString('a'));
self::assertEquals(null, $this->request->getDataBool('a'));
self::assertEquals(null, $this->request->getDataInt('a'));
self::assertEquals(null, $this->request->getDataFloat('a'));
self::assertEquals(null, $this->request->getDataDateTime('a'));
}
}

View File

@ -67,6 +67,12 @@ final class ResponseAbstractTest extends \PHPUnit\Framework\TestCase
self::assertEquals([1], $this->response->jsonSerialize());
}
public function testDataAllInputOutput() : void
{
$this->response->set('asdf', false);
self::assertFalse(['asdf' => false], $this->response->getData());
}
/**
* @testdox Data can be set and returned for the response
* @covers phpOMS\Message\ResponseAbstract
@ -77,4 +83,82 @@ final class ResponseAbstractTest extends \PHPUnit\Framework\TestCase
$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'));
self::assertEquals([1,2,3], $this->response->getData('asdf', 'json'));
}
/**
* @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'));
self::assertEquals((new \DateTime('2023-01-01'))->format('Y-m-d'), $this->response->getData('asdf', 'float'));
}
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'));
}
}

View File

@ -0,0 +1,52 @@
<?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\Stdlib\Tree;
use phpOMS\Stdlib\Tree\BinarySearchTree;
use phpOMS\Stdlib\Tree\Node;
/**
* @testdox phpOMS\tests\Stdlib\Tree\BinarySearchTreeTest: Priority queue
*
* @internal
*/
final class BinarySearchTreeTest extends \PHPUnit\Framework\TestCase
{
public function testBST() : void
{
$bst = new BinarySearchTree();
$bst->insert(new Node('D', 'D'));
$bst->insert(new Node('I', 'I'));
$bst->insert(new Node('N', 'N'));
$bst->insert(new Node('O', 'O'));
$bst->insert(new Node('S', 'S'));
$bst->insert(new Node('A', 'A'));
$bst->insert(new Node('U', 'U'));
$bst->insert(new Node('R', 'R'));
$bst->delete($bst->search('I'));
$bst->insert(new Node('Z', 'Z'));
$bst->delete($bst->search('S'));
$bst->insert(new Node('T', 'T'));
self::assertEquals(
[
'key' => 'D',
0 => ['key' => 'I'],
1 => ['key' => 'I'],
],
$bst->toArray()
);
}
}

View File

@ -52,4 +52,9 @@ final class SystemUtilsTest extends \PHPUnit\Framework\TestCase
{
self::assertGreaterThan(0, SystemUtils::getCpuUsage());
}
public function testHostname() : void
{
self::assertEquals('localhost.localdomain', SystemUtils::getHostname());
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,43 @@
<?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\Utils\Parser\Calendar;
include_once __DIR__ . '/../../../Autoloader.php';
use phpOMS\System\File\Local\Directory;
use phpOMS\Utils\Parser\Calendar\ICalParser;
/**
* @internal
*/
final class ICalParserTest extends \PHPUnit\Framework\TestCase
{
public function testParsing() : void
{
$files = Directory::list(__DIR__ . '/data');
foreach ($files as $file) {
$data = \explode('.', $file);
if ($data[1] === 'ical'
&& (\json_decode(\file_get_contents(__DIR__ . '/data/' . $data[0] . '.json'), true) !== ($parsed = ICalParser::parse(\file_get_contents(__DIR__ . '/data/' . $data[0] . '.ical'))))
) {
self::assertTrue(false, $file . "\n\n" . $parsed);
}
}
self::assertTrue(true);
}
}

View File

@ -0,0 +1,22 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ZContent.net//Zap Calendar 1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
SUMMARY:Abraham Lincoln
UID:c7614cff-3549-4a00-9152-d25cc1fe077d
SEQUENCE:0
STATUS:CONFIRMED
TRANSP:TRANSPARENT
RRULE:FREQ=YEARLY;INTERVAL=1;BYMONTH=2;BYMONTHDAY=12
DTSTART:20080212
DTEND:20080213
DTSTAMP:20150421T141403
CATEGORIES:U.S. Presidents,Civil War People
LOCATION:Hodgenville\, Kentucky
GEO:37.5739497;-85.7399606
DESCRIPTION:Born February 12\, 1809\nSixteenth President (1861-1865)\n\n\n\nhttp://AmericanHistoryCalendar.com
URL:http://americanhistorycalendar.com/peoplecalendar/1,328-abraham-lincoln
END:VEVENT
END:VCALENDAR

View File

@ -0,0 +1,21 @@
[
{
"uid": "c7614cff-3549-4a00-9152-d25cc1fe077d",
"status": "CONFIRMED",
"start": "20080212",
"end": "20080213",
"organizer": "",
"summary": "Abraham Lincoln",
"description": "Born February 12, 1809\nSixteenth President (1861-1865)\n\n\n\nhttp://AmericanHistoryCalendar.com",
"location": "Hodgenville, Kentucky",
"geo": {
"lat": -85.7399606,
"lon": 37.5739497
},
"url": "http://americanhistorycalendar.com/peoplecalendar/1,328-abraham-lincoln",
"freq": "YEARLY",
"interval": "1",
"count": "",
"until": ""
}
]

View File

@ -0,0 +1,35 @@
<?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\Utils\Parser\Document;
include_once __DIR__ . '/../../../Autoloader.php';
use PhpOffice\PhpWord\IOFactory;
use phpOMS\Utils\Parser\Document\DocumentWriter;
/**
* @internal
*/
final class DocumentWriterTest extends \PHPUnit\Framework\TestCase
{
public function testParsing() : void
{
$doc = IOFactory::load(__DIR__ . '/data/Word.docx');
$writer = new DocumentWriter($doc);
$pdf = $writer->toPdfString(__DIR__ . '/data/Mpdf.pdf');
self::assertTrue(\is_file(__DIR__ . '/data/Mpdf.pdf'));
}
}

Binary file not shown.

View File

@ -52,4 +52,34 @@ final class MarkdownTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_get_contents(__DIR__ . '/manualdata/xss_bad_url.html') === ($parsed = $parser->text(\file_get_contents(__DIR__ . '/manualdata/xss_bad_url.md'))), $parsed);
}
public function testTablespan() : void
{
$parser = new Markdown([
'tables' => [
'tablespan' => true
]
]);
self::assertTrue(\file_get_contents(__DIR__ . '/manualdata/tablespan.html') === ($parsed = $parser->text(\file_get_contents(__DIR__ . '/manualdata/tablespan.md'))), $parsed);
}
public function testMath() : void
{
$parser = new Markdown([
'math' => true
]);
self::assertTrue(\file_get_contents(__DIR__ . '/manualdata/katex.html') === ($parsed = $parser->text(\file_get_contents(__DIR__ . '/manualdata/katex.md'))), $parsed);
}
public function testTOC() : void
{
$parser = new Markdown([
'toc' => true
]);
self::assertTrue(\file_get_contents(__DIR__ . '/manualdata/toc.html') === ($parsed = $parser->text(\file_get_contents(__DIR__ . '/manualdata/toc.md'))), $parsed);
self::assertTrue('' === $parser->contentsList());
}
}

View File

@ -0,0 +1,2 @@
```chartjs
```

View File

@ -0,0 +1,13 @@
```mermaid
sequenceDiagram
participant Alice
participant Bob
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts<br/>prevail...
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
```

View File

@ -0,0 +1,13 @@
```mermaid
sequenceDiagram
participant Alice
participant Bob
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts<br/>prevail...
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
```

View File

@ -0,0 +1 @@
[video src="https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley"]

View File

@ -0,0 +1,3 @@
$$
x = {-b \pm \sqrt{b^2-4ac} \over 2a}.
$$

View File

@ -0,0 +1,3 @@
$$
x = {-b \pm \sqrt{b^2-4ac} \over 2a}.
$$

View File

@ -0,0 +1,36 @@
<table>
<thead>
<tr>
<th align="center" colspan="3">Colspan</th>
<th colspan="2">for thead</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">Lorem</td>
<td align="center">ipsum</td>
<td align="right">dolor</td>
<td>sit</td>
<td>amet</td>
</tr>
<tr>
<td align="center">-</td>
<td align="right" colspan="2">right align</td>
<td>.</td>
</tr>
<tr>
<td>,</td>
<td align="center" colspan="2">center align</td>
<td colspan="2" rowspan="2">2x2 cell</td>
</tr>
<tr>
<td align="center" colspan="2" rowspan="2">another 2x2</td>
<td align="right">+</td>
</tr>
<tr>
<td align="right"></td>
<td></td>
<td>!</td>
</tr>
</tbody>
</table>

View File

@ -0,0 +1,7 @@
| > | > | Colspan | > | for thead |
| ----- | :---------: | -----------: | ----------- | --------- |
| Lorem | ipsum | dolor | sit | amet |
| ^ | - | > | right align | . |
| , | > | center align | > | 2x2 cell |
| > | another 2x2 | + | > | ^ |
| > | ^ | | | ! |

View File

@ -0,0 +1,7 @@
# A
## 1
### i
# B

View File

@ -0,0 +1,38 @@
<?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\Utils\Parser\Presentation;
include_once __DIR__ . '/../../../Autoloader.php';
use phpOMS\Utils\Parser\Presentation\PresentationWriter;
use PhpOffice\PhpPresentation\IOFactory;
/**
* @internal
*/
final class PresentationWriterTest extends \PHPUnit\Framework\TestCase
{
public function testParsing() : void
{
$presentation = IOFactory::load(__DIR__ . '/data/Powerpoint.pptx');
$writer = new PresentationWriter($presentation);
self::assertEquals(
\file_get_contents(__DIR__ . '/data/Powerpoint.html'),
$writer->renderHtml()
);
}
}

Binary file not shown.

View File

@ -0,0 +1,35 @@
<?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\Utils\Parser\Spreadsheet;
include_once __DIR__ . '/../../../Autoloader.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use phpOMS\Utils\Parser\Spreadsheet\SpreadsheetWriter;
/**
* @internal
*/
final class SpreadsheetWriterTest extends \PHPUnit\Framework\TestCase
{
public function testParsing() : void
{
$sheet = IOFactory::load(__DIR__ . '/data/Excel.xlsx');
$writer = new SpreadsheetWriter($sheet);
$pdf = $writer->toPdfString(__DIR__ . '/data/Mpdf.pdf');
self::assertTrue(\is_file(__DIR__ . '/data/Mpdf.pdf'));
}
}

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 47 KiB

View File

Before

Width:  |  Height:  |  Size: 481 KiB

After

Width:  |  Height:  |  Size: 481 KiB

BIN
tests/Utils/img/img1.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

BIN
tests/Utils/img/img1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View File

Before

Width:  |  Height:  |  Size: 513 KiB

After

Width:  |  Height:  |  Size: 513 KiB

BIN
tests/Utils/img/img2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

BIN
tests/Utils/img/img2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

Before

Width:  |  Height:  |  Size: 507 KiB

After

Width:  |  Height:  |  Size: 507 KiB

0
tests/Utils/logo.png → tests/Utils/img/logo.png Executable file → Normal file
View File

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 88 KiB

View File

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 54 KiB