Prepared configs for API tests

This commit is contained in:
Alejandro Celaya 2019-01-26 10:19:20 +01:00
parent c4af1471f0
commit 22d61fead7
6 changed files with 90 additions and 1 deletions

View file

@ -78,6 +78,7 @@
"psr-4": {
"ShlinkioTest\\Shlink\\CLI\\": "module/CLI/test",
"ShlinkioTest\\Shlink\\Rest\\": "module/Rest/test",
"ShlinkioApiTest\\Shlink\\Rest\\": "module/Rest/test-api",
"ShlinkioTest\\Shlink\\Core\\": [
"module/Core/test",
"module/Core/test-db"
@ -112,6 +113,11 @@
"test:unit": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov",
"test:unit:ci": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --coverage-clover=build/clover.xml --coverage-xml=build/coverage-xml --log-junit=build/phpunit.junit.xml",
"test:db": "APP_ENV=test phpdbg -qrr vendor/bin/phpunit --order-by=random -c phpunit-db.xml --coverage-php build/coverage-db.cov",
"test:api": [
"APP_ENV=test vendor/bin/zend-expressive-swoole start -d",
"APP_ENV=test phpdbg -qrr vendor/bin/phpunit --order-by=random -c phpunit-api.xml",
"APP_ENV=test vendor/bin/zend-expressive-swoole stop"
],
"test:pretty": [
"@test",

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common;
use Psr\Container\ContainerInterface;
use function file_exists;
use function touch;
// Create an empty .env file
if (! file_exists('.env')) {
touch('.env');
}
/** @var ContainerInterface $container */
$container = require __DIR__ . '/../container.php';
$container->get(TestHelper::class)->createTestDb();
ApiTest\ApiTestCase::setApiClient($container->get('shlink_test_api_client'));

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink;
use GuzzleHttp\Client;
use Zend\ServiceManager\Factory\InvokableFactory;
use function realpath;
use function sys_get_temp_dir;
@ -12,12 +13,17 @@ return [
'zend-expressive-swoole' => [
'swoole-http-server' => [
'port' => 9999,
'host' => '127.0.0.1',
'process-name' => 'shlink_test',
],
],
'dependencies' => [
'factories' => [
Common\TestHelper::class => InvokableFactory::class,
'shlink_test_api_client' => function () {
return new Client(['base_uri' => 'http://localhost:9999/']);
},
],
],

View file

@ -3,8 +3,27 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\ApiTest;
use Fig\Http\Message\RequestMethodInterface;
use Fig\Http\Message\StatusCodeInterface;
use GuzzleHttp\ClientInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
abstract class ApiTestCase extends TestCase
abstract class ApiTestCase extends TestCase implements StatusCodeInterface, RequestMethodInterface
{
/** @var ClientInterface */
private static $client;
public static function setApiClient(ClientInterface $client): void
{
self::$client = $client;
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function callApi(string $method, string $uri, array $options = []): ResponseInterface
{
return self::$client->request($method, $uri, $options);
}
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Middleware;
use GuzzleHttp\Exception\ClientException;
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase;
class AuthenticationTest extends ApiTestCase
{
/**
* @test
*/
public function unauthorizedIsReturnedIfNoAuthenticationIsSent()
{
$this->expectException(ClientException::class);
$this->expectExceptionCode(self::STATUS_UNAUTHORIZED);
$this->callApi(self::METHOD_GET, '/rest/v1/short-codes');
}
}

19
phpunit-api.xml Normal file
View file

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
bootstrap="./config/test/bootstrap_api_tests.php"
colors="true"
>
<testsuites>
<testsuite name="Shlink API tests">
<directory>./module/*/test-api</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./module/*/src</directory>
</whitelist>
</filter>
</phpunit>