Improved API tests by adding fixtures

This commit is contained in:
Alejandro Celaya
2019-01-27 12:14:18 +01:00
parent ce515767ce
commit 05c7672de3
9 changed files with 196 additions and 3 deletions

View 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;
}
}

View 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);
}
}

View 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();
}
}

View 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();
}
}

View File

@@ -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'],
];
}
}