Skip to content

Commit

Permalink
Merge pull request #29 from stefanak-michal/phpunit
Browse files Browse the repository at this point in the history
Phpunit
  • Loading branch information
stefanak-michal authored Oct 23, 2020
2 parents 034be7c + ccb2bd2 commit 9e391eb
Show file tree
Hide file tree
Showing 83 changed files with 1,186 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/

.vscode/
phpunit*.phar
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"ext-sockets": "*",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": ">=7.5.0"
},
"support": {
"issues": "https://github.com/stefanak-michal/Bolt/issues",
"source": "https://github.com/stefanak-michal/Bolt"
Expand All @@ -30,7 +33,7 @@
],
"autoload": {
"psr-4": {
"Bolt\\": "/"
"Bolt\\": "src/"
}
}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @link https://github.com/stefanak-michal/Bolt
*/

require_once 'autoload.php';
require_once 'src' . DIRECTORY_SEPARATOR . 'autoload.php';

set_time_limit(3);

Expand Down
19 changes: 19 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="Bolt">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<var name="NEO_USER" value="neo4j"/>
<var name="NEO_PASS" value="nothing"/>
</php>
<filter>
<whitelist>
<directory suffix=".php">connection</directory>
<directory suffix=".php">PackStream</directory>
<directory suffix=".php">protocol</directory>
<file>Bolt.php</file>
</whitelist>
</filter>
</phpunit>
20 changes: 13 additions & 7 deletions Bolt.php → src/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Bolt\PackStream\{IPacker, IUnpacker};
use Bolt\protocol\AProtocol;
use Bolt\connection\IConnection;

/**
* Main class Bolt
Expand Down Expand Up @@ -32,9 +33,9 @@ final class Bolt
private $protocol;

/**
* @var Socket
* @var IConnection
*/
private $socket;
private $connection;

/**
* @var array
Expand Down Expand Up @@ -77,7 +78,7 @@ final class Bolt
*/
public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15)
{
$this->socket = new Socket($ip, $port, $timeout);
$this->connection = new \Bolt\connection\Socket($ip, $port, $timeout);

$packerClass = "\\Bolt\\PackStream\\v" . $this->packStreamVersion . "\\Packer";
if (!class_exists($packerClass)) {
Expand Down Expand Up @@ -142,8 +143,8 @@ private function handshake(): bool
if (self::$debug)
echo 'HANDSHAKE';

$this->socket->write(chr(0x60) . chr(0x60) . chr(0xb0) . chr(0x17));
$this->socket->write($this->packProtocolVersions());
$this->connection->write(chr(0x60) . chr(0x60) . chr(0xb0) . chr(0x17));
$this->connection->write($this->packProtocolVersions());

$this->unpackProtocolVersion();
if (empty($this->version)) {
Expand All @@ -155,7 +156,7 @@ private function handshake(): bool
if (!class_exists($protocolClass)) {
Bolt::error('Requested Protocol version (' . $this->version . ') not yet implemented');
} else {
$this->protocol = new $protocolClass($this->packer, $this->unpacker, $this->socket);
$this->protocol = new $protocolClass($this->packer, $this->unpacker, $this->connection);
}

return $this->protocol instanceof AProtocol;
Expand All @@ -168,7 +169,7 @@ private function unpackProtocolVersion()
{
$result = [];

foreach (str_split($this->socket->read(4)) as $ch)
foreach (str_split($this->connection->read(4)) as $ch)
$result[] = unpack('C', $ch)[1] ?? 0;

$result = array_filter($result);
Expand Down Expand Up @@ -218,6 +219,9 @@ private function packProtocolVersions(): string
*/
public function init(string $name, string $user, string $password, array $routing = null): bool
{
if (!$this->connection->connect())
return false;

if (!$this->handshake())
return false;

Expand Down Expand Up @@ -397,6 +401,8 @@ public function __destruct()
echo 'GOODBYE';
$this->protocol->goodbye();
}

$this->connection->disconnect();
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions autoload.php → src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
spl_autoload_register(function ($name) {
$parts = explode("\\", $name);
$parts = array_filter($parts);
array_shift($parts);
if ($parts[0] != 'Bolt')
return;
array_shift($parts);

/*
* namespace calls
*/

//compose standart namespaced path to file
$path = '.' . DS . implode(DS, $parts) . '.php';
$path = __DIR__ . DS . implode(DS, $parts) . '.php';
if (file_exists($path)) {
require_once $path;
return;
Expand Down
21 changes: 21 additions & 0 deletions src/connection/IConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Bolt\connection;

/**
* Interface IConnection
*
* @author Michal Stefanak
* @link https://github.com/stefanak-michal/Bolt
* @package Bolt\connection
*/
interface IConnection
{
public function connect(): bool;

public function write(string $buffer);

public function read(int $length = 2048): string;

public function disconnect();
}
62 changes: 49 additions & 13 deletions Socket.php → src/connection/Socket.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
<?php

namespace Bolt;
namespace Bolt\connection;

use Bolt\Bolt;

/**
* Static class Socket
* Socket class
*
* @author Michal Stefanak
* @link https://github.com/stefanak-michal/Bolt
* @package Bolt
* @package Bolt\connection
*/
final class Socket
class Socket implements IConnection
{

/**
* @var string
*/
private $ip;

/**
* @var int
*/
private $port;

/**
* @var int
*/
private $timeout;

/**
* @var resource
*/
Expand All @@ -26,30 +44,44 @@ public function __construct(string $ip, int $port, int $timeout)
{
if (!extension_loaded('sockets')) {
Bolt::error('PHP Extension sockets not enabled');
return;
}

$this->ip = $ip;
$this->port = $port;
$this->timeout = $timeout;
}

/**
* Create socket connection
* @return bool
* @throws \Exception
*/
public function connect(): bool
{
$this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($this->socket)) {
Bolt::error('Cannot create socket');
return;
return false;
}

if (socket_set_block($this->socket) === false) {
Bolt::error('Cannot set socket into blocking mode');
return;
return false;
}

socket_set_option($this->socket, SOL_TCP, TCP_NODELAY, 1);
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $timeout, 'usec' => 0]);
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $timeout, 'usec' => 0]);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $this->timeout, 'usec' => 0]);
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => $this->timeout, 'usec' => 0]);

