You've already forked Traefik-Redis-Api
160 lines
4.4 KiB
PHP
160 lines
4.4 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\Facades\RedisClient;
|
|
use Siteworxpro\App\Traefik\EntityEnum;
|
|
use Siteworxpro\App\Traefik\ProtocolEnum;
|
|
use Siteworxpro\Tests\Unit;
|
|
|
|
class RedisClientRoutersTest extends Unit
|
|
{
|
|
private 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() . '/routers/foo/*'])
|
|
->andReturn([0, []]);
|
|
|
|
Redis::expects('set')
|
|
->with('traefik/' . $protocol->getValue() . '/routers/foo/rule', 'Host(`foo.localhost`)')
|
|
->once()
|
|
->andReturn(true);
|
|
|
|
Redis::expects('set')
|
|
->with('traefik/' . $protocol->getValue() . '/routers/foo/service', 'foo')
|
|
->once()
|
|
->andReturn(true);
|
|
|
|
RedisClient::createOrReplace('foo', [
|
|
'rule' => 'Host(`foo.localhost`)',
|
|
'service' => 'foo',
|
|
], EntityEnum::ROUTER, $protocol);
|
|
}
|
|
|
|
public function testCreateRouterHttp()
|
|
{
|
|
$this->createTest(ProtocolEnum::HTTP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testCreateRouterTcp()
|
|
{
|
|
$this->createTest(ProtocolEnum::TCP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testCreateRouterUcp()
|
|
{
|
|
$this->createTest(ProtocolEnum::UDP);
|
|
$this->assertTrue(true);
|
|
}
|
|
|
|
public function testGetOneHttp(): void
|
|
{
|
|
$this->getOneTest(ProtocolEnum::HTTP);
|
|
}
|
|
|
|
public function testGetOneTcp(): void
|
|
{
|
|
$this->getOneTest(ProtocolEnum::TCP);
|
|
}
|
|
|
|
public function testGetOneUdp(): void
|
|
{
|
|
$this->getOneTest(ProtocolEnum::UDP);
|
|
}
|
|
|
|
private function getOneTest(ProtocolEnum $protocolEnum): void
|
|
{
|
|
$commandFactory = Mockery::mock(FactoryInterface::class)
|
|
->expects('supports')
|
|
->andReturn(true)
|
|
->getMock();
|
|
|
|
Redis::expects('getCommandFactory')
|
|
->andReturn($commandFactory);
|
|
|
|
Redis::expects('scan')
|
|
->with(0, ['MATCH' => 'traefik/' . $protocolEnum->getValue() . '/routers/foo/*'])
|
|
->andReturn([0, [
|
|
'traefik/' . $protocolEnum->getValue() . '/routers/foo/rule',
|
|
'traefik/' . $protocolEnum->getValue() . '/routers/foo/service',
|
|
]]);
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocolEnum->getValue() . '/routers/foo/rule')
|
|
->andReturn('Host(`foo.localhost`)');
|
|
|
|
Redis::expects('get')
|
|
->with('traefik/' . $protocolEnum->getValue() . '/routers/foo/service')
|
|
->andReturn('foo');
|
|
|
|
$routers = RedisClient::getRouter('foo', $protocolEnum);
|
|
|
|
$this->assertIsArray($routers);
|
|
|
|
$this->assertCount(2, $routers);
|
|
|
|
$this->assertEquals([
|
|
'rule' => 'Host(`foo.localhost`)',
|
|
'service' => 'foo',
|
|
], $routers);
|
|
}
|
|
|
|
private function allTest(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() . '/routers/*'])
|
|
->andReturn([0, [
|
|
'traefik/' . $protocol->getValue() . '/routers/foo/url/0',
|
|
'traefik/' . $protocol->getValue() . '/routers/foo/healthcheck/path',
|
|
'traefik/' . $protocol->getValue() . '/routers/bar/url/0',
|
|
]]);
|
|
|
|
$routers = RedisClient::getAllRouters($protocol);
|
|
|
|
$this->assertIsArray($routers);
|
|
$this->assertCount(2, $routers);
|
|
$this->assertEquals([
|
|
'bar',
|
|
'foo'
|
|
], $routers);
|
|
}
|
|
|
|
public function testAllRoutersHttp()
|
|
{
|
|
$this->allTest(ProtocolEnum::HTTP);
|
|
}
|
|
|
|
public function testAllRoutersTcp()
|
|
{
|
|
$this->allTest(ProtocolEnum::TCP);
|
|
}
|
|
|
|
public function testAllRoutersUdp()
|
|
{
|
|
$this->allTest(ProtocolEnum::UDP);
|
|
}
|
|
}
|