shlink/module/Rest/test-api/Fixtures/ShortUrlsFixture.php

106 lines
4.1 KiB
PHP
Raw Normal View History

2019-01-27 14:14:18 +03:00
<?php
2019-10-05 18:26:10 +03:00
2019-01-27 14:14:18 +03:00
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;
2019-01-27 14:35:00 +03:00
use ReflectionObject;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2019-01-27 14:14:18 +03:00
class ShortUrlsFixture extends AbstractFixture implements DependentFixtureInterface
2019-01-27 14:14:18 +03:00
{
public function getDependencies(): array
{
return [ApiKeyFixture::class, TagsFixture::class];
}
2019-01-27 14:14:18 +03:00
public function load(ObjectManager $manager): void
{
2022-10-24 21:25:06 +03:00
$relationResolver = new PersistenceShortUrlRelationResolver($manager); // @phpstan-ignore-line
/** @var ApiKey $authorApiKey */
$authorApiKey = $this->getReference('author_api_key');
$abcShortUrl = $this->setShortUrlDate(
ShortUrl::create(ShortUrlCreation::fromRawData([
'customSlug' => 'abc123',
'apiKey' => $authorApiKey,
'longUrl' => 'https://shlink.io',
'tags' => ['foo'],
'title' => 'My cool title',
2021-05-22 08:35:47 +03:00
'crawlable' => true,
'maxVisits' => 2,
]), $relationResolver),
2020-01-01 22:48:31 +03:00
'2018-05-01',
);
2019-01-27 14:14:18 +03:00
$manager->persist($abcShortUrl);
$defShortUrl = $this->setShortUrlDate(ShortUrl::create(ShortUrlCreation::fromRawData([
'validSince' => Chronos::parse('2020-05-01'),
'customSlug' => 'def456',
'apiKey' => $authorApiKey,
'longUrl' =>
'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
'tags' => ['foo', 'bar'],
]), $relationResolver), '2019-01-01 00:00:10');
2019-01-27 14:14:18 +03:00
$manager->persist($defShortUrl);
$customShortUrl = $this->setShortUrlDate(ShortUrl::create(ShortUrlCreation::fromRawData([
'customSlug' => 'custom',
'maxVisits' => 2,
'apiKey' => $authorApiKey,
'longUrl' => 'https://shlink.io',
'forwardQuery' => false,
])), '2019-01-01 00:00:20');
2019-01-27 14:14:18 +03:00
$manager->persist($customShortUrl);
$ghiShortUrl = $this->setShortUrlDate(
ShortUrl::create(ShortUrlCreation::fromRawData([
'customSlug' => 'ghi789',
'longUrl' => 'https://shlink.io/documentation/',
'validUntil' => Chronos::parse('2020-05-01'), // In the past
])),
'2018-05-01',
);
$manager->persist($ghiShortUrl);
$withDomainDuplicatingShortCode = $this->setShortUrlDate(ShortUrl::create(ShortUrlCreation::fromRawData([
'domain' => 'example.com',
'customSlug' => 'ghi789',
'longUrl' => 'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-'
. 'source-software-projects/',
'tags' => ['foo'],
]), $relationResolver), '2019-01-01 00:00:30');
$manager->persist($withDomainDuplicatingShortCode);
$withDomainAndSlugShortUrl = $this->setShortUrlDate(ShortUrl::create(ShortUrlCreation::fromRawData(
['domain' => 'some-domain.com', 'customSlug' => 'custom-with-domain', 'longUrl' => 'https://google.com'],
)), '2018-10-20');
$manager->persist($withDomainAndSlugShortUrl);
2019-01-27 14:14:18 +03:00
$manager->flush();
$this->addReference('abc123_short_url', $abcShortUrl);
$this->addReference('def456_short_url', $defShortUrl);
$this->addReference('ghi789_short_url', $ghiShortUrl);
$this->addReference('example_short_url', $withDomainDuplicatingShortCode);
2019-01-27 14:14:18 +03:00
}
2019-01-27 14:35:00 +03:00
private function setShortUrlDate(ShortUrl $shortUrl, string $date): ShortUrl
2019-01-27 14:35:00 +03:00
{
$ref = new ReflectionObject($shortUrl);
$dateProp = $ref->getProperty('dateCreated');
$dateProp->setAccessible(true);
$dateProp->setValue($shortUrl, Chronos::parse($date));
2019-01-27 14:35:00 +03:00
return $shortUrl;
}
2019-01-27 14:14:18 +03:00
}