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;
|
|
|
|
|
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
|
|
|
use Nelmio\ApiDocBundle\Tests\TestCase;
|
|
|
|
|
|
|
|
class ApiDocTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testConstructWithoutData()
|
|
|
|
{
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +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());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertFalse(isset($array['description']));
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($annot->getInput());
|
2012-12-26 12:23:28 +01:00
|
|
|
$this->assertFalse($array['authentication']);
|
2013-06-24 14:27:22 +02:00
|
|
|
$this->assertTrue(is_array($array['authenticationRoles']));
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructWithInvalidData()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'unknown' => 'foo',
|
|
|
|
'array' => array('bar' => 'bar'),
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +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());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertFalse(isset($array['description']));
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstruct()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'description' => 'Heya',
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +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());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertEquals($data['description'], $array['description']);
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructDefinesAFormType()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'description' => 'Heya',
|
2012-07-23 15:44:37 -04:00
|
|
|
'input' => 'My\Form\Type',
|
2012-04-13 15:20:45 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +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());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertEquals($data['description'], $array['description']);
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertEquals($data['input'], $annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructMethodIsResource()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'resource' => true,
|
|
|
|
'description' => 'Heya',
|
2013-03-18 08:40:03 +01:00
|
|
|
'deprecated' => true,
|
2012-07-23 15:44:37 -04:00
|
|
|
'input' => 'My\Form\Type',
|
2012-04-13 15:20:45 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +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());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertEquals($data['description'], $array['description']);
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertEquals($data['input'], $annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructMethodResourceIsFalse()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'resource' => false,
|
|
|
|
'description' => 'Heya',
|
2013-03-18 08:40:03 +01:00
|
|
|
'deprecated' => false,
|
2012-07-23 15:44:37 -04:00
|
|
|
'input' => 'My\Form\Type',
|
2012-04-13 15:20:45 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertTrue(is_array($array));
|
|
|
|
$this->assertFalse(isset($array['filters']));
|
2012-04-13 15:20:45 +02:00
|
|
|
$this->assertFalse($annot->isResource());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertEquals($data['description'], $array['description']);
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2013-03-18 08:40:03 +01:00
|
|
|
$this->assertEquals($data['deprecated'], $array['deprecated']);
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertEquals($data['input'], $annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructMethodHasFilters()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'resource' => true,
|
2013-03-18 08:40:03 +01:00
|
|
|
'deprecated' => false,
|
2012-04-13 15:20:45 +02:00
|
|
|
'description' => 'Heya',
|
|
|
|
'filters' => array(
|
|
|
|
array('name' => 'a-filter'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertTrue(is_array($array));
|
|
|
|
$this->assertTrue(is_array($array['filters']));
|
|
|
|
$this->assertCount(1, $array['filters']);
|
|
|
|
$this->assertEquals(array('a-filter' => array()), $array['filters']);
|
2012-04-13 15:20:45 +02:00
|
|
|
$this->assertTrue($annot->isResource());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertEquals($data['description'], $array['description']);
|
2013-07-17 20:41:27 +02:00
|
|
|
$this->assertFalse(isset($array['requirements']));
|
|
|
|
$this->assertFalse(isset($array['parameters']));
|
2013-03-18 08:40:03 +01:00
|
|
|
$this->assertEquals($data['deprecated'], $array['deprecated']);
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertNull($annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testConstructMethodHasFiltersWithoutName()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'description' => 'Heya',
|
|
|
|
'filters' => array(
|
|
|
|
array('parameter' => 'foo'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructNoFiltersIfFormTypeDefined()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'resource' => true,
|
|
|
|
'description' => 'Heya',
|
2012-07-23 15:44:37 -04:00
|
|
|
'input' => 'My\Form\Type',
|
2012-04-13 15:20:45 +02:00
|
|
|
'filters' => array(
|
|
|
|
array('name' => 'a-filter'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
2012-07-20 00:58:58 +02:00
|
|
|
$array = $annot->toArray();
|
2012-04-13 15:20:45 +02:00
|
|
|
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertTrue(is_array($array));
|
|
|
|
$this->assertFalse(isset($array['filters']));
|
2012-04-13 15:20:45 +02:00
|
|
|
$this->assertTrue($annot->isResource());
|
2012-07-20 00:58:58 +02:00
|
|
|
$this->assertEquals($data['description'], $array['description']);
|
2012-07-23 15:44:37 -04:00
|
|
|
$this->assertEquals($data['input'], $annot->getInput());
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|
2012-11-13 04:45:07 +00:00
|
|
|
|
2013-02-18 21:07:59 +01:00
|
|
|
public function testConstructWithStatusCodes()
|
2012-11-13 04:45:07 +00:00
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'description' => 'Heya',
|
|
|
|
'statusCodes' => array(
|
|
|
|
200 => "Returned when successful",
|
2013-02-18 21:07:59 +01:00
|
|
|
403 => "Returned when the user is not authorized",
|
|
|
|
404 => array(
|
|
|
|
"Returned when the user is not found",
|
|
|
|
"Returned when when something else is not found"
|
|
|
|
)
|
2012-11-13 04:45:07 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
|
|
|
$array = $annot->toArray();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($array));
|
|
|
|
$this->assertTrue(is_array($array['statusCodes']));
|
|
|
|
foreach ($data['statusCodes'] as $code => $message) {
|
2013-02-18 21:07:59 +01:00
|
|
|
$this->assertEquals($array['statusCodes'][$code], !is_array($message) ? array($message) : $message);
|
2012-11-13 04:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-26 12:23:28 +01:00
|
|
|
|
|
|
|
public function testConstructWithAuthentication()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'authentication' => true
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
|
|
|
$array = $annot->toArray();
|
|
|
|
|
|
|
|
$this->assertTrue($array['authentication']);
|
|
|
|
}
|
2013-03-27 16:20:42 +01:00
|
|
|
|
|
|
|
public function testConstructWithCache()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'cache' => '60'
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
|
|
|
$array = $annot->toArray();
|
|
|
|
|
|
|
|
$this->assertEquals($data['cache'], $array['cache']);
|
|
|
|
}
|
2013-07-17 20:41:27 +02:00
|
|
|
|
|
|
|
public function testConstructWithRequirements()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'requirements' => array(
|
|
|
|
array(
|
|
|
|
'name' => 'fooId',
|
|
|
|
'requirement' => '\d+',
|
|
|
|
'dataType' => 'integer',
|
|
|
|
'description' => 'This requirement might be used withing action method directly from Request object'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
|
|
|
$array = $annot->toArray();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($array));
|
|
|
|
$this->assertTrue(isset($array['requirements']['fooId']));
|
|
|
|
$this->assertTrue(isset($array['requirements']['fooId']['dataType']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructWithParameters()
|
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
'parameters' => array(
|
|
|
|
array(
|
|
|
|
'name' => 'fooId',
|
|
|
|
'dataType' => 'integer',
|
|
|
|
'description' => 'Some description'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$annot = new ApiDoc($data);
|
|
|
|
$array = $annot->toArray();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($array));
|
|
|
|
$this->assertTrue(isset($array['parameters']['fooId']));
|
|
|
|
$this->assertTrue(isset($array['parameters']['fooId']['dataType']));
|
|
|
|
}
|
2012-04-13 15:20:45 +02:00
|
|
|
}
|