$conn = socket_connect($this->socket, $ip, $port);
$conn = @socket_connect($this->socket, $this->ip, $this->port);
if (!$conn) {
$code = socket_last_error($this->socket);
Bolt::error(socket_strerror($code), $code);
return false;
}

return true;
}

/**
Expand Down Expand Up @@ -132,10 +164,14 @@ private function printHex(string $str, bool $write = true)
}

/**
* Close socket
* Close socket connection
*/
public function __destruct()
public function disconnect()
{
@socket_close($this->socket);
if (is_resource($this->socket)) {
@socket_shutdown($this->socket);
@socket_close($this->socket);
}
}

}
22 changes: 11 additions & 11 deletions protocol/AProtocol.php → src/protocol/AProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Bolt\Bolt;
use Bolt\PackStream\{IPacker, IUnpacker};
use Bolt\Socket;
use Bolt\connection\IConnection;
use Exception;

/**
Expand Down Expand Up @@ -33,21 +33,21 @@ abstract class AProtocol implements IProtocol
protected $unpacker;

/**
* @var Socket
* @var IConnection
*/
private $socket;
private $connection;

/**
* AProtocol constructor.
* @param IPacker $packer
* @param IUnpacker $unpacker
* @param Socket $socket
* @param IConnection $connection
*/
public function __construct(IPacker $packer, IUnpacker $unpacker, Socket $socket)
public function __construct(IPacker $packer, IUnpacker $unpacker, IConnection $connection)
{
$this->packer = $packer;
$this->unpacker = $unpacker;
$this->socket = $socket;
$this->connection = $connection;
}

public function begin(...$args): bool
Expand All @@ -71,17 +71,17 @@ public function goodbye(...$args)
}

/**
* Write to socket
* Write to connection
* @param string $buffer
* @throws Exception
*/
protected function write(string $buffer)
{
$this->socket->write($buffer);
$this->connection->write($buffer);
}

/**
* Read from socket
* Read from connection
* @param int|null $signature
* @return mixed|null
* @throws Exception
Expand All @@ -90,11 +90,11 @@ protected function read(?int &$signature)
{
$msg = '';
while (true) {
$header = $this->socket->read(2);
$header = $this->connection->read(2);
if (ord($header[0]) == 0x00 && ord($header[1]) == 0x00)
break;
$length = unpack('n', $header)[1] ?? 0;
$msg .= $this->socket->read($length);
$msg .= $this->connection->read($length);
}

$output = null;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion protocol/V3.php → src/protocol/V3.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function goodbye(...$args)
$msg = $this->packer->pack(0x02);
} catch (Exception $ex) {
Bolt::error($ex->getMessage());
return false;
return;
}

$this->write($msg);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9e391eb

Please sign in to comment.