| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace ShlinkioTest\Shlink\Rest\Action\Visit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Laminas\Diactoros\Response\JsonResponse; | 
					
						
							|  |  |  | use Laminas\Diactoros\ServerRequestFactory; | 
					
						
							| 
									
										
										
										
											2023-02-09 20:42:18 +01:00
										 |  |  | use PHPUnit\Framework\Attributes\Test; | 
					
						
							| 
									
										
										
										
											2022-10-23 22:23:08 +02:00
										 |  |  | use PHPUnit\Framework\MockObject\MockObject; | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							|  |  |  | use Shlinkio\Shlink\Core\Visit\Model\VisitsStats; | 
					
						
							|  |  |  | use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface; | 
					
						
							|  |  |  | use Shlinkio\Shlink\Rest\Action\Visit\GlobalVisitsAction; | 
					
						
							| 
									
										
										
										
											2021-01-04 11:27:55 +01:00
										 |  |  | use Shlinkio\Shlink\Rest\Entity\ApiKey; | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class GlobalVisitsActionTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private GlobalVisitsAction $action; | 
					
						
							| 
									
										
										
										
											2022-10-24 19:53:13 +02:00
										 |  |  |     private MockObject & VisitsStatsHelperInterface $helper; | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-11 12:02:49 +02:00
										 |  |  |     protected function setUp(): void | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-23 22:23:08 +02:00
										 |  |  |         $this->helper = $this->createMock(VisitsStatsHelperInterface::class); | 
					
						
							|  |  |  |         $this->action = new GlobalVisitsAction($this->helper); | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-09 20:42:18 +01:00
										 |  |  |     #[Test]
 | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  |     public function statsAreReturnedFromHelper(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-14 09:59:35 +01:00
										 |  |  |         $apiKey = ApiKey::create(); | 
					
						
							| 
									
										
										
										
											2021-02-08 22:44:58 +01:00
										 |  |  |         $stats = new VisitsStats(5, 3); | 
					
						
							| 
									
										
										
										
											2022-10-23 22:23:08 +02:00
										 |  |  |         $this->helper->expects($this->once())->method('getVisitsStats')->with($apiKey)->willReturn($stats); | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var JsonResponse $resp */ | 
					
						
							| 
									
										
										
										
											2021-01-04 11:27:55 +01:00
										 |  |  |         $resp = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, $apiKey)); | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  |         $payload = $resp->getPayload(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 00:35:14 +02:00
										 |  |  |         self::assertEquals($payload, ['visits' => $stats]); | 
					
						
							| 
									
										
										
										
											2020-05-01 11:57:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |