NelmioApiDocBundle/Tests/Functional/FunctionalTest.php

242 lines
8.1 KiB
PHP
Raw Normal View History

2016-07-12 00:33:55 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-07-12 00:33:55 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-07-12 00:33:55 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-12-29 12:09:26 +01:00
namespace Nelmio\ApiDocBundle\Tests\Functional;
2016-07-12 00:33:55 +02:00
2017-01-14 17:36:56 +01:00
use EXSyst\Component\Swagger\Operation;
2017-05-31 18:35:02 +02:00
use EXSyst\Component\Swagger\Tag;
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
2016-07-12 00:33:55 +02:00
class FunctionalTest extends WebTestCase
{
public function testConfiguredDocumentation()
{
$this->assertEquals('My Test App', $this->getSwaggerDefinition()->getInfo()->getTitle());
}
public function testUndocumentedAction()
{
$paths = $this->getSwaggerDefinition()->getPaths();
$this->assertFalse($paths->has('/undocumented'));
$this->assertFalse($paths->has('/api/admin'));
}
2017-06-13 13:34:26 +02:00
public function testFetchArticleAction()
{
$operation = $this->getOperation('/api/article/{id}', 'get');
$responses = $operation->getResponses();
$this->assertTrue($responses->has('200'));
$this->assertEquals('#/definitions/Article', $responses->get('200')->getSchema()->getRef());
// Ensure that groups are supported
$modelProperties = $this->getModel('Article')->getProperties();
$this->assertCount(1, $modelProperties);
$this->assertTrue($modelProperties->has('author'));
$this->assertFalse($modelProperties->has('content'));
}
2017-03-16 19:35:04 +01:00
public function testFilteredAction()
{
$paths = $this->getSwaggerDefinition()->getPaths();
$this->assertFalse($paths->has('/filtered'));
}
/**
* Tests that the paths are automatically resolved in Swagger annotations.
*
* @dataProvider swaggerActionPathsProvider
*/
public function testSwaggerAction($path)
{
$operation = $this->getOperation($path, 'get');
$responses = $operation->getResponses();
$this->assertTrue($responses->has('201'));
$this->assertEquals('An example resource', $responses->get('201')->getDescription());
}
public function swaggerActionPathsProvider()
{
return [['/api/swagger'], ['/api/swagger2']];
}
/**
* @dataProvider implicitSwaggerActionMethodsProvider
*/
public function testImplicitSwaggerAction($method)
{
$operation = $this->getOperation('/api/swagger/implicit', $method);
2017-12-22 17:42:18 +00:00
$this->assertEquals([new Tag('implicit')], $operation->getTags());
2017-03-17 19:45:46 +01:00
$responses = $operation->getResponses();
$this->assertTrue($responses->has('201'));
2017-01-14 17:36:56 +01:00
$response = $responses->get('201');
$this->assertEquals('Operation automatically detected', $response->getDescription());
$this->assertEquals('#/definitions/User', $response->getSchema()->getRef());
$parameters = $operation->getParameters();
2017-01-14 17:36:56 +01:00
$this->assertTrue($parameters->has('foo', 'body'));
$parameter = $parameters->get('foo', 'body');
$this->assertEquals('This is a parameter', $parameter->getDescription());
$this->assertEquals('#/definitions/User', $parameter->getSchema()->getItems()->getRef());
}
public function implicitSwaggerActionMethodsProvider()
{
return [['get'], ['post']];
}
2016-07-13 23:05:14 +02:00
public function testUserAction()
2016-07-12 00:33:55 +02:00
{
$operation = $this->getOperation('/api/test/{user}', 'get');
2016-07-12 00:33:55 +02:00
2016-07-29 18:40:56 +02:00
$this->assertEquals(['https'], $operation->getSchemes());
2016-07-13 23:05:14 +02:00
$this->assertEmpty($operation->getSummary());
$this->assertEmpty($operation->getDescription());
2016-08-01 19:58:57 +02:00
$this->assertNull($operation->getDeprecated());
2017-06-02 21:30:31 +02:00
$this->assertTrue($operation->getResponses()->has(200));
2016-07-12 00:33:55 +02:00
$parameters = $operation->getParameters();
2016-07-29 18:40:56 +02:00
$this->assertTrue($parameters->has('user', 'path'));
2016-07-12 00:33:55 +02:00
2016-07-29 18:40:56 +02:00
$parameter = $parameters->get('user', 'path');
2016-07-12 00:33:55 +02:00
$this->assertTrue($parameter->getRequired());
$this->assertEquals('string', $parameter->getType());
$this->assertEquals('/foo/', $parameter->getPattern());
$this->assertEmpty($parameter->getFormat());
2016-07-12 00:33:55 +02:00
}
2016-11-30 16:21:03 +01:00
public function testFOSRestAction()
{
$operation = $this->getOperation('/api/fosrest', 'post');
$parameters = $operation->getParameters();
$this->assertTrue($parameters->has('foo', 'query'));
$this->assertTrue($parameters->has('bar', 'formData'));
2016-12-01 17:19:33 +01:00
// The _format path attribute should be removed
$this->assertFalse($parameters->has('_format', 'path'));
2016-11-30 16:21:03 +01:00
}
2016-07-13 23:05:14 +02:00
public function testDeprecatedAction()
{
$operation = $this->getOperation('/api/deprecated', 'get');
2016-07-13 23:05:14 +02:00
$this->assertEquals('This action is deprecated.', $operation->getSummary());
$this->assertEquals('Please do not use this action.', $operation->getDescription());
$this->assertTrue($operation->getDeprecated());
}
2016-08-01 19:58:57 +02:00
public function testApiPlatform()
2016-07-28 10:20:59 +02:00
{
2016-08-01 19:58:57 +02:00
$operation = $this->getOperation('/api/dummies', 'get');
$operation = $this->getOperation('/api/foo', 'get');
$operation = $this->getOperation('/api/foo', 'post');
$operation = $this->getOperation('/api/dummies/{id}', 'get');
2016-07-28 10:20:59 +02:00
}
2017-01-14 17:36:56 +01:00
public function testUserModel()
{
$this->assertEquals(
[
'type' => 'object',
'properties' => [
'money' => [
'type' => 'number',
'format' => 'float',
2017-12-18 21:07:44 +01:00
'default' => 0.0,
],
'id' => [
'type' => 'integer',
2017-12-17 10:44:07 +01:00
'description' => 'User id',
'readOnly' => true,
2017-12-17 10:44:07 +01:00
'title' => 'userid',
'example' => 1,
],
'email' => [
'type' => 'string',
'readOnly' => false,
],
'roles' => [
'title' => 'roles',
'type' => 'array',
'description' => 'User roles',
'example' => '["ADMIN","SUPERUSER"]',
'items' => ['type' => 'string'],
'default' => ['user'],
],
'friendsNumber' => [
'type' => 'string',
],
'createdAt' => [
'type' => 'string',
'format' => 'date-time',
],
'users' => [
'items' => [
'$ref' => '#/definitions/User',
],
'type' => 'array',
],
'dummy' => [
'$ref' => '#/definitions/Dummy2',
],
'status' => [
'type' => 'string',
2017-12-18 21:07:44 +01:00
'enum' => ['disabled', 'enabled'],
],
],
],
$this->getModel('User')->toArray()
);
2017-01-14 17:36:56 +01:00
}
2017-06-24 17:49:00 +02:00
public function testFormSupport()
{
$this->assertEquals([
'type' => 'object',
'properties' => [
'dummy' => ['$ref' => '#/definitions/DummyType'],
'dummies' => [
'items' => ['$ref' => '#/definitions/DummyType'],
'type' => 'array',
2017-12-17 10:44:07 +01:00
'example' => sprintf('[{%s}]', DummyType::class),
],
],
'required' => ['dummy', 'dummies'],
], $this->getModel('UserType')->toArray());
2017-06-24 17:49:00 +02:00
$this->assertEquals([
'type' => 'object',
'properties' => [
'bar' => [
'type' => 'string',
],
'foo' => [
'type' => 'string',
'enum' => ['male', 'female'],
],
'baz' => [
2017-12-17 10:44:07 +01:00
'type' => 'boolean',
],
'bey' => [
'type' => 'integer',
],
2017-06-24 17:49:00 +02:00
],
'required' => ['foo'],
], $this->getModel('DummyType')->toArray());
}
2016-07-12 00:33:55 +02:00
}