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;
|
2021-01-10 10:40:18 +03:00
|
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
2019-12-16 23:46:27 +03:00
|
|
|
use Doctrine\Persistence\ObjectManager;
|
2019-01-27 14:35:00 +03:00
|
|
|
use ReflectionObject;
|
2019-01-27 14:14:18 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2021-01-10 10:40:18 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2019-01-27 14:14:18 +03:00
|
|
|
|
2021-01-10 10:40:18 +03:00
|
|
|
class ShortUrlsFixture extends AbstractFixture implements DependentFixtureInterface
|
2019-01-27 14:14:18 +03:00
|
|
|
{
|
2021-01-10 10:40:18 +03:00
|
|
|
public function getDependencies(): array
|
|
|
|
{
|
2021-01-31 13:01:38 +03:00
|
|
|
return [ApiKeyFixture::class, TagsFixture::class];
|
2021-01-10 10:40:18 +03:00
|
|
|
}
|
|
|
|
|
2019-01-27 14:14:18 +03:00
|
|
|
public function load(ObjectManager $manager): void
|
|
|
|
{
|
2021-01-31 13:01:38 +03:00
|
|
|
$relationResolver = new PersistenceShortUrlRelationResolver($manager);
|
|
|
|
|
2021-01-10 10:40:18 +03:00
|
|
|
/** @var ApiKey $authorApiKey */
|
|
|
|
$authorApiKey = $this->getReference('author_api_key');
|
|
|
|
|
2019-10-11 10:14:25 +03:00
|
|
|
$abcShortUrl = $this->setShortUrlDate(
|
2021-01-31 13:01:38 +03:00
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
|
|
|
|
'customSlug' => 'abc123',
|
|
|
|
'apiKey' => $authorApiKey,
|
|
|
|
'longUrl' => 'https://shlink.io',
|
|
|
|
'tags' => ['foo'],
|
|
|
|
]), $relationResolver),
|
2020-01-01 22:48:31 +03:00
|
|
|
'2018-05-01',
|
2019-10-11 10:14:25 +03:00
|
|
|
);
|
2019-01-27 14:14:18 +03:00
|
|
|
$manager->persist($abcShortUrl);
|
|
|
|
|
2021-01-30 16:18:44 +03:00
|
|
|
$defShortUrl = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::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/',
|
2021-01-31 13:01:38 +03:00
|
|
|
'tags' => ['foo', 'bar'],
|
|
|
|
]), $relationResolver), '2019-01-01 00:00:10');
|
2019-01-27 14:14:18 +03:00
|
|
|
$manager->persist($defShortUrl);
|
|
|
|
|
2021-01-30 16:18:44 +03:00
|
|
|
$customShortUrl = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
|
|
|
|
['customSlug' => 'custom', 'maxVisits' => 2, 'apiKey' => $authorApiKey, 'longUrl' => 'https://shlink.io'],
|
|
|
|
)), '2019-01-01 00:00:20');
|
2019-01-27 14:14:18 +03:00
|
|
|
$manager->persist($customShortUrl);
|
|
|
|
|
2020-02-02 11:51:17 +03:00
|
|
|
$ghiShortUrl = $this->setShortUrlDate(
|
2021-01-30 16:18:44 +03:00
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
|
|
|
|
['customSlug' => 'ghi789', 'longUrl' => 'https://shlink.io/documentation/'],
|
|
|
|
)),
|
2020-02-02 11:51:17 +03:00
|
|
|
'2018-05-01',
|
|
|
|
);
|
|
|
|
$manager->persist($ghiShortUrl);
|
|
|
|
|
2021-01-30 16:18:44 +03:00
|
|
|
$withDomainDuplicatingShortCode = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
|
|
|
|
'domain' => 'example.com',
|
|
|
|
'customSlug' => 'ghi789',
|
|
|
|
'longUrl' => 'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-'
|
|
|
|
. 'source-software-projects/',
|
2021-01-31 13:01:38 +03:00
|
|
|
'tags' => ['foo'],
|
|
|
|
]), $relationResolver), '2019-01-01 00:00:30');
|
2020-02-02 11:51:17 +03:00
|
|
|
$manager->persist($withDomainDuplicatingShortCode);
|
2019-10-01 21:24:11 +03:00
|
|
|
|
2021-01-30 16:18:44 +03:00
|
|
|
$withDomainAndSlugShortUrl = $this->setShortUrlDate(ShortUrl::fromMeta(ShortUrlMeta::fromRawData(
|
|
|
|
['domain' => 'some-domain.com', 'customSlug' => 'custom-with-domain', 'longUrl' => 'https://google.com'],
|
|
|
|
)), '2018-10-20');
|
2019-10-02 21:13:25 +03:00
|
|
|
$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);
|
2020-02-02 12:46:38 +03:00
|
|
|
$this->addReference('ghi789_short_url', $ghiShortUrl);
|
2021-01-10 11:54:19 +03:00
|
|
|
$this->addReference('example_short_url', $withDomainDuplicatingShortCode);
|
2019-01-27 14:14:18 +03:00
|
|
|
}
|
2019-01-27 14:35:00 +03:00
|
|
|
|
2019-12-18 17:26:23 +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);
|
2019-12-18 17:26:23 +03:00
|
|
|
$dateProp->setValue($shortUrl, Chronos::parse($date));
|
2019-01-27 14:35:00 +03:00
|
|
|
|
|
|
|
return $shortUrl;
|
|
|
|
}
|
2019-01-27 14:14:18 +03:00
|
|
|
}
|