diff --git a/tests/Pest.php b/tests/Pest.php deleted file mode 100644 index 5949c61..0000000 --- a/tests/Pest.php +++ /dev/null @@ -1,45 +0,0 @@ -in('Feature'); - -/* -|-------------------------------------------------------------------------- -| Expectations -|-------------------------------------------------------------------------- -| -| When you're writing tests, you often need to check that values meet certain conditions. The -| "expect()" function gives you access to a set of "expectations" methods that you can use -| to assert different things. Of course, you may extend the Expectation API at any time. -| -*/ - -expect()->extend('toBeOne', function () { - return $this->toBe(1); -}); - -/* -|-------------------------------------------------------------------------- -| Functions -|-------------------------------------------------------------------------- -| -| While Pest is very powerful out-of-the-box, you may have some testing code specific to your -| project that you don't want to repeat in every file. Here you can also expose helpers as -| global functions to help you to reduce the number of lines of code in your test files. -| -*/ - -function something() -{ - // .. -} diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index cfb05b6..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,10 +0,0 @@ -redisFactory = Mockery::mock(RedisFactory::class); + $this->connection = Mockery::mock(Connection::class); + $this->redisFactory + ->shouldReceive('connection') + ->andReturn($this->connection); + $this->repository = new RedisRepository($this->redisFactory); +}); + +afterEach(function () { + Mockery::close(); +}); + +it('saves handled request and events to redis', function () { + $request = new HandledRequest('GET', '/test', 100, 200, 1622505600); + $eventCounter = new EventCounter(['some-event' => 5, 'another-event' => 10]); + $pipe = Mockery::spy(); + + $this + ->connection + ->shouldReceive('pipeline') + ->once() + ->with(Mockery::on(function ($callback) use ($pipe) { + $callback($pipe); + return true; + })) + ->andReturnNull(); + + $this->repository->save($request, $eventCounter); + + $key = "top-requests:$request->timestamp"; + $routeKey = "$request->method:$request->uri:data"; + + foreach ($eventCounter->get() as $event => $times) { + $pipe + ->shouldHaveReceived('hIncrBy') + ->with($key, "$routeKey:$event", $times) + ->once(); + } + + $pipe + ->shouldHaveReceived('hIncrBy') + ->with($key, "$routeKey:hits", 1) + ->once(); + + $pipe + ->shouldHaveReceived('hIncrBy') + ->with($key, "$routeKey:memory", $request->memory) + ->once(); + + $pipe + ->shouldHaveReceived('hIncrBy') + ->with($key, "$routeKey:duration", $request->duration) + ->once(); + + $pipe + ->shouldHaveReceived('expire') + ->with($key, 10) + ->once(); +}); + +it('fetches request summary from redis', function () { + $this + ->connection + ->shouldReceive('eval') + ->once() + ->andReturn(json_encode(['averageRequestPerSecond' => 1, 'averageMemoryUsage' => 2, 'averageDuration' => 3])); + + $summary = $this->repository->getRequestSummary(); + + expect($summary)->toBeInstanceOf(RequestSummary::class); +}); + +it('fetches database summary from redis', function () { + $this + ->connection + ->shouldReceive('eval') + ->once() + ->andReturn(json_encode(['averageQueryPerSecond' => 1, 'averageQueryDuration' => 2])); + + $summary = $this->repository->getDatabaseSummary(); + + expect($summary)->toBeInstanceOf(DatabaseSummary::class); +}); + +it('fetches cache summary from redis', function () { + $this + ->connection + ->shouldReceive('eval') + ->once() + ->andReturn(json_encode([ + 'averageHitPerSecond' => 1, 'averageMissPerSecond' => 2, 'averageWritePerSecond' => 3 + ])); + + $summary = $this->repository->getCacheSummary(); + + expect($summary)->toBeInstanceOf(CacheSummary::class); +}); + +it('fetches top routes from redis', function () { + $this + ->connection + ->shouldReceive('eval') + ->once() + ->andReturn(json_encode([ + [ + 'uri' => '', 'method' => '', 'averageRequestPerSecond' => 1, 'averageMemoryUsage' => 1, + 'averageDuration' => 1 + ] + ])); + + $routes = $this->repository->getTopRoutes(); + + expect($routes)->toBeInstanceOf(RouteCollection::class); +});