diff --git a/Account/Account.php b/Account/Account.php index 44579fb7d..d01c9714d 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -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; diff --git a/Account/PermissionAbstract.php b/Account/PermissionAbstract.php index 490e4e04c..8f829df80 100644 --- a/Account/PermissionAbstract.php +++ b/Account/PermissionAbstract.php @@ -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. * diff --git a/DataStorage/Cache/Connection/FileCache.php b/DataStorage/Cache/Connection/FileCache.php index 29b2c6fdc..dfebe1f59 100644 --- a/DataStorage/Cache/Connection/FileCache.php +++ b/DataStorage/Cache/Connection/FileCache.php @@ -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.'); } /** diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index 7866565fa..0c4240f03 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -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); } /** diff --git a/Localization/Money.php b/Localization/Money.php index ee3b07abc..216444237 100644 --- a/Localization/Money.php +++ b/Localization/Money.php @@ -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 */ diff --git a/Message/Http/Header.php b/Message/Http/Header.php index d539b67e6..3d0402f2d 100644 --- a/Message/Http/Header.php +++ b/Message/Http/Header.php @@ -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; } } diff --git a/Message/Http/Response.php b/Message/Http/Response.php index dbd7e386c..b07d043e0 100644 --- a/Message/Http/Response.php +++ b/Message/Http/Response.php @@ -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) { diff --git a/Message/Http/Rest.php b/Message/Http/Rest.php index 6b9b13090..b870a5178 100644 --- a/Message/Http/Rest.php +++ b/Message/Http/Rest.php @@ -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); diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index d6ec81291..057bafc7e 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -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'); } /** diff --git a/Router/Router.php b/Router/Router.php index 84bcf8edb..c5f272834 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -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) { diff --git a/Stdlib/Map/MultiMap.php b/Stdlib/Map/MultiMap.php index 05dfa9084..3172c872a 100644 --- a/Stdlib/Map/MultiMap.php +++ b/Stdlib/Map/MultiMap.php @@ -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); } /** diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index e5c956eb1..43c19a21c 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -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'); } } diff --git a/Utils/RnG/Text.php b/Utils/RnG/Text.php index dd3b5b513..c02491ff6 100644 --- a/Utils/RnG/Text.php +++ b/Utils/RnG/Text.php @@ -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'; diff --git a/Utils/StringUtils.php b/Utils/StringUtils.php index 9c2e84b51..918e3b4d8 100644 --- a/Utils/StringUtils.php +++ b/Utils/StringUtils.php @@ -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 and 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 '' . $new . ''; } - if ($splitNew === false) { + if ($splitNew === false + || (!empty($old) && empty($new)) + || (!empty($delim) && \count($splitNew) === 1 && $splitNew[0] === '') + ) { return '' . $old . ''; } @@ -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 .= ''; + $result = (!empty($delim) ? \rtrim($result, $delim) : $result) . '' . $delim; break; case 1: - $result .= ''; + $result = (!empty($delim) ? \rtrim($result, $delim) : $result) . '' . $delim; break; } switch ($mc) { case -1: - $result .= ''; + $result = (!empty($delim) && ($pmc === 1 || $pmc === -1) ? \rtrim($result, $delim) : $result) . ''; break; case 1: - $result .= ''; + $result = (!empty($delim) && ($pmc === 1 || $pmc === -1) ? \rtrim($result, $delim) : $result) . ''; break; } } - $result .= $diffval[$i]; + $result .= $diffval[$i] . (!empty($delim) ? $delim : ''); $pmc = $mc; } + $result = (!empty($delim) ? \rtrim($result, $delim) : $result); + switch ($pmc) { case -1: $result .= ''; diff --git a/Views/View.php b/Views/View.php index bd6b9b2d5..2f1843879 100644 --- a/Views/View.php +++ b/Views/View.php @@ -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); diff --git a/tests/DataStorage/Cache/Connection/FileCacheTest.php b/tests/DataStorage/Cache/Connection/FileCacheTest.php index 1f9c53dc9..983ad6bb7 100644 --- a/tests/DataStorage/Cache/Connection/FileCacheTest.php +++ b/tests/DataStorage/Cache/Connection/FileCacheTest.php @@ -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); + } } diff --git a/tests/DataStorage/Cookie/CookieJarTest.php b/tests/DataStorage/Cookie/CookieJarTest.php index cc002acb1..b1906dc0a 100644 --- a/tests/DataStorage/Cookie/CookieJarTest.php +++ b/tests/DataStorage/Cookie/CookieJarTest.php @@ -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 diff --git a/tests/Dispatcher/DispatcherTest.php b/tests/Dispatcher/DispatcherTest.php index 1043e66c0..5691ede43 100644 --- a/tests/Dispatcher/DispatcherTest.php +++ b/tests/Dispatcher/DispatcherTest.php @@ -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 diff --git a/tests/Dispatcher/TestController.php b/tests/Dispatcher/TestController.php index 9c2d13832..8612cb5a4 100644 --- a/tests/Dispatcher/TestController.php +++ b/tests/Dispatcher/TestController.php @@ -19,6 +19,11 @@ class TestController return true; } + public function testFunctionNoPara() + { + return true; + } + public static function testFunctionStatic($req, $resp, $data = null) { return true; diff --git a/tests/Log/FileLoggerTest.php b/tests/Log/FileLoggerTest.php index 7a62fe7b6..1980dbb79 100644 --- a/tests/Log/FileLoggerTest.php +++ b/tests/Log/FileLoggerTest.php @@ -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); diff --git a/tests/Message/Http/HeaderTest.php b/tests/Message/Http/HeaderTest.php index f4306fa22..534a612ec 100644 --- a/tests/Message/Http/HeaderTest.php +++ b/tests/Message/Http/HeaderTest.php @@ -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 diff --git a/tests/Message/Http/RequestTest.php b/tests/Message/Http/RequestTest.php index ab1c4e81b..4dcbe3851 100644 --- a/tests/Message/Http/RequestTest.php +++ b/tests/Message/Http/RequestTest.php @@ -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')); diff --git a/tests/Message/Http/ResponseTest.php b/tests/Message/Http/ResponseTest.php index e3e9b4d5a..7567a1595 100644 --- a/tests/Message/Http/ResponseTest.php +++ b/tests/Message/Http/ResponseTest.php @@ -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()); + } } diff --git a/tests/Router/RouterTest.php b/tests/Router/RouterTest.php index 922741eb1..8e29689ea 100644 --- a/tests/Router/RouterTest.php +++ b/tests/Router/RouterTest.php @@ -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 + ) + ); + } } diff --git a/tests/Router/routerTestFilePermission.php b/tests/Router/routerTestFilePermission.php new file mode 100644 index 000000000..fa13038d8 --- /dev/null +++ b/tests/Router/routerTestFilePermission.php @@ -0,0 +1,20 @@ + [ + 0 => [ + "dest" => "\Modules\Admin\Controller:viewSettingsGeneral", + "verb" => RouteVerb::GET, + 'permission' => [ + 'module' => BackendController::MODULE_NAME, + 'type' => PermissionType::READ, + 'state' => PermissionState::SETTINGS, + ], + ] + ] +]; diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index 5cb9398c0..f3f3702b4 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -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 {}]); + } } diff --git a/tests/Utils/RnG/TextTest.php b/tests/Utils/RnG/TextTest.php index f19e64048..815aae726 100644 --- a/tests/Utils/RnG/TextTest.php +++ b/tests/Utils/RnG/TextTest.php @@ -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()); } } diff --git a/tests/Utils/StringUtilsTest.php b/tests/Utils/StringUtilsTest.php index cba678d4e..19780a080 100644 --- a/tests/Utils/StringUtilsTest.php +++ b/tests/Utils/StringUtilsTest.php @@ -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 tnestw string.', + StringUtils::createDiffMarkup($original, $new) + ); + + self::assertEquals( + 'This is a testnew string.', + StringUtils::createDiffMarkup($original, $new, ' ') + ); + + $original = ''; + $new = 'This is a new string.'; + + self::assertEquals( + '' . $new . '', + StringUtils::createDiffMarkup($original, $new) + ); + + $original = 'This is a new string.'; + $new = ''; + + self::assertEquals( + '' . $original . '', + StringUtils::createDiffMarkup($original, $new) + ); + + $original = 'This is a new string'; + $new = 'This is a new string!'; + + self::assertEquals( + $original . '!', + StringUtils::createDiffMarkup($original, $new) + ); + + $original = 'This is a new string.'; + $new = 'This is a new string'; + + self::assertEquals( + $new . '.', + StringUtils::createDiffMarkup($original, $new) + ); } } diff --git a/tests/Views/ViewTest.php b/tests/Views/ViewTest.php index 65123dd79..8e3e867e1 100644 --- a/tests/Views/ViewTest.php +++ b/tests/Views/ViewTest.php @@ -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('Test', $view->render()); } + public function testSerialize() : void + { + $view = new View(); + self::assertEquals('[]', $view->serialize()); + + $view->setTemplate('/phpOMS/tests/Views/testTemplate'); + self::assertEquals('Test', $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 => 'Test', + 'sub' => ['Test'], + ], + $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();