From 27915121395ca8049ff76d592df412bff6c54fbe Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 13 Apr 2012 14:11:54 +0200 Subject: [PATCH] Add basic tests --- Tests/ApiDocExtratorTest.php | 34 ++++++++ Tests/Fixtures/Controller/TestController.php | 41 +++++++++ Tests/Fixtures/Form/TestType.php | 42 +++++++++ Tests/Fixtures/Model/Test.php | 27 ++++++ Tests/Fixtures/NelmioApiDocTestBundle.php | 18 ++++ Tests/Fixtures/app/AppKernel.php | 90 ++++++++++++++++++++ Tests/Fixtures/app/config/default.yml | 20 +++++ Tests/Fixtures/app/config/routing.yml | 15 ++++ Tests/SimpleFormatterTest.php | 65 ++++++++++++++ Tests/WebTestCase.php | 56 ++++++++++++ Tests/bootstrap.php | 17 ++++ phpunit.xml.dist | 31 +++++++ 12 files changed, 456 insertions(+) create mode 100644 Tests/ApiDocExtratorTest.php create mode 100644 Tests/Fixtures/Controller/TestController.php create mode 100644 Tests/Fixtures/Form/TestType.php create mode 100644 Tests/Fixtures/Model/Test.php create mode 100644 Tests/Fixtures/NelmioApiDocTestBundle.php create mode 100644 Tests/Fixtures/app/AppKernel.php create mode 100644 Tests/Fixtures/app/config/default.yml create mode 100644 Tests/Fixtures/app/config/routing.yml create mode 100644 Tests/SimpleFormatterTest.php create mode 100644 Tests/WebTestCase.php create mode 100644 Tests/bootstrap.php create mode 100644 phpunit.xml.dist diff --git a/Tests/ApiDocExtratorTest.php b/Tests/ApiDocExtratorTest.php new file mode 100644 index 0000000..6f977f5 --- /dev/null +++ b/Tests/ApiDocExtratorTest.php @@ -0,0 +1,34 @@ + + * + * 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'])); + } +} diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php new file mode 100644 index 0000000..ee63543 --- /dev/null +++ b/Tests/Fixtures/Controller/TestController.php @@ -0,0 +1,41 @@ + + * + * 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() + { + } +} diff --git a/Tests/Fixtures/Form/TestType.php b/Tests/Fixtures/Form/TestType.php new file mode 100644 index 0000000..d2bd511 --- /dev/null +++ b/Tests/Fixtures/Form/TestType.php @@ -0,0 +1,42 @@ + + * + * 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 ''; + } +} diff --git a/Tests/Fixtures/Model/Test.php b/Tests/Fixtures/Model/Test.php new file mode 100644 index 0000000..96992b8 --- /dev/null +++ b/Tests/Fixtures/Model/Test.php @@ -0,0 +1,27 @@ + + * + * 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; +} diff --git a/Tests/Fixtures/NelmioApiDocTestBundle.php b/Tests/Fixtures/NelmioApiDocTestBundle.php new file mode 100644 index 0000000..6664ed8 --- /dev/null +++ b/Tests/Fixtures/NelmioApiDocTestBundle.php @@ -0,0 +1,18 @@ + + * + * 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 +{ +} diff --git a/Tests/Fixtures/app/AppKernel.php b/Tests/Fixtures/app/AppKernel.php new file mode 100644 index 0000000..1d08af4 --- /dev/null +++ b/Tests/Fixtures/app/AppKernel.php @@ -0,0 +1,90 @@ + + * + * 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)); + } +} diff --git a/Tests/Fixtures/app/config/default.yml b/Tests/Fixtures/app/config/default.yml new file mode 100644 index 0000000..cd9f567 --- /dev/null +++ b/Tests/Fixtures/app/config/default.yml @@ -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% diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml new file mode 100644 index 0000000..6800e95 --- /dev/null +++ b/Tests/Fixtures/app/config/routing.yml @@ -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: / diff --git a/Tests/SimpleFormatterTest.php b/Tests/SimpleFormatterTest.php new file mode 100644 index 0000000..a5b3f67 --- /dev/null +++ b/Tests/SimpleFormatterTest.php @@ -0,0 +1,65 @@ + + * + * 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); + } +} diff --git a/Tests/WebTestCase.php b/Tests/WebTestCase.php new file mode 100644 index 0000000..eb39dd5 --- /dev/null +++ b/Tests/WebTestCase.php @@ -0,0 +1,56 @@ + + * + * 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 + ); + } +} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php new file mode 100644 index 0000000..1aeca39 --- /dev/null +++ b/Tests/bootstrap.php @@ -0,0 +1,17 @@ + + + + + + + + Tests + + + + + + . + + Resources + Tests + + + +