mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Created TestUtils module
This commit is contained in:
67
module/TestUtils/src/ApiTest/ApiTestCase.php
Normal file
67
module/TestUtils/src/ApiTest/ApiTestCase.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\TestUtils\ApiTest;
|
||||
|
||||
use Fig\Http\Message\RequestMethodInterface;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
use function Shlinkio\Shlink\Common\json_decode;
|
||||
use function sprintf;
|
||||
|
||||
abstract class ApiTestCase extends TestCase implements StatusCodeInterface, RequestMethodInterface
|
||||
{
|
||||
private const REST_PATH_PREFIX = '/rest/v1';
|
||||
|
||||
/** @var ClientInterface */
|
||||
private static $client;
|
||||
/** @var callable|null */
|
||||
private static $seedFixtures;
|
||||
|
||||
public static function setApiClient(ClientInterface $client): void
|
||||
{
|
||||
self::$client = $client;
|
||||
}
|
||||
|
||||
public static function setSeedFixturesCallback(callable $seedFixtures): void
|
||||
{
|
||||
self::$seedFixtures = $seedFixtures;
|
||||
}
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
if (self::$seedFixtures !== null) {
|
||||
(self::$seedFixtures)();
|
||||
}
|
||||
}
|
||||
|
||||
protected function callApi(string $method, string $uri, array $options = []): ResponseInterface
|
||||
{
|
||||
return self::$client->request($method, sprintf('%s%s', self::REST_PATH_PREFIX, $uri), $options);
|
||||
}
|
||||
|
||||
protected function callApiWithKey(string $method, string $uri, array $options = []): ResponseInterface
|
||||
{
|
||||
$headers = $options[RequestOptions::HEADERS] ?? [];
|
||||
$headers['X-Api-Key'] = 'valid_api_key';
|
||||
$options[RequestOptions::HEADERS] = $headers;
|
||||
|
||||
return $this->callApi($method, $uri, $options);
|
||||
}
|
||||
|
||||
protected function getJsonResponsePayload(ResponseInterface $resp): array
|
||||
{
|
||||
return json_decode((string) $resp->getBody());
|
||||
}
|
||||
|
||||
protected function callShortUrl(string $shortCode): ResponseInterface
|
||||
{
|
||||
return self::$client->request(self::METHOD_GET, sprintf('/%s', $shortCode), [
|
||||
RequestOptions::ALLOW_REDIRECTS => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
36
module/TestUtils/src/DbTest/DatabaseTestCase.php
Normal file
36
module/TestUtils/src/DbTest/DatabaseTestCase.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\TestUtils\DbTest;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class DatabaseTestCase extends TestCase
|
||||
{
|
||||
protected const ENTITIES_TO_EMPTY = [];
|
||||
|
||||
/** @var EntityManagerInterface */
|
||||
private static $em;
|
||||
|
||||
public static function setEntityManager(EntityManagerInterface $em): void
|
||||
{
|
||||
self::$em = $em;
|
||||
}
|
||||
|
||||
protected function getEntityManager(): EntityManagerInterface
|
||||
{
|
||||
return self::$em;
|
||||
}
|
||||
|
||||
public function tearDown(): void
|
||||
{
|
||||
foreach (static::ENTITIES_TO_EMPTY as $entityClass) {
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->delete($entityClass, 'x');
|
||||
$qb->getQuery()->execute();
|
||||
}
|
||||
|
||||
$this->getEntityManager()->clear();
|
||||
}
|
||||
}
|
||||
40
module/TestUtils/src/Helper/TestHelper.php
Normal file
40
module/TestUtils/src/Helper/TestHelper.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\TestUtils\Helper;
|
||||
|
||||
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
|
||||
use Doctrine\Common\DataFixtures\Loader;
|
||||
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class TestHelper
|
||||
{
|
||||
public function createTestDb(): void
|
||||
{
|
||||
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:drop', '--force', '--no-interaction', '-q']);
|
||||
$process->inheritEnvironmentVariables()
|
||||
->mustRun();
|
||||
|
||||
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q']);
|
||||
$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, new ORMPurger());
|
||||
$executor->execute($loader->getFixtures());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user