Merge pull request #461 from acelaya/feature/test-utils-external

Used TestUtils module from external library
This commit is contained in:
Alejandro Celaya 2019-08-11 21:30:38 +02:00 committed by GitHub
commit 0c7dd18b7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 2 additions and 178 deletions

View file

@ -54,7 +54,6 @@
},
"require-dev": {
"devster/ubench": "^2.0",
"doctrine/data-fixtures": "^1.3",
"eaglewu/swoole-ide-helper": "dev-master",
"filp/whoops": "^2.4",
"infection/infection": "^0.12.2",
@ -63,6 +62,7 @@
"phpunit/phpunit": "^8.3",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~1.2.2",
"shlinkio/shlink-test-utils": "^1.0",
"symfony/dotenv": "^4.3",
"symfony/var-dumper": "^4.3",
"zendframework/zend-component-installer": "^2.1",
@ -95,8 +95,7 @@
"ShlinkioTest\\Shlink\\Common\\": "module/Common/test",
"ShlinkioTest\\Shlink\\EventDispatcher\\": "module/EventDispatcher/test",
"ShlinkioTest\\Shlink\\IpGeolocation\\": "module/IpGeolocation/test",
"ShlinkioTest\\Shlink\\PreviewGenerator\\": "module/PreviewGenerator/test",
"Shlinkio\\Shlink\\TestUtils\\": "module/TestUtils/src"
"ShlinkioTest\\Shlink\\PreviewGenerator\\": "module/PreviewGenerator/test"
}
},
"scripts": {

View file

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2019 Alejandro Celaya
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,10 +0,0 @@
# Shlink test utils
Helpers and utilities to run different types of tests in Shlink.
Provided base test classes:
* `ApiTestCase` for API e2e tests.
* `DbTestCase` for database integration tests.
Both classes extends [phpunit]'s `TestCase` class.

View file

@ -1,67 +0,0 @@
<?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,
]);
}
}

View file

@ -1,36 +0,0 @@
<?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();
}
}

View file

@ -1,40 +0,0 @@
<?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());
}
}

View file

@ -35,7 +35,6 @@
<exclude>
<directory suffix=".php">./module/Core/src/Repository</directory>
<directory suffix=".php">./module/TestUtils</directory>
</exclude>
</whitelist>
</filter>