test fixes

This commit is contained in:
Dennis Eichhorn 2020-12-26 13:19:20 +01:00
parent a5a9d20941
commit 69ff0e0035
18 changed files with 397 additions and 42 deletions

View File

@ -153,7 +153,7 @@ final class FileCache extends ConnectionAbstract
return;
}
$path = Directory::sanitize($key, self::SANITIZE);
$path = Directory::sanitize((string) $key, self::SANITIZE);
$fp = \fopen($this->con . '/' . \trim($path, '/') . '.cache', 'w+');
if (\flock($fp, \LOCK_EX)) {
@ -462,10 +462,6 @@ final class FileCache extends ConnectionAbstract
$created = File::created($path)->getTimestamp();
$now = \time();
if ($expire >= 0 && $created + $expire < $now) {
return;
}
$raw = \file_get_contents($path);
if ($raw === false) {
return;
@ -499,10 +495,6 @@ final class FileCache extends ConnectionAbstract
$created = File::created($path)->getTimestamp();
$now = \time();
if ($expire >= 0 && $created + $expire < $now) {
return;
}
$raw = \file_get_contents($path);
if ($raw === false) {
return;
@ -632,7 +624,7 @@ final class FileCache extends ConnectionAbstract
*/
public function updateExpire(int|string $key, int $expire = -1) : bool
{
$value = $this->get($key);
$value = $this->get($key, $expire);
$this->delete($key);
$this->set($key, $value, $expire);
@ -703,7 +695,7 @@ final class FileCache extends ConnectionAbstract
*/
private function getPath(int|string $key) : string
{
$path = Directory::sanitize($key, self::SANITIZE);
$path = Directory::sanitize((string) $key, self::SANITIZE);
return $this->con . '/' . \trim($path, '/') . '.cache';
}
}

View File

@ -190,7 +190,7 @@ final class MemCached extends ConnectionAbstract
$values = [];
foreach ($keys as $key) {
if (\preg_match($key, $key) === 1) {
if (\preg_match('/' . $pattern . '/', $key) === 1) {
$result = $this->con->get($key);
if (\is_string($result)) {
$type = (int) $result[0];
@ -216,7 +216,7 @@ final class MemCached extends ConnectionAbstract
$keys = $this->con->getAllKeys();
foreach ($keys as $key) {
if (\preg_match($key, $key) === 1) {
if (\preg_match('/' . $pattern . '/', $key) === 1) {
$this->con->delete($key);
}
}

View File

@ -121,7 +121,7 @@ final class NullCache extends ConnectionAbstract
*/
public function exists(int|string $key, int $expire = -1) : bool
{
return true;
return false;
}
/**

View File

@ -220,7 +220,7 @@ final class RedisCache extends ConnectionAbstract
$values = [];
foreach ($keys as $key) {
if (\preg_match($key, $key) === 1) {
if (\preg_match('/' . $pattern . '/', $key) === 1) {
$result = $this->con->get($key);
if (\is_string($result)) {
$type = (int) $result[0];
@ -246,7 +246,7 @@ final class RedisCache extends ConnectionAbstract
$keys = $this->con->keys('*');
foreach ($keys as $key) {
if (\preg_match($key, $key) === 1) {
if (\preg_match('/' . $pattern . '/', $key) === 1) {
$this->con->del($key);
}
}

View File

@ -114,7 +114,7 @@ final class Dispatcher implements DispatcherInterface
$views = [];
$dispatch = \explode(':', $controller);
if (!Autoloader::exists($dispatch[0])) {
if (!Autoloader::exists($dispatch[0]) && !isset($this->controllers[$dispatch[0]])) {
throw new PathException($dispatch[0]);
}

View File

@ -60,6 +60,11 @@ class AutoloaderTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\in_array(\realpath(__DIR__ . '/TestLoad2.php'), $includes));
}
public function testPathFinding() : void
{
self::assertCount(1, Autoloader::findPaths('\phpOMS\Autoloader'));
}
/**
* @covers phpOMS\Autoloader
* @group framework

View File

@ -124,6 +124,52 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase
self::assertEquals('testValAdd', $this->cache->get('addKey'));
}
public function testGetLike() : void
{
$this->cache->set('key1', 'testVal1');
$this->cache->set('key2', 'testVal2');
self::assertEquals([], \array_diff(['testVal1', 'testVal2'], $this->cache->getLike('key\d')));
}
public function testIncrement() : void
{
$this->cache->set(1, 1);
$this->cache->increment(1, 2);
self::assertEquals(3, $this->cache->get(1));
}
public function testDecrement() : void
{
$this->cache->set(1, 3);
$this->cache->decrement(1, 2);
self::assertEquals(1, $this->cache->get(1));
}
public function testRename() : void
{
$this->cache->set('a', 'testVal1');
$this->cache->rename('a', 'b');
self::assertEquals('testVal1', $this->cache->get('b'));
}
public function testDeleteLike() : void
{
$this->cache->set('key1', 'testVal1');
$this->cache->set('key2', 'testVal2');
self::assertTrue($this->cache->deleteLike('key\d'));
self::assertEquals([], $this->cache->getLike('key\d'));
}
public function testUpdateExpire() : void
{
$this->cache->set('key2', 'testVal2', 1);
self::assertEquals('testVal2', $this->cache->get('key2', 1));
\sleep(2);
self::assertNull($this->cache->get('key2', 1));
self::assertTrue($this->cache->updateExpire(10000));
self::assertEquals('testVal2', $this->cache->get('key2', 1));
}
/**
* @testdox Cache data cannot be added if it already exists
* @covers phpOMS\DataStorage\Cache\Connection\FileCache<extended>
@ -338,7 +384,7 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase
{
$this->expectException(\phpOMS\DataStorage\Cache\Exception\InvalidConnectionConfigException::class);
$cache = new FileCache("/root/etc/invalidPathOrPermission^$:?><");
$this->cache = new FileCache("/root/etc/invalidPathOrPermission^$:?><");
}
/**

View File

@ -125,6 +125,52 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase
self::assertEquals('testValAdd', $this->cache->get('addKey'));
}
public function testGetLike() : void
{
$this->cache->set('key1', 'testVal1');
$this->cache->set('key2', 'testVal2');
self::assertEquals(['testVal1', 'testVal2'], $this->cache->getLike('key\d'));
}
public function testIncrement() : void
{
$this->cache->set(1, 1);
$this->cache->increment(1, 2);
self::assertEquals(3, $this->cache->get(1));
}
public function testDecrement() : void
{
$this->cache->set(1, 3);
$this->cache->decrement(1, 2);
self::assertEquals(1, $this->cache->get(1));
}
public function testRename() : void
{
$this->cache->set('a', 'testVal1');
$this->cache->rename('a', 'b');
self::assertEquals('testVal1', $this->cache->get('b'));
}
public function testDeleteLike() : void
{
$this->cache->set('key1', 'testVal1');
$this->cache->set('key2', 'testVal2');
self::assertTrue($this->cache->deleteLike('key\d'));
self::assertEquals([], $this->cache->getLike('key\d'));
}
public function testUpdateExpire() : void
{
$this->cache->set('key2', 'testVal2', 1);
self::assertEquals('testVal2', $this->cache->get('key2', 1));
\sleep(2);
self::assertNull($this->cache->get('key2', 1));
self::assertTrue($this->cache->updateExpire(10000));
self::assertEquals('testVal2', $this->cache->get('key2', 1));
}
/**
* @testdox Cache data cannot be added if it already exists
* @covers phpOMS\DataStorage\Cache\Connection\MemCached<extended>

View File

@ -15,6 +15,7 @@ declare(strict_types=1);
namespace phpOMS\tests\DataStorage\Cache\Connection;
use phpOMS\DataStorage\Cache\CacheType;
use phpOMS\DataStorage\Cache\CacheStatus;
use phpOMS\DataStorage\Cache\Connection\NullCache;
/**
@ -24,27 +25,107 @@ use phpOMS\DataStorage\Cache\Connection\NullCache;
*/
final class NullCacheTest extends \PHPUnit\Framework\TestCase
{
protected NullCache $cache;
protected function setUp() : void
{
$this->cache = new NullCache([]);
}
/**
* @testdox The default cache has the expected default values after initialization
* @covers phpOMS\DataStorage\Cache\Connection\NullCache<extended>
* @group framework
*/
public function testCache() : void
public function testDefault() : void
{
$cache = new NullCache();
$cache->connect([]);
self::assertEquals(CacheType::UNDEFINED, $this->cache->getType());
self::assertEquals([], $this->cache->stats());
self::assertEquals(0, $this->cache->getThreshold());
}
self::assertEquals(CacheType::UNDEFINED, $cache->getType());
self::assertTrue($cache->add(1, 1));
public function testConnect() : void
{
$this->cache->connect([]);
self::assertEquals(CacheStatus::CLOSED, $this->cache->getStatus());
}
$cache->set(1, 1);
self::assertNull($cache->get(1));
public function testSetInputOutput() : void
{
$this->cache->set(1, 1);
self::assertNull($this->cache->get(1));
}
self::assertTrue($cache->delete(1));
self::assertTrue($cache->flush(1));
self::assertTrue($cache->flushAll());
self::assertTrue($cache->replace(1, 1));
self::assertEquals([], $cache->stats());
self::assertEquals(0, $cache->getThreshold());
public function testAddInputOutput() : void
{
self::assertTrue($this->cache->add(1, 1));
self::assertNull($this->cache->get(1));
}
public function testGetLike() : void
{
self::assertEquals([], $this->cache->getLike(''));
}
public function testIncrement() : void
{
$this->cache->increment(1, 1);
self::assertNull($this->cache->get(1));
}
public function testDecrement() : void
{
$this->cache->increment(1, 1);
self::assertNull($this->cache->get(1));
}
public function testReplace() : void
{
self::assertTrue($this->cache->replace(1, 1));
}
public function testRename() : void
{
$this->cache->set(1, 1);
$this->cache->rename(1, 2);
self::assertNull($this->cache->get(2));
}
public function testDelete() : void
{
self::assertTrue($this->cache->delete(1));
}
public function testDeleteLike() : void
{
self::assertTrue($this->cache->deleteLike(''));
}
public function testFlush() : void
{
self::assertTrue($this->cache->flush(1));
}
public function testFlushAll() : void
{
self::assertTrue($this->cache->flushAll());
}
public function testExists() : void
{
$this->cache->set(1, 1);
self::assertFalse($this->cache->exists(1));
}
public function testUpdateExpire() : void
{
self::assertTrue($this->cache->updateExpire(1));
}
public function testStats() : void
{
$this->cache->set(1, 1);
$this->cache->add(2, 2);
self::assertEquals([], $this->cache->stats());
}
}

View File

@ -121,6 +121,52 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase
self::assertEquals('testValAdd', $this->cache->get('addKey'));
}
public function testGetLike() : void
{
$this->cache->set('key1', 'testVal1');
$this->cache->set('key2', 'testVal2');
self::assertEquals(['testVal1', 'testVal2'], $this->cache->getLike('key\d'));
}
public function testIncrement() : void
{
$this->cache->set(1, 1);
$this->cache->increment(1, 2);
self::assertEquals(3, $this->cache->get(1));
}
public function testDecrement() : void
{
$this->cache->set(1, 3);
$this->cache->decrement(1, 2);
self::assertEquals(1, $this->cache->get(1));
}
public function testRename() : void
{
$this->cache->set('a', 'testVal1');
$this->cache->rename('a', 'b');
self::assertEquals('testVal1', $this->cache->get('b'));
}
public function testDeleteLike() : void
{
$this->cache->set('key1', 'testVal1');
$this->cache->set('key2', 'testVal2');
self::assertTrue($this->cache->deleteLike('key\d'));
self::assertEquals([], $this->cache->getLike('key\d'));
}
public function testUpdateExpire() : void
{
$this->cache->set('key2', 'testVal2', 1);
self::assertEquals('testVal2', $this->cache->get('key2', 1));
\sleep(2);
self::assertNull($this->cache->get('key2', 1));
self::assertTrue($this->cache->updateExpire(10000));
self::assertEquals('testVal2', $this->cache->get('key2', 1));
}
/**
* @testdox Cache data cannot be added if it already exists
* @covers phpOMS\DataStorage\Cache\Connection\RedisCache<extended>

View File

@ -91,4 +91,37 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
->toSql()
);
}
public function testMysqlAlter() : void
{/*
$query = new Builder($this->con);
$sql = 'CREATE TABLE `user_roles` (`user_id` INT NOT NULL AUTO_INCREMENT, `role_id` VARCHAR(10) DEFAULT \'1\' NULL, PRIMARY KEY (`user_id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`ext1_id`), FOREIGN KEY (`role_id`) REFERENCES `roles` (`ext2_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;';
self::assertEquals(
$sql,
$query->createTable('user_roles')
->field('user_id', 'INT', null, false, true, false, true, 'users', 'ext1_id')
->field('role_id', 'VARCHAR(10)', '1', true, false, false, false, 'roles', 'ext2_id')
->toSql()
);*/
}
public function testMysqlCreateFromSchema() : void
{
Builder::createFromSchema(
\json_decode(
\file_get_contents(__DIR__ . '/Grammar/testSchema.json'), true
)['test'],
$this->con
);
$table = new Builder($this->con);
$tables = $table->selectTables()->execute()->fetchAll(\PDO::FETCH_COLUMN);
self::assertContains('test', $tables);
self::assertContains('test_foreign', $tables);
$delete = new Builder($this->con);
$delete->dropTable('test')->execute();
$delete->dropTable('test_foreign')->execute();
}
}

View File

@ -21,6 +21,7 @@ use phpOMS\Message\Http\HttpRequest;
use phpOMS\Message\Http\HttpResponse;
use phpOMS\Router\WebRouter;
use phpOMS\Uri\HttpUri;
use phpOMS\Module\ModuleAbstract;
require_once __DIR__ . '/../Autoloader.php';
@ -35,9 +36,10 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase
protected function setUp() : void
{
$this->app = new class() extends ApplicationAbstract {
protected string $appName = 'Api';
};
$this->app = new class() extends ApplicationAbstract {
protected string $appName = 'Api';
};
$this->app->router = new WebRouter();
$this->app->dispatcher = new Dispatcher($this->app);
}
@ -52,6 +54,23 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase
self::assertObjectHasAttribute('controllers', $this->app->dispatcher);
}
public function testControllerInputOutput() : void
{
$this->app->dispatcher->set(new class extends ModuleAbstract { public string $name = 'test'; public function testFunction() { return $this->name; } }, 'test');
$localization = new Localization();
self::assertTrue(
!empty(
$this->app->dispatcher->dispatch(
'test:testFunction',
new HttpRequest(new HttpUri(''), $localization),
new HttpResponse($localization)
)
)
);
}
/**
* @testdox The dispatcher can dispatch a function/closure
* @covers phpOMS\Dispatcher\Dispatcher

View File

@ -0,0 +1,40 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Message;
require_once __DIR__ . '/../../Autoloader.php';
use phpOMS\Message\Mail\Email;
/**
* @testdox phpOMS\tests\Message\MailHandlerTest: Abstract mail handler
*
* @internal
*/
class EmailTestTest extends \PHPUnit\Framework\TestCase
{
public function testEmailParsing() : void
{
self::assertEquals(
[['name' => 'Test Name', 'address' => 'test@orange-management.org']],
Email::parseAddresses('Test Name <test@orange-management.org>')
);
self::assertEquals(
[['name' => 'Test Name', 'address' => 'test@orange-management.org']],
Email::parseAddresses('Test Name <test@orange-management.org>', false)
);
}
}

View File

@ -30,13 +30,20 @@ class MailHandlerTest extends \PHPUnit\Framework\TestCase
{
public function testSendTextWithMail() : void
{
if (!\file_exists('/usr/sbin/sendmail') && empty(\ini_get('sendmail_path'))) {
self::markTestSkipped();
}
$mailer = new MailHandler();
$mailer->setMailer(SubmitType::MAIL);
$mail = new Email();
$mail->setFrom('dennis.eichhorn@orange-management.org', 'Dennis Eichhorn');
$mail->addTo('info@orange-management.org', 'Dennis Eichhorn');
$mail->subject = 'Test email';
$mail->setFrom('info@orange-management.org', 'Dennis Eichhorn');
$mail->addTo('test@orange-management.email', 'Dennis Eichhorn');
$mail->addCC('test2@orange-management.email', 'Dennis Eichhorn');
$mail->addBCC('test3@orange-management.email', 'Dennis Eichhorn');
$mail->addReplyTo('test4@orange-management.email', 'Dennis Eichhorn');
$mail->subject = 'testSendTextWithMail';
$mail->body = 'This is some content';
self::assertTrue($mailer->send($mail));
@ -44,13 +51,20 @@ class MailHandlerTest extends \PHPUnit\Framework\TestCase
public function testSendTextWithSendmail() : void
{
if (!\file_exists('/usr/sbin/sendmail') && empty(\ini_get('sendmail_path'))) {
self::markTestSkipped();
}
$mailer = new MailHandler();
$mailer->setMailer(SubmitType::SENDMAIL);
$mail = new Email();
$mail->setFrom('dennis.eichhorn@orange-management.org', 'Dennis Eichhorn');
$mail->addTo('info@orange-management.org', 'Dennis Eichhorn');
$mail->subject = 'Test email';
$mail->setFrom('info@orange-management.org', 'Dennis Eichhorn');
$mail->addTo('test@orange-management.email', 'Dennis Eichhorn');
$mail->addCC('test2@orange-management.email', 'Dennis Eichhorn');
$mail->addBCC('test3@orange-management.email', 'Dennis Eichhorn');
$mail->addReplyTo('test4@orange-management.email', 'Dennis Eichhorn');
$mail->subject = 'testSendTextWithSendmail';
$mail->body = 'This is some content';
self::assertTrue($mailer->send($mail));

View File

@ -313,4 +313,22 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase
{
self::assertEquals([1, 3, 4], ArrayUtils::abs([-1, 3, -4]));
}
public function testArrayDiffAssocResursive() : void
{
self::assertEquals(
['a' => 1, 'b' => ['c' => 2]],
ArrayUtils::array_diff_assoc_recursive(['a' => 1, 'b' => ['c' => 2]], [])
);
self::assertEquals(
['b' => ['d' => 3]],
ArrayUtils::array_diff_assoc_recursive(['a' => 1, 'b' => ['c' => 2, 'd' => 3]], ['a' => 1, 'b' => ['c' => 2]])
);
self::assertEquals(
[],
ArrayUtils::array_diff_assoc_recursive([], ['a' => 1, 'b' => ['c' => 2]])
);
}
}

View File

@ -28,11 +28,19 @@ class CsvSettingsTest extends \PHPUnit\Framework\TestCase
* @covers phpOMS\Utils\IO\Csv\CsvSettings
* @group framework
*/
public function testDelimiter() : void
public function testFileDelimiter() : void
{
self::assertEquals(':', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/colon.csv', 'r')));
self::assertEquals(',', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/comma.csv', 'r')));
self::assertEquals('|', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/pipe.csv', 'r')));
self::assertEquals(';', CsvSettings::getFileDelimiter(\fopen(__DIR__ . '/semicolon.csv', 'r')));
}
public function testStringDelimiter() : void
{
self::assertEquals(':', CsvSettings::getStringDelimiter(\file_get_contents(__DIR__ . '/colon.csv')));
self::assertEquals(',', CsvSettings::getStringDelimiter(\file_get_contents(__DIR__ . '/comma.csv')));
self::assertEquals('|', CsvSettings::getStringDelimiter(\file_get_contents(__DIR__ . '/pipe.csv')));
self::assertEquals(';', CsvSettings::getStringDelimiter(\file_get_contents(__DIR__ . '/semicolon.csv')));
}
}

View File

@ -28,11 +28,18 @@ class HostnameTest extends \PHPUnit\Framework\TestCase
* @covers phpOMS\Validation\Network\Hostname
* @group framework
*/
public function testHostname() : void
public function testHostnameDomain() : void
{
self::assertTrue(Hostname::isValid('test.com'));
self::assertFalse(Hostname::isValid('http://test.com'));
self::assertFalse(Hostname::isValid('test.com/test?something=a'));
self::assertFalse(Hostname::isValid('//somethign/wrong'));
}
public function testHostnameIp() : void
{
self::assertTrue(Hostname::isValid('127.0.0.1'));
self::assertTrue(Hostname::isValid('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]'));
self::assertFalse(Hostname::isValid('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
}
}