ShortUrlsFixture.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
  4. use Cake\Chronos\Chronos;
  5. use Doctrine\Common\DataFixtures\AbstractFixture;
  6. use Doctrine\Common\DataFixtures\DependentFixtureInterface;
  7. use Doctrine\Persistence\ObjectManager;
  8. use ReflectionObject;
  9. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  10. use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
  11. use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
  12. use Shlinkio\Shlink\Rest\Entity\ApiKey;
  13. class ShortUrlsFixture extends AbstractFixture implements DependentFixtureInterface
  14. {
  15. public function getDependencies(): array
  16. {
  17. return [ApiKeyFixture::class, TagsFixture::class];
  18. }
  19. public function load(ObjectManager $manager): void
  20. {
  21. $relationResolver = new PersistenceShortUrlRelationResolver($manager);
  22. /** @var ApiKey $authorApiKey */
  23. $authorApiKey = $this->getReference('author_api_key');
  24. $abcShortUrl = $this->setShortUrlDate(
  25. ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
  26. 'customSlug' => 'abc123',
  27. 'apiKey' => $authorApiKey,
  28. 'longUrl' => 'https://shlink.io',
  29. 'tags' => ['foo'],
  30. 'title' => 'My cool title',
  31. ]), $relationResolver),
  32. '2018-05-01',
  33. );
  34. $manager->persist($abcShortUrl);
  35. $defShortUrl = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
  36. 'validSince' => Chronos::parse('2020-05-01'),
  37. 'customSlug' => 'def456',
  38. 'apiKey' => $authorApiKey,
  39. 'longUrl' =>
  40. 'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
  41. 'tags' => ['foo', 'bar'],
  42. ]), $relationResolver), '2019-01-01 00:00:10');
  43. $manager->persist($defShortUrl);
  44. $customShortUrl = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
  45. ['customSlug' => 'custom', 'maxVisits' => 2, 'apiKey' => $authorApiKey, 'longUrl' => 'https://shlink.io'],
  46. )), '2019-01-01 00:00:20');
  47. $manager->persist($customShortUrl);
  48. $ghiShortUrl = $this->setShortUrlDate(
  49. ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
  50. ['customSlug' => 'ghi789', 'longUrl' => 'https://shlink.io/documentation/'],
  51. )),
  52. '2018-05-01',
  53. );
  54. $manager->persist($ghiShortUrl);
  55. $withDomainDuplicatingShortCode = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
  56. 'domain' => 'example.com',
  57. 'customSlug' => 'ghi789',
  58. 'longUrl' => 'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-'
  59. . 'source-software-projects/',
  60. 'tags' => ['foo'],
  61. ]), $relationResolver), '2019-01-01 00:00:30');
  62. $manager->persist($withDomainDuplicatingShortCode);
  63. $withDomainAndSlugShortUrl = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
  64. ['domain' => 'some-domain.com', 'customSlug' => 'custom-with-domain', 'longUrl' => 'https://google.com'],
  65. )), '2018-10-20');
  66. $manager->persist($withDomainAndSlugShortUrl);
  67. $manager->flush();
  68. $this->addReference('abc123_short_url', $abcShortUrl);
  69. $this->addReference('def456_short_url', $defShortUrl);
  70. $this->addReference('ghi789_short_url', $ghiShortUrl);
  71. $this->addReference('example_short_url', $withDomainDuplicatingShortCode);
  72. }
  73. private function setShortUrlDate(ShortUrl $shortUrl, string $date): ShortUrl
  74. {
  75. $ref = new ReflectionObject($shortUrl);
  76. $dateProp = $ref->getProperty('dateCreated');
  77. $dateProp->setAccessible(true);
  78. $dateProp->setValue($shortUrl, Chronos::parse($date));
  79. return $shortUrl;
  80. }
  81. }