You've already forked Traefik-Redis-Api
143 lines
4.2 KiB
PHP
143 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\Tests\Traefik;
|
|
|
|
use Mockery;
|
|
use Predis\Command\FactoryInterface;
|
|
use Siteworxpro\App\Facades\Redis;
|
|
use Siteworxpro\App\Traefik\EntityEnum;
|
|
use Siteworxpro\App\Traefik\ProtocolEnum;
|
|
use Siteworxpro\App\Traefik\RedisClient;
|
|
use Siteworxpro\Tests\Unit;
|
|
|
|
class RedisClientServiceTest extends Unit
|
|
{
|
|
|
|
public function testCreateServiceHttp(): void
|
|
{
|
|
$this->createTest(ProtocolEnum::HTTP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testCreateServiceTcp(): void
|
|
{
|
|
$this->createTest(ProtocolEnum::TCP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testCreateServiceUdp(): void
|
|
{
|
|
$this->createTest(ProtocolEnum::UDP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
|
|
public function createTest(ProtocolEnum $protocol): void
|
|
{
|
|
|
|
$commandFactory = Mockery::mock(FactoryInterface::class)
|
|
->expects('supports')
|
|
->andReturn(true)
|
|
->getMock();
|
|
|
|
Redis::expects('getCommandFactory')
|
|
->andReturn($commandFactory);
|
|
|
|
Redis::expects('scan')
|
|
->with(0, ['MATCH' => 'traefik/' . $protocol->getValue() . '/services/foo/*'])
|
|
->andReturn([0, []]);
|
|
|
|
Redis::expects('set')
|
|
->with('traefik/' . $protocol->getValue() . '/services/foo/loadbalancer/servers/server1/url', 'http://foo.localhost:80')
|
|
->once()
|
|
->andReturn(true);
|
|
|
|
RedisClient::createOrReplace('foo', [
|
|
'loadbalancer' => [
|
|
'servers' => [
|
|
'server1' => [
|
|
'url' => 'http://foo.localhost:80',
|
|
],
|
|
],
|
|
],
|
|
], EntityEnum::SERVICE, $protocol);
|
|
}
|
|
|
|
public function testGetOneHttp(): void
|
|
{
|
|
$this->runGet(ProtocolEnum::HTTP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testGetOneTcp(): void
|
|
{
|
|
$this->runGet(ProtocolEnum::TCP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testGetOneUdp(): void
|
|
{
|
|
$this->runGet(ProtocolEnum::UDP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
private function runGet(ProtocolEnum $protocol): void
|
|
{
|
|
$commandFactory = Mockery::mock(FactoryInterface::class)
|
|
->expects('supports')
|
|
->andReturn(true)
|
|
->getMock();
|
|
|
|
Redis::expects('getCommandFactory')
|
|
->andReturn($commandFactory);
|
|
|
|
Redis::expects('scan')
|
|
->with(0, ['MATCH' => 'traefik/' . $protocol->getValue() . '/services/foo/*'])
|
|
->andReturn([0, [
|
|
'traefik/' . $protocol->getValue() . '/services/foo/loadbalancer/servers/server1/url',
|
|
]]);
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocol->getValue() . '/services/foo/loadbalancer/servers/server1/url')
|
|
->andReturn('http://foo.localhost:80');
|
|
|
|
$services = RedisClient::getService('foo', $protocol);
|
|
|
|
$this->assertIsArray($services);
|
|
$this->assertArrayHasKey('loadbalancer', $services);
|
|
$this->assertArrayHasKey('servers', $services['loadbalancer']);
|
|
$this->assertArrayHasKey('server1', $services['loadbalancer']['servers']);
|
|
$this->assertEquals('http://foo.localhost:80', $services['loadbalancer']['servers']['server1']['url']);
|
|
}
|
|
|
|
public function testGetAllHttp()
|
|
{
|
|
$this->runGetAll(ProtocolEnum::HTTP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
private function runGetAll(ProtocolEnum $protocol): void
|
|
{
|
|
$commandFactory = Mockery::mock(FactoryInterface::class)
|
|
->expects('supports')
|
|
->andReturn(true)
|
|
->getMock();
|
|
|
|
Redis::expects('getCommandFactory')
|
|
->andReturn($commandFactory);
|
|
|
|
Redis::expects('scan')
|
|
->with(0, ['MATCH' => 'traefik/' . $protocol->getValue() . '/services/*'])
|
|
->andReturn([0, [
|
|
'traefik/' . $protocol->getValue() . '/services/foo/loadbalancer/servers/server1/url',
|
|
'traefik/' . $protocol->getValue() . '/services/bar/loadbalancer/servers/server2/url',
|
|
]]);
|
|
|
|
$services = RedisClient::getAllServices($protocol);
|
|
|
|
$this->assertIsArray($services);
|
|
$this->assertCount(2, $services);
|
|
}
|
|
} |