Add basic tests

This commit is contained in:
Jordi Boggiano 2012-04-13 14:11:54 +02:00
parent 4c984c2ca9
commit 2791512139
12 changed files with 456 additions and 0 deletions

View 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']));
}
}

View 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()
{
}
}

View 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 '';
}
}

View 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;
}

View 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
{
}

View 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));
}
}

View 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%

View 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: /

View 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
View 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
View 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
View 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>