411 lines
12 KiB
PHP
Raw Normal View History

2012-04-13 15:20:45 +02:00
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Annotation;
2024-10-01 23:00:23 +03:00
use Nelmio\ApiDocBundle\Attribute\ApiDoc;
2012-04-13 15:20:45 +02:00
use Nelmio\ApiDocBundle\Tests\TestCase;
use Symfony\Component\Routing\Route;
2012-04-13 15:20:45 +02:00
class ApiDocTest extends TestCase
{
2024-10-01 15:54:04 +03:00
public function testConstructWithoutData(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc();
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['filters']));
2012-04-13 15:20:45 +02:00
$this->assertFalse($annot->isResource());
$this->assertEmpty($annot->getViews());
2013-03-18 08:40:03 +01:00
$this->assertFalse($annot->getDeprecated());
$this->assertFalse(isset($array['description']));
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
$this->assertNull($annot->getInput());
2016-05-19 18:20:41 +03:00
$this->assertFalse(isset($array['headers']));
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructWithInvalidData(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc();
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['filters']));
2012-04-13 15:20:45 +02:00
$this->assertFalse($annot->isResource());
2013-03-18 08:40:03 +01:00
$this->assertFalse($annot->getDeprecated());
$this->assertFalse(isset($array['description']));
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
$this->assertNull($annot->getInput());
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstruct(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
2012-04-13 15:20:45 +02:00
'description' => 'Heya',
2024-10-01 15:54:04 +03:00
];
2012-04-13 15:20:45 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(description: $data['description']);
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['filters']));
2012-04-13 15:20:45 +02:00
$this->assertFalse($annot->isResource());
2013-03-18 08:40:03 +01:00
$this->assertFalse($annot->getDeprecated());
$this->assertEquals($data['description'], $array['description']);
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
$this->assertNull($annot->getInput());
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructDefinesAFormType(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
'description' => 'Heya',
'input' => 'My\Form\Type',
];
2012-04-13 15:20:45 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
description: $data['description'],
input: $data['input']
);
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['filters']));
2012-04-13 15:20:45 +02:00
$this->assertFalse($annot->isResource());
2013-03-18 08:40:03 +01:00
$this->assertFalse($annot->getDeprecated());
$this->assertEquals($data['description'], $array['description']);
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
$this->assertEquals($data['input'], $annot->getInput());
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructMethodIsResource(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
'resource' => true,
'description' => 'Heya',
'deprecated' => true,
'input' => 'My\Form\Type',
];
2012-04-13 15:20:45 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
resource: $data['resource'],
description: $data['description'],
deprecated: $data['deprecated'],
input: $data['input']
);
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['filters']));
2012-04-13 15:20:45 +02:00
$this->assertTrue($annot->isResource());
2013-03-18 08:40:03 +01:00
$this->assertTrue($annot->getDeprecated());
$this->assertEquals($data['description'], $array['description']);
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
$this->assertEquals($data['input'], $annot->getInput());
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructMethodResourceIsFalse(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
'resource' => false,
'description' => 'Heya',
'deprecated' => false,
'input' => 'My\Form\Type',
];
2012-04-13 15:20:45 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
resource: $data['resource'],
description: $data['description'],
deprecated: $data['deprecated'],
input: $data['input']
);
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['filters']));
2012-04-13 15:20:45 +02:00
$this->assertFalse($annot->isResource());
$this->assertEquals($data['description'], $array['description']);
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
2013-03-18 08:40:03 +01:00
$this->assertEquals($data['deprecated'], $array['deprecated']);
$this->assertEquals($data['input'], $annot->getInput());
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructMethodHasFilters(): void
2012-04-13 15:20:45 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
'resource' => true,
'deprecated' => false,
'description' => 'Heya',
'filters' => [
['name' => 'a-filter'],
],
];
2012-04-13 15:20:45 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
resource: $data['resource'],
description: $data['description'],
deprecated: $data['deprecated'],
filters: $data['filters']
);
$array = $annot->toArray();
2012-04-13 15:20:45 +02:00
$this->assertTrue(is_array($array));
$this->assertTrue(is_array($array['filters']));
$this->assertCount(1, $array['filters']);
2024-10-01 15:54:04 +03:00
$this->assertEquals(['a-filter' => []], $array['filters']);
2012-04-13 15:20:45 +02:00
$this->assertTrue($annot->isResource());
$this->assertEquals($data['description'], $array['description']);
$this->assertFalse(isset($array['requirements']));
$this->assertFalse(isset($array['parameters']));
2013-03-18 08:40:03 +01:00
$this->assertEquals($data['deprecated'], $array['deprecated']);
$this->assertNull($annot->getInput());
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructMethodHasFiltersWithoutName(): void
2012-04-13 15:20:45 +02:00
{
$this->expectException(\InvalidArgumentException::class);
2024-10-01 15:54:04 +03:00
$data = [
'description' => 'Heya',
'filters' => [
['parameter' => 'foo'],
],
];
2012-04-13 15:20:45 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
description: $data['description'],
filters: $data['filters']
);
2012-04-13 15:20:45 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructWithStatusCodes(): void
2012-11-13 04:45:07 +00:00
{
2024-10-01 15:54:04 +03:00
$data = [
2012-11-13 04:45:07 +00:00
'description' => 'Heya',
2024-10-01 15:54:04 +03:00
'statusCodes' => [
200 => 'Returned when successful',
403 => 'Returned when the user is not authorized',
404 => [
'Returned when the user is not found',
'Returned when when something else is not found',
],
],
];
2012-11-13 04:45:07 +00:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
description: $data['description'],
statusCodes: $data['statusCodes']
);
2012-11-13 04:45:07 +00:00
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(is_array($array['statusCodes']));
foreach ($data['statusCodes'] as $code => $message) {
2024-10-01 15:54:04 +03:00
$this->assertEquals($array['statusCodes'][$code], !is_array($message) ? [$message] : $message);
2012-11-13 04:45:07 +00:00
}
}
2012-12-26 12:23:28 +01:00
2024-10-01 15:54:04 +03:00
public function testConstructWithRequirements(): void
{
2024-10-01 15:54:04 +03:00
$data = [
'requirements' => [
[
'name' => 'fooId',
'requirement' => '\d+',
'dataType' => 'integer',
2024-10-01 15:54:04 +03:00
'description' => 'This requirement might be used withing action method directly from Request object',
],
],
];
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
requirements: $data['requirements']
);
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(isset($array['requirements']['fooId']));
$this->assertTrue(isset($array['requirements']['fooId']['dataType']));
}
2024-10-01 15:54:04 +03:00
public function testConstructWithParameters(): void
{
2024-10-01 15:54:04 +03:00
$data = [
'parameters' => [
[
'name' => 'fooId',
'dataType' => 'integer',
2024-10-01 15:54:04 +03:00
'description' => 'Some description',
],
],
];
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
parameters: $data['parameters']
);
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(isset($array['parameters']['fooId']));
$this->assertTrue(isset($array['parameters']['fooId']['dataType']));
}
2014-05-27 13:33:50 +02:00
2024-10-01 15:54:04 +03:00
public function testConstructWithHeaders(): void
2016-05-19 18:20:41 +03:00
{
2024-10-01 15:54:04 +03:00
$data = [
'headers' => [
[
2016-05-19 18:20:41 +03:00
'name' => 'headerName',
2024-10-01 15:54:04 +03:00
'description' => 'Some description',
],
],
];
2016-05-19 18:20:41 +03:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
headers: $data['headers']
);
2016-05-19 18:20:41 +03:00
$array = $annot->toArray();
$this->assertArrayHasKey('headerName', $array['headers']);
$this->assertNotEmpty($array['headers']['headerName']);
$keys = array_keys($array['headers']);
$this->assertEquals($data['headers'][0]['name'], $keys[0]);
$this->assertEquals($data['headers'][0]['description'], $array['headers']['headerName']['description']);
}
2024-10-01 15:54:04 +03:00
public function testConstructWithOneTag(): void
2014-05-27 13:33:50 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
'tags' => 'beta',
];
2014-05-27 13:33:50 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
tags: $data['tags']
);
2014-05-27 13:33:50 +02:00
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(is_array($array['tags']), 'Single tag should be put in array');
2024-10-01 15:54:04 +03:00
$this->assertEquals(['beta'], $array['tags']);
2014-05-27 13:33:50 +02:00
}
2024-10-01 15:54:04 +03:00
public function testConstructWithOneTagAndColorCode(): void
{
2024-10-01 15:54:04 +03:00
$data = [
'tags' => [
'beta' => '#ff0000',
],
];
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
tags: $data['tags']
);
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(is_array($array['tags']), 'Single tag should be put in array');
2024-10-01 15:54:04 +03:00
$this->assertEquals(['beta' => '#ff0000'], $array['tags']);
}
2024-10-01 15:54:04 +03:00
public function testConstructWithMultipleTags(): void
2014-05-27 13:33:50 +02:00
{
2024-10-01 15:54:04 +03:00
$data = [
'tags' => [
'experimental' => '#0000ff',
'beta' => '#0000ff',
2024-10-01 15:54:04 +03:00
],
];
2014-05-27 13:33:50 +02:00
2024-10-01 23:00:23 +03:00
$annot = new ApiDoc(
tags: $data['tags']
);
2014-05-27 13:33:50 +02:00
$array = $annot->toArray();
$this->assertTrue(is_array($array));
$this->assertTrue(is_array($array['tags']), 'Tags should be in array');
$this->assertEquals($data['tags'], $array['tags']);
}
2024-10-01 15:54:04 +03:00
public function testAlignmentOfOutputAndResponseModels(): void
{
2024-10-01 15:54:04 +03:00
$data = [
'output' => 'FooBar',
2024-10-01 15:54:04 +03:00
'responseMap' => [
400 => 'Foo\\ValidationErrorCollection',
2024-10-01 15:54:04 +03:00
],
];
2024-10-01 23:00:23 +03:00
$apiDoc = new ApiDoc(
output: $data['output'],
responseMap: $data['responseMap']
);
$map = $apiDoc->getResponseMap();
$this->assertCount(2, $map);
$this->assertArrayHasKey(200, $map);
$this->assertArrayHasKey(400, $map);
$this->assertEquals($data['output'], $map[200]);
}
2024-10-01 15:54:04 +03:00
public function testAlignmentOfOutputAndResponseModels2(): void
{
2024-10-01 15:54:04 +03:00
$data = [
'responseMap' => [
200 => 'FooBar',
400 => 'Foo\\ValidationErrorCollection',
2024-10-01 15:54:04 +03:00
],
];
2024-10-01 23:00:23 +03:00
$apiDoc = new ApiDoc(
responseMap: $data['responseMap']
);
$map = $apiDoc->getResponseMap();
$this->assertCount(2, $map);
$this->assertArrayHasKey(200, $map);
$this->assertArrayHasKey(400, $map);
$this->assertEquals($apiDoc->getOutput(), $map[200]);
}
2024-10-01 15:54:04 +03:00
public function testSetRoute(): void
{
$route = new Route(
'/path/{foo}',
[
'foo' => 'bar',
'nested' => [
'key1' => 'value1',
'key2' => 'value2',
2024-10-01 15:54:04 +03:00
],
],
[],
[],
'{foo}.awesome_host.com'
);
2024-10-01 23:00:23 +03:00
$apiDoc = new ApiDoc();
$apiDoc->setRoute($route);
$this->assertSame($route, $apiDoc->getRoute());
$this->assertEquals('bar.awesome_host.com', $apiDoc->getHost());
$this->assertEquals('ANY', $apiDoc->getMethod());
}
2012-04-13 15:20:45 +02:00
}