mirror of
https://github.com/Karaka-Management/oms-OnlineResourceWatcher.git
synced 2026-02-15 23:48:40 +00:00
drafting setup
This commit is contained in:
parent
d7b1fed424
commit
0ef4ce5331
|
|
@ -44,13 +44,13 @@
|
||||||
"type": "DATETIME",
|
"type": "DATETIME",
|
||||||
"null": false
|
"null": false
|
||||||
},
|
},
|
||||||
"orw_resource_org": {
|
"orw_resource_owner": {
|
||||||
"name": "orw_resource_org",
|
"name": "orw_resource_owner",
|
||||||
"type": "INT",
|
"type": "INT",
|
||||||
"null": true,
|
"null": true,
|
||||||
"default": null,
|
"default": null,
|
||||||
"foreignTable": "org",
|
"foreignTable": "account",
|
||||||
"foreignKey": "org_id"
|
"foreignKey": "account_id"
|
||||||
},
|
},
|
||||||
"orw_resource_created_at": {
|
"orw_resource_created_at": {
|
||||||
"name": "orw_resource_created_at",
|
"name": "orw_resource_created_at",
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,16 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Modules\OnlineResourceWatcher\Controller;
|
namespace Modules\OnlineResourceWatcher\Controller;
|
||||||
|
|
||||||
|
use Modules\Admin\Models\NullAccount;
|
||||||
|
use Modules\OnlineResourceWatcher\Models\Resource;
|
||||||
|
use Modules\OnlineResourceWatcher\Models\ResourceMapper;
|
||||||
|
use phpOMS\Message\Http\RequestStatusCode;
|
||||||
|
use phpOMS\Message\NotificationLevel;
|
||||||
|
use phpOMS\Message\RequestAbstract;
|
||||||
|
use phpOMS\Message\ResponseAbstract;
|
||||||
|
use phpOMS\Model\Message\FormValidation;
|
||||||
|
use phpOMS\System\SystemUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OnlineResourceWatcher controller class.
|
* OnlineResourceWatcher controller class.
|
||||||
*
|
*
|
||||||
|
|
@ -24,5 +34,93 @@ namespace Modules\OnlineResourceWatcher\Controller;
|
||||||
*/
|
*/
|
||||||
final class ApiController extends Controller
|
final class ApiController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Validate resource create request
|
||||||
|
*
|
||||||
|
* @param RequestAbstract $request Request
|
||||||
|
*
|
||||||
|
* @return array<string, bool>
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private function validateResourceCreate(RequestAbstract $request) : array
|
||||||
|
{
|
||||||
|
$val = [];
|
||||||
|
if (($val['title'] = empty($request->getData('title')))
|
||||||
|
|| ($val['path'] = empty($request->getData('path')))
|
||||||
|
) {
|
||||||
|
return $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api method to create resource
|
||||||
|
*
|
||||||
|
* @param RequestAbstract $request Request
|
||||||
|
* @param ResponseAbstract $response Response
|
||||||
|
* @param mixed $data Generic data
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function apiResourceCreate(RequestAbstract $request, ResponseAbstract $response, mixed $data = null) : void
|
||||||
|
{
|
||||||
|
if (!empty($val = $this->validateResourceCreate($request))) {
|
||||||
|
$response->set('resource_create', new FormValidation($val));
|
||||||
|
$response->header->status = RequestStatusCode::R_400;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$resource = $this->createResourceFromRequest($request);
|
||||||
|
$this->createModel($request->header->account, $resource, ResourceMapper::class, 'resource', $request->getOrigin());
|
||||||
|
|
||||||
|
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Resource', 'Resource successfully created', $resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to create news article from request.
|
||||||
|
*
|
||||||
|
* @param RequestAbstract $request Request
|
||||||
|
*
|
||||||
|
* @return Resource
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private function createResourceFromRequest(RequestAbstract $request) : Resource
|
||||||
|
{
|
||||||
|
$resource = new Resource();
|
||||||
|
$resource->owner = new NullAccount($request->header->account);
|
||||||
|
$resource->title = (string) ($request->getData('title') ?? '');
|
||||||
|
$resource->path = $request->getData('path') ?? '';
|
||||||
|
|
||||||
|
return $resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api method to create resource
|
||||||
|
*
|
||||||
|
* @param RequestAbstract $request Request
|
||||||
|
* @param ResponseAbstract $response Response
|
||||||
|
* @param mixed $data Generic data
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function apiCheckResources(RequestAbstract $request, ResponseAbstract $response, mixed $data = null) : void
|
||||||
|
{
|
||||||
|
SystemUtils::runProc(
|
||||||
|
__DIR__ . '/server/bin/App', ''
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Resources', 'Resources are getting checked.', null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
100
Models/Resource.php
Normal file
100
Models/Resource.php
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Karaka
|
||||||
|
*
|
||||||
|
* PHP Version 8.1
|
||||||
|
*
|
||||||
|
* @package Modules\OnlineResourceWatcher\Models
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Modules\OnlineResourceWatcher\Models;
|
||||||
|
|
||||||
|
use Modules\Admin\Models\Account;
|
||||||
|
use Modules\Admin\Models\NullAccount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resource class.
|
||||||
|
*
|
||||||
|
* @package Modules\OnlineResourceWatcher\Models
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
class Resource implements \JsonSerializable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Article ID.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
protected int $id = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Owner.
|
||||||
|
*
|
||||||
|
* @var Account
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public Account $owner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created.
|
||||||
|
*
|
||||||
|
* @var \DateTimeImmutable
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public \DateTimeImmutable $createdAt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Title.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public string $title = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public string $path = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->owner = new NullAccount();
|
||||||
|
$this->createdAt = new \DateTimeImmutable('now');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toArray() : array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'createdAt' => $this->createdAt,
|
||||||
|
'owner' => $this->owner,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function jsonSerialize() : mixed
|
||||||
|
{
|
||||||
|
return $this->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
69
Models/ResourceMapper.php
Normal file
69
Models/ResourceMapper.php
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Karaka
|
||||||
|
*
|
||||||
|
* PHP Version 8.1
|
||||||
|
*
|
||||||
|
* @package Modules\OnlineResourceWatcher\Models
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Modules\OnlineResourceWatcher\Models;
|
||||||
|
|
||||||
|
use Modules\Admin\Models\AccountMapper;
|
||||||
|
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resource mapper class.
|
||||||
|
*
|
||||||
|
* @package Modules\OnlineResourceWatcher\Models
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
final class ResourceMapper extends DataMapperFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Columns.
|
||||||
|
*
|
||||||
|
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public const COLUMNS = [
|
||||||
|
'orw_resource_id' => ['name' => 'orw_resource_id', 'type' => 'int', 'internal' => 'id'],
|
||||||
|
'orw_resource_owner' => ['name' => 'orw_resource_owner', 'type' => 'int', 'internal' => 'owner', 'readonly' => true],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Belongs to.
|
||||||
|
*
|
||||||
|
* @var array<string, array{mapper:string, external:string, column?:string, by?:string}>
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public const BELONGS_TO = [
|
||||||
|
'owner' => [
|
||||||
|
'mapper' => AccountMapper::class,
|
||||||
|
'external' => 'orw_resource_owner',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Primary table.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public const TABLE = 'orw_resource';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Primary field name.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public const PRIMARYFIELD ='orw_resource_id';
|
||||||
|
}
|
||||||
27
tests/Admin/AdminTest.php
Executable file
27
tests/Admin/AdminTest.php
Executable file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Karaka
|
||||||
|
*
|
||||||
|
* PHP Version 8.1
|
||||||
|
*
|
||||||
|
* @package tests
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Modules\OnlineResourceWatcher\tests\Admin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class AdminTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
protected const NAME = 'OnlineResourceWatcher';
|
||||||
|
|
||||||
|
protected const URI_LOAD = 'http://127.0.0.1/en/backend/orw';
|
||||||
|
|
||||||
|
use \Modules\tests\ModuleTestTrait;
|
||||||
|
}
|
||||||
94
tests/Autoloader.php
Executable file
94
tests/Autoloader.php
Executable file
|
|
@ -0,0 +1,94 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Karaka
|
||||||
|
*
|
||||||
|
* PHP Version 8.1
|
||||||
|
*
|
||||||
|
* @package Modules/tests
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace tests;
|
||||||
|
|
||||||
|
\spl_autoload_register('\tests\Autoloader::defaultAutoloader');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autoloader class.
|
||||||
|
*
|
||||||
|
* @package tests
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
final class Autoloader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Base paths for autoloading
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private static $paths = [
|
||||||
|
__DIR__ . '/../',
|
||||||
|
__DIR__ . '/../Karaka/',
|
||||||
|
__DIR__ . '/../../',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*/
|
||||||
|
private function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add base path for autoloading
|
||||||
|
*
|
||||||
|
* @param string $path Absolute base path with / at the end
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function addPath(string $path) : void
|
||||||
|
{
|
||||||
|
self::$paths[] = $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loading classes by namespace + class name.
|
||||||
|
*
|
||||||
|
* @param string $class Class path
|
||||||
|
*
|
||||||
|
* @example Autoloader::defaultAutoloader('\phpOMS\Autoloader') // void
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @throws AutoloadException Throws this exception if the class to autoload doesn't exist. This could also be related to a wrong namespace/file path correlation.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function defaultAutoloader(string $class) : void
|
||||||
|
{
|
||||||
|
$class = \ltrim($class, '\\');
|
||||||
|
$class = \str_replace(['_', '\\'], '/', $class);
|
||||||
|
|
||||||
|
foreach (self::$paths as $path) {
|
||||||
|
$file = $path . $class . '.php';
|
||||||
|
$file = \str_replace('/Modules/', '/', $file);
|
||||||
|
|
||||||
|
if (\is_file($file)) {
|
||||||
|
include_once $file;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
396
tests/Bootstrap.php
Executable file
396
tests/Bootstrap.php
Executable file
|
|
@ -0,0 +1,396 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
// Modules/tests
|
||||||
|
|
||||||
|
\ini_set('memory_limit', '2048M');
|
||||||
|
\ini_set('display_errors', '1');
|
||||||
|
\ini_set('display_startup_errors', '1');
|
||||||
|
\error_reporting(\E_ALL);
|
||||||
|
\setlocale(\LC_ALL, 'en_US.UTF-8');
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
require_once __DIR__ . '/Autoloader.php';
|
||||||
|
|
||||||
|
use phpOMS\DataStorage\Database\DatabasePool;
|
||||||
|
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||||
|
use phpOMS\DataStorage\Session\HttpSession;
|
||||||
|
|
||||||
|
$CONFIG = [
|
||||||
|
'db' => [
|
||||||
|
'core' => [
|
||||||
|
'masters' => [
|
||||||
|
'admin' => [
|
||||||
|
'db' => 'mysql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '3306', /* db host port */
|
||||||
|
'login' => 'root', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'insert' => [
|
||||||
|
'db' => 'mysql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '3306', /* db host port */
|
||||||
|
'login' => 'root', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'select' => [
|
||||||
|
'db' => 'mysql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '3306', /* db host port */
|
||||||
|
'login' => 'root', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'update' => [
|
||||||
|
'db' => 'mysql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '3306', /* db host port */
|
||||||
|
'login' => 'root', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'delete' => [
|
||||||
|
'db' => 'mysql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '3306', /* db host port */
|
||||||
|
'login' => 'root', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'schema' => [
|
||||||
|
'db' => 'mysql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '3306', /* db host port */
|
||||||
|
'login' => 'root', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'postgresql' => [
|
||||||
|
'admin' => [
|
||||||
|
'db' => 'pgsql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '5432', /* db host port */
|
||||||
|
'login' => 'postgres', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'insert' => [
|
||||||
|
'db' => 'pgsql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '5432', /* db host port */
|
||||||
|
'login' => 'postgres', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'select' => [
|
||||||
|
'db' => 'pgsql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '5432', /* db host port */
|
||||||
|
'login' => 'postgres', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'update' => [
|
||||||
|
'db' => 'pgsql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '5432', /* db host port */
|
||||||
|
'login' => 'postgres', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'delete' => [
|
||||||
|
'db' => 'pgsql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '5432', /* db host port */
|
||||||
|
'login' => 'postgres', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'schema' => [
|
||||||
|
'db' => 'pgsql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '5432', /* db host port */
|
||||||
|
'login' => 'postgres', /* db login name */
|
||||||
|
'password' => 'root', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'sqlite' => [
|
||||||
|
'admin' => [
|
||||||
|
'db' => 'sqlite', /* db type */
|
||||||
|
'database' => __DIR__ . '/test.sqlite', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'insert' => [
|
||||||
|
'db' => 'sqlite', /* db type */
|
||||||
|
'database' => __DIR__ . '/test.sqlite', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'select' => [
|
||||||
|
'db' => 'sqlite', /* db type */
|
||||||
|
'database' => __DIR__ . '/test.sqlite', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'update' => [
|
||||||
|
'db' => 'sqlite', /* db type */
|
||||||
|
'database' => __DIR__ . '/test.sqlite', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'delete' => [
|
||||||
|
'db' => 'sqlite', /* db type */
|
||||||
|
'database' => __DIR__ . '/test.sqlite', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'schema' => [
|
||||||
|
'db' => 'sqlite', /* db type */
|
||||||
|
'database' => __DIR__ . '/test.sqlite', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'mssql' => [
|
||||||
|
'admin' => [
|
||||||
|
'db' => 'mssql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '1433', /* db host port */
|
||||||
|
'login' => 'sa', /* db login name */
|
||||||
|
'password' => 'R00troot', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'insert' => [
|
||||||
|
'db' => 'mssql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '1433', /* db host port */
|
||||||
|
'login' => 'sa', /* db login name */
|
||||||
|
'password' => 'R00troot', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'select' => [
|
||||||
|
'db' => 'mssql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '1433', /* db host port */
|
||||||
|
'login' => 'sa', /* db login name */
|
||||||
|
'password' => 'R00troot', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'update' => [
|
||||||
|
'db' => 'mssql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '1433', /* db host port */
|
||||||
|
'login' => 'sa', /* db login name */
|
||||||
|
'password' => 'R00troot', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'delete' => [
|
||||||
|
'db' => 'mssql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '1433', /* db host port */
|
||||||
|
'login' => 'sa', /* db login name */
|
||||||
|
'password' => 'R00troot', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
'schema' => [
|
||||||
|
'db' => 'mssql', /* db type */
|
||||||
|
'host' => '127.0.0.1', /* db host address */
|
||||||
|
'port' => '1433', /* db host port */
|
||||||
|
'login' => 'sa', /* db login name */
|
||||||
|
'password' => 'R00troot', /* db login password */
|
||||||
|
'database' => 'oms', /* db name */
|
||||||
|
'weight' => 1000, /* db table prefix */
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'cache' => [
|
||||||
|
'redis' => [
|
||||||
|
'db' => 1,
|
||||||
|
'host' => '127.0.0.1',
|
||||||
|
'port' => 6379,
|
||||||
|
],
|
||||||
|
'memcached' => [
|
||||||
|
'host' => '127.0.0.1',
|
||||||
|
'port' => 11211,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'mail' => [
|
||||||
|
'imap' => [
|
||||||
|
'host' => '127.0.0.1',
|
||||||
|
'port' => 143,
|
||||||
|
'ssl' => false,
|
||||||
|
'user' => 'test',
|
||||||
|
'password' => '123456',
|
||||||
|
],
|
||||||
|
'pop3' => [
|
||||||
|
'host' => '127.0.0.1',
|
||||||
|
'port' => 25,
|
||||||
|
'ssl' => false,
|
||||||
|
'user' => 'test',
|
||||||
|
'password' => '123456',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'log' => [
|
||||||
|
'file' => [
|
||||||
|
'path' => __DIR__ . '/Logs',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'page' => [
|
||||||
|
'root' => '/',
|
||||||
|
'https' => false,
|
||||||
|
],
|
||||||
|
'socket' => [
|
||||||
|
'master' => [
|
||||||
|
'host' => '127.0.0.1',
|
||||||
|
'limit' => 300,
|
||||||
|
'port' => 4310,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'language' => [
|
||||||
|
'en',
|
||||||
|
],
|
||||||
|
'apis' => [
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
// Reset database
|
||||||
|
if (\defined('RESET') && RESET === '1') {
|
||||||
|
if (\extension_loaded('pdo_mysql')) {
|
||||||
|
try {
|
||||||
|
$db = new \PDO('mysql:host=' .
|
||||||
|
$CONFIG['db']['core']['masters']['admin']['host'],
|
||||||
|
$CONFIG['db']['core']['masters']['admin']['login'],
|
||||||
|
$CONFIG['db']['core']['masters']['admin']['password']
|
||||||
|
);
|
||||||
|
$db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['masters']['admin']['database']);
|
||||||
|
$db->exec('CREATE DATABASE IF NOT EXISTS ' . $CONFIG['db']['core']['masters']['admin']['database']);
|
||||||
|
$db = null;
|
||||||
|
} catch (\Throwable $t) {
|
||||||
|
echo "\nCouldn't connect to MYSQL DB\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\extension_loaded('pdo_pgsql')) {
|
||||||
|
try {
|
||||||
|
$db = new \PDO('pgsql:host=' .
|
||||||
|
$CONFIG['db']['core']['postgresql']['admin']['host'],
|
||||||
|
$CONFIG['db']['core']['postgresql']['admin']['login'],
|
||||||
|
$CONFIG['db']['core']['postgresql']['admin']['password']
|
||||||
|
);
|
||||||
|
$db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['postgresql']['admin']['database']);
|
||||||
|
$db->exec('CREATE DATABASE ' . $CONFIG['db']['core']['postgresql']['admin']['database']);
|
||||||
|
$db = null;
|
||||||
|
} catch (\Throwable $t) {
|
||||||
|
echo "\nCouldn't connect to POSTGRESQL DB\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\extension_loaded('pdo_sqlsrv')) {
|
||||||
|
try {
|
||||||
|
$db = new \PDO('sqlsrv:Server=' .
|
||||||
|
$CONFIG['db']['core']['mssql']['admin']['host'],
|
||||||
|
$CONFIG['db']['core']['mssql']['admin']['login'],
|
||||||
|
$CONFIG['db']['core']['mssql']['admin']['password']
|
||||||
|
);
|
||||||
|
$db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['mssql']['admin']['database']);
|
||||||
|
$db->exec('CREATE DATABASE ' . $CONFIG['db']['core']['mssql']['admin']['database']);
|
||||||
|
$db = null;
|
||||||
|
} catch (\Throwable $t) {
|
||||||
|
echo "\nCouldn't connect to MSSQL DB\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$httpSession = new HttpSession();
|
||||||
|
$GLOBALS['session'] = $httpSession;
|
||||||
|
|
||||||
|
$GLOBALS['dbpool'] = new DatabasePool();
|
||||||
|
$GLOBALS['dbpool']->create('admin', $CONFIG['db']['core']['masters']['admin']);
|
||||||
|
$GLOBALS['dbpool']->create('select', $CONFIG['db']['core']['masters']['select']);
|
||||||
|
$GLOBALS['dbpool']->create('update', $CONFIG['db']['core']['masters']['update']);
|
||||||
|
$GLOBALS['dbpool']->create('delete', $CONFIG['db']['core']['masters']['delete']);
|
||||||
|
$GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']);
|
||||||
|
$GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']);
|
||||||
|
|
||||||
|
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||||
|
|
||||||
|
$GLOBALS['frameworkpath'] = '/phpOMS/';
|
||||||
|
|
||||||
|
function phpServe() : void
|
||||||
|
{
|
||||||
|
// OS detection
|
||||||
|
$isWindows = \stristr(\php_uname('s'), 'Windows') !== false;
|
||||||
|
|
||||||
|
// Command that starts the built-in web server
|
||||||
|
if ($isWindows) {
|
||||||
|
$command = \sprintf(
|
||||||
|
'wmic process call create "php -S %s:%d -t %s" | find "ProcessId"',
|
||||||
|
WEB_SERVER_HOST,
|
||||||
|
WEB_SERVER_PORT,
|
||||||
|
__DIR__ . '/../' . WEB_SERVER_DOCROOT
|
||||||
|
);
|
||||||
|
|
||||||
|
$killCommand = 'taskkill /f /pid ';
|
||||||
|
} else {
|
||||||
|
$command = \sprintf(
|
||||||
|
'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!',
|
||||||
|
WEB_SERVER_HOST,
|
||||||
|
WEB_SERVER_PORT,
|
||||||
|
WEB_SERVER_DOCROOT
|
||||||
|
);
|
||||||
|
|
||||||
|
$killCommand = 'kill ';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the command and store the process ID
|
||||||
|
$output = [];
|
||||||
|
echo \sprintf('Starting server...') . \PHP_EOL;
|
||||||
|
echo \sprintf(' Current directory: %s', \getcwd()) . \PHP_EOL;
|
||||||
|
echo \sprintf(' %s', $command);
|
||||||
|
\exec($command, $output);
|
||||||
|
|
||||||
|
// Get PID
|
||||||
|
if ($isWindows) {
|
||||||
|
$pid = \explode('=', $output[0]);
|
||||||
|
$pid = \str_replace(' ', '', $pid[1]);
|
||||||
|
$pid = \str_replace(';', '', $pid);
|
||||||
|
} else {
|
||||||
|
$pid = (int) $output[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log
|
||||||
|
echo \sprintf(
|
||||||
|
' %s - Web server started on %s:%d with PID %d',
|
||||||
|
\date('r'),
|
||||||
|
WEB_SERVER_HOST,
|
||||||
|
WEB_SERVER_PORT,
|
||||||
|
$pid
|
||||||
|
) . \PHP_EOL;
|
||||||
|
|
||||||
|
// Kill the web server when the process ends
|
||||||
|
\register_shutdown_function(function() use ($killCommand, $pid) : void {
|
||||||
|
echo \PHP_EOL . \sprintf('Stopping server...') . \PHP_EOL;
|
||||||
|
echo \sprintf(' %s - Killing process with ID %d', \date('r'), $pid) . \PHP_EOL;
|
||||||
|
\exec($killCommand . $pid);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
\phpServe();
|
||||||
82
tests/Controller/ApiControllerTest.php
Executable file
82
tests/Controller/ApiControllerTest.php
Executable file
|
|
@ -0,0 +1,82 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Karaka
|
||||||
|
*
|
||||||
|
* PHP Version 8.1
|
||||||
|
*
|
||||||
|
* @package tests
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link https://karaka.app
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Modules\OnlineResourceWatcher\tests\Controller;
|
||||||
|
|
||||||
|
use Model\CoreSettings;
|
||||||
|
use Modules\Admin\Models\AccountPermission;
|
||||||
|
use phpOMS\Account\Account;
|
||||||
|
use phpOMS\Account\AccountManager;
|
||||||
|
use phpOMS\Account\PermissionType;
|
||||||
|
use phpOMS\Application\ApplicationAbstract;
|
||||||
|
use phpOMS\Dispatcher\Dispatcher;
|
||||||
|
use phpOMS\Event\EventManager;
|
||||||
|
use phpOMS\Module\ModuleManager;
|
||||||
|
use phpOMS\Router\WebRouter;
|
||||||
|
use phpOMS\Utils\TestUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @testdox Modules\OnlineResourceWatcher\tests\Controller\ApiControllerTest: OnlineResourceWatcher api controller
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class ControllerTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
protected $app = null;
|
||||||
|
|
||||||
|
protected $module = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp() : void
|
||||||
|
{
|
||||||
|
$this->app = new class() extends ApplicationAbstract
|
||||||
|
{
|
||||||
|
protected string $appName = 'Api';
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->app->dbPool = $GLOBALS['dbpool'];
|
||||||
|
$this->app->orgId = 1;
|
||||||
|
$this->app->accountManager = new AccountManager($GLOBALS['session']);
|
||||||
|
$this->app->appSettings = new CoreSettings();
|
||||||
|
$this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules/');
|
||||||
|
$this->app->dispatcher = new Dispatcher($this->app);
|
||||||
|
$this->app->eventManager = new EventManager($this->app->dispatcher);
|
||||||
|
$this->app->eventManager->importFromFile(__DIR__ . '/../../../Web/Api/Hooks.php');
|
||||||
|
|
||||||
|
$account = new Account();
|
||||||
|
TestUtils::setMember($account, 'id', 1);
|
||||||
|
|
||||||
|
$permission = new AccountPermission();
|
||||||
|
$permission->setUnit(1);
|
||||||
|
$permission->setApp('backend');
|
||||||
|
$permission->setPermission(
|
||||||
|
PermissionType::READ
|
||||||
|
| PermissionType::CREATE
|
||||||
|
| PermissionType::MODIFY
|
||||||
|
| PermissionType::DELETE
|
||||||
|
| PermissionType::PERMISSION
|
||||||
|
);
|
||||||
|
|
||||||
|
$account->addPermission($permission);
|
||||||
|
|
||||||
|
$this->app->accountManager->add($account);
|
||||||
|
$this->app->router = new WebRouter();
|
||||||
|
|
||||||
|
$this->module = $this->app->moduleManager->get('OnlineResourceWatcher');
|
||||||
|
|
||||||
|
TestUtils::setMember($this->module, 'app', $this->app);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user