From 67bafbe44eb9aa65f609bc6fca705c9031f4e11d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 28 Feb 2024 08:53:35 +0100 Subject: [PATCH] Add API test for redirect rules list --- .../test-api/Action/ListRedirectRulesTest.php | 104 ++++++++++++++++++ .../Fixtures/ShortUrlRedirectRulesFixture.php | 53 ++++----- 2 files changed, 131 insertions(+), 26 deletions(-) create mode 100644 module/Rest/test-api/Action/ListRedirectRulesTest.php diff --git a/module/Rest/test-api/Action/ListRedirectRulesTest.php b/module/Rest/test-api/Action/ListRedirectRulesTest.php new file mode 100644 index 00000000..5115d2d2 --- /dev/null +++ b/module/Rest/test-api/Action/ListRedirectRulesTest.php @@ -0,0 +1,104 @@ + 'language-en', + 'type' => 'language', + 'matchKey' => null, + 'matchValue' => 'en', + ]; + private const QUERY_FOO_BAR_CONDITION = [ + 'name' => 'query-foo-bar', + 'type' => 'query', + 'matchKey' => 'foo', + 'matchValue' => 'bar', + ]; + + #[Test] + public function errorIsReturnedWhenInvalidUrlIsFetched(): void + { + $response = $this->callApiWithKey(self::METHOD_GET, '/short-urls/invalid/redirect-rules'); + $payload = $this->getJsonResponsePayload($response); + + self::assertEquals(404, $response->getStatusCode()); + self::assertEquals(404, $payload['status']); + self::assertEquals('invalid', $payload['shortCode']); + self::assertEquals('No URL found with short code "invalid"', $payload['detail']); + self::assertEquals('Short URL not found', $payload['title']); + self::assertEquals('https://shlink.io/api/error/short-url-not-found', $payload['type']); + } + + #[Test] + #[TestWith(['abc123', []])] + #[TestWith(['def456', [ + [ + 'longUrl' => 'https://example.com/english-and-foo-query', + 'priority' => 1, + 'conditions' => [ + self::LANGUAGE_EN_CONDITION, + self::QUERY_FOO_BAR_CONDITION, + ], + ], + [ + 'longUrl' => 'https://example.com/multiple-query-params', + 'priority' => 2, + 'conditions' => [ + self::QUERY_FOO_BAR_CONDITION, + [ + 'name' => 'query-hello-world', + 'type' => 'query', + 'matchKey' => 'hello', + 'matchValue' => 'world', + ], + ], + ], + [ + 'longUrl' => 'https://example.com/only-english', + 'priority' => 3, + 'conditions' => [self::LANGUAGE_EN_CONDITION], + ], + [ + 'longUrl' => 'https://blog.alejandrocelaya.com/android', + 'priority' => 4, + 'conditions' => [ + [ + 'name' => 'device-android', + 'type' => 'device', + 'matchKey' => null, + 'matchValue' => 'android', + ], + ], + ], + [ + 'longUrl' => 'https://blog.alejandrocelaya.com/ios', + 'priority' => 5, + 'conditions' => [ + [ + 'name' => 'device-ios', + 'type' => 'device', + 'matchKey' => null, + 'matchValue' => 'ios', + ], + ], + ], + ]])] + public function returnsListOfRulesForShortUrl(string $shortCode, array $expectedRules): void + { + $response = $this->callApiWithKey(self::METHOD_GET, sprintf('/short-urls/%s/redirect-rules', $shortCode)); + $payload = $this->getJsonResponsePayload($response); + + self::assertEquals(200, $response->getStatusCode()); + self::assertEquals($expectedRules, $payload['redirectRules']); + } +} diff --git a/module/Rest/test-api/Fixtures/ShortUrlRedirectRulesFixture.php b/module/Rest/test-api/Fixtures/ShortUrlRedirectRulesFixture.php index ab0e8dce..7607724b 100644 --- a/module/Rest/test-api/Fixtures/ShortUrlRedirectRulesFixture.php +++ b/module/Rest/test-api/Fixtures/ShortUrlRedirectRulesFixture.php @@ -40,43 +40,44 @@ class ShortUrlRedirectRulesFixture extends AbstractFixture implements DependentF $iosCondition = RedirectCondition::forDevice(DeviceType::IOS); $manager->persist($iosCondition); - $englishAndFooQueryRule = new ShortUrlRedirectRule( - $defShortUrl, - 1, - 'https://example.com/english-and-foo-query', - new ArrayCollection([$englishCondition, $fooQueryCondition]), - ); - $manager->persist($englishAndFooQueryRule); - + // Create rules disordered to make sure the order by priority works $multipleQueryParamsRule = new ShortUrlRedirectRule( - $defShortUrl, - 2, - 'https://example.com/multiple-query-params', - new ArrayCollection([$helloQueryCondition, $fooQueryCondition]), + shortUrl: $defShortUrl, + priority: 2, + longUrl: 'https://example.com/multiple-query-params', + conditions: new ArrayCollection([$helloQueryCondition, $fooQueryCondition]), ); $manager->persist($multipleQueryParamsRule); - $onlyEnglishRule = new ShortUrlRedirectRule( - $defShortUrl, - 3, - 'https://example.com/only-english', - new ArrayCollection([$englishCondition]), + $englishAndFooQueryRule = new ShortUrlRedirectRule( + shortUrl: $defShortUrl, + priority: 1, + longUrl: 'https://example.com/english-and-foo-query', + conditions: new ArrayCollection([$englishCondition, $fooQueryCondition]), ); - $manager->persist($onlyEnglishRule); + $manager->persist($englishAndFooQueryRule); $androidRule = new ShortUrlRedirectRule( - $defShortUrl, - 4, - 'https://blog.alejandrocelaya.com/android', - new ArrayCollection([$androidCondition]), + shortUrl: $defShortUrl, + priority: 4, + longUrl: 'https://blog.alejandrocelaya.com/android', + conditions: new ArrayCollection([$androidCondition]), ); $manager->persist($androidRule); + $onlyEnglishRule = new ShortUrlRedirectRule( + shortUrl: $defShortUrl, + priority: 3, + longUrl: 'https://example.com/only-english', + conditions: new ArrayCollection([$englishCondition]), + ); + $manager->persist($onlyEnglishRule); + $iosRule = new ShortUrlRedirectRule( - $defShortUrl, - 5, - 'https://blog.alejandrocelaya.com/ios', - new ArrayCollection([$iosCondition]), + shortUrl: $defShortUrl, + priority: 5, + longUrl: 'https://blog.alejandrocelaya.com/ios', + conditions: new ArrayCollection([$iosCondition]), ); $manager->persist($iosRule);