331 lines
11 KiB
PHP
Raw Normal View History

2016-07-12 00:33:55 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-07-12 00:33:55 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-07-12 00:33:55 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2017-06-14 13:47:53 +02:00
namespace Nelmio\ApiDocBundle\Tests\Functional;
2016-07-29 10:22:40 +02:00
use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle;
use FOS\RestBundle\FOSRestBundle;
use Hateoas\Configuration\Embedded;
2017-06-25 15:40:07 +02:00
use JMS\SerializerBundle\JMSSerializerBundle;
2016-12-29 12:09:26 +01:00
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\BazingaUser;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex;
2019-03-11 12:53:35 +01:00
use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\PrivateProtectedExposure;
2021-11-06 07:35:50 -05:00
use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraintsWithValidationGroups;
use Nelmio\ApiDocBundle\Tests\Functional\ModelDescriber\VirtualTypeClassDoesNotExistsHandlerDefinedDescriber;
2016-07-12 00:33:55 +02:00
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
2016-11-18 22:16:53 +01:00
use Symfony\Bundle\TwigBundle\TwigBundle;
2016-07-12 00:33:55 +02:00
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
2016-07-12 00:33:55 +02:00
use Symfony\Component\HttpKernel\Kernel;
2021-11-30 15:12:38 +01:00
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Serializer\Annotation\SerializedName;
2016-07-12 00:33:55 +02:00
class TestKernel extends Kernel
{
const USE_JMS = 1;
const USE_BAZINGA = 2;
const ERROR_ARRAY_ITEMS = 4;
const USE_VALIDATION_GROUPS = 8;
2016-07-12 00:33:55 +02:00
use MicroKernelTrait;
private $flags;
2017-06-25 15:40:07 +02:00
public function __construct(int $flags = 0)
2017-06-25 15:40:07 +02:00
{
parent::__construct('test'.$flags, true);
2017-06-25 15:40:07 +02:00
$this->flags = $flags;
2017-06-25 15:40:07 +02:00
}
2016-07-12 00:33:55 +02:00
/**
* {@inheritdoc}
*/
2021-12-11 14:19:43 +01:00
public function registerBundles(): iterable
2016-07-12 00:33:55 +02:00
{
2017-06-25 15:40:07 +02:00
$bundles = [
2016-07-12 00:33:55 +02:00
new FrameworkBundle(),
2016-11-18 22:16:53 +01:00
new TwigBundle(),
2016-07-12 00:33:55 +02:00
new SensioFrameworkExtraBundle(),
2016-07-29 10:22:40 +02:00
new ApiPlatformBundle(),
2016-12-29 12:09:26 +01:00
new NelmioApiDocBundle(),
2016-07-29 10:22:40 +02:00
new TestBundle(),
new FOSRestBundle(),
2016-07-12 00:33:55 +02:00
];
2017-06-25 15:40:07 +02:00
if ($this->flags & self::USE_JMS) {
2017-06-25 15:40:07 +02:00
$bundles[] = new JMSSerializerBundle();
2020-11-20 17:10:21 +01:00
if ($this->flags & self::USE_BAZINGA) {
$bundles[] = new BazingaHateoasBundle();
}
2017-06-25 15:40:07 +02:00
}
return $bundles;
2016-07-12 00:33:55 +02:00
}
2016-07-15 00:04:07 +02:00
2016-07-12 00:33:55 +02:00
/**
* {@inheritdoc}
*/
2021-11-30 15:12:38 +01:00
protected function configureRoutes($routes)
2016-07-12 00:33:55 +02:00
{
2021-11-30 15:12:38 +01:00
$this->import($routes, __DIR__.'/Resources/routes.yaml', '/', 'yaml');
2017-06-25 15:40:07 +02:00
if (class_exists(SerializedName::class)) {
2021-11-30 15:12:38 +01:00
$this->import($routes, __DIR__.'/Controller/SerializedNameController.php', '/', 'annotation');
}
if ($this->flags & self::USE_JMS) {
2021-11-30 15:12:38 +01:00
$this->import($routes, __DIR__.'/Controller/JMSController.php', '/', 'annotation');
2017-06-25 15:40:07 +02:00
}
if ($this->flags & self::USE_BAZINGA) {
2021-11-30 15:12:38 +01:00
$this->import($routes, __DIR__.'/Controller/BazingaController.php', '/', 'annotation');
try {
new \ReflectionMethod(Embedded::class, 'getType');
2021-11-30 15:12:38 +01:00
$this->import($routes, __DIR__.'/Controller/BazingaTypedController.php', '/', 'annotation');
} catch (\ReflectionException $e) {
}
}
if ($this->flags & self::ERROR_ARRAY_ITEMS) {
2021-11-30 15:12:38 +01:00
$this->import($routes, __DIR__.'/Controller/ArrayItemsErrorController.php', '/', 'annotation');
}
}
/**
* BC for sf < 5.1.
*/
2021-12-21 13:46:02 +01:00
private function import($routes, $resource, $prefix, $type)
{
2021-11-30 15:12:38 +01:00
if ($routes instanceof RoutingConfigurator) {
$routes->withPath($prefix)->import($resource, $type);
} else {
$routes->import($resource, $prefix, $type);
}
2016-07-12 00:33:55 +02:00
}
/**
* {@inheritdoc}
*/
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
2019-11-19 18:24:58 +01:00
$framework = [
2019-11-22 14:07:00 +01:00
'assets' => true,
2016-07-12 00:33:55 +02:00
'secret' => 'MySecretKey',
2016-07-29 10:22:40 +02:00
'test' => null,
'validation' => null,
2017-06-24 17:49:00 +02:00
'form' => null,
2017-06-14 13:47:53 +02:00
'serializer' => ['enable_annotations' => true],
'property_access' => true,
2019-11-19 18:24:58 +01:00
];
$c->loadFromExtension('framework', $framework);
2018-11-04 12:24:45 +01:00
$c->loadFromExtension('twig', [
'strict_variables' => '%kernel.debug%',
'exception_controller' => null,
2018-11-04 12:24:45 +01:00
]);
2019-11-19 18:24:58 +01:00
$c->loadFromExtension('sensio_framework_extra', [
'router' => [
'annotations' => false,
],
]);
$c->loadFromExtension('api_platform', [
'mapping' => ['paths' => ['%kernel.project_dir%/Tests/Functional/Entity']],
]);
$c->loadFromExtension('fos_rest', [
'format_listener' => [
'rules' => [
[
'path' => '^/',
'fallback_format' => 'json',
],
],
],
]);
// If FOSRestBundle 2.8
if (class_exists(\FOS\RestBundle\EventListener\ResponseStatusCodeListener::class)) {
$c->loadFromExtension('fos_rest', [
'exception' => [
'enabled' => false,
'exception_listener' => false,
'serialize_exceptions' => false,
],
'body_listener' => false,
'routing_loader' => false,
]);
}
// Filter routes
2016-12-29 12:09:26 +01:00
$c->loadFromExtension('nelmio_api_doc', [
'use_validation_groups' => boolval($this->flags & self::USE_VALIDATION_GROUPS),
'documentation' => [
'servers' => [ // from https://github.com/nelmio/NelmioApiDocBundle/issues/1691
[
'url' => 'https://api.example.com/secured/{version}',
'variables' => ['version' => ['default' => 'v1']],
],
],
'info' => [
2018-10-06 14:42:47 +02:00
'title' => 'My Default App',
],
'paths' => [
// Ensures we can define routes in Yaml without defining OperationIds
// See https://github.com/zircote/swagger-php/issues/1153
'/api/test-from-yaml' => ['get' => [
'responses' => [
200 => ['description' => 'success'],
],
]],
'/api/test-from-yaml2' => ['get' => [
'responses' => [
200 => ['description' => 'success'],
],
]],
],
'components' => [
'schemas' => [
'Test' => [
'type' => 'string',
],
// Ensures https://github.com/nelmio/NelmioApiDocBundle/issues/1650 is working
'Pet' => [
'type' => 'object',
],
'Cat' => [
'allOf' => [
['$ref' => '#/components/schemas/Pet'],
['type' => 'object'],
],
],
2021-06-07 18:20:25 +02:00
'AddProp' => [
'type' => 'object',
'additionalProperties' => false,
],
],
'parameters' => [
'test' => [
'name' => 'id',
'in' => 'path',
'required' => true,
],
],
'responses' => [
'201' => [
'description' => 'Awesome description',
],
],
],
],
'areas' => [
2018-07-27 09:59:45 +05:00
'default' => [
'path_patterns' => ['^/api(?!/admin)'],
'host_patterns' => ['^api\.'],
],
'test' => [
'path_patterns' => ['^/test'],
'host_patterns' => ['^api-test\.'],
'documentation' => [
'info' => [
'title' => 'My Test App',
],
2018-07-27 09:44:19 +05:00
],
2018-07-27 09:59:45 +05:00
],
],
2019-03-11 12:53:35 +01:00
'models' => [
'names' => [
[
'alias' => 'PrivateProtectedExposure',
'type' => PrivateProtectedExposure::class,
],
2019-03-11 12:53:35 +01:00
[
'alias' => 'JMSPicture_mini',
'type' => JMSPicture::class,
'groups' => ['mini'],
],
[
'alias' => 'BazingaUser_grouped',
'type' => BazingaUser::class,
'groups' => ['foo'],
],
[
'alias' => 'JMSComplex',
'type' => JMSComplex::class,
'groups' => [
'list',
'details',
'User' => ['list'],
],
],
[
'alias' => 'JMSComplexDefault',
'type' => JMSComplex::class,
'groups' => null,
],
[
'alias' => 'SymfonyConstraintsTestGroup',
'type' => SymfonyConstraintsWithValidationGroups::class,
'groups' => ['test'],
],
[
'alias' => 'SymfonyConstraintsDefaultGroup',
'type' => SymfonyConstraintsWithValidationGroups::class,
'groups' => null,
],
2019-03-11 12:53:35 +01:00
],
],
]);
$def = new Definition(VirtualTypeClassDoesNotExistsHandlerDefinedDescriber::class);
$def->addTag('nelmio_api_doc.model_describer');
$c->setDefinition('nelmio.test.jms.virtual_type.describer', $def);
2016-07-12 00:33:55 +02:00
}
2017-06-25 15:40:07 +02:00
/**
* {@inheritdoc}
*/
2021-12-11 14:19:43 +01:00
public function getCacheDir(): string
2017-06-25 15:40:07 +02:00
{
return parent::getCacheDir().'/'.$this->flags;
2017-06-25 15:40:07 +02:00
}
/**
* {@inheritdoc}
*/
2021-12-11 14:19:43 +01:00
public function getLogDir(): string
2017-06-25 15:40:07 +02:00
{
return parent::getLogDir().'/'.$this->flags;
2017-06-25 15:40:07 +02:00
}
public function serialize()
{
return serialize($this->useJMS);
}
public function unserialize($str)
{
$this->__construct(unserialize($str));
}
2016-07-12 00:33:55 +02:00
}