Browse Source

Laravel classes renamed, tests of Laravel classes added

pull/40/head
Paul Rock 6 years ago
parent
commit
fd8ca3e356
  1. 16
      src/Client.php
  2. 6
      src/Laravel/Facade.php
  3. 4
      src/Laravel/ServiceProvider.php
  4. 49
      src/Laravel/Wrapper.php
  5. 65
      tests/Laravel/ServiceProviderTests.php
  6. 35
      tests/Laravel/TestCase.php

16
src/Client.php

@ -39,13 +39,14 @@ class Client implements Interfaces\ClientInterface
/** /**
* Client constructor. * Client constructor.
* *
* @param array|\RouterOS\Config $config
* @param array|\RouterOS\Interfaces\ConfigInterface $config Array with configuration or Config object
* @param bool $autoConnect If false it will skip auto-connect stage if not need to instantiate connection
* *
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
public function __construct($config)
public function __construct($config, bool $autoConnect = true)
{ {
// If array then need create object // If array then need create object
if (is_array($config)) { if (is_array($config)) {
@ -60,6 +61,11 @@ class Client implements Interfaces\ClientInterface
// Save config if everything is okay // Save config if everything is okay
$this->_config = $config; $this->_config = $config;
// Skip next step if not need to instantiate connection
if (false === $autoConnect) {
return;
}
// Throw error if cannot to connect // Throw error if cannot to connect
if (false === $this->connect()) { if (false === $this->connect()) {
throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port'));
@ -385,7 +391,7 @@ class Client implements Interfaces\ClientInterface
* @param string $value * @param string $value
* @param array $matches * @param array $matches
*/ */
private function pregResponse(string $value, &$matches)
private function pregResponse(string $value, &$matches): void
{ {
preg_match_all('/^[=|.](.*)=(.*)/', $value, $matches); preg_match_all('/^[=|.](.*)=(.*)/', $value, $matches);
} }
@ -454,7 +460,7 @@ class Client implements Interfaces\ClientInterface
* @return bool * @return bool
* @throws ConfigException * @throws ConfigException
*/ */
private function isLegacy(array &$response): bool
private function isLegacy(array $response): bool
{ {
return count($response) > 1 && $response[0] === '!done' && !$this->config('legacy'); return count($response) > 1 && $response[0] === '!done' && !$this->config('legacy');
} }
@ -467,7 +473,7 @@ class Client implements Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
private function connect(): bool
public function connect(): bool
{ {
// By default we not connected // By default we not connected
$connected = false; $connected = false;

6
src/Laravel/Facade.php

@ -2,9 +2,9 @@
namespace RouterOS\Laravel; namespace RouterOS\Laravel;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\Facade as BaseFacade;
class ClientFacade extends Facade
class Facade extends BaseFacade
{ {
/** /**
* Get the registered name of the component. * Get the registered name of the component.
@ -13,6 +13,6 @@ class ClientFacade extends Facade
*/ */
protected static function getFacadeAccessor(): string protected static function getFacadeAccessor(): string
{ {
return ClientWrapper::class;
return Wrapper::class;
} }
} }

4
src/Laravel/ServiceProvider.php

@ -4,7 +4,7 @@ namespace RouterOS\Laravel;
use Illuminate\Support\ServiceProvider as BaseServiceProvider; use Illuminate\Support\ServiceProvider as BaseServiceProvider;
class ClientServiceProvider extends BaseServiceProvider
class ServiceProvider extends BaseServiceProvider
{ {
/** /**
* Bootstrap any application services. * Bootstrap any application services.
@ -29,6 +29,6 @@ class ClientServiceProvider extends BaseServiceProvider
__DIR__ . '/../../configs/routeros-api.php', 'routeros-api' __DIR__ . '/../../configs/routeros-api.php', 'routeros-api'
); );
$this->app->bind(ClientWrapper::class);
$this->app->bind(Wrapper::class);
} }
} }

49
src/Laravel/Wrapper.php

@ -3,22 +3,61 @@
namespace RouterOS\Laravel; namespace RouterOS\Laravel;
use RouterOS\Client; use RouterOS\Client;
use RouterOS\Config;
use RouterOS\Interfaces\ClientInterface;
use RouterOS\Interfaces\ConfigInterface;
class ClientWrapper
class Wrapper
{ {
/** /**
* Alias for \RouterOS::client() method
*
* @param array $params * @param array $params
* *
* @return \RouterOS\Client * @return \RouterOS\Client
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
* @deprecated
* @codeCoverageIgnore
*/
public function getClient(array $params = []): ClientInterface
{
return $this->client($params);
}
/**
* Get configs of library
*
* @param array $params
*
* @return \RouterOS\Interfaces\ConfigInterface
* @throws \RouterOS\Exceptions\ConfigException
*/
public function config(array $params = []): ConfigInterface
{
$config = config('routeros-api');
$config = array_merge($config, $params);
$config = new Config($config);
return $config;
}
/**
* Instantiate client object
*
* @param array $params
* @param bool $autoConnect
*
* @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException
*/ */
public function getClient(array $params = []): Client
public function client(array $params = [], bool $autoConnect = true): ClientInterface
{ {
$configs = config('routeros-api');
$configs = array_merge($configs, $params);
$config = $this->config($params);
return new Client($configs);
return new Client($config, $autoConnect);
} }
} }

65
tests/Laravel/ServiceProviderTests.php

@ -0,0 +1,65 @@
<?php
namespace RouterOS\Tests\Laravel;
use RouterOS\Config;
use RouterOS\Laravel\Wrapper;
class ServiceProviderTests extends TestCase
{
private $client = [
"__construct",
"write",
"query",
"read",
"readAsIterator",
"parseResponse",
"connect",
"getSocket",
"w",
"q",
"r",
"ri",
"wr",
"qr",
"wri",
"qri",
];
public function testAbstractsAreLoaded(): void
{
$manager = app(Wrapper::class);
$this->assertInstanceOf(Wrapper::class, $manager);
}
public function testConfig(): void
{
$config = \RouterOS::config([
'host' => '192.168.1.3',
'user' => 'admin',
'pass' => 'admin'
]);
$this->assertInstanceOf(Config::class, $config);
$params = $config->getParameters();
$this->assertArrayHasKey('host', $params);
$this->assertArrayHasKey('user', $params);
$this->assertArrayHasKey('pass', $params);
$this->assertArrayHasKey('ssl', $params);
$this->assertArrayHasKey('legacy', $params);
$this->assertArrayHasKey('timeout', $params);
$this->assertArrayHasKey('attempts', $params);
$this->assertArrayHasKey('delay', $params);
}
public function testClient(): void
{
$client = \RouterOS::client([
'host' => '192.168.1.3',
'user' => 'admin',
'pass' => 'admin'
], false);
$this->assertEquals(get_class_methods($client), $this->client);
}
}

35
tests/Laravel/TestCase.php

@ -0,0 +1,35 @@
<?php
namespace RouterOS\Tests\Laravel;
use RouterOS\Laravel\Facade;
use RouterOS\Laravel\ServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
/**
* Class TestCase
*
* @package Tests
*/
abstract class TestCase extends Orchestra
{
/**
* @inheritdoc
*/
protected function getPackageProviders($app): array
{
return [
ServiceProvider::class,
];
}
/**
* @inheritdoc
*/
protected function getPackageAliases($app): array
{
return [
'RouterOS' => Facade::class,
];
}
}
Loading…
Cancel
Save