test fixes

This commit is contained in:
Dennis Eichhorn 2021-10-14 23:50:26 +02:00
parent 8ec4d17ff7
commit 2a288de983
36 changed files with 445 additions and 134 deletions

View File

@ -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.

View File

@ -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;
}
/**

View File

@ -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();
}
/**

View File

@ -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;
}
/**

View File

@ -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
}
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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)] ?? []);
}
/**

View File

@ -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));

View File

@ -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;
}
}

View File

@ -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))
)
)
)

View File

@ -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;

View File

@ -584,7 +584,7 @@ final class ModuleManager
return true;
} catch (\Throwable $t) {
return false;
return false; // @codeCoverageIgnore
}
}

View File

@ -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 */

View File

@ -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

View File

@ -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 . '/');

View File

@ -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) {

View File

@ -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<extended>

View File

@ -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<extended>
@ -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'));

View File

@ -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));
}

View File

@ -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<extended>
@ -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'));

View File

@ -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<extended>
* @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);
}
/**

View File

@ -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

View File

@ -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<extended>
* @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<extended>
* @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<extended>
* @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<extended>
* @group framework
*/
public function testInvalidResponseDataRender() : void
{
$this->response->set('invalid', new class() {});
self::assertEquals('', $this->response->render());
}
}

View File

@ -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<extended>

View File

@ -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

View File

@ -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;
/**

View File

@ -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'));
}

View File

@ -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));
}
/**

View File

@ -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');
}
}

View File

@ -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

View File

@ -0,0 +1 @@
asdlf

Binary file not shown.

View File

@ -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'));

View File

@ -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'));
}
}

View File

@ -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<extended>