mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-08 18:49:26 +03:00
Add basic tests
This commit is contained in:
parent
4c984c2ca9
commit
2791512139
34
Tests/ApiDocExtratorTest.php
Normal file
34
Tests/ApiDocExtratorTest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?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;
|
||||
|
||||
class ApiDocExtractorTest extends WebTestCase
|
||||
{
|
||||
public function testAll()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
||||
$data = $extractor->all();
|
||||
|
||||
$this->assertCount(2, $data);
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
||||
$data = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::indexAction', 'test_route_1');
|
||||
|
||||
$this->assertTrue(isset($data['route']));
|
||||
$this->assertTrue(isset($data['annotation']));
|
||||
}
|
||||
}
|
41
Tests/Fixtures/Controller/TestController.php
Normal file
41
Tests/Fixtures/Controller/TestController.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?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\Fixtures\Controller;
|
||||
|
||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||
|
||||
class TestController
|
||||
{
|
||||
/**
|
||||
* @ApiDoc(
|
||||
* resource=true,
|
||||
* description="index action",
|
||||
* filters={
|
||||
* {"name"="a", "dataType"="integer"},
|
||||
* {"name"="b", "dataType"="string", "arbitrary"={"arg1", "arg2"}}
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @ApiDoc(
|
||||
* description="create test",
|
||||
* formType="Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType"
|
||||
* )
|
||||
*/
|
||||
public function postTestAction()
|
||||
{
|
||||
}
|
||||
}
|
42
Tests/Fixtures/Form/TestType.php
Normal file
42
Tests/Fixtures/Form/TestType.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?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\Fixtures\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilder;
|
||||
|
||||
class TestType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilder $builder, array $options)
|
||||
{
|
||||
$builder->add('a');
|
||||
$builder->add('b');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultOptions()
|
||||
{
|
||||
return array(
|
||||
'data_class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
|
||||
);
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
}
|
27
Tests/Fixtures/Model/Test.php
Normal file
27
Tests/Fixtures/Model/Test.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?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\Fixtures\Model;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
class Test
|
||||
{
|
||||
/**
|
||||
* @Assert\MinLength("foo");
|
||||
*/
|
||||
public $a;
|
||||
|
||||
/**
|
||||
* @Assert\Type("DateTime");
|
||||
*/
|
||||
public $b;
|
||||
}
|
18
Tests/Fixtures/NelmioApiDocTestBundle.php
Normal file
18
Tests/Fixtures/NelmioApiDocTestBundle.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?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\Fixtures;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class NelmioApiDocTestBundle extends Bundle
|
||||
{
|
||||
}
|
90
Tests/Fixtures/app/AppKernel.php
Normal file
90
Tests/Fixtures/app/AppKernel.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
||||
|
||||
// get the autoload file
|
||||
$dir = __DIR__;
|
||||
$lastDir = null;
|
||||
while ($dir !== $lastDir) {
|
||||
$lastDir = $dir;
|
||||
|
||||
if (is_file($dir.'/autoload.php')) {
|
||||
require_once $dir.'/autoload.php';
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_file($dir.'/autoload.php.dist')) {
|
||||
require_once $dir.'/autoload.php.dist';
|
||||
break;
|
||||
}
|
||||
|
||||
$dir = dirname($dir);
|
||||
}
|
||||
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
/**
|
||||
* App Test Kernel for functional tests.
|
||||
*/
|
||||
class AppKernel extends Kernel
|
||||
{
|
||||
public function __construct($environment, $debug)
|
||||
{
|
||||
parent::__construct($environment, $debug);
|
||||
}
|
||||
|
||||
public function registerBundles()
|
||||
{
|
||||
return array(
|
||||
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||
new \Symfony\Bundle\TwigBundle\TwigBundle(),
|
||||
new \Nelmio\ApiDocBundle\NelmioApiDocBundle(),
|
||||
new \Nelmio\ApiDocBundle\Tests\Fixtures\NelmioApiDocTestBundle(),
|
||||
);
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
public function getRootDir()
|
||||
{
|
||||
return __DIR__;
|
||||
}
|
||||
|
||||
public function getCacheDir()
|
||||
{
|
||||
return sys_get_temp_dir().'/'.Kernel::VERSION.'/nelmio-api-doc/cache/'.$this->environment;
|
||||
}
|
||||
|
||||
public function getLogDir()
|
||||
{
|
||||
return sys_get_temp_dir().'/'.Kernel::VERSION.'/nelmio-api-doc/logs';
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(__DIR__.'/config/'.$this->environment.'.yml');
|
||||
}
|
||||
|
||||
public function serialize()
|
||||
{
|
||||
return serialize(array($this->getEnvironment(), $this->isDebug()));
|
||||
}
|
||||
|
||||
public function unserialize($str)
|
||||
{
|
||||
call_user_func_array(array($this, '__construct'), unserialize($str));
|
||||
}
|
||||
}
|
20
Tests/Fixtures/app/config/default.yml
Normal file
20
Tests/Fixtures/app/config/default.yml
Normal file
@ -0,0 +1,20 @@
|
||||
framework:
|
||||
charset: UTF-8
|
||||
secret: test
|
||||
csrf_protection:
|
||||
enabled: true
|
||||
router: { resource: "%kernel.root_dir%/config/routing.yml" }
|
||||
validation: { enabled: true, enable_annotations: true }
|
||||
form: ~
|
||||
test: ~
|
||||
default_locale: en
|
||||
session:
|
||||
auto_start: true
|
||||
storage_id: session.storage.mock_file
|
||||
profiler: { only_exceptions: false }
|
||||
templating: { engines: ['twig'] }
|
||||
|
||||
# Twig Configuration
|
||||
twig:
|
||||
debug: %kernel.debug%
|
||||
strict_variables: %kernel.debug%
|
15
Tests/Fixtures/app/config/routing.yml
Normal file
15
Tests/Fixtures/app/config/routing.yml
Normal file
@ -0,0 +1,15 @@
|
||||
test_route_1:
|
||||
pattern: /tests
|
||||
defaults: { _controller: NelmioApiDocTestBundle:Test:index, _format: json }
|
||||
requirements:
|
||||
_method: GET
|
||||
|
||||
test_route_2:
|
||||
pattern: /tests
|
||||
defaults: { _controller: NelmioApiDocTestBundle:Test:postTest, _format: json }
|
||||
requirements:
|
||||
_method: POST
|
||||
|
||||
NelmioApiDocBundle:
|
||||
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
|
||||
prefix: /
|
65
Tests/SimpleFormatterTest.php
Normal file
65
Tests/SimpleFormatterTest.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?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;
|
||||
|
||||
class SimpleFormatterTest extends WebTestCase
|
||||
{
|
||||
public function testFormat()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
||||
$data = $extractor->all();
|
||||
$result = $container->get('nelmio_api_doc.formatter.simple_formatter')->format($data);
|
||||
|
||||
$expected = array(
|
||||
'/tests' => array(
|
||||
array(
|
||||
'method' => 'GET',
|
||||
'uri' => '/tests',
|
||||
'requirements' => array(),
|
||||
'filters' => array(
|
||||
'a' => array(
|
||||
'dataType' => 'integer',
|
||||
),
|
||||
'b' => array(
|
||||
'dataType' => 'string',
|
||||
'arbitrary' => array(
|
||||
'arg1',
|
||||
'arg2',
|
||||
),
|
||||
),
|
||||
),
|
||||
'description' => 'index action',
|
||||
),
|
||||
array(
|
||||
'method' => 'POST',
|
||||
'uri' => '/tests',
|
||||
'requirements' => array(),
|
||||
'parameters' => array(
|
||||
'a' => array(
|
||||
'dataType' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
'b' => array(
|
||||
'dataType' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
'description' => 'create test',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
56
Tests/WebTestCase.php
Normal file
56
Tests/WebTestCase.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Tests;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
abstract class WebTestCase extends BaseWebTestCase
|
||||
{
|
||||
protected function deleteTmpDir($testCase)
|
||||
{
|
||||
if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/'.$testCase)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fs = new Filesystem();
|
||||
$fs->remove($dir);
|
||||
}
|
||||
|
||||
protected function getContainer(array $options = array())
|
||||
{
|
||||
if (!static::$kernel) {
|
||||
static::$kernel = static::createKernel($options);
|
||||
}
|
||||
static::$kernel->boot();
|
||||
|
||||
return static::$kernel->getContainer();
|
||||
}
|
||||
|
||||
static protected function getKernelClass()
|
||||
{
|
||||
require_once __DIR__.'/Fixtures/app/AppKernel.php';
|
||||
|
||||
return 'Nelmio\ApiDocBundle\Tests\Functional\AppKernel';
|
||||
}
|
||||
|
||||
static protected function createKernel(array $options = array())
|
||||
{
|
||||
$class = self::getKernelClass();
|
||||
|
||||
return new $class(
|
||||
'default',
|
||||
isset($options['debug']) ? $options['debug'] : true
|
||||
);
|
||||
}
|
||||
}
|
17
Tests/bootstrap.php
Normal file
17
Tests/bootstrap.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
function includeIfExists($file) {
|
||||
if (file_exists($file)) {
|
||||
return include $file;
|
||||
}
|
||||
}
|
||||
|
||||
if ((!$loader = includeIfExists(__DIR__.'/../../../../../.composer/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../vendor/.composer/autoload.php'))) {
|
||||
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
|
||||
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
|
||||
'php composer.phar install'.PHP_EOL);
|
||||
}
|
||||
|
||||
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
|
||||
|
||||
return $loader;
|
31
phpunit.xml.dist
Normal file
31
phpunit.xml.dist
Normal file
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
|
||||
<phpunit
|
||||
backupGlobals = "false"
|
||||
backupStaticAttributes = "false"
|
||||
colors = "true"
|
||||
convertErrorsToExceptions = "true"
|
||||
convertNoticesToExceptions = "true"
|
||||
convertWarningsToExceptions = "true"
|
||||
processIsolation = "false"
|
||||
stopOnFailure = "false"
|
||||
syntaxCheck = "false"
|
||||
bootstrap = "Tests/bootstrap.php" >
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Project Test Suite">
|
||||
<directory>Tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>.</directory>
|
||||
<exclude>
|
||||
<directory>Resources</directory>
|
||||
<directory>Tests</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
Loading…
x
Reference in New Issue
Block a user