From 1a54e269e2797a2e4f3da8a67bbb4f4a4436469c Mon Sep 17 00:00:00 2001 From: Aaqibhafeez Khan Date: Sat, 24 Oct 2026 20:26:50 +0530 Subject: [PATCH] test: cover StatisticsService caching and empty user stats --- tests/Service/StatisticsServiceTest.php | 134 ++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/Service/StatisticsServiceTest.php diff --git a/tests/Service/StatisticsServiceTest.php b/tests/Service/StatisticsServiceTest.php new file mode 100644 index 0000000..f6d6c4f --- /dev/null +++ b/tests/Service/StatisticsServiceTest.php @@ -0,0 +1,134 @@ +routeRepository = $this->createMock(ClimbingRouteRepository::class); + $this->userRepository = $this->createMock(UserRepository::class); + $this->locationRepository = $this->createMock(LocationRepository::class); + $this->completedRouteRepository = $this->createMock(CompletedRouteRepository::class); + $this->cache = new ArrayAdapter(); + + $this->service = new StatisticsService( + $this->routeRepository, + $this->userRepository, + $this->locationRepository, + $this->completedRouteRepository, + $this->cache, + $this->cache, + ); + } + + public function testGetGeneralStatsReturnsRepositoryTotals(): void + { + $this->routeRepository->expects($this->once()) + ->method('count') + ->with([]) + ->willReturn(12); + $this->userRepository->expects($this->once()) + ->method('count') + ->with([]) + ->willReturn(8); + $this->locationRepository->expects($this->once()) + ->method('count') + ->with([]) + ->willReturn(5); + + $this->assertSame([ + 'totalRoutes' => 12, + 'totalUsers' => 8, + 'totalLocations' => 5, + ], $this->service->getGeneralStats()); + } + + public function testGetGeneralStatsUsesCacheOnSubsequentCalls(): void + { + $this->routeRepository->expects($this->once()) + ->method('count') + ->with([]) + ->willReturn(12); + $this->userRepository->expects($this->once()) + ->method('count') + ->with([]) + ->willReturn(8); + $this->locationRepository->expects($this->once()) + ->method('count') + ->with([]) + ->willReturn(5); + + $expected = [ + 'totalRoutes' => 12, + 'totalUsers' => 8, + 'totalLocations' => 5, + ]; + + $this->assertSame($expected, $this->service->getGeneralStats()); + $this->assertSame($expected, $this->service->getGeneralStats()); + } + + public function testInvalidateGeneralStatsRefreshesCachedTotals(): void + { + $this->routeRepository->expects($this->exactly(2)) + ->method('count') + ->with([]) + ->willReturnOnConsecutiveCalls(12, 13); + $this->userRepository->expects($this->exactly(2)) + ->method('count') + ->with([]) + ->willReturnOnConsecutiveCalls(8, 9); + $this->locationRepository->expects($this->exactly(2)) + ->method('count') + ->with([]) + ->willReturnOnConsecutiveCalls(5, 6); + + $this->assertSame([ + 'totalRoutes' => 12, + 'totalUsers' => 8, + 'totalLocations' => 5, + ], $this->service->getGeneralStats()); + + $this->service->invalidateGeneralStats(); + + $this->assertSame([ + 'totalRoutes' => 13, + 'totalUsers' => 9, + 'totalLocations' => 6, + ], $this->service->getGeneralStats()); + } + + public function testGetUserStatsReturnsEmptyArrayWhenUserHasNoCompletedRoutes(): void + { + $user = $this->createMock(UserInterface::class); + + $this->completedRouteRepository->expects($this->once()) + ->method('findBy') + ->with(['user' => $user], ['completedAt' => 'DESC']) + ->willReturn([]); + + $this->assertSame([], $this->service->getUserStats($user)); + } +}