ListShortUrlsTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioApiTest\Shlink\Rest\Action;
  4. use Cake\Chronos\Chronos;
  5. use GuzzleHttp\RequestOptions;
  6. use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
  7. use function count;
  8. class ListShortUrlsTest extends ApiTestCase
  9. {
  10. private const SHORT_URL_SHLINK_WITH_TITLE = [
  11. 'shortCode' => 'abc123',
  12. 'shortUrl' => 'http://doma.in/abc123',
  13. 'longUrl' => 'https://shlink.io',
  14. 'dateCreated' => '2018-05-01T00:00:00+00:00',
  15. 'visitsCount' => 3,
  16. 'tags' => ['foo'],
  17. 'meta' => [
  18. 'validSince' => null,
  19. 'validUntil' => null,
  20. 'maxVisits' => null,
  21. ],
  22. 'domain' => null,
  23. 'title' => 'My cool title',
  24. ];
  25. private const SHORT_URL_DOCS = [
  26. 'shortCode' => 'ghi789',
  27. 'shortUrl' => 'http://doma.in/ghi789',
  28. 'longUrl' => 'https://shlink.io/documentation/',
  29. 'dateCreated' => '2018-05-01T00:00:00+00:00',
  30. 'visitsCount' => 2,
  31. 'tags' => [],
  32. 'meta' => [
  33. 'validSince' => null,
  34. 'validUntil' => null,
  35. 'maxVisits' => null,
  36. ],
  37. 'domain' => null,
  38. 'title' => null,
  39. ];
  40. private const SHORT_URL_CUSTOM_SLUG_AND_DOMAIN = [
  41. 'shortCode' => 'custom-with-domain',
  42. 'shortUrl' => 'http://some-domain.com/custom-with-domain',
  43. 'longUrl' => 'https://google.com',
  44. 'dateCreated' => '2018-10-20T00:00:00+00:00',
  45. 'visitsCount' => 0,
  46. 'tags' => [],
  47. 'meta' => [
  48. 'validSince' => null,
  49. 'validUntil' => null,
  50. 'maxVisits' => null,
  51. ],
  52. 'domain' => 'some-domain.com',
  53. 'title' => null,
  54. ];
  55. private const SHORT_URL_META = [
  56. 'shortCode' => 'def456',
  57. 'shortUrl' => 'http://doma.in/def456',
  58. 'longUrl' =>
  59. 'https://blog.alejandrocelaya.com/2017/12/09'
  60. . '/acmailer-7-0-the-most-important-release-in-a-long-time/',
  61. 'dateCreated' => '2019-01-01T00:00:10+00:00',
  62. 'visitsCount' => 2,
  63. 'tags' => ['bar', 'foo'],
  64. 'meta' => [
  65. 'validSince' => '2020-05-01T00:00:00+00:00',
  66. 'validUntil' => null,
  67. 'maxVisits' => null,
  68. ],
  69. 'domain' => null,
  70. 'title' => null,
  71. ];
  72. private const SHORT_URL_CUSTOM_SLUG = [
  73. 'shortCode' => 'custom',
  74. 'shortUrl' => 'http://doma.in/custom',
  75. 'longUrl' => 'https://shlink.io',
  76. 'dateCreated' => '2019-01-01T00:00:20+00:00',
  77. 'visitsCount' => 0,
  78. 'tags' => [],
  79. 'meta' => [
  80. 'validSince' => null,
  81. 'validUntil' => null,
  82. 'maxVisits' => 2,
  83. ],
  84. 'domain' => null,
  85. 'title' => null,
  86. ];
  87. private const SHORT_URL_CUSTOM_DOMAIN = [
  88. 'shortCode' => 'ghi789',
  89. 'shortUrl' => 'http://example.com/ghi789',
  90. 'longUrl' =>
  91. 'https://blog.alejandrocelaya.com/2019/04/27'
  92. . '/considerations-to-properly-use-open-source-software-projects/',
  93. 'dateCreated' => '2019-01-01T00:00:30+00:00',
  94. 'visitsCount' => 0,
  95. 'tags' => ['foo'],
  96. 'meta' => [
  97. 'validSince' => null,
  98. 'validUntil' => null,
  99. 'maxVisits' => null,
  100. ],
  101. 'domain' => 'example.com',
  102. 'title' => null,
  103. ];
  104. /**
  105. * @test
  106. * @dataProvider provideFilteredLists
  107. */
  108. public function shortUrlsAreProperlyListed(array $query, array $expectedShortUrls, string $apiKey): void
  109. {
  110. $resp = $this->callApiWithKey(self::METHOD_GET, '/short-urls', [RequestOptions::QUERY => $query], $apiKey);
  111. $respPayload = $this->getJsonResponsePayload($resp);
  112. self::assertEquals(self::STATUS_OK, $resp->getStatusCode());
  113. self::assertEquals([
  114. 'shortUrls' => [
  115. 'data' => $expectedShortUrls,
  116. 'pagination' => $this->buildPagination(count($expectedShortUrls)),
  117. ],
  118. ], $respPayload);
  119. }
  120. public function provideFilteredLists(): iterable
  121. {
  122. yield [[], [
  123. self::SHORT_URL_SHLINK_WITH_TITLE,
  124. self::SHORT_URL_DOCS,
  125. self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
  126. self::SHORT_URL_META,
  127. self::SHORT_URL_CUSTOM_SLUG,
  128. self::SHORT_URL_CUSTOM_DOMAIN,
  129. ], 'valid_api_key'];
  130. yield [['orderBy' => 'shortCode'], [
  131. self::SHORT_URL_SHLINK_WITH_TITLE,
  132. self::SHORT_URL_CUSTOM_SLUG,
  133. self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
  134. self::SHORT_URL_META,
  135. self::SHORT_URL_DOCS,
  136. self::SHORT_URL_CUSTOM_DOMAIN,
  137. ], 'valid_api_key'];
  138. yield [['orderBy' => ['shortCode' => 'DESC']], [ // Deprecated
  139. self::SHORT_URL_DOCS,
  140. self::SHORT_URL_CUSTOM_DOMAIN,
  141. self::SHORT_URL_META,
  142. self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
  143. self::SHORT_URL_CUSTOM_SLUG,
  144. self::SHORT_URL_SHLINK_WITH_TITLE,
  145. ], 'valid_api_key'];
  146. yield [['orderBy' => 'shortCode-DESC'], [
  147. self::SHORT_URL_DOCS,
  148. self::SHORT_URL_CUSTOM_DOMAIN,
  149. self::SHORT_URL_META,
  150. self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
  151. self::SHORT_URL_CUSTOM_SLUG,
  152. self::SHORT_URL_SHLINK_WITH_TITLE,
  153. ], 'valid_api_key'];
  154. yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [
  155. self::SHORT_URL_META,
  156. self::SHORT_URL_CUSTOM_SLUG,
  157. self::SHORT_URL_CUSTOM_DOMAIN,
  158. ], 'valid_api_key'];
  159. yield [['endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
  160. self::SHORT_URL_SHLINK_WITH_TITLE,
  161. self::SHORT_URL_DOCS,
  162. self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
  163. ], 'valid_api_key'];
  164. yield [['tags' => ['foo']], [
  165. self::SHORT_URL_SHLINK_WITH_TITLE,
  166. self::SHORT_URL_META,
  167. self::SHORT_URL_CUSTOM_DOMAIN,
  168. ], 'valid_api_key'];
  169. yield [['tags' => ['bar']], [
  170. self::SHORT_URL_META,
  171. ], 'valid_api_key'];
  172. yield [['tags' => ['foo'], 'endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
  173. self::SHORT_URL_SHLINK_WITH_TITLE,
  174. ], 'valid_api_key'];
  175. yield [['searchTerm' => 'alejandro'], [
  176. self::SHORT_URL_META,
  177. self::SHORT_URL_CUSTOM_DOMAIN,
  178. ], 'valid_api_key'];
  179. yield [['searchTerm' => 'cool'], [
  180. self::SHORT_URL_SHLINK_WITH_TITLE,
  181. ], 'valid_api_key'];
  182. yield [['searchTerm' => 'example.com'], [
  183. self::SHORT_URL_CUSTOM_DOMAIN,
  184. ], 'valid_api_key'];
  185. yield [[], [
  186. self::SHORT_URL_SHLINK_WITH_TITLE,
  187. self::SHORT_URL_META,
  188. self::SHORT_URL_CUSTOM_SLUG,
  189. ], 'author_api_key'];
  190. yield [[], [
  191. self::SHORT_URL_CUSTOM_DOMAIN,
  192. ], 'domain_api_key'];
  193. }
  194. private function buildPagination(int $itemsCount): array
  195. {
  196. return [
  197. 'currentPage' => 1,
  198. 'pagesCount' => 1,
  199. 'itemsPerPage' => 10,
  200. 'itemsInCurrentPage' => $itemsCount,
  201. 'totalItems' => $itemsCount,
  202. ];
  203. }
  204. }