diff --git a/module/Rest/test-api/Action/OrphanVisitsTest.php b/module/Rest/test-api/Action/OrphanVisitsTest.php new file mode 100644 index 00000000..ea890f9f --- /dev/null +++ b/module/Rest/test-api/Action/OrphanVisitsTest.php @@ -0,0 +1,59 @@ + 'https://doma.in/foo', + 'date' => '2020-03-01T00:00:00+00:00', + 'userAgent' => 'shlink-tests-agent', + 'visitLocation' => null, + 'visitedUrl' => 'foo.com', + 'type' => 'invalid_short_url', + + ]; + private const REGULAR_NOT_FOUND = [ + 'referer' => 'https://doma.in/foo/bar', + 'date' => '2020-02-01T00:00:00+00:00', + 'userAgent' => 'shlink-tests-agent', + 'visitLocation' => null, + 'visitedUrl' => '', + 'type' => 'regular_404', + ]; + private const BASE_URL = [ + 'referer' => 'https://doma.in', + 'date' => '2020-01-01T00:00:00+00:00', + 'userAgent' => 'shlink-tests-agent', + 'visitLocation' => null, + 'visitedUrl' => '', + 'type' => 'base_url', + ]; + + /** + * @test + * @dataProvider provideQueries + */ + public function properVisitsAreReturnedBasedInQuery(array $query, int $expectedAmount, array $expectedVisits): void + { + $resp = $this->callApiWithKey(self::METHOD_GET, '/visits/orphan', [RequestOptions::QUERY => $query]); + $payload = $this->getJsonResponsePayload($resp); + $visits = $payload['visits']['data'] ?? []; + + self::assertEquals(3, $payload['visits']['pagination']['totalItems'] ?? -1); + self::assertCount($expectedAmount, $visits); + self::assertEquals($expectedVisits, $visits); + } + + public function provideQueries(): iterable + { + yield 'all data' => [[], 3, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND, self::BASE_URL]]; + yield 'limit items' => [['itemsPerPage' => 2], 2, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND]]; + yield 'limit items and page' => [['itemsPerPage' => 2, 'page' => 2], 1, [self::BASE_URL]]; + } +} diff --git a/module/Rest/test-api/Fixtures/VisitsFixture.php b/module/Rest/test-api/Fixtures/VisitsFixture.php index 9fb53ac1..412c79d5 100644 --- a/module/Rest/test-api/Fixtures/VisitsFixture.php +++ b/module/Rest/test-api/Fixtures/VisitsFixture.php @@ -4,9 +4,11 @@ declare(strict_types=1); namespace ShlinkioApiTest\Shlink\Rest\Fixtures; +use Cake\Chronos\Chronos; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Doctrine\Persistence\ObjectManager; +use ReflectionObject; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Model\Visitor; @@ -47,14 +49,29 @@ class VisitsFixture extends AbstractFixture implements DependentFixtureInterface Visit::forValidShortUrl($ghiShortUrl, new Visitor('shlink-tests-agent', 'https://app.shlink.io', '', '')), ); - $manager->persist(Visit::forBasePath(new Visitor('shlink-tests-agent', 'https://doma.in', '1.2.3.4', ''))); - $manager->persist( + $manager->persist($this->setVisitDate( + Visit::forBasePath(new Visitor('shlink-tests-agent', 'https://doma.in', '1.2.3.4', '')), + '2020-01-01', + )); + $manager->persist($this->setVisitDate( Visit::forRegularNotFound(new Visitor('shlink-tests-agent', 'https://doma.in/foo/bar', '1.2.3.4', '')), - ); - $manager->persist( - Visit::forInvalidShortUrl(new Visitor('shlink-tests-agent', 'https://doma.in/foo', '1.2.3.4', '')), - ); + '2020-02-01', + )); + $manager->persist($this->setVisitDate( + Visit::forInvalidShortUrl(new Visitor('shlink-tests-agent', 'https://doma.in/foo', '1.2.3.4', 'foo.com')), + '2020-03-01', + )); $manager->flush(); } + + private function setVisitDate(Visit $visit, string $date): Visit + { + $ref = new ReflectionObject($visit); + $dateProp = $ref->getProperty('date'); + $dateProp->setAccessible(true); + $dateProp->setValue($visit, Chronos::parse($date)); + + return $visit; + } } diff --git a/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php b/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php index 36273d09..9fec7e1f 100644 --- a/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php +++ b/module/Rest/test/Action/Visit/OrphanVisitsActionTest.php @@ -11,7 +11,6 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; -use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Core\Entity\Visit;