2023-05-18 10:09:44 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
|
|
|
|
|
|
|
class DeleteOrphanVisitsTest extends ApiTestCase
|
|
|
|
{
|
|
|
|
#[Test]
|
2023-05-31 10:22:40 +03:00
|
|
|
public function deletesOrphanVisitsWithoutAffectingTheRest(): void
|
2023-05-18 10:09:44 +03:00
|
|
|
{
|
|
|
|
self::assertEquals(7, $this->getTotalVisits());
|
|
|
|
self::assertEquals(3, $this->getOrphanVisits());
|
|
|
|
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, '/visits/orphan');
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
|
|
|
self::assertEquals(3, $payload['deletedVisits']);
|
|
|
|
self::assertEquals(7, $this->getTotalVisits()); // This verifies that regular visits have not been affected
|
|
|
|
self::assertEquals(0, $this->getOrphanVisits());
|
|
|
|
}
|
|
|
|
|
2023-05-31 10:22:40 +03:00
|
|
|
#[Test]
|
|
|
|
public function doesNotDeleteOrphanVisitsForRestrictedApiKey(): void
|
|
|
|
{
|
|
|
|
self::assertEquals(7, $this->getTotalVisits());
|
|
|
|
self::assertEquals(3, $this->getOrphanVisits());
|
|
|
|
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, '/visits/orphan', apiKey: 'no_orphans_api_key');
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
|
|
|
self::assertEquals(0, $payload['deletedVisits']);
|
|
|
|
self::assertEquals(7, $this->getTotalVisits()); // This verifies that regular visits have not been affected
|
|
|
|
self::assertEquals(3, $this->getOrphanVisits()); // This verifies that all orphan visits still exist
|
|
|
|
}
|
|
|
|
|
2023-05-18 10:09:44 +03:00
|
|
|
private function getTotalVisits(): int
|
|
|
|
{
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/visits/non-orphan');
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
return $payload['visits']['pagination']['totalItems'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getOrphanVisits(): int
|
|
|
|
{
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_GET, '/visits/orphan');
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
return $payload['visits']['pagination']['totalItems'];
|
|
|
|
}
|
|
|
|
}
|