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

59 lines
2 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;
use ReflectionObject;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
2019-01-27 14:14:18 +03:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ApiKeyFixture extends AbstractFixture implements DependentFixtureInterface
2019-01-27 14:14:18 +03:00
{
public function getDependencies(): array
{
return [DomainFixture::class];
}
2019-01-27 14:14:18 +03:00
public function load(ObjectManager $manager): void
{
$manager->persist($this->buildApiKey('valid_api_key', true));
$manager->persist($this->buildApiKey('disabled_api_key', false));
2022-02-13 14:20:02 +03:00
$manager->persist($this->buildApiKey('expired_api_key', true, Chronos::now()->subDay()->startOfDay()));
$authorApiKey = $this->buildApiKey('author_api_key', true);
$authorApiKey->registerRole(RoleDefinition::forAuthoredShortUrls());
$manager->persist($authorApiKey);
$this->addReference('author_api_key', $authorApiKey);
/** @var Domain $exampleDomain */
$exampleDomain = $this->getReference('example_domain');
$domainApiKey = $this->buildApiKey('domain_api_key', true);
$domainApiKey->registerRole(RoleDefinition::forDomain($exampleDomain));
$manager->persist($domainApiKey);
2019-01-27 14:14:18 +03:00
$manager->flush();
}
private function buildApiKey(string $key, bool $enabled, ?Chronos $expiresAt = null): ApiKey
2019-01-27 14:14:18 +03:00
{
2023-09-19 10:10:17 +03:00
$apiKey = ApiKey::fromMeta(ApiKeyMeta::fromParams(expirationDate: $expiresAt));
$ref = new ReflectionObject($apiKey);
$keyProp = $ref->getProperty('key');
$keyProp->setAccessible(true);
$keyProp->setValue($apiKey, $key);
2019-01-27 14:14:18 +03:00
if (! $enabled) {
$apiKey->disable();
}
return $apiKey;
}
}