diff --git a/composer.json b/composer.json index 7f519c51..7cf7dcc5 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/test/bootstrap_api_tests.php b/config/test/bootstrap_api_tests.php new file mode 100644 index 00000000..6ccde6b7 --- /dev/null +++ b/config/test/bootstrap_api_tests.php @@ -0,0 +1,18 @@ +get(TestHelper::class)->createTestDb(); +ApiTest\ApiTestCase::setApiClient($container->get('shlink_test_api_client')); diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 8757bb34..72f6e307 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -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/']); + }, ], ], diff --git a/module/Common/test-db/ApiTest/ApiTestCase.php b/module/Common/test-db/ApiTest/ApiTestCase.php index e947eba9..0a190822 100644 --- a/module/Common/test-db/ApiTest/ApiTestCase.php +++ b/module/Common/test-db/ApiTest/ApiTestCase.php @@ -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); + } } diff --git a/module/Rest/test-api/Middleware/AuthenticationTest.php b/module/Rest/test-api/Middleware/AuthenticationTest.php new file mode 100644 index 00000000..dc6032fc --- /dev/null +++ b/module/Rest/test-api/Middleware/AuthenticationTest.php @@ -0,0 +1,21 @@ +expectException(ClientException::class); + $this->expectExceptionCode(self::STATUS_UNAUTHORIZED); + + $this->callApi(self::METHOD_GET, '/rest/v1/short-codes'); + } +} diff --git a/phpunit-api.xml b/phpunit-api.xml new file mode 100644 index 00000000..69132097 --- /dev/null +++ b/phpunit-api.xml @@ -0,0 +1,19 @@ + + + + + ./module/*/test-api + + + + + + ./module/*/src + + +