mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 12:11:19 +03:00
Prepared configs for API tests
This commit is contained in:
parent
c4af1471f0
commit
22d61fead7
6 changed files with 90 additions and 1 deletions
|
@ -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",
|
||||
|
|
18
config/test/bootstrap_api_tests.php
Normal file
18
config/test/bootstrap_api_tests.php
Normal 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'));
|
|
@ -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/']);
|
||||
},
|
||||
],
|
||||
],
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
21
module/Rest/test-api/Middleware/AuthenticationTest.php
Normal file
21
module/Rest/test-api/Middleware/AuthenticationTest.php
Normal 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
19
phpunit-api.xml
Normal 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>
|
Loading…
Add table
Reference in a new issue