mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-20 13:28:42 +00:00
More unit tests
This commit is contained in:
parent
1c4dd55396
commit
2596659125
|
|
@ -561,7 +561,7 @@ class Account implements ArrayableInterface, \JsonSerializable
|
|||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws \Exception Throws this exception if the password_hash function fails
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
|
@ -570,7 +570,7 @@ class Account implements ArrayableInterface, \JsonSerializable
|
|||
$temp = \password_hash($password, \PASSWORD_DEFAULT);
|
||||
|
||||
if ($temp === false) {
|
||||
throw new \Exception();
|
||||
throw new \Exception('Internal password_hash error.');
|
||||
}
|
||||
|
||||
$this->password = $temp;
|
||||
|
|
|
|||
|
|
@ -99,6 +99,26 @@ class PermissionAbstract implements \JsonSerializable
|
|||
*/
|
||||
protected $permission = PermissionType::NONE;
|
||||
|
||||
public function __construct(
|
||||
int $unit = null,
|
||||
string $app = null,
|
||||
string $module = null,
|
||||
int $from = 0,
|
||||
int $type = null,
|
||||
int $element = null,
|
||||
int $component = null,
|
||||
int $permission = PermissionType::NONE
|
||||
) {
|
||||
$this->unit = $unit;
|
||||
$this->app = $app;
|
||||
$this->module = $module;
|
||||
$this->from = $from;
|
||||
$this->type = $type;
|
||||
$this->element = $element;
|
||||
$this->component = $component;
|
||||
$this->permission = $permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get permission id.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -200,7 +200,7 @@ class FileCache extends ConnectionAbstract
|
|||
* @param mixed $value Data to cache
|
||||
*
|
||||
* @return int Returns the cache type for a value
|
||||
*
|
||||
*
|
||||
* @throws \InvalidArgumentException This exception is thrown if an unsupported datatype is used
|
||||
*
|
||||
* @since 1.0.0
|
||||
|
|
@ -225,7 +225,7 @@ class FileCache extends ConnectionAbstract
|
|||
return CacheValueType::_JSONSERIALIZABLE;
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException('Invalid value');
|
||||
throw new \InvalidArgumentException('Invalid value type.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -131,10 +131,10 @@ final class Dispatcher
|
|||
throw new \Exception();
|
||||
}
|
||||
|
||||
$views[$controller] = $function(...$data);
|
||||
$views[$controller] = $data === null ? $function() : $function(...$data);
|
||||
} elseif ($c === 2) {
|
||||
$this->getController($dispatch[0]);
|
||||
$views[$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}(...$data);
|
||||
$views[$controller] = $data === null ? $this->controllers[$dispatch[0]]->{$dispatch[1]}() : $this->controllers[$dispatch[0]]->{$dispatch[1]}(...$data);
|
||||
} else {
|
||||
throw new \UnexpectedValueException('Unexpected function.');
|
||||
}
|
||||
|
|
@ -156,11 +156,7 @@ final class Dispatcher
|
|||
{
|
||||
$views = [];
|
||||
foreach ($controller as $controllerSingle) {
|
||||
if ($data === null) {
|
||||
$views += $this->dispatch($controllerSingle);
|
||||
} else {
|
||||
$views += $this->dispatch($controllerSingle, ...$data);
|
||||
}
|
||||
$views += $data === null ? $this->dispatch($controllerSingle) : $this->dispatch($controllerSingle, ...$data);
|
||||
}
|
||||
|
||||
return $views;
|
||||
|
|
@ -178,7 +174,7 @@ final class Dispatcher
|
|||
*/
|
||||
private function dispatchClosure(\Closure $controller, array $data = null)
|
||||
{
|
||||
return $controller($this->app, ...$data);
|
||||
return $data === null ? $controller($this->app) : $controller($this->app, ...$data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ final class Money implements \Serializable
|
|||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws \Exception This exception is thrown if an internal explode or substr error occurs.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
|
@ -111,7 +111,7 @@ final class Money implements \Serializable
|
|||
$split = \explode($decimal, $value);
|
||||
|
||||
if ($split === false) {
|
||||
throw new \Exception();
|
||||
throw new \Exception('Internal explode error.');
|
||||
}
|
||||
|
||||
$left = $split[0];
|
||||
|
|
@ -124,7 +124,7 @@ final class Money implements \Serializable
|
|||
|
||||
$right = \substr($right, 0, self::MAX_DECIMALS);
|
||||
if ($right === false) {
|
||||
throw new \Exception();
|
||||
throw new \Exception('Internal substr error.');
|
||||
}
|
||||
|
||||
return ((int) $left) * 10 ** self::MAX_DECIMALS + (int) \str_pad($right, self::MAX_DECIMALS, '0');
|
||||
|
|
@ -189,7 +189,7 @@ final class Money implements \Serializable
|
|||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws \Exception This exception is thrown if an internal substr error occurs.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -168,7 +168,17 @@ final class Header extends HeaderAbstract
|
|||
foreach ($_SERVER as $name => $value) {
|
||||
$part = \substr($name, 5);
|
||||
if ($part === 'HTTP_') {
|
||||
$headers[\str_replace(' ', '-', \ucwords(\strtolower(\str_replace('_', ' ', $part))))] = $value;
|
||||
$headers[
|
||||
\str_replace(
|
||||
' ',
|
||||
'-',
|
||||
\ucwords(
|
||||
\strtolower(
|
||||
\str_replace('_', ' ', $part)
|
||||
)
|
||||
)
|
||||
)
|
||||
] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -167,9 +167,9 @@ final class Response extends ResponseAbstract implements RenderableInterface
|
|||
try {
|
||||
foreach ($this->response as $key => $response) {
|
||||
if ($response instanceof View) {
|
||||
$result += $response->toArray();
|
||||
$result[] = $response->toArray();
|
||||
} elseif (\is_array($response)) {
|
||||
$result += $response;
|
||||
$result[] = $response;
|
||||
} elseif (\is_scalar($response)) {
|
||||
$result[] = $response;
|
||||
} elseif ($response instanceof \JsonSerializable) {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ final class Rest
|
|||
*
|
||||
* @return string Returns the request result
|
||||
*
|
||||
* @throws \Exception This exception is thrown if an internal curl_init error occurs.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function request(Request $request) : string
|
||||
|
|
@ -39,7 +41,7 @@ final class Rest
|
|||
$curl = \curl_init();
|
||||
|
||||
if ($curl === false) {
|
||||
throw new \Exception();
|
||||
throw new \Exception('Internal curl_init error.');
|
||||
}
|
||||
|
||||
\curl_setopt($curl, CURLOPT_NOBODY, true);
|
||||
|
|
|
|||
|
|
@ -599,15 +599,11 @@ final class ModuleManager
|
|||
*/
|
||||
public function get(string $module) : ModuleAbstract
|
||||
{
|
||||
try {
|
||||
if (!isset($this->running[$module])) {
|
||||
$this->initModule($module);
|
||||
}
|
||||
|
||||
return $this->running[$module] ?? new NullModule();
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
if (!isset($this->running[$module])) {
|
||||
$this->initModule($module);
|
||||
}
|
||||
|
||||
return $this->running[$module] ?? new NullModule();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -626,11 +622,7 @@ final class ModuleManager
|
|||
$modules = (array) $modules;
|
||||
|
||||
foreach ($modules as $module) {
|
||||
try {
|
||||
$this->initModuleController($module);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw $e;
|
||||
}
|
||||
$this->initModuleController($module);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -649,12 +641,8 @@ final class ModuleManager
|
|||
*/
|
||||
private function initModuleController(string $module) : void
|
||||
{
|
||||
try {
|
||||
$this->running[$module] = ModuleFactory::getInstance($module, $this->app);
|
||||
$this->app->dispatcher->set($this->running[$module], '\Modules\\Controller\\' . $module . '\\' . $this->app->appName . 'Controller');
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
$this->running[$module] = ModuleFactory::getInstance($module, $this->app);
|
||||
$this->app->dispatcher->set($this->running[$module], '\Modules\\Controller\\' . $module . '\\' . $this->app->appName . 'Controller');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ final class Router
|
|||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function route(string $request, int $verb = RouteVerb::GET, string $app = '', int $orgId = 1, $account = null) : array
|
||||
public function route(string $request, int $verb = RouteVerb::GET, string $app = null, int $orgId = null, $account = null) : array
|
||||
{
|
||||
$bound = [];
|
||||
foreach ($this->routes as $route => $destination) {
|
||||
|
|
|
|||
|
|
@ -260,9 +260,11 @@ class MultiMap implements \Countable
|
|||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return $this->set(\implode(':', $key), $value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->set(\implode(':', $key), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ final class ArrayUtils
|
|||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
|
@ -257,7 +257,7 @@ final class ArrayUtils
|
|||
$str .= $key . ' => null, ';
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('Unknown default type');
|
||||
throw new \InvalidArgumentException('Unknown default type');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -74,6 +74,20 @@ class Text
|
|||
*/
|
||||
private $sentences = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param bool $hasFormatting Text should have formatting
|
||||
* @param bool $hasParagraphs Text should have paragraphs
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct(bool $hasFormatting = false, bool $hasParagraphs = false)
|
||||
{
|
||||
$this->setFormatting($hasFormatting);
|
||||
$this->setParagraphs($hasParagraphs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the text should have formatting.
|
||||
*
|
||||
|
|
@ -169,7 +183,7 @@ class Text
|
|||
$newSentence = true;
|
||||
}
|
||||
|
||||
$word = $words[rand(0, $wordCount - 1)] ?? '';
|
||||
$word = $words[\mt_rand(0, $wordCount - 1)] ?? '';
|
||||
|
||||
if ($newSentence) {
|
||||
$word = \ucfirst($word);
|
||||
|
|
@ -226,24 +240,24 @@ class Text
|
|||
$punctuation = [];
|
||||
|
||||
for ($i = 0; $i < $length;) {
|
||||
$sentenceLength = \rand($minSentences, $maxSentences);
|
||||
$sentenceLength = \mt_rand($minSentences, $maxSentences);
|
||||
|
||||
if ($i + $sentenceLength > $length || $length - ($i + $sentenceLength) < $minSentences) {
|
||||
$sentenceLength = $length - $i;
|
||||
}
|
||||
|
||||
/* Handle comma */
|
||||
$commaHere = (\rand(0, 100) <= $probComma * 100 && $sentenceLength >= 2 * $minCommaSpacing ? true : false);
|
||||
$commaHere = (\mt_rand(0, 100) <= $probComma * 100 && $sentenceLength >= 2 * $minCommaSpacing ? true : false);
|
||||
$posComma = [];
|
||||
|
||||
if ($commaHere) {
|
||||
$posComma[] = \rand($minCommaSpacing, $sentenceLength - $minCommaSpacing);
|
||||
$posComma[] = \mt_rand($minCommaSpacing, $sentenceLength - $minCommaSpacing);
|
||||
$punctuation[] = [$i + $posComma[0], ','];
|
||||
|
||||
$commaHere = (\rand(0, 100) <= $probComma * 100 && $posComma[0] + $minCommaSpacing * 2 < $sentenceLength ? true : false);
|
||||
$commaHere = (\mt_rand(0, 100) <= $probComma * 100 && $posComma[0] + $minCommaSpacing * 2 < $sentenceLength ? true : false);
|
||||
|
||||
if ($commaHere) {
|
||||
$posComma[] = \rand($posComma[0] + $minCommaSpacing, $sentenceLength - $minCommaSpacing);
|
||||
$posComma[] = \mt_rand($posComma[0] + $minCommaSpacing, $sentenceLength - $minCommaSpacing);
|
||||
$punctuation[] = [$i + $posComma[1], ','];
|
||||
}
|
||||
}
|
||||
|
|
@ -251,14 +265,14 @@ class Text
|
|||
$i += $sentenceLength;
|
||||
|
||||
/* Handle sentence ending */
|
||||
$isDot = (\rand(0, 100) <= $probDot * 100 ? true : false);
|
||||
$isDot = (\mt_rand(0, 100) <= $probDot * 100 ? true : false);
|
||||
|
||||
if ($isDot) {
|
||||
$punctuation[] = [$i, '.'];
|
||||
continue;
|
||||
}
|
||||
|
||||
$isEx = (\rand(0, 100) <= $probExc * 100 ? true : false);
|
||||
$isEx = (\mt_rand(0, 100) <= $probExc * 100 ? true : false);
|
||||
|
||||
if ($isEx) {
|
||||
$punctuation[] = [$i, '!'];
|
||||
|
|
@ -288,7 +302,7 @@ class Text
|
|||
$paragraph = [];
|
||||
|
||||
for ($i = 0; $i < $length;) {
|
||||
$paragraphLength = \rand($minSentence, $maxSentence);
|
||||
$paragraphLength = \mt_rand($minSentence, $maxSentence);
|
||||
|
||||
if ($i + $paragraphLength > $length || $length - ($i + $paragraphLength) < $minSentence) {
|
||||
$paragraphLength = $length - $i;
|
||||
|
|
@ -319,9 +333,9 @@ class Text
|
|||
$formatting = [];
|
||||
|
||||
for ($i = 0; $i < $length; ++$i) {
|
||||
$isCursive = (\rand(0, 1000) <= 1000 * $probCursive ? true : false);
|
||||
$isBold = (\rand(0, 1000) <= 1000 * $probBold ? true : false);
|
||||
$isUline = (\rand(0, 1000) <= 1000 * $probUline ? true : false);
|
||||
$isCursive = (\mt_rand(0, 1000) <= 1000 * $probCursive ? true : false);
|
||||
$isBold = (\mt_rand(0, 1000) <= 1000 * $probBold ? true : false);
|
||||
$isUline = (\mt_rand(0, 1000) <= 1000 * $probUline ? true : false);
|
||||
|
||||
if ($isUline) {
|
||||
$formatting[$i] = 'u';
|
||||
|
|
|
|||
|
|
@ -430,23 +430,30 @@ final class StringUtils
|
|||
/**
|
||||
* Create string difference markup
|
||||
*
|
||||
* @param string $old Old strings
|
||||
* @param string $new New strings
|
||||
* @param string $old Old strings
|
||||
* @param string $new New strings
|
||||
* @param string $delim Delim (e.g '' = compare by character, ' ' = compare by words)
|
||||
*
|
||||
* @return string Markup using <del> and <ins> tags
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function createDiffMarkup(string $old, string $new) : string
|
||||
public static function createDiffMarkup(string $old, string $new, string $delim = '') : string
|
||||
{
|
||||
$splitOld = \str_split($old);
|
||||
$splitNew = \str_split($new);
|
||||
$splitOld = !empty($delim) ? \explode($delim, $old) : \str_split($old);
|
||||
$splitNew = !empty($delim) ? \explode($delim, $new) : \str_split($new);
|
||||
|
||||
if ($splitOld === false) {
|
||||
if ($splitOld === false
|
||||
|| (empty($old) && !empty($new))
|
||||
|| (!empty($delim) && \count($splitOld) === 1 && $splitOld[0] === '')
|
||||
) {
|
||||
return '<ins>' . $new . '</ins>';
|
||||
}
|
||||
|
||||
if ($splitNew === false) {
|
||||
if ($splitNew === false
|
||||
|| (!empty($old) && empty($new))
|
||||
|| (!empty($delim) && \count($splitNew) === 1 && $splitNew[0] === '')
|
||||
) {
|
||||
return '<del>' . $old . '</del>';
|
||||
}
|
||||
|
||||
|
|
@ -461,30 +468,32 @@ final class StringUtils
|
|||
for ($i = 0; $i < $n; ++$i) {
|
||||
$mc = $diffmask[$i];
|
||||
|
||||
if ($mc != $pmc) {
|
||||
if ($mc !== $pmc) {
|
||||
switch ($pmc) {
|
||||
case -1:
|
||||
$result .= '</del>';
|
||||
$result = (!empty($delim) ? \rtrim($result, $delim) : $result) . '</del>' . $delim;
|
||||
break;
|
||||
case 1:
|
||||
$result .= '</ins>';
|
||||
$result = (!empty($delim) ? \rtrim($result, $delim) : $result) . '</ins>' . $delim;
|
||||
break;
|
||||
}
|
||||
|
||||
switch ($mc) {
|
||||
case -1:
|
||||
$result .= '<del>';
|
||||
$result = (!empty($delim) && ($pmc === 1 || $pmc === -1) ? \rtrim($result, $delim) : $result) . '<del>';
|
||||
break;
|
||||
case 1:
|
||||
$result .= '<ins>';
|
||||
$result = (!empty($delim) && ($pmc === 1 || $pmc === -1) ? \rtrim($result, $delim) : $result) . '<ins>';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$result .= $diffval[$i];
|
||||
$result .= $diffval[$i] . (!empty($delim) ? $delim : '');
|
||||
$pmc = $mc;
|
||||
}
|
||||
|
||||
$result = (!empty($delim) ? \rtrim($result, $delim) : $result);
|
||||
|
||||
switch ($pmc) {
|
||||
case -1:
|
||||
$result .= '</del>';
|
||||
|
|
|
|||
|
|
@ -197,7 +197,7 @@ class View extends ViewAbstract
|
|||
|
||||
$module = $module ?? $this->module;
|
||||
$theme = $theme ?? $this->theme;
|
||||
|
||||
|
||||
/** @var string $module */
|
||||
/** @var string $theme */
|
||||
return $this->app->l11nManager->getText($this->l11n->getLanguage(), $module, $theme, $translation);
|
||||
|
|
@ -219,7 +219,7 @@ class View extends ViewAbstract
|
|||
$match = '/Modules/';
|
||||
|
||||
if (($start = \strripos($this->template, $match)) === false) {
|
||||
throw new InvalidModuleException('');
|
||||
throw new InvalidModuleException($this->template);
|
||||
}
|
||||
|
||||
$start = $start + \strlen($match);
|
||||
|
|
@ -247,7 +247,7 @@ class View extends ViewAbstract
|
|||
$match = '/Theme/';
|
||||
|
||||
if (($start = \strripos($this->template, $match)) === false) {
|
||||
throw new InvalidThemeException('');
|
||||
throw new InvalidThemeException($this->template);
|
||||
}
|
||||
|
||||
$start = $start + \strlen($match);
|
||||
|
|
|
|||
|
|
@ -201,4 +201,12 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
$cache = new FileCache('/etc/invalidPathOrPermission^$:?><');
|
||||
}
|
||||
|
||||
public function testInvalidDataType() : void
|
||||
{
|
||||
self::expectException(\InvalidArgumentException::class);
|
||||
|
||||
$cache = new FileCache(__DIR__ . '/Cache');
|
||||
$cache->add('invalid', $cache);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ class CookieJarTest extends \PHPUnit\Framework\TestCase
|
|||
$jar = new CookieJar();
|
||||
|
||||
self::assertFalse(CookieJar::isLocked());
|
||||
self::assertEquals(null, $jar->get('asd'));
|
||||
self::assertFalse($jar->delete('abc'));
|
||||
self::assertFalse($jar->delete('asd'));
|
||||
}
|
||||
|
||||
public function testCookie() : void
|
||||
|
|
|
|||
|
|
@ -52,6 +52,14 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase
|
|||
)
|
||||
)
|
||||
);
|
||||
|
||||
self::assertTrue(
|
||||
!empty(
|
||||
$this->app->dispatcher->dispatch(
|
||||
function($req) { return true; }
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testPathMethod() : void
|
||||
|
|
@ -82,6 +90,14 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase
|
|||
)
|
||||
)
|
||||
);
|
||||
|
||||
self::assertTrue(
|
||||
!empty(
|
||||
$this->app->dispatcher->dispatch(
|
||||
['dest' => 'phpOMS\tests\Dispatcher\TestController:testFunctionNoPara']
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testPathStatic() : void
|
||||
|
|
|
|||
|
|
@ -19,6 +19,11 @@ class TestController
|
|||
return true;
|
||||
}
|
||||
|
||||
public function testFunctionNoPara()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function testFunctionStatic($req, $resp, $data = null)
|
||||
{
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -130,8 +130,15 @@ class FileLoggerTest extends \PHPUnit\Framework\TestCase
|
|||
'file' => self::class,
|
||||
]);
|
||||
$ob = ob_get_clean();
|
||||
self::assertEquals(1, $log->countLogs()['info'] ?? 0);
|
||||
|
||||
// test without output
|
||||
$log->console(FileLogger::MSG_FULL, false, [
|
||||
'message' => 'msg',
|
||||
'line' => 11,
|
||||
'file' => self::class,
|
||||
]);
|
||||
self::assertTrue(\stripos($ob, 'msg;') !== false);
|
||||
self::assertEquals(2, $log->countLogs()['info'] ?? 0);
|
||||
|
||||
\ob_start();
|
||||
$log->console('test', true);
|
||||
|
|
|
|||
|
|
@ -62,6 +62,8 @@ class HeaderTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
$header->setAccount(2);
|
||||
self::AssertEquals(2, $header->getAccount(2));
|
||||
|
||||
$header->setDownloadable('testname', 'mp3');
|
||||
}
|
||||
|
||||
public function testLockedHeaderSet() : void
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ class RequestTest extends \PHPUnit\Framework\TestCase
|
|||
self::assertEquals('http://', $request->__toString());
|
||||
self::assertFalse($request->hasData('key'));
|
||||
self::assertEquals(null, $request->getData('key'));
|
||||
self::assertEquals('en', $request->getRequestLanguage());
|
||||
self::assertEquals('en_US', $request->getLocale());
|
||||
}
|
||||
|
||||
public function testSetGet() : void
|
||||
|
|
@ -92,6 +94,48 @@ class RequestTest extends \PHPUnit\Framework\TestCase
|
|||
self::assertEquals('http://www.google.com/test/path2', $request->__toString());
|
||||
}
|
||||
|
||||
public function testDataJson() : void
|
||||
{
|
||||
$request = new Request(new Http(''));
|
||||
|
||||
$data = [
|
||||
1, 2, 3,
|
||||
'a' => 'b',
|
||||
'b' => [4, 5],
|
||||
];
|
||||
|
||||
$request->setData('abc', \json_encode($data));
|
||||
self::assertEquals($data, $request->getDataJson('abc'));
|
||||
self::assertEquals([], $request->getDataJson('def'));
|
||||
}
|
||||
|
||||
public function testDataList() : void
|
||||
{
|
||||
$request = new Request(new Http(''));
|
||||
|
||||
$data = [
|
||||
1, 2, 3,
|
||||
'a', 'b',
|
||||
];
|
||||
|
||||
$request->setData('abc', \implode(',', $data));
|
||||
self::assertEquals($data, $request->getDataList('abc'));
|
||||
self::assertEquals([], $request->getDataList('def'));
|
||||
}
|
||||
|
||||
public function testDataLike() : void
|
||||
{
|
||||
$request = new Request(new Http(''));
|
||||
|
||||
$data = 'this is a test';
|
||||
|
||||
$request->setData('abcde', $data);
|
||||
self::assertEquals(['abcde' => $data], $request->getLike('.*'));
|
||||
self::assertEquals(['abcde' => $data], $request->getLike('[a-z]*'));
|
||||
self::assertEquals([], $request->getLike('[a-z]*\d'));
|
||||
self::assertEquals([], $request->getLike('abcdef'));
|
||||
}
|
||||
|
||||
public function testToString() : void
|
||||
{
|
||||
$request = new Request(new Http('http://www.google.com/test/path'));
|
||||
|
|
|
|||
|
|
@ -15,12 +15,13 @@ namespace phpOMS\tests\Message\Http;
|
|||
|
||||
use phpOMS\Localization\Localization;
|
||||
use phpOMS\Message\Http\Response;
|
||||
use phpOMS\System\MimeType;
|
||||
|
||||
class ResponseTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDefault() : void
|
||||
{
|
||||
$response = new Response(new Localization());
|
||||
$response = new Response();
|
||||
self::assertEquals('', $response->getBody());
|
||||
self::assertEquals('', $response->render());
|
||||
self::assertEquals([], $response->toArray());
|
||||
|
|
@ -30,10 +31,56 @@ class ResponseTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public function testSetGet() : void
|
||||
{
|
||||
$response = new Response(new Localization());
|
||||
$response = new Response();
|
||||
|
||||
$response->setResponse(['a' => 1]);
|
||||
self::assertTrue($response->remove('a'));
|
||||
self::assertFalse($response->remove('a'));
|
||||
}
|
||||
|
||||
public function testWithData() : void
|
||||
{
|
||||
$response = new Response();
|
||||
|
||||
$data = [
|
||||
['view_string'],
|
||||
[1, 2, 3, 'a', 'b', [4, 5]],
|
||||
'stringVal',
|
||||
6,
|
||||
false,
|
||||
1.13,
|
||||
'json_string'
|
||||
];
|
||||
|
||||
$response->set('view', new class extends \phpOMS\Views\View {
|
||||
public function toArray() : array
|
||||
{
|
||||
return ['view_string'];
|
||||
}
|
||||
});
|
||||
$response->set('array', $data[1]);
|
||||
$response->set('string', $data[2]);
|
||||
$response->set('int', $data[3]);
|
||||
$response->set('bool', $data[4]);
|
||||
$response->set('float', $data[5]);
|
||||
$response->set('jsonSerializable', new class implements \JsonSerializable {
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return 'json_string';
|
||||
}
|
||||
});
|
||||
$response->set('null', null);
|
||||
|
||||
self::assertEquals($data, $response->toArray());
|
||||
|
||||
$response->getHeader()->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true);
|
||||
self::assertEquals(\json_encode($data), $response->render());
|
||||
}
|
||||
|
||||
public function testInvalidResponseData() : void
|
||||
{
|
||||
$response = new Response();
|
||||
$response->set('invalid', new class {});
|
||||
self::assertEquals([], $response->toArray());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,11 @@ namespace phpOMS\tests\Router;
|
|||
|
||||
use phpOMS\Message\Http\Request;
|
||||
use phpOMS\Router\Router;
|
||||
use phpOMS\Account\Account;
|
||||
use Modules\Admin\Controller\BackendController;
|
||||
use Modules\Admin\Models\PermissionState;
|
||||
use phpOMS\Account\PermissionType;
|
||||
use phpOMS\Account\PermissionAbstract;
|
||||
use phpOMS\Router\RouteVerb;
|
||||
use phpOMS\Uri\Http;
|
||||
|
||||
|
|
@ -73,4 +78,84 @@ class RouterTest extends \PHPUnit\Framework\TestCase
|
|||
$router->route('http://test.com/backends/admin/settings/general/something?test', RouteVerb::GET)
|
||||
);
|
||||
}
|
||||
|
||||
public function testWithPermissions() : void
|
||||
{
|
||||
$router = new Router();
|
||||
self::assertTrue($router->importFromFile(__Dir__ . '/routerTestFilePermission.php'));
|
||||
|
||||
$perm = new class(
|
||||
null,
|
||||
null,
|
||||
BackendController::MODULE_NAME,
|
||||
0,
|
||||
PermissionState::SETTINGS,
|
||||
null,
|
||||
null,
|
||||
PermissionType::READ
|
||||
) extends PermissionAbstract {};
|
||||
|
||||
$account = new Account();
|
||||
$account->addPermission($perm);
|
||||
|
||||
self::assertEquals(
|
||||
[['dest' => '\Modules\Admin\Controller:viewSettingsGeneral']],
|
||||
$router->route(
|
||||
'http://test.com/backend/admin/settings/general/something?test',
|
||||
RouteVerb::GET,
|
||||
null,
|
||||
null,
|
||||
$account
|
||||
)
|
||||
);
|
||||
|
||||
$perm2 = new class(
|
||||
null,
|
||||
null,
|
||||
BackendController::MODULE_NAME,
|
||||
0,
|
||||
PermissionState::SETTINGS,
|
||||
null,
|
||||
null,
|
||||
PermissionType::CREATE
|
||||
) extends PermissionAbstract {};
|
||||
|
||||
$perm3 = new class(
|
||||
null,
|
||||
null,
|
||||
'InvalidModule',
|
||||
0,
|
||||
PermissionState::SETTINGS,
|
||||
null,
|
||||
null,
|
||||
PermissionType::READ
|
||||
) extends PermissionAbstract {};
|
||||
|
||||
$perm4 = new class(
|
||||
null,
|
||||
null,
|
||||
BackendController::MODULE_NAME,
|
||||
0,
|
||||
99,
|
||||
null,
|
||||
null,
|
||||
PermissionType::READ
|
||||
) extends PermissionAbstract {};
|
||||
|
||||
$account2 = new Account();
|
||||
$account2->addPermission($perm2);
|
||||
$account2->addPermission($perm3);
|
||||
$account2->addPermission($perm4);
|
||||
|
||||
self::assertNotEquals(
|
||||
[['dest' => '\Modules\Admin\Controller:viewSettingsGeneral']],
|
||||
$router->route(
|
||||
'http://test.com/backend/admin/settings/general/something?test',
|
||||
RouteVerb::GET,
|
||||
null,
|
||||
null,
|
||||
$account2
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
20
tests/Router/routerTestFilePermission.php
Normal file
20
tests/Router/routerTestFilePermission.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
use Modules\Admin\Controller\BackendController;
|
||||
use Modules\Admin\Models\PermissionState;
|
||||
use phpOMS\Account\PermissionType;
|
||||
use phpOMS\Router\RouteVerb;
|
||||
|
||||
return [
|
||||
"^.*/backend/admin/settings/general.*$" => [
|
||||
0 => [
|
||||
"dest" => "\Modules\Admin\Controller:viewSettingsGeneral",
|
||||
"verb" => RouteVerb::GET,
|
||||
'permission' => [
|
||||
'module' => BackendController::MODULE_NAME,
|
||||
'type' => PermissionType::READ,
|
||||
'state' => PermissionState::SETTINGS,
|
||||
],
|
||||
]
|
||||
]
|
||||
];
|
||||
|
|
@ -133,4 +133,11 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase
|
|||
self::assertTrue(\stripos(ArrayUtils::getArg('--configuration', $_SERVER['argv']), '.xml') !== false);
|
||||
}
|
||||
}
|
||||
|
||||
public function testInvalidArrayStringify() : void
|
||||
{
|
||||
self::expectException(\InvalidArgumentException::class);
|
||||
|
||||
ArrayUtils::stringify([new class {}]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,11 +13,21 @@
|
|||
|
||||
namespace phpOMS\tests\Utils\RnG;
|
||||
|
||||
use phpOMS\Utils\RnG\Text;
|
||||
|
||||
class TextTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testPlaceholder() : void
|
||||
public function testRnG() : void
|
||||
{
|
||||
self::markTestIncomplete();
|
||||
$text = new Text(true, true);
|
||||
|
||||
self::assertEquals('', $text->generateText(0));
|
||||
|
||||
self::assertNotEquals(
|
||||
$text->generateText(300),
|
||||
$text->generateText(300)
|
||||
);
|
||||
|
||||
self::assertGreaterThan(0, $text->getSentences());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
namespace phpOMS\tests\Utils;
|
||||
|
||||
use phpOMS\Utils\StringUtils;
|
||||
use phpOMS\Contract\RenderableInterface;
|
||||
|
||||
require_once __DIR__ . '/../Autoloader.php';
|
||||
|
||||
|
|
@ -118,5 +119,60 @@ class StringUtilsTest extends \PHPUnit\Framework\TestCase
|
|||
return 'abc';
|
||||
}
|
||||
}));
|
||||
|
||||
self::assertEquals('abc', StringUtils::stringify(new class implements RenderableInterface {
|
||||
public function render() : string
|
||||
{
|
||||
return 'abc';
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public function testStringDiffHtml() : void
|
||||
{
|
||||
$original = 'This is a test string.';
|
||||
$new = 'This is a new string.';
|
||||
|
||||
self::assertEquals(
|
||||
'This is a <del>t</del><ins>n</ins>e<del>st</del><ins>w</ins> string.',
|
||||
StringUtils::createDiffMarkup($original, $new)
|
||||
);
|
||||
|
||||
self::assertEquals(
|
||||
'This is a <del>test</del><ins>new</ins> string.',
|
||||
StringUtils::createDiffMarkup($original, $new, ' ')
|
||||
);
|
||||
|
||||
$original = '';
|
||||
$new = 'This is a new string.';
|
||||
|
||||
self::assertEquals(
|
||||
'<ins>' . $new . '</ins>',
|
||||
StringUtils::createDiffMarkup($original, $new)
|
||||
);
|
||||
|
||||
$original = 'This is a new string.';
|
||||
$new = '';
|
||||
|
||||
self::assertEquals(
|
||||
'<del>' . $original . '</del>',
|
||||
StringUtils::createDiffMarkup($original, $new)
|
||||
);
|
||||
|
||||
$original = 'This is a new string';
|
||||
$new = 'This is a new string!';
|
||||
|
||||
self::assertEquals(
|
||||
$original . '<ins>!</ins>',
|
||||
StringUtils::createDiffMarkup($original, $new)
|
||||
);
|
||||
|
||||
$original = 'This is a new string.';
|
||||
$new = 'This is a new string';
|
||||
|
||||
self::assertEquals(
|
||||
$new . '<del>.</del>',
|
||||
StringUtils::createDiffMarkup($original, $new)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,17 +111,45 @@ class ViewTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public function testRender() : void
|
||||
{
|
||||
$view = new View($this->app, new Request(), new Response());
|
||||
$view = new View();
|
||||
|
||||
$view->setTemplate('/phpOMS/tests/Views/testTemplate');
|
||||
self::assertEquals('<strong>Test</strong>', $view->render());
|
||||
}
|
||||
|
||||
public function testSerialize() : void
|
||||
{
|
||||
$view = new View();
|
||||
self::assertEquals('[]', $view->serialize());
|
||||
|
||||
$view->setTemplate('/phpOMS/tests/Views/testTemplate');
|
||||
self::assertEquals('<strong>Test</strong>', $view->serialize());
|
||||
}
|
||||
|
||||
public function testArray() : void
|
||||
{
|
||||
$view = new View();
|
||||
self::assertEquals([], $view->toArray());
|
||||
|
||||
$view->setTemplate('/phpOMS/tests/Views/testTemplate');
|
||||
|
||||
$view2 = new View();
|
||||
$view2->setTemplate('/phpOMS/tests/Views/testTemplate');
|
||||
|
||||
$view->addView('sub', $view2, 1);
|
||||
self::assertEquals([
|
||||
0 => '<strong>Test</strong>',
|
||||
'sub' => ['<strong>Test</strong>'],
|
||||
],
|
||||
$view->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderException() : void
|
||||
{
|
||||
self::expectException(\phpOMS\System\File\PathException::class);
|
||||
|
||||
$view = new View($this->app, new Request(new Http('')), new Response());
|
||||
$view = new View($this->app);
|
||||
$view->setTemplate('something.txt');
|
||||
|
||||
$view->render();
|
||||
|
|
@ -131,7 +159,7 @@ class ViewTest extends \PHPUnit\Framework\TestCase
|
|||
{
|
||||
self::expectException(\phpOMS\System\File\PathException::class);
|
||||
|
||||
$view = new View($this->app, new Request(new Http('')), new Response());
|
||||
$view = new View($this->app);
|
||||
$view->setTemplate('something.txt');
|
||||
|
||||
$view->serialize();
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user