From ed6a33607d0c7b1d2bc35ea68c86788601fa6297 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Tue, 6 May 2025 12:58:05 -0400 Subject: [PATCH] that coulda been bad --- tests/Traefik/RedisClientRoutersTest.php | 113 +++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/Traefik/RedisClientRoutersTest.php diff --git a/tests/Traefik/RedisClientRoutersTest.php b/tests/Traefik/RedisClientRoutersTest.php new file mode 100644 index 0000000..11dd0b9 --- /dev/null +++ b/tests/Traefik/RedisClientRoutersTest.php @@ -0,0 +1,113 @@ +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); + } + + 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); + } +} \ No newline at end of file