From 2a288de9836cac37d5d8215a24881944501e223a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 14 Oct 2021 23:50:26 +0200 Subject: [PATCH] test fixes --- .../Cache/Connection/ConnectionInterface.php | 4 +- DataStorage/Cache/Connection/FileCache.php | 8 +- DataStorage/Cache/Connection/MemCached.php | 14 +- DataStorage/Cache/Connection/NullCache.php | 3 +- DataStorage/Cache/Connection/RedisCache.php | 37 +++-- .../Connection/SqlServerConnection.php | 2 +- Math/Number/Prime.php | 14 +- Message/Console/ConsoleHeader.php | 11 +- Message/Console/ConsoleRequest.php | 13 +- Message/Console/ConsoleResponse.php | 61 ++++---- Message/Http/HttpHeader.php | 4 +- Message/Http/HttpResponse.php | 2 +- Module/ModuleManager.php | 2 +- Module/UninstallerAbstract.php | 2 +- Utils/IO/Zip/Tar.php | 4 + Utils/IO/Zip/Zip.php | 4 +- Validation/Finance/CreditCard.php | 2 +- .../Cache/Connection/FileCacheTest.php | 32 ++++- .../Cache/Connection/MemCachedTest.php | 38 ++++- .../Cache/Connection/NullCacheTest.php | 2 +- .../Cache/Connection/RedisCacheTest.php | 40 +++++- tests/Message/Console/ConsoleHeaderTest.php | 15 +- tests/Message/Console/ConsoleRequestTest.php | 18 +++ tests/Message/Console/ConsoleResponseTest.php | 131 ++++++++++++++++-- tests/Message/Http/HttpHeaderTest.php | 17 +++ tests/Message/Mail/EmailTest.php | 2 +- tests/Message/Mail/MailHandlerTest.php | 1 + tests/Module/StatusAbstractTest.php | 7 +- tests/Utils/ArrayUtilsTest.php | 12 +- tests/Utils/IO/Zip/TarTest.php | 31 +++-- tests/Utils/IO/Zip/ZipTest.php | 9 ++ tests/Utils/IO/Zip/malformed.tar | 1 + tests/Utils/IO/Zip/malformed.zip | Bin 0 -> 4 bytes tests/Validation/Finance/CreditCardTest.php | 1 + tests/Validation/Finance/IbanTest.php | 29 +++- tests/Validation/ValidatorTest.php | 6 + 36 files changed, 445 insertions(+), 134 deletions(-) create mode 100644 tests/Utils/IO/Zip/malformed.tar create mode 100644 tests/Utils/IO/Zip/malformed.zip diff --git a/DataStorage/Cache/Connection/ConnectionInterface.php b/DataStorage/Cache/Connection/ConnectionInterface.php index 87173b14f..e4e221ed8 100644 --- a/DataStorage/Cache/Connection/ConnectionInterface.php +++ b/DataStorage/Cache/Connection/ConnectionInterface.php @@ -70,11 +70,11 @@ interface ConnectionInterface extends DataStorageConnectionInterface * @param int|string $new Unique cache key * @param int $expire Valid duration (in s). Negative expiration means no expiration. * - * @return void + * @return bool * * @since 1.0.0 */ - public function rename(int | string $old, int | string $new, int $expire = -1) : void; + public function rename(int | string $old, int | string $new, int $expire = -1) : bool; /** * Adding new data if it doesn't exist. diff --git a/DataStorage/Cache/Connection/FileCache.php b/DataStorage/Cache/Connection/FileCache.php index 90e0f91b9..983d42cfc 100644 --- a/DataStorage/Cache/Connection/FileCache.php +++ b/DataStorage/Cache/Connection/FileCache.php @@ -355,7 +355,7 @@ final class FileCache extends ConnectionAbstract return $obj; default: - return null; + return null; // @codeCoverageIgnore } } @@ -530,15 +530,17 @@ final class FileCache extends ConnectionAbstract /** * {@inheritdoc} */ - public function rename(int | string $old, int | string $new, int $expire = -1) : void + public function rename(int | string $old, int | string $new, int $expire = -1) : bool { if ($this->status !== CacheStatus::OK) { - return; + return false; } $value = $this->get($old); $this->set($new, $value, $expire); $this->delete($old); + + return true; } /** diff --git a/DataStorage/Cache/Connection/MemCached.php b/DataStorage/Cache/Connection/MemCached.php index 7612801f2..873f20e22 100644 --- a/DataStorage/Cache/Connection/MemCached.php +++ b/DataStorage/Cache/Connection/MemCached.php @@ -142,7 +142,7 @@ final class MemCached extends ConnectionAbstract $result = $this->con->delete((string) $key); - return $this->con->getResultCode() === \Memcached::RES_NOTFOUND ? false : $result; + return $this->con->getResultCode() === \Memcached::RES_NOTFOUND ? true : $result; } /** @@ -184,15 +184,17 @@ final class MemCached extends ConnectionAbstract /** * {@inheritdoc} */ - public function rename(int | string $old, int | string $new, int $expire = -1) : void + public function rename(int | string $old, int | string $new, int $expire = -1) : bool { if ($this->status !== CacheStatus::OK) { - return; + return false; } $value = $this->get((string) $old); $this->set((string) $new, $value, $expire); $this->delete((string) $old); + + return true; } /** @@ -206,6 +208,7 @@ final class MemCached extends ConnectionAbstract * * @since 1.0.0 */ + /* private function reverseValue(int $type, string $raw, int $expireEnd) : mixed { switch ($type) { @@ -246,9 +249,10 @@ final class MemCached extends ConnectionAbstract return $obj; default: - return null; + return null; // @codeCoverageIgnore } } + */ /** * {@inheritdoc} @@ -275,7 +279,7 @@ final class MemCached extends ConnectionAbstract return false; } - return $this->flush(); + return $this->flushAll(); } /** diff --git a/DataStorage/Cache/Connection/NullCache.php b/DataStorage/Cache/Connection/NullCache.php index 2c02d42f3..44ca6e815 100644 --- a/DataStorage/Cache/Connection/NullCache.php +++ b/DataStorage/Cache/Connection/NullCache.php @@ -129,8 +129,9 @@ final class NullCache extends ConnectionAbstract /** * {@inheritdoc} */ - public function rename(int | string $old, int | string $new, int $expire = -1) : void + public function rename(int | string $old, int | string $new, int $expire = -1) : bool { + return true; } /** diff --git a/DataStorage/Cache/Connection/RedisCache.php b/DataStorage/Cache/Connection/RedisCache.php index 2710de130..6499043ff 100644 --- a/DataStorage/Cache/Connection/RedisCache.php +++ b/DataStorage/Cache/Connection/RedisCache.php @@ -146,7 +146,8 @@ final class RedisCache extends ConnectionAbstract $result = $this->con->get((string) $key); - if (\is_string($result)) { + if (\is_string($result) && ($result[0] ?? null) === self::DELIM) { + $result = \substr($result, 1); $type = (int) $result[0]; $start = (int) \strpos($result, self::DELIM); $result = $this->reverseValue($type, $result, $start); @@ -164,7 +165,9 @@ final class RedisCache extends ConnectionAbstract return false; } - return $this->con->del((string) $key) > 0; + $this->con->del((string) $key); + + return true; } /** @@ -184,7 +187,9 @@ final class RedisCache extends ConnectionAbstract */ public function increment(int | string $key, int $value = 1) : bool { - if ($this->status !== CacheStatus::OK) { + if ($this->status !== CacheStatus::OK + || !$this->exists((string) $key) + ) { return false; } @@ -198,7 +203,9 @@ final class RedisCache extends ConnectionAbstract */ public function decrement(int | string $key, int $value = 1) : bool { - if ($this->status !== CacheStatus::OK) { + if ($this->status !== CacheStatus::OK + || !$this->exists((string) $key) + ) { return false; } @@ -210,10 +217,10 @@ final class RedisCache extends ConnectionAbstract /** * {@inheritdoc} */ - public function rename(int | string $old, int | string $new, int $expire = -1) : void + public function rename(int | string $old, int | string $new, int $expire = -1) : bool { if ($this->status !== CacheStatus::OK) { - return; + return false; } $this->con->rename((string) $old, (string) $new); @@ -221,6 +228,8 @@ final class RedisCache extends ConnectionAbstract if ($expire > 0) { $this->con->expire((string) $new, $expire); } + + return true; } /** @@ -238,7 +247,8 @@ final class RedisCache extends ConnectionAbstract foreach ($keys as $key) { if (\preg_match('/' . $pattern . '/', $key) === 1) { $result = $this->con->get((string) $key); - if (\is_string($result)) { + if (\is_string($result) && ($result[0] ?? null) === self::DELIM) { + $result = \substr($result, 1); $type = (int) $result[0]; $start = (int) \strpos($result, self::DELIM); $result = $this->reverseValue($type, $result, $start); @@ -368,7 +378,7 @@ final class RedisCache extends ConnectionAbstract } /** - * Removing all cache elements larger or equal to the expiration date. Call flushAll for removing persistent cache elements (expiration is negative) as well. + * Build value * * @param mixed $value Data to cache * @@ -381,7 +391,10 @@ final class RedisCache extends ConnectionAbstract $type = $this->dataType($value); $raw = $this->cachify($value, $type); - return \is_string($raw) ? $type . self::DELIM . $raw : $raw; + return \is_string($raw) + && ($type !== CacheValueType::_INT && $type !== CacheValueType::_FLOAT) + ? self::DELIM . $type . self::DELIM . $raw + : $raw; } /** @@ -448,7 +461,7 @@ final class RedisCache extends ConnectionAbstract $namespace = \substr($raw, $namespaceStart + 1, $namespaceEnd - $namespaceStart - 1); if ($namespace === false) { - return null; + return null; // @codeCoverageIgnore } return new $namespace(); @@ -458,7 +471,7 @@ final class RedisCache extends ConnectionAbstract $namespace = \substr($raw, $namespaceStart + 1, $namespaceEnd - $namespaceStart - 1); if ($namespace === false) { - return null; + return null; // @codeCoverageIgnore } $obj = new $namespace(); @@ -466,7 +479,7 @@ final class RedisCache extends ConnectionAbstract return $obj; default: - return null; + return null; // @codeCoverageIgnore } } } diff --git a/DataStorage/Database/Connection/SqlServerConnection.php b/DataStorage/Database/Connection/SqlServerConnection.php index cd5705241..70a6ac285 100644 --- a/DataStorage/Database/Connection/SqlServerConnection.php +++ b/DataStorage/Database/Connection/SqlServerConnection.php @@ -77,7 +77,7 @@ final class SqlServerConnection extends ConnectionAbstract try { $this->con = new \PDO('sqlsrv:Server=' . $this->dbdata['host'] . ',' . $this->dbdata['port'] . ';Database=' . $this->dbdata['database'] . ';ConnectionPooling=0', $this->dbdata['login'], $this->dbdata['password']); - $this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + //$this->con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); // Not working! $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->status = DatabaseStatus::OK; diff --git a/Math/Number/Prime.php b/Math/Number/Prime.php index 46d865f80..1345c827e 100644 --- a/Math/Number/Prime.php +++ b/Math/Number/Prime.php @@ -96,22 +96,22 @@ final class Prime $a = \mt_rand(2, $n - 1); $x = \bcpowmod((string) $a, (string) $d, (string) $n); + if ($x === false) { + return false; + } + if ($x == 1 || $x == $n - 1) { continue; } for ($j = 1; $j < $s; ++$j) { - if ($x === null || $x === false) { - return false; - } - $mul = \bcmul($x, $x); - if ($mul === null) { + /*if ($mul === null) { return false; - } + }*/ $x = \bcmod($mul, (string) $n); - if ($x == 1 || $x === null) { + if ($x == 1) { return false; } diff --git a/Message/Console/ConsoleHeader.php b/Message/Console/ConsoleHeader.php index c7844d8da..8272239ef 100644 --- a/Message/Console/ConsoleHeader.php +++ b/Message/Console/ConsoleHeader.php @@ -4,7 +4,7 @@ * * PHP Version 8.0 * - * @package phpOMS\Message\Http + * @package phpOMS\Message\Console * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -19,7 +19,7 @@ use phpOMS\Message\HeaderAbstract; /** * Response class. * - * @package phpOMS\Message\Http + * @package phpOMS\Message\Console * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 @@ -51,7 +51,6 @@ final class ConsoleHeader extends HeaderAbstract */ public function __construct() { - $this->set('Content-Type', 'text/html; charset=utf-8'); parent::__construct(); } @@ -142,11 +141,7 @@ final class ConsoleHeader extends HeaderAbstract */ public function get(string $key = null) : array { - if ($key === null) { - return $this->header; - } - - return $this->header[\strtolower($key)] ?? []; + return $key === null ? $this->header : ($this->header[\strtolower($key)] ?? []); } /** diff --git a/Message/Console/ConsoleRequest.php b/Message/Console/ConsoleRequest.php index 39ee7f1fd..8fee477fa 100644 --- a/Message/Console/ConsoleRequest.php +++ b/Message/Console/ConsoleRequest.php @@ -99,13 +99,18 @@ final class ConsoleRequest extends RequestAbstract */ public function createRequestHashs(int $start = 0) : void { - $this->hash = []; + $this->hash = [\sha1('')]; $pathArray = $this->uri->getPathElements(); + $pathLength = \count($pathArray); + + for ($i = $start; $i < $pathLength; ++$i) { + if ($pathArray[$i] === '') { + continue; + } - foreach ($pathArray as $key => $path) { $paths = []; - for ($i = $start; $i < $key + 1; ++$i) { - $paths[] = $pathArray[$i]; + for ($j = $start; $j <= $i; ++$j) { + $paths[] = $pathArray[$j]; } $this->hash[] = \sha1(\implode('', $paths)); diff --git a/Message/Console/ConsoleResponse.php b/Message/Console/ConsoleResponse.php index 0cc881ea6..6298a3026 100644 --- a/Message/Console/ConsoleResponse.php +++ b/Message/Console/ConsoleResponse.php @@ -4,7 +4,7 @@ * * PHP Version 8.0 * - * @package phpOMS\Message\Http + * @package phpOMS\Message\Console * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -20,12 +20,13 @@ use phpOMS\Log\FileLogger; use phpOMS\Message\Http\RequestStatusCode; use phpOMS\Message\ResponseAbstract; use phpOMS\System\MimeType; +use phpOMS\Utils\StringUtils; use phpOMS\Views\View; /** * Response class. * - * @package phpOMS\Message\Http + * @package phpOMS\Message\Console * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 @@ -133,13 +134,7 @@ final class ConsoleResponse extends ResponseAbstract implements RenderableInterf $render = ''; foreach ($this->response as $key => $response) { - if ($response instanceof \Serializable) { - $render .= $response->serialize(); - } elseif (\is_string($response) || \is_numeric($response)) { - $render .= $response; - } else { - throw new \Exception('Wrong response type'); - } + $render .= StringUtils::stringify($response); } return $render; @@ -152,35 +147,27 @@ final class ConsoleResponse extends ResponseAbstract implements RenderableInterf { $result = []; - try { - foreach ($this->response as $key => $response) { - if ($response instanceof View) { - $result += $response->toArray(); - } elseif (\is_array($response)) { - $result += $response; - } elseif (\is_scalar($response)) { - $result[] = $response; - } elseif ($response instanceof \JsonSerializable) { - $result[] = $response->jsonSerialize(); - } elseif ($response === null) { - continue; - } else { - throw new \Exception('Wrong response type'); - } + foreach ($this->response as $key => $response) { + if ($response instanceof View) { + $result[] = $response->toArray(); + } elseif (\is_array($response) || \is_scalar($response)) { + $result[] = $response; + } elseif ($response instanceof \JsonSerializable) { + $result[] = $response->jsonSerialize(); + } elseif ($response === null) { + continue; + } else { + FileLogger::getInstance('', false) + ->error( + FileLogger::MSG_FULL, [ + 'message' => 'Unknown type.', + 'line' => __LINE__, + 'file' => self::class, + ] + ); } - } catch (\Exception $e) { - FileLogger::getInstance('', false) - ->error( - FileLogger::MSG_FULL, [ - 'message' => $e->getMessage(), - 'line' => __LINE__, - 'file' => self::class, - ] - ); - - $result = []; - } finally { - return $result; } + + return $result; } } diff --git a/Message/Http/HttpHeader.php b/Message/Http/HttpHeader.php index e022c7fbf..7d787ea4e 100644 --- a/Message/Http/HttpHeader.php +++ b/Message/Http/HttpHeader.php @@ -149,7 +149,7 @@ final class HttpHeader extends HeaderAbstract } foreach ($_SERVER as $name => $value) { - $part = \substr($name, 5); + $part = \substr($name, 0, 5); if ($part === 'HTTP_') { self::$serverHeaders[ \str_replace( @@ -157,7 +157,7 @@ final class HttpHeader extends HeaderAbstract '-', \ucwords( \strtolower( - \str_replace('_', ' ', $part) + \str_replace('_', ' ', \substr($name, 5)) ) ) ) diff --git a/Message/Http/HttpResponse.php b/Message/Http/HttpResponse.php index a84506694..919f03544 100644 --- a/Message/Http/HttpResponse.php +++ b/Message/Http/HttpResponse.php @@ -211,7 +211,7 @@ final class HttpResponse extends ResponseAbstract implements RenderableInterface public function endAllOutputBuffering(int $levels = 0) : void { if (!$this->header->isLocked()) { - $this->header->push(); + $this->header->push(); // @codeCoverageIgnore } $levels = $levels === 0 ? \ob_get_level() : $levels; diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 7a4cf39bc..cf1df90b0 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -584,7 +584,7 @@ final class ModuleManager return true; } catch (\Throwable $t) { - return false; + return false; // @codeCoverageIgnore } } diff --git a/Module/UninstallerAbstract.php b/Module/UninstallerAbstract.php index b97fd5299..2afb1c451 100644 --- a/Module/UninstallerAbstract.php +++ b/Module/UninstallerAbstract.php @@ -66,7 +66,7 @@ abstract class UninstallerAbstract protected static function deactivate(DatabasePool $dbPool, ModuleInfo $info) : void { if (($path = \realpath(static::PATH)) === false) { - return; // @codeCoverageIgnore; + return; // @codeCoverageIgnore } /** @var string $classPath */ diff --git a/Utils/IO/Zip/Tar.php b/Utils/IO/Zip/Tar.php index 6f3654d06..a87208497 100644 --- a/Utils/IO/Zip/Tar.php +++ b/Utils/IO/Zip/Tar.php @@ -22,6 +22,10 @@ use phpOMS\System\File\Local\Directory; * * Providing basic zip support * + * IMPORTANT: + * PharData seems to cache created files, which means even if the previously created file is deleted, it cannot create a new file with the same destination. + * bug? https://bugs.php.net/bug.php?id=75101 + * * @package phpOMS\Utils\IO\Zip * @license OMS License 1.0 * @link https://orange-management.org diff --git a/Utils/IO/Zip/Zip.php b/Utils/IO/Zip/Zip.php index 699b60103..33e45c0e4 100644 --- a/Utils/IO/Zip/Zip.php +++ b/Utils/IO/Zip/Zip.php @@ -43,7 +43,7 @@ class Zip implements ArchiveInterface $zip = new \ZipArchive(); if (!$zip->open($destination, $overwrite ? \ZipArchive::CREATE | \ZipArchive::OVERWRITE : \ZipArchive::CREATE)) { - return false; + return false; // @codeCoverageIgnore } if (\is_string($sources)) { @@ -116,7 +116,7 @@ class Zip implements ArchiveInterface try { $zip = new \ZipArchive(); if (!$zip->open($source)) { - return false; + return false; // @codeCoverageIgnore } $zip->extractTo($destination . '/'); diff --git a/Validation/Finance/CreditCard.php b/Validation/Finance/CreditCard.php index 3f3f6e66b..3726f9587 100644 --- a/Validation/Finance/CreditCard.php +++ b/Validation/Finance/CreditCard.php @@ -46,7 +46,7 @@ final class CreditCard extends ValidatorAbstract for ($i = 0; $i < $numberLength; ++$i) { $digit = (int) $value[$i]; // Multiply alternate digits by two - if ($i % 2 == $parity) { + if ($i % 2 === $parity) { $digit *= 2; // If the sum is two digits, add them together (in effect) if ($digit > 9) { diff --git a/tests/DataStorage/Cache/Connection/FileCacheTest.php b/tests/DataStorage/Cache/Connection/FileCacheTest.php index 5c659aaf7..bf1afe872 100644 --- a/tests/DataStorage/Cache/Connection/FileCacheTest.php +++ b/tests/DataStorage/Cache/Connection/FileCacheTest.php @@ -186,6 +186,12 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase self::assertFalse($this->cache->increment('invalid', 2)); } + public function testIncrementInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->increment('invalid', 2)); + } + public function testDecrement() : void { $this->cache->set(1, 3); @@ -198,13 +204,31 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase self::assertFalse($this->cache->decrement('invalid', 2)); } + public function testDecrementInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->decrement('invalid', 2)); + } + public function testRename() : void { $this->cache->set('a', 'testVal1'); - $this->cache->rename('a', 'b'); + self::assertTrue($this->cache->rename('a', 'b')); self::assertEquals('testVal1', $this->cache->get('b')); } + public function testRenameInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->rename('old', 'new')); + } + + public function testDeleteInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->delete('invalid', 2)); + } + public function testDeleteLike() : void { $this->cache->set('key1', 'testVal1'); @@ -239,6 +263,12 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase self::assertEquals('testVal2', $this->cache->get('key2')); } + public function testUpdateExpireInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->updateExpire('invalid', 2)); + } + /** * @testdox Cache data cannot be added if it already exists * @covers phpOMS\DataStorage\Cache\Connection\FileCache diff --git a/tests/DataStorage/Cache/Connection/MemCachedTest.php b/tests/DataStorage/Cache/Connection/MemCachedTest.php index 5711b930a..21607f42d 100644 --- a/tests/DataStorage/Cache/Connection/MemCachedTest.php +++ b/tests/DataStorage/Cache/Connection/MemCachedTest.php @@ -139,7 +139,7 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase $this->cache->set('key2', 'testVal2', 2); \sleep(1); self::assertTrue($this->cache->exists('key2')); - self::assertFalse($this->cache->exists('key2', 0)); + self::assertTrue($this->cache->exists('key2', 0)); \sleep(3); self::assertFalse($this->cache->exists('key2')); } @@ -162,6 +162,12 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase self::assertFalse($this->cache->increment('invalid', 2)); } + public function testIncrementInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->increment('invalid', 2)); + } + public function testDecrement() : void { $this->cache->set(1, 3); @@ -169,6 +175,12 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase self::assertEquals(1, $this->cache->get(1)); } + public function testDecrementInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->decrement('invalid', 2)); + } + public function testInvalidKeyDecrement() : void { self::assertFalse($this->cache->decrement('invalid', 2)); @@ -177,19 +189,31 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase public function testRename() : void { $this->cache->set('a', 'testVal1'); - $this->cache->rename('a', 'b'); + self::assertTrue($this->cache->rename('a', 'b')); self::assertEquals('testVal1', $this->cache->get('b')); } + public function testRenameInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->rename('old', 'new')); + } + public function testUpdateExpire() : void { - $this->cache->set('key2', 'testVal2', 1); + $this->cache->set('key2', 'testVal2', 10); self::assertEquals('testVal2', $this->cache->get('key2', 1)); \sleep(2); - self::assertTrue($this->cache->updateExpire('key2', \time() + 10000)); + self::assertTrue($this->cache->updateExpire('key2', 30)); self::assertEquals('testVal2', $this->cache->get('key2')); } + public function testUpdateExpireInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->updateExpire('invalid', 2)); + } + /** * @testdox Cache data cannot be added if it already exists * @covers phpOMS\DataStorage\Cache\Connection\MemCached @@ -240,6 +264,12 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase self::assertNull($this->cache->get('key4')); } + public function testDeleteInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->delete('invalid', 2)); + } + public function testInvalidKeyDelete() : void { self::assertTrue($this->cache->delete('invalid')); diff --git a/tests/DataStorage/Cache/Connection/NullCacheTest.php b/tests/DataStorage/Cache/Connection/NullCacheTest.php index e50b4c46d..26bb38832 100644 --- a/tests/DataStorage/Cache/Connection/NullCacheTest.php +++ b/tests/DataStorage/Cache/Connection/NullCacheTest.php @@ -90,7 +90,7 @@ final class NullCacheTest extends \PHPUnit\Framework\TestCase public function testRename() : void { $this->cache->set(1, 1); - $this->cache->rename(1, 2); + self::assertTrue($this->cache->rename(1, 2)); self::assertNull($this->cache->get(2)); } diff --git a/tests/DataStorage/Cache/Connection/RedisCacheTest.php b/tests/DataStorage/Cache/Connection/RedisCacheTest.php index 753ddaa71..c755a24ea 100644 --- a/tests/DataStorage/Cache/Connection/RedisCacheTest.php +++ b/tests/DataStorage/Cache/Connection/RedisCacheTest.php @@ -135,7 +135,7 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase $this->cache->set('key2', 'testVal2', 2); \sleep(1); self::assertTrue($this->cache->exists('key2')); - self::assertFalse($this->cache->exists('key2', 0)); + self::assertTrue($this->cache->exists('key2', 0)); \sleep(3); self::assertFalse($this->cache->exists('key2')); } @@ -163,7 +163,7 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase $this->cache->set('key2', 'testVal2', 2); \sleep(1); self::assertEquals([], \array_diff(['testVal1', 'testVal2'], $this->cache->getLike('key\d'))); - self::assertEquals([], $this->cache->getLike('key\d', 0)); + self::assertEquals([], \array_diff(['testVal1', 'testVal2'], $this->cache->getLike('key\d', 0))); \sleep(3); self::assertEquals([], $this->cache->getLike('key\d')); } @@ -181,6 +181,12 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase self::assertEquals(3, $this->cache->get(1)); } + public function testIncrementInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->increment('invalid', 2)); + } + public function testInvalidKeyIncrement() : void { self::assertFalse($this->cache->increment('invalid', 2)); @@ -193,6 +199,12 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase self::assertEquals(1, $this->cache->get(1)); } + public function testDecrementInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->decrement('invalid', 2)); + } + public function testInvalidKeyDecrement() : void { self::assertFalse($this->cache->decrement('invalid', 2)); @@ -201,10 +213,16 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase public function testRename() : void { $this->cache->set('a', 'testVal1'); - $this->cache->rename('a', 'b'); + self::assertTrue($this->cache->rename('a', 'b')); self::assertEquals('testVal1', $this->cache->get('b')); } + public function testRenameInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->rename('old', 'new')); + } + public function testDeleteLike() : void { $this->cache->set('key1', 'testVal1'); @@ -221,13 +239,19 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase public function testUpdateExpire() : void { - $this->cache->set('key2', 'testVal2', 1); + $this->cache->set('key2', 'testVal2', 10); self::assertEquals('testVal2', $this->cache->get('key2', 1)); \sleep(2); - self::assertTrue($this->cache->updateExpire('key2', \time() + 10000)); + self::assertTrue($this->cache->updateExpire('key2', 30)); self::assertEquals('testVal2', $this->cache->get('key2')); } + public function testUpdateExpireInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->updateExpire('invalid', 2)); + } + /** * @testdox Cache data cannot be added if it already exists * @covers phpOMS\DataStorage\Cache\Connection\RedisCache @@ -278,6 +302,12 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase self::assertNull($this->cache->get('key4')); } + public function testDeleteInvalidStatus() : void + { + TestUtils::setMember($this->cache, 'status', CacheStatus::FAILURE); + self::assertFalse($this->cache->delete('invalid', 2)); + } + public function testInvalidKeyDelete() : void { self::assertTrue($this->cache->delete('invalid')); diff --git a/tests/Message/Console/ConsoleHeaderTest.php b/tests/Message/Console/ConsoleHeaderTest.php index 49c40ac6f..cbefe9b1c 100644 --- a/tests/Message/Console/ConsoleHeaderTest.php +++ b/tests/Message/Console/ConsoleHeaderTest.php @@ -16,6 +16,7 @@ namespace phpOMS\tests\Message\Console; use phpOMS\Localization\Localization; use phpOMS\Message\Console\ConsoleHeader; +use phpOMS\Message\Http\RequestStatusCode; /** * @internal @@ -43,6 +44,7 @@ class ConsoleHeaderTest extends \PHPUnit\Framework\TestCase self::assertEquals('1.0', $this->header->getProtocolVersion()); self::assertEquals('', $this->header->getReasonPhrase()); self::assertEquals([], $this->header->get('key')); + self::assertEquals([], $this->header->get()); self::assertFalse($this->header->has('key')); self::assertInstanceOf(Localization::class, $this->header->l11n); self::assertEquals(0, $this->header->account); @@ -102,6 +104,17 @@ class ConsoleHeaderTest extends \PHPUnit\Framework\TestCase self::assertFalse($this->header->remove('key')); } + /** + * @testdox The header can generate default http headers based on status codes + * @covers phpOMS\Message\Console\ConsoleHeader + * @group framework + */ + public function testHeaderGeneration() : void + { + self::markTestIncomplete(); + $this->header->generate(RequestStatusCode::R_500); + } + /** * @covers phpOMS\Message\Console\ConsoleHeader * @group framework @@ -109,7 +122,7 @@ class ConsoleHeaderTest extends \PHPUnit\Framework\TestCase public function testAccount() : void { $this->header->account = 2; - self::AssertEquals(2, $this->header->account); + self::assertEquals(2, $this->header->account); } /** diff --git a/tests/Message/Console/ConsoleRequestTest.php b/tests/Message/Console/ConsoleRequestTest.php index 402b38d6a..6daec1f04 100644 --- a/tests/Message/Console/ConsoleRequestTest.php +++ b/tests/Message/Console/ConsoleRequestTest.php @@ -81,6 +81,24 @@ class ConsoleRequestTest extends \PHPUnit\Framework\TestCase self::assertEquals('get:some/test/path', $this->request->uri->__toString()); } + /** + * @testdox The url hashes for the different paths get correctly generated + * @covers phpOMS\Message\Console\ConsoleRequest + * @group framework + */ + public function testHashingInputOutput() : void + { + $request = new ConsoleRequest(new Argument(':test/path ?para1=abc ?para2=2 #frag'), $l11n = new Localization()); + + $request->createRequestHashs(0); + self::assertEquals([ + 'da39a3ee5e6b4b0d3255bfef95601890afd80709', + 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3', + '328413d996ab9b79af9d4098af3a65b885c4ca64', + ], $request->getHash()); + self::assertEquals($l11n, $request->header->l11n); + } + /** * @covers phpOMS\Message\Console\ConsoleRequest * @group framework diff --git a/tests/Message/Console/ConsoleResponseTest.php b/tests/Message/Console/ConsoleResponseTest.php index 8a44fa716..fad568c7a 100644 --- a/tests/Message/Console/ConsoleResponseTest.php +++ b/tests/Message/Console/ConsoleResponseTest.php @@ -16,24 +16,34 @@ namespace phpOMS\tests\Message\Console; use phpOMS\Localization\Localization; use phpOMS\Message\Console\ConsoleResponse; +use phpOMS\System\MimeType; /** * @internal */ class ConsoleResponseTest extends \PHPUnit\Framework\TestCase { + protected ConsoleResponse $response; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->response = new ConsoleResponse(); + } /** * @covers phpOMS\Message\Console\ConsoleResponse * @group framework */ public function testDefault() : void { - $response = new ConsoleResponse(new Localization()); - self::assertEquals('', $response->getBody()); - self::assertEquals('', $response->render()); - self::assertEquals([], $response->toArray()); - self::assertInstanceOf('\phpOMS\Localization\Localization', $response->header->l11n); - self::assertInstanceOf('\phpOMS\Message\Console\ConsoleHeader', $response->header); + $this->response = new ConsoleResponse(new Localization()); + self::assertEquals('', $this->response->getBody()); + self::assertEquals('', $this->response->render()); + self::assertEquals([], $this->response->toArray()); + self::assertInstanceOf('\phpOMS\Localization\Localization', $this->response->header->l11n); + self::assertInstanceOf('\phpOMS\Message\Console\ConsoleHeader', $this->response->header); } /** @@ -42,10 +52,111 @@ class ConsoleResponseTest extends \PHPUnit\Framework\TestCase */ public function testSetGet() : void { - $response = new ConsoleResponse(new Localization()); + $this->response = new ConsoleResponse(new Localization()); - $response->setResponse(['a' => 1]); - self::assertTrue($response->remove('a')); - self::assertFalse($response->remove('a')); + $this->response->setResponse(['a' => 1]); + self::assertTrue($this->response->remove('a')); + self::assertFalse($this->response->remove('a')); + } + + /** + * @testdox Response data can be turned into an array + * @covers phpOMS\Message\Console\ConsoleResponse + * @group framework + */ + public function testToArray() : void + { + $data = [ + ['view_string'], + [1, 2, 3, 'a', 'b', [4, 5]], + 'stringVal', + 6, + false, + 1.13, + 'json_string', + ]; + + $this->response->set('view', new class() extends \phpOMS\Views\View { + public function toArray() : array + { + return ['view_string']; + } + }); + $this->response->set('array', $data[1]); + $this->response->set('string', $data[2]); + $this->response->set('int', $data[3]); + $this->response->set('bool', $data[4]); + $this->response->set('float', $data[5]); + $this->response->set('jsonSerializable', new class() implements \JsonSerializable { + public function jsonSerialize() + { + return 'json_string'; + } + }); + $this->response->set('null', null); + + self::assertEquals($data, $this->response->toArray()); + } + + /** + * @testdox A response with json as content-type is automatically rendered as json data + * @covers phpOMS\Message\Console\ConsoleResponse + * @group framework + */ + public function testJsonRender() : void + { + $data = [ + ['view_string'], + [1, 2, 3, 'a', 'b', [4, 5]], + 'stringVal', + 6, + false, + 1.13, + 'json_string', + ]; + + $this->response->set('view', new class() extends \phpOMS\Views\View { + public function toArray() : array + { + return ['view_string']; + } + }); + $this->response->set('array', $data[1]); + $this->response->set('string', $data[2]); + $this->response->set('int', $data[3]); + $this->response->set('bool', $data[4]); + $this->response->set('float', $data[5]); + $this->response->set('jsonSerializable', new class() implements \JsonSerializable { + public function jsonSerialize() + { + return 'json_string'; + } + }); + $this->response->set('null', null); + + $this->response->header->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true); + self::assertEquals(\json_encode($data), $this->response->render()); + } + + /** + * @testdox Invalid response data results in an empty array + * @covers phpOMS\Message\Console\ConsoleResponse + * @group framework + */ + public function testInvalidResponseDataToArray() : void + { + $this->response->set('invalid', new class() {}); + self::assertEquals([], $this->response->toArray()); + } + + /** + * @testdox Invalid response data results in an empty render + * @covers phpOMS\Message\Console\ConsoleResponse + * @group framework + */ + public function testInvalidResponseDataRender() : void + { + $this->response->set('invalid', new class() {}); + self::assertEquals('', $this->response->render()); } } diff --git a/tests/Message/Http/HttpHeaderTest.php b/tests/Message/Http/HttpHeaderTest.php index f3c1e00e5..0df332ad7 100644 --- a/tests/Message/Http/HttpHeaderTest.php +++ b/tests/Message/Http/HttpHeaderTest.php @@ -49,6 +49,7 @@ class HttpHeaderTest extends \PHPUnit\Framework\TestCase self::assertEmpty(HttpHeader::getAllHeaders()); self::assertEquals('', $this->header->getReasonPhrase()); self::assertEquals([], $this->header->get('key')); + self::assertEquals([], $this->header->get()); self::assertFalse($this->header->has('key')); self::assertInstanceOf(Localization::class, $this->header->l11n); self::assertEquals(0, $this->header->account); @@ -207,6 +208,22 @@ class HttpHeaderTest extends \PHPUnit\Framework\TestCase self::assertEquals(500, \http_response_code()); } + public function testGetAllHeaders() : void + { + $dummyHeaders = '{"REDIRECT_STATUS":"200","HTTP_HOST":"127.0.0.1","HTTP_CONNECTION":"keep-alive","HTTP_CACHE_CONTROL":"max-age=0","HTTP_SEC_CH_UA":"\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"","HTTP_ACCEPT":"text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/avif,image\/webp,image\/apng,*\/*;q=0.8,application\/signed-exchange;v=b3;q=0.9","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_SEC_CH_UA_MOBILE":"?0","HTTP_USER_AGENT":"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.114 Safari\/537.36","HTTP_SEC_FETCH_SITE":"same-origin","HTTP_SEC_FETCH_MODE":"same-origin","HTTP_SEC_FETCH_DEST":"empty","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_COOKIE":"PHPSESSID=4olihfuke6ihkgpgkkluda9qm0","PATH":"\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/snap\/bin","SERVER_SIGNATURE":"Apache\/2.4.46 (Ubuntu) Server at 127.0.0.1 Port 80<\/address>\n","SERVER_SOFTWARE":"Apache\/2.4.46 (Ubuntu)","SERVER_NAME":"127.0.0.1","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"80","REMOTE_ADDR":"127.0.0.1","DOCUMENT_ROOT":"\/home\/spl1nes\/Orange-Management","REQUEST_SCHEME":"http","CONTEXT_PREFIX":"","CONTEXT_DOCUMENT_ROOT":"\/home\/spl1nes\/Orange-Management","SERVER_ADMIN":"webmaster@localhost","SCRIPT_FILENAME":"\/home\/spl1nes\/Orange-Management\/index.php","REMOTE_PORT":"52430","REDIRECT_URL":"\/en\/backend","REDIRECT_QUERY_STRING":"{QUERY_STRING}","GATEWAY_INTERFACE":"CGI\/1.1","SERVER_PROTOCOL":"HTTP\/1.1","REQUEST_METHOD":"GET","QUERY_STRING":"{QUERY_STRING}","REQUEST_URI":"\/en\/backend","SCRIPT_NAME":"\/index.php","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1634157950.359451,"REQUEST_TIME":1634157950}'; + + $tmp = $_SERVER; + + $_SERVER = \json_decode($dummyHeaders, true); + self::assertEquals('127.0.0.1', $this->header->getAllHeaders()['Host']); + + // If headers are loaded once, only the cached version is used! + $_SERVER = \json_decode('{"HTTP_HOST": "invalid"}', true); + self::assertEquals('127.0.0.1', $this->header->getAllHeaders()['Host']); + + $_SERVER = $tmp; + } + /** * @testdox Security header data cannot be changed once defined * @covers phpOMS\Message\Http\HttpHeader diff --git a/tests/Message/Mail/EmailTest.php b/tests/Message/Mail/EmailTest.php index ccf36f323..804690df2 100644 --- a/tests/Message/Mail/EmailTest.php +++ b/tests/Message/Mail/EmailTest.php @@ -170,7 +170,7 @@ class EmailTestTest extends \PHPUnit\Framework\TestCase public function testInvalidCustomHeaderInputOutput() : void { self::assertFalse($this->mail->addCustomHeader('', '')); - self::AssertEquals([], $this->mail->getCustomHeaders()); + self::assertEquals([], $this->mail->getCustomHeaders()); } public function testEmailParsing() : void diff --git a/tests/Message/Mail/MailHandlerTest.php b/tests/Message/Mail/MailHandlerTest.php index 80895acc6..4302c5d5e 100644 --- a/tests/Message/Mail/MailHandlerTest.php +++ b/tests/Message/Mail/MailHandlerTest.php @@ -21,6 +21,7 @@ use phpOMS\Message\Mail\Imap; use phpOMS\Message\Mail\MailHandler; use phpOMS\Message\Mail\SubmitType; use phpOMS\System\CharsetType; +use phpOMS\System\SystemType; use phpOMS\System\OperatingSystem; /** diff --git a/tests/Module/StatusAbstractTest.php b/tests/Module/StatusAbstractTest.php index df907e369..0f1a64a46 100644 --- a/tests/Module/StatusAbstractTest.php +++ b/tests/Module/StatusAbstractTest.php @@ -17,6 +17,7 @@ namespace phpOMS\tests\Module; require_once __DIR__ . '/../Autoloader.php'; use phpOMS\Module\StatusAbstract; +use phpOMS\Module\ModuleInfo; /** * @testdox phpOMS\tests\Module\StatusAbstractTest: Abstract module @@ -44,8 +45,10 @@ class StatusAbstractTest extends \PHPUnit\Framework\TestCase */ public function testInvalidModulePathActivation() : void { - $this->status::activateRoutes(); - $this->status::activateHooks(); + $moduleInfo = new ModuleInfo(__DIR__ . '/info.json'); + + $this->status::activateRoutes($moduleInfo); + $this->status::activateHooks($moduleInfo); self::assertFalse(\is_dir(__DIR__ . '/../../../Modules/Invalid')); } diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index a4574f791..dcd977ce0 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -242,8 +242,8 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase */ public function testArgHas() : void { - if (ArrayUtils::getArg('--configuration', $_SERVER['argv']) !== null) { - self::assertGreaterThan(0, ArrayUtils::hasArg('--configuration', $_SERVER['argv'])); + if (ArrayUtils::getArg('--configuration', $_SERVER['argv'] ?? null) !== null) { + self::assertGreaterThan(0, ArrayUtils::hasArg('--configuration', $_SERVER['argv'] ?? null)); } } @@ -254,7 +254,7 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase */ public function testInvalidArgHas() : void { - self::assertEquals(-1, ArrayUtils::hasArg('--testNull', $_SERVER['argv'])); + self::assertEquals(-1, ArrayUtils::hasArg('--testNull', $_SERVER['argv'] ?? null)); } /** @@ -264,8 +264,8 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase */ public function testArgGet() : void { - if (ArrayUtils::getArg('--configuration', $_SERVER['argv']) !== null) { - self::assertTrue(\stripos(ArrayUtils::getArg('--configuration', $_SERVER['argv']), '.xml') !== false); + if (ArrayUtils::getArg('--configuration', $_SERVER['argv'] ?? null) !== null) { + self::assertTrue(\stripos(ArrayUtils::getArg('--configuration', $_SERVER['argv'] ?? null), '.xml') !== false); } } @@ -276,7 +276,7 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase */ public function testInvalidArgGet() : void { - self::assertNull(ArrayUtils::getArg('--testNull', $_SERVER['argv'])); + self::assertNull(ArrayUtils::getArg('--testNull', $_SERVER['argv'] ?? null)); } /** diff --git a/tests/Utils/IO/Zip/TarTest.php b/tests/Utils/IO/Zip/TarTest.php index 13004e015..c7aef8636 100644 --- a/tests/Utils/IO/Zip/TarTest.php +++ b/tests/Utils/IO/Zip/TarTest.php @@ -30,6 +30,10 @@ class TarTest extends \PHPUnit\Framework\TestCase 'The Phar extension is not available.' ); } + + if (\is_dir('new_dir')) { + \rmdir('new_dir'); + } } /** @@ -83,14 +87,13 @@ class TarTest extends \PHPUnit\Framework\TestCase \unlink(__DIR__ . '/test.tar'); - /* @todo not working, somehow it cannot open the test.tar // second test self::assertTrue(Tar::pack( __DIR__ . '/test', - __DIR__ . '/test.tar' + __DIR__ . '/test2.tar' )); - self::assertTrue(Tar::unpack(__DIR__ . '/test.tar', __DIR__ . '/new_dir')); + self::assertTrue(Tar::unpack(__DIR__ . '/test2.tar', __DIR__ . '/new_dir')); self::assertFileExists(__DIR__ . '/new_dir'); self::assertEquals($c, \file_get_contents(__DIR__ . '/new_dir/test c.txt')); @@ -100,8 +103,12 @@ class TarTest extends \PHPUnit\Framework\TestCase \rmdir(__DIR__ . '/new_dir/sub'); \rmdir(__DIR__ . '/new_dir'); - \unlink(__DIR__ . '/test.tar'); - */ + \unlink(__DIR__ . '/test2.tar'); + } + + public function testInvalidArchiveUnpack() : void + { + self::assertFalse(Tar::unpack(__DIR__ . '/malformed.tar', __DIR__)); } /** @@ -135,7 +142,7 @@ class TarTest extends \PHPUnit\Framework\TestCase __DIR__ . '/test b.md' => 'test b.md', __DIR__ . '/test' => 'test', ], - __DIR__ . '/test2.tar' + __DIR__ . '/test3.tar' ); self::assertFalse(Tar::pack( @@ -144,10 +151,10 @@ class TarTest extends \PHPUnit\Framework\TestCase __DIR__ . '/test b.md' => 'test b.md', __DIR__ . '/test' => 'test', ], - __DIR__ . '/test2.tar' + __DIR__ . '/test3.tar' )); - \unlink(__DIR__ . '/test2.tar'); + \unlink(__DIR__ . '/test3.tar'); } /** @@ -173,12 +180,12 @@ class TarTest extends \PHPUnit\Framework\TestCase __DIR__ . '/test b.md' => 'test b.md', __DIR__ . '/test' => 'test', ], - __DIR__ . '/test3.tar' + __DIR__ . '/test4.tar' )); - Tar::unpack(__DIR__ . '/abc/test3.tar', __DIR__); - self::assertFalse(Tar::unpack(__DIR__ . '/abc/test3.tar', __DIR__)); + Tar::unpack(__DIR__ . '/abc/test4.tar', __DIR__); + self::assertFalse(Tar::unpack(__DIR__ . '/abc/test4.tar', __DIR__)); - \unlink(__DIR__ . '/test3.tar'); + \unlink(__DIR__ . '/test4.tar'); } } diff --git a/tests/Utils/IO/Zip/ZipTest.php b/tests/Utils/IO/Zip/ZipTest.php index f552ec567..f365afd02 100644 --- a/tests/Utils/IO/Zip/ZipTest.php +++ b/tests/Utils/IO/Zip/ZipTest.php @@ -30,6 +30,10 @@ class ZipTest extends \PHPUnit\Framework\TestCase 'The ZIP extension is not available.' ); } + + if (\is_dir('new_dir')) { + \rmdir('new_dir'); + } } /** @@ -166,6 +170,11 @@ class ZipTest extends \PHPUnit\Framework\TestCase \unlink(__DIR__ . '/test2.zip'); } + public function testInvalidArchiveUnpack() : void + { + self::assertFalse(Zip::unpack(__DIR__ . '/malformed.zip', __DIR__)); + } + /** * @testdox A none-existing source cannot be unpacked * @covers phpOMS\Utils\IO\Zip\Zip diff --git a/tests/Utils/IO/Zip/malformed.tar b/tests/Utils/IO/Zip/malformed.tar new file mode 100644 index 000000000..743f5bf3e --- /dev/null +++ b/tests/Utils/IO/Zip/malformed.tar @@ -0,0 +1 @@ +asdlf \ No newline at end of file diff --git a/tests/Utils/IO/Zip/malformed.zip b/tests/Utils/IO/Zip/malformed.zip new file mode 100644 index 0000000000000000000000000000000000000000..5e40c0877058c504203932e5136051cf3cd3519b GIT binary patch literal 4 LcmYc+PDuj*1P=k9 literal 0 HcmV?d00001 diff --git a/tests/Validation/Finance/CreditCardTest.php b/tests/Validation/Finance/CreditCardTest.php index 146e849ea..ff94831b4 100644 --- a/tests/Validation/Finance/CreditCardTest.php +++ b/tests/Validation/Finance/CreditCardTest.php @@ -30,6 +30,7 @@ class CreditCardTest extends \PHPUnit\Framework\TestCase */ public function testCreditCard() : void { + self::assertTrue(CreditCard::isValid('49927398716')); self::assertTrue(CreditCard::isValid('4242424242424242')); self::assertFalse(CreditCard::isValid('4242424242424241')); diff --git a/tests/Validation/Finance/IbanTest.php b/tests/Validation/Finance/IbanTest.php index c875c0713..3bcadd64e 100644 --- a/tests/Validation/Finance/IbanTest.php +++ b/tests/Validation/Finance/IbanTest.php @@ -28,13 +28,36 @@ class IbanTest extends \PHPUnit\Framework\TestCase * @covers phpOMS\Validation\Finance\Iban * @group framework */ - public function testValid() : void + public function testValidation() : void { self::assertTrue(Iban::isValid('DE22 6008 0000 0960 0280 00')); self::assertFalse(Iban::isValid('DE22 6X08 0000 0960 0280 00')); - self::assertFalse(Iban::isValid('DE22 6008 0000 0960 0280 0')); + } + + public function testNumeric() : void + { + self::assertFalse(Iban::isValid('IL02 0108 s800 0000 2149 431')); + self::assertTrue(Iban::isValid('IL02 0108 3800 0000 2149 431')); + } + + public function testZero() : void + { + self::assertFalse(Iban::isValid('MU03 MCBL 0901 0000 0187 9025 010U SD')); + self::assertTrue(Iban::isValid('MU03 MCBL 0901 0000 0187 9025 000U SD')); + } + + public function testInvalidName() : void + { self::assertFalse(Iban::isValid('QQ22 6008 0000 0960 0280 00')); - self::assertFalse(Iban::isValid('MU22 6118 1111 1961 1281 1281 0111 23')); + } + + public function testInvalidLength() : void + { + self::assertFalse(Iban::isValid('DE22 6008 0000 0960 0280 0')); + } + + public function testInvalidChecksum() : void + { self::assertFalse(Iban::isValid('DZ22 6118 1111 1961 1281 2211')); } } diff --git a/tests/Validation/ValidatorTest.php b/tests/Validation/ValidatorTest.php index 5ce226b3c..b0e8ea809 100644 --- a/tests/Validation/ValidatorTest.php +++ b/tests/Validation/ValidatorTest.php @@ -97,6 +97,12 @@ class ValidatorTest extends \PHPUnit\Framework\TestCase self::assertTrue(Validator::isValid('value', ['phpOMS\Validation\Validator::hasLength' => [4]])); } + public function testInvalidValidatorCall() : void + { + $this->expectException(\BadFunctionCallException::class); + Validator::isValid('value', ['\invalid_call' => []]); + } + /** * @testdox A value can be checked to match a regular expression * @covers phpOMS\Validation\Validator