mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-09 21:48:41 +00:00
create draft test for socket-server
This commit is contained in:
parent
1e0a49a1b5
commit
cfe14cb55b
|
|
@ -14,13 +14,69 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace phpOMS\tests\Socket\Server;
|
namespace phpOMS\tests\Socket\Server;
|
||||||
|
|
||||||
|
use Model\CoreSettings;
|
||||||
|
use Modules\Admin\Models\AccountPermission;
|
||||||
|
use phpOMS\Account\Account;
|
||||||
|
use phpOMS\Account\AccountManager;
|
||||||
|
use phpOMS\Account\PermissionType;
|
||||||
|
use phpOMS\ApplicationAbstract;
|
||||||
|
use phpOMS\DataStorage\Cache\CachePool;
|
||||||
|
use phpOMS\Dispatcher\Dispatcher;
|
||||||
|
use phpOMS\Event\EventManager;
|
||||||
|
use phpOMS\Localization\L11nManager;
|
||||||
|
use phpOMS\Log\FileLogger;
|
||||||
|
use phpOMS\Module\ModuleManager;
|
||||||
|
use phpOMS\Router\WebRouter;
|
||||||
|
use phpOMS\Socket\Server\Server;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
class ServerTest extends \PHPUnit\Framework\TestCase
|
class ServerTest extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
public function testPlaceholder() : void
|
protected $app = null;
|
||||||
|
|
||||||
|
protected function setUp() : void
|
||||||
|
{
|
||||||
|
$this->app = new class() extends ApplicationAbstract
|
||||||
|
{
|
||||||
|
protected string $appName = 'Socket';
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->app->logger = FileLogger::getInstance(__DIR__ . '/server.log', true);
|
||||||
|
$this->app->dbPool = $GLOBALS['dbpool'];
|
||||||
|
$this->app->orgId = 1;
|
||||||
|
$this->app->cachePool = new CachePool($this->app->dbPool);
|
||||||
|
$this->app->accountManager = new AccountManager($GLOBALS['session']);
|
||||||
|
$this->app->appSettings = new CoreSettings($this->app->dbPool->get());
|
||||||
|
$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__ . '/../../../Socket/Hooks.php');
|
||||||
|
$this->app->l11nManager = new L11nManager($this->app->appName);
|
||||||
|
$this->app->router = new WebRouter();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown() : void
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
\delete(__DIR__ . '/client.log');
|
||||||
|
\delete(__DIR__ . '/server.log');*/
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetupTCPSocket() : void
|
||||||
{
|
{
|
||||||
self::markTestIncomplete();
|
self::markTestIncomplete();
|
||||||
|
return;
|
||||||
|
$pipes = [];
|
||||||
|
$process = \proc_open('php ServerTestHelper.php 127.0.0.1', [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, __DIR__);
|
||||||
|
|
||||||
|
$socket = new Server($this->app);
|
||||||
|
$socket->create('127.0.0.1', $GLOBALS['CONFIG']['socket']['master']['port']);
|
||||||
|
$socket->setLimit(1);
|
||||||
|
$socket->run();
|
||||||
|
|
||||||
|
// todo: assert content of server.log
|
||||||
|
// todo: assert content of client.log
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
69
tests/Socket/Server/ServerTestHelper.php
Normal file
69
tests/Socket/Server/ServerTestHelper.php
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Orange Management
|
||||||
|
*
|
||||||
|
* PHP Version 7.4
|
||||||
|
*
|
||||||
|
* @package test
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link http://orange-management.com
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace phpOMS\tests\Socket\Server;
|
||||||
|
|
||||||
|
use phpOMS\Socket\Client\Client;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../../Autoloader.php';
|
||||||
|
$config = require_once __DIR__ . '/../../../../config.php';
|
||||||
|
|
||||||
|
\sleep(5);
|
||||||
|
|
||||||
|
function handleSocketError($sock) : void
|
||||||
|
{
|
||||||
|
if (\is_resource($sock) && ($err = \socket_last_error($sock)) !== 0) {
|
||||||
|
\file_put_contents(__DIR__ . '/client.log', \socket_strerror($err) . "\n", \FILE_APPEND);
|
||||||
|
\socket_clear_error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$sock = @\socket_create(\AF_INET, \SOCK_STREAM, \SOL_TCP);
|
||||||
|
\socket_set_nonblock($sock);
|
||||||
|
@\socket_connect($sock, '127.0.0.1', $config['socket']['master']['port']);
|
||||||
|
handleSocketError($sock);
|
||||||
|
|
||||||
|
$msgs = [
|
||||||
|
'help' . "\r",
|
||||||
|
'shutdown' . "\r",
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($msgs as $msg) {
|
||||||
|
var_dump($msg);
|
||||||
|
@\socket_write($sock, $msg, \strlen($msg));
|
||||||
|
handleSocketError($sock);
|
||||||
|
|
||||||
|
$data = @\socket_read($sock, 1024);
|
||||||
|
handleSocketError($sock);
|
||||||
|
|
||||||
|
/* Server no data */
|
||||||
|
if ($data === false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Normalize */
|
||||||
|
$data = \trim($data);
|
||||||
|
|
||||||
|
\file_put_contents(__DIR__ . '/client.log', $data, \FILE_APPEND);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSocketError($sock);
|
||||||
|
\socket_close($sock);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
\file_put_contents(__DIR__ . '/client.log', $e->getMessage(), \FILE_APPEND);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSocketError($sock);
|
||||||
|
$sock = null;
|
||||||
Loading…
Reference in New Issue
Block a user