Skip to content

Commit

Permalink
Make API controller services public for FOSRest (#1654)
Browse files Browse the repository at this point in the history
Fixes #1652
  • Loading branch information
Jeroen de Kok committed Feb 10, 2020
1 parent cf039fb commit e9a4c7d
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 7 deletions.
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"php": "^7.1",
"guzzlehttp/psr7": "^1.0",
"imagine/imagine": "^0.6 || ^0.7 || ^1.0",
"jms/serializer-bundle": "^1.0 || ^2.0 || ^3.0",
"jms/serializer-bundle": "^2.3.1 || ^3.0",
"knplabs/gaufrette": "^0.8 || ^0.9",
"kriswallsmith/buzz": "^0.15 || ^0.16",
"psr/log": "^1.0",
Expand All @@ -43,7 +43,7 @@
"symfony/filesystem": "^3.4 || ^4.2",
"symfony/finder": "^3.4 || ^4.2",
"symfony/form": "^3.4.24 || ^4.2",
"symfony/framework-bundle": "^3.4 || ^4.2",
"symfony/framework-bundle": "^3.4.31 || ^4.2",
"symfony/http-foundation": "^3.4 || ^4.2",
"symfony/http-kernel": "^3.4 || ^4.2",
"symfony/options-resolver": "^3.4 || ^4.2",
Expand Down Expand Up @@ -71,18 +71,20 @@
},
"require-dev": {
"aws/aws-sdk-php": "^2.8",
"friendsofsymfony/rest-bundle": "^2.1",
"friendsofsymfony/rest-bundle": "^2.6",
"jackalope/jackalope-doctrine-dbal": "^1.1",
"liip/imagine-bundle": "^1.9 || ^2.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
"nelmio/api-doc-bundle": "^2.11",
"sonata-project/admin-bundle": "^3.31",
"nelmio/api-doc-bundle": "^2.13.4",
"sensio/framework-extra-bundle": "^5.1",
"sonata-project/admin-bundle": "^3.18.3",
"sonata-project/block-bundle": "^3.16.1",
"sonata-project/datagrid-bundle": "^2.3",
"sonata-project/doctrine-orm-admin-bundle": "^3.4",
"sonata-project/datagrid-bundle": "^2.5",
"sonata-project/doctrine-orm-admin-bundle": "^3.14",
"sonata-project/formatter-bundle": "^3.4 || ^4.0",
"sonata-project/notification-bundle": "^3.3",
"sonata-project/seo-bundle": "^2.5",
"symfony/browser-kit": "^3.4 || ^4.2",
"symfony/phpunit-bridge": "^5.0",
"symfony/security-csrf": "^3.4 || ^4.2"
},
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/api_controllers.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="true"/>
<service id="sonata.media.controller.api.gallery" class="Sonata\MediaBundle\Controller\Api\GalleryController">
<argument type="service" id="sonata.media.manager.gallery"/>
<argument type="service" id="sonata.media.manager.media"/>
Expand Down
86 changes: 86 additions & 0 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\App;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use FOS\RestBundle\FOSRestBundle;
use JMS\SerializerBundle\JMSSerializerBundle;
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
use Sonata\MediaBundle\SonataMediaBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

final class AppKernel extends Kernel
{
use MicroKernelTrait;

public function __construct()
{
parent::__construct('test', false);
}

public function registerBundles()
{
return [
new FrameworkBundle(),
new SecurityBundle(),
new TwigBundle(),
new FOSRestBundle(),
new SonataMediaBundle(),
new JMSSerializerBundle(),
new DoctrineBundle(),
new NelmioApiDocBundle(),
new SensioFrameworkExtraBundle(),
];
}

public function getCacheDir(): string
{
return $this->getBaseDir().'cache';
}

public function getLogDir(): string
{
return $this->getBaseDir().'log';
}

public function getProjectDir(): string
{
return __DIR__;
}

protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->import(__DIR__.'/routes.yml', '/', 'yaml');
}

protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader)
{
$loader->load(__DIR__.'/config.yml');
$loader->load(__DIR__.'/security.yml');
$containerBuilder->setParameter('app.base_dir', $this->getBaseDir());
}

private function getBaseDir(): string
{
return sys_get_temp_dir().'/sonata-media-bundle/var/';
}
}
39 changes: 39 additions & 0 deletions tests/App/Entity/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sonata\MediaBundle\Tests\Entity\Media as BaseMedia;

/**
* @ORM\Entity
* @ORM\Table(name="media__media")
*/
class Media extends BaseMedia
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;

/**
* Get id.
*/
public function getId(): int
{
return $this->id;
}
}
74 changes: 74 additions & 0 deletions tests/App/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
framework:
secret: secret
form:
enabled: true
translator:
enabled: true

twig:
paths:
- '%kernel.project_dir%/templates'

sonata_media:
db_driver: doctrine_orm
class:
media: Sonata\MediaBundle\Tests\App\Entity\Media
default_context: default
contexts:
default:
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
- sonata.media.provider.vimeo

formats:
small: {width: 100, quality: 70}
big: {width: 500, quality: 70}

cdn:
server:
path: /uploads/media

filesystem:
local:
directory: "%kernel.root_dir%/../public/uploads/media"
create: false

fos_rest:
param_fetcher_listener: true
body_listener: false
format_listener:
rules:
- {path: ^/, priorities: [json], fallback_format: ~, prefer_extension: true}
view:
view_response_listener: true
body_converter:
enabled: false
validate: false

sensio_framework_extra:
router: {annotations: true}
request: {converters: true}
view: {annotations: true}

doctrine:
dbal:
driver: pdo_sqlite
path: "%app.base_dir%sonata_media_test.db3"
orm:
entity_managers:
default:
mappings:
SonataMediaBundle: ~
SonataMediaTest:
type: annotation
dir: "%kernel.project_dir%/Entity"
is_bundle: false
prefix: Sonata\MediaBundle\Tests\App\Entity

services:
_defaults:
autowire: true
autoconfigure: true
4 changes: 4 additions & 0 deletions tests/App/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sonata_api_media:
type: rest
prefix: /api
resource: "@SonataMediaBundle/Resources/config/routing/api.xml"
13 changes: 13 additions & 0 deletions tests/App/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
security:
role_hierarchy: ~

providers:
in_memory:
memory:
users: ~

firewalls:
test:
security: false

access_control: ~
Empty file added tests/App/templates/.gitkeep
Empty file.
56 changes: 56 additions & 0 deletions tests/Functional/Api/MediaControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\Functional;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
use Sonata\MediaBundle\Tests\App\AppKernel;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;

final class MediaControllerTest extends WebTestCase
{
public function testGetMediaAction(): void
{
$kernel = new AppKernel();
$kernel->boot();

$application = new Application($kernel);
$application->setAutoExit(false);

$application->run(new ArrayInput([
'command' => 'doctrine:database:drop',
'--force' => '1',
]));

$application->run(new ArrayInput([
'command' => 'doctrine:database:create',
]));

$command = new CreateSchemaDoctrineCommand();
$application->add($command);
$input = new ArrayInput([
'command' => 'doctrine:schema:create',
]);
$input->setInteractive(false);

$command->run($input, new ConsoleOutput());

$client = new Client($kernel);
$client->request('GET', '/api/media');
$this->assertSame(200, $client->getResponse()->getStatusCode());
}
}

0 comments on commit e9a4c7d

Please sign in to comment.