mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Improved API tests by adding fixtures
This commit is contained in:
parent
ce515767ce
commit
05c7672de3
@ -51,6 +51,7 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"devster/ubench": "^2.0",
|
||||
"doctrine/data-fixtures": "^1.3",
|
||||
"filp/whoops": "^2.0",
|
||||
"infection/infection": "^0.11.0",
|
||||
"phpstan/phpstan": "^0.10.0",
|
||||
|
@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Common;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use function file_exists;
|
||||
use function touch;
|
||||
@ -14,5 +15,12 @@ if (! file_exists('.env')) {
|
||||
|
||||
/** @var ContainerInterface $container */
|
||||
$container = require __DIR__ . '/../container.php';
|
||||
$container->get(TestHelper::class)->createTestDb();
|
||||
$testHelper = $container->get(TestHelper::class);
|
||||
$config = $container->get('config');
|
||||
|
||||
$testHelper->createTestDb();
|
||||
|
||||
$em = $container->get(EntityManager::class);
|
||||
$testHelper->seedFixtures($em, $config['data_fixtures'] ?? []);
|
||||
|
||||
ApiTest\ApiTestCase::setApiClient($container->get('shlink_test_api_client'));
|
||||
|
@ -41,4 +41,10 @@ return [
|
||||
],
|
||||
],
|
||||
|
||||
'data_fixtures' => [
|
||||
'paths' => [
|
||||
__DIR__ . '/../../module/Rest/test-api/Fixtures',
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
@ -3,6 +3,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Common;
|
||||
|
||||
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
|
||||
use Doctrine\Common\DataFixtures\Loader;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Process\Process;
|
||||
use function file_exists;
|
||||
use function realpath;
|
||||
@ -22,4 +25,20 @@ class TestHelper
|
||||
$process->inheritEnvironmentVariables()
|
||||
->mustRun();
|
||||
}
|
||||
|
||||
public function seedFixtures(EntityManagerInterface $em, array $config): void
|
||||
{
|
||||
$paths = $config['paths'] ?? [];
|
||||
if (empty($paths)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$loader = new Loader();
|
||||
foreach ($paths as $path) {
|
||||
$loader->loadFromDirectory($path);
|
||||
}
|
||||
|
||||
$executor = new ORMExecutor($em);
|
||||
$executor->execute($loader->getFixtures(), true);
|
||||
}
|
||||
}
|
||||
|
36
module/Rest/test-api/Fixtures/ApiKeyFixture.php
Normal file
36
module/Rest/test-api/Fixtures/ApiKeyFixture.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\Common\DataFixtures\FixtureInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use ReflectionObject;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class ApiKeyFixture implements FixtureInterface
|
||||
{
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
$manager->persist($this->buildApiKey('valid_api_key', true));
|
||||
$manager->persist($this->buildApiKey('disabled_api_key', false));
|
||||
$manager->persist($this->buildApiKey('expired_api_key', true, Chronos::now()->subDay()));
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
private function buildApiKey(string $key, bool $enabled, Chronos $expiresAt = null): ApiKey
|
||||
{
|
||||
$apiKey = new ApiKey($expiresAt);
|
||||
$refObj = new ReflectionObject($apiKey);
|
||||
$keyProp = $refObj->getProperty('key');
|
||||
$keyProp->setAccessible(true);
|
||||
$keyProp->setValue($apiKey, $key);
|
||||
|
||||
if (! $enabled) {
|
||||
$apiKey->disable();
|
||||
}
|
||||
|
||||
return $apiKey;
|
||||
}
|
||||
}
|
41
module/Rest/test-api/Fixtures/ShortUrlsFixture.php
Normal file
41
module/Rest/test-api/Fixtures/ShortUrlsFixture.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
|
||||
class ShortUrlsFixture extends AbstractFixture
|
||||
{
|
||||
/**
|
||||
* Load data fixtures with the passed EntityManager
|
||||
*
|
||||
* @param ObjectManager $manager
|
||||
*/
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
$abcShortUrl = (new ShortUrl('https://shlink.io'))->setShortCode('abc123');
|
||||
$manager->persist($abcShortUrl);
|
||||
|
||||
$defShortUrl = (new ShortUrl(
|
||||
'https://shlink.io',
|
||||
ShortUrlMeta::createFromParams(Chronos::now()->addDays(3))
|
||||
))->setShortCode('def456');
|
||||
$manager->persist($defShortUrl);
|
||||
|
||||
$customShortUrl = new ShortUrl(
|
||||
'https://shlink.io',
|
||||
ShortUrlMeta::createFromParams(null, null, 'custom', 2)
|
||||
);
|
||||
$manager->persist($customShortUrl);
|
||||
|
||||
$manager->flush();
|
||||
|
||||
$this->addReference('abc123_short_url', $abcShortUrl);
|
||||
$this->addReference('def456_short_url', $defShortUrl);
|
||||
}
|
||||
}
|
37
module/Rest/test-api/Fixtures/TagsFixture.php
Normal file
37
module/Rest/test-api/Fixtures/TagsFixture.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
|
||||
class TagsFixture extends AbstractFixture implements DependentFixtureInterface
|
||||
{
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return [ShortUrlsFixture::class];
|
||||
}
|
||||
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
$fooTag = new Tag('foo');
|
||||
$manager->persist($fooTag);
|
||||
$barTag = new Tag('bar');
|
||||
$manager->persist($barTag);
|
||||
|
||||
/** @var ShortUrl $abcShortUrl */
|
||||
$abcShortUrl = $this->getReference('abc123_short_url');
|
||||
$abcShortUrl->setTags(new ArrayCollection([$fooTag]));
|
||||
|
||||
/** @var ShortUrl $defShortUrl */
|
||||
$defShortUrl = $this->getReference('def456_short_url');
|
||||
$defShortUrl->setTags(new ArrayCollection([$fooTag, $barTag]));
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
}
|
35
module/Rest/test-api/Fixtures/VisitsFixture.php
Normal file
35
module/Rest/test-api/Fixtures/VisitsFixture.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
||||
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||
|
||||
class VisitsFixture extends AbstractFixture implements DependentFixtureInterface
|
||||
{
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return [ShortUrlsFixture::class];
|
||||
}
|
||||
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
/** @var ShortUrl $abcShortUrl */
|
||||
$abcShortUrl = $this->getReference('abc123_short_url');
|
||||
$manager->persist(new Visit($abcShortUrl, new Visitor('shlink-tests-agent', '', '44.55.66.77')));
|
||||
$manager->persist(new Visit($abcShortUrl, new Visitor('shlink-tests-agent', 'https://google.com', '4.5.6.7')));
|
||||
$manager->persist(new Visit($abcShortUrl, new Visitor('shlink-tests-agent', '', '1.2.3.4')));
|
||||
|
||||
/** @var ShortUrl $defShortUrl */
|
||||
$defShortUrl = $this->getReference('def456_short_url');
|
||||
$manager->persist(new Visit($defShortUrl, new Visitor('shlink-tests-agent', '', '127.0.0.1')));
|
||||
$manager->persist(new Visit($defShortUrl, new Visitor('shlink-tests-agent', 'https://app.shlink.io', '')));
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
}
|
@ -38,13 +38,14 @@ class AuthenticationTest extends ApiTestCase
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideInvalidApiKeys
|
||||
*/
|
||||
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid()
|
||||
public function apiKeyErrorIsReturnedWhenProvidedApiKeyIsInvalid(string $apiKey)
|
||||
{
|
||||
try {
|
||||
$this->callApi(self::METHOD_GET, '/short-codes', [
|
||||
'headers' => [
|
||||
ApiKeyHeaderPlugin::HEADER_NAME => 'invalid',
|
||||
ApiKeyHeaderPlugin::HEADER_NAME => $apiKey,
|
||||
],
|
||||
]);
|
||||
} catch (ClientException $e) {
|
||||
@ -55,4 +56,13 @@ class AuthenticationTest extends ApiTestCase
|
||||
$this->assertEquals('Provided API key does not exist or is invalid.', $message);
|
||||
}
|
||||
}
|
||||
|
||||
public function provideInvalidApiKeys(): array
|
||||
{
|
||||
return [
|
||||
'key which does not exist' => ['invalid'],
|
||||
'key which is expired' => ['expired_api_key'],
|
||||
'key which is disabled' => ['disabled_api_key'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user