Skip to content

Commit

Permalink
Remove templating engine (#653)
Browse files Browse the repository at this point in the history
* Remove templating engine usage

* Adapt unit test

* Clean configuration

* Update composer dependencies

* Adapt twig test configuration

* Add changelog
  • Loading branch information
deguif committed Sep 17, 2020
1 parent 1387cdb commit d8f41fe
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 82 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This changelog references the relevant changes done in 6.0 versions.
* Dropped support for PHP 7.1 [[#651](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/651)]
* Dropped support for Symfony versions anterior to `4.4` [[#648](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/648)]
* Fixed form submission/validation [[#643](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/643)]
* **[BC break]** Changed signature of method `FOS\OAuthServerBundle\Controller\AuthorizeController::renderAuthorize()` [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]
* **[BC break]** Removed support for templating engine [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]

### 2.0.0-ALPHA0 (2018-05-01)

Expand Down
44 changes: 15 additions & 29 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -31,6 +30,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment as TwigEnvironment;

/**
* Controller handling basic authorization.
Expand Down Expand Up @@ -64,11 +64,6 @@ class AuthorizeController
*/
private $oAuth2Server;

/**
* @var EngineInterface
*/
private $templating;

/**
* @var RequestStack
*/
Expand All @@ -79,6 +74,11 @@ class AuthorizeController
*/
private $tokenStorage;

/**
* @var TwigEnvironment
*/
private $twig;

/**
* @var UrlGeneratorInterface
*/
Expand All @@ -89,11 +89,6 @@ class AuthorizeController
*/
private $clientManager;

/**
* @var string
*/
private $templateEngineType;

/**
* @var EventDispatcherInterface
*/
Expand All @@ -106,32 +101,29 @@ class AuthorizeController
* @todo This controller could be refactored to not rely on so many dependencies
*
* @param SessionInterface $session
* @param string $templateEngineType
*/
public function __construct(
RequestStack $requestStack,
Form $authorizeForm,
AuthorizeFormHandler $authorizeFormHandler,
OAuth2 $oAuth2Server,
EngineInterface $templating,
TokenStorageInterface $tokenStorage,
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcherInterface $eventDispatcher,
SessionInterface $session = null,
$templateEngineType = 'twig'
TwigEnvironment $twig,
SessionInterface $session = null
) {
$this->requestStack = $requestStack;
$this->session = $session;
$this->authorizeForm = $authorizeForm;
$this->authorizeFormHandler = $authorizeFormHandler;
$this->oAuth2Server = $oAuth2Server;
$this->templating = $templating;
$this->tokenStorage = $tokenStorage;
$this->router = $router;
$this->clientManager = $clientManager;
$this->templateEngineType = $templateEngineType;
$this->eventDispatcher = $eventDispatcher;
$this->twig = $twig;
}

/**
Expand Down Expand Up @@ -169,12 +161,10 @@ public function authorizeAction(Request $request)
return $this->processSuccess($user, $formHandler, $request);
}

$data = [
return $this->renderAuthorize([
'form' => $form->createView(),
'client' => $this->getClient(),
];

return $this->renderAuthorize($data, $this->templating, $this->templateEngineType);
]);
}

/**
Expand Down Expand Up @@ -217,7 +207,7 @@ protected function getRedirectionUrl(UserInterface $user)
}

/**
* @return ClientInterface
* @return ClientInterface
*/
protected function getClient()
{
Expand All @@ -243,14 +233,10 @@ protected function getClient()
return $this->client;
}

/**
* @throws \RuntimeException
*/
protected function renderAuthorize(array $data, EngineInterface $engine, string $engineType): Response
protected function renderAuthorize(array $context): Response
{
return $engine->renderResponse(
'@FOSOAuthServer/Authorize/authorize.html.'.$engineType,
$data
return new Response(
$this->twig->render('@FOSOAuthServer/Authorize/authorize.html.twig', $context)
);
}

Expand Down
15 changes: 0 additions & 15 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public function getConfigTreeBuilder()

$this->addAuthorizeSection($rootNode);
$this->addServiceSection($rootNode);
$this->addTemplateSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -135,18 +134,4 @@ private function addServiceSection(ArrayNodeDefinition $node)
->end()
;
}

private function addTemplateSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('template')
->addDefaultsIfNotSet()
->children()
->scalarNode('engine')->defaultValue('twig')->end()
->end()
->end()
->end()
;
}
}
1 change: 0 additions & 1 deletion DependencyInjection/FOSOAuthServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function load(array $configs, ContainerBuilder $container)
'refresh_token_class' => 'fos_oauth_server.model.refresh_token.class',
'auth_code_class' => 'fos_oauth_server.model.auth_code.class',
],
'template' => 'fos_oauth_server.template.%s',
]);

// Handle the MongoDB document manager name in a specific way as it does not have a registry to make it easy
Expand Down
3 changes: 1 addition & 2 deletions Resources/config/authorize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
<argument type="service" id="fos_oauth_server.authorize.form" />
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
<argument type="service" id="fos_oauth_server.server" />
<argument type="service" id="templating" />
<argument type="service" id="security.token_storage" />
<argument type="service" id="router" />
<argument type="service" id="fos_oauth_server.client_manager" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="twig" />
<argument type="service" id="session" on-invalid="null" />
<argument>%fos_oauth_server.template.engine%</argument>
</service>
</services>

Expand Down
49 changes: 19 additions & 30 deletions Tests/Controller/AuthorizeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use OAuth2\OAuth2;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormView;
Expand All @@ -33,6 +32,7 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment as TwigEnvironment;

class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -62,14 +62,14 @@ class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
protected $oAuth2Server;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|EngineInterface
* @var \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface
*/
protected $templateEngine;
protected $tokenStorage;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface
* @var \PHPUnit_Framework_MockObject_MockObject|TwigEnvironment
*/
protected $tokenStorage;
protected $twig;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|UrlGeneratorInterface
Expand All @@ -86,11 +86,6 @@ class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
*/
protected $eventDispatcher;

/**
* @var string
*/
protected $templateEngineType;

/**
* @var AuthorizeController
*/
Expand Down Expand Up @@ -149,11 +144,11 @@ public function setUp()
->disableOriginalConstructor()
->getMock()
;
$this->templateEngine = $this->getMockBuilder(EngineInterface::class)
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)
->disableOriginalConstructor()
->getMock()
;
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)
$this->twig = $this->getMockBuilder(TwigEnvironment::class)
->disableOriginalConstructor()
->getMock()
;
Expand All @@ -173,20 +168,18 @@ public function setUp()
->disableOriginalConstructor()
->getMock()
;
$this->templateEngineType = 'twig';

$this->instance = new AuthorizeController(
$this->requestStack,
$this->form,
$this->authorizeFormHandler,
$this->oAuth2Server,
$this->templateEngine,
$this->tokenStorage,
$this->router,
$this->clientManager,
$this->eventDispatcher,
$this->session,
$this->templateEngineType
$this->twig,
$this->session
);

/** @var \PHPUnit_Framework_MockObject_MockObject&Request $request */
Expand Down Expand Up @@ -307,22 +300,20 @@ public function testAuthorizeActionWillRenderTemplate()
->willReturn($this->formView)
;

$response = new Response();

$this->templateEngine
->expects($this->at(0))
->method('renderResponse')
$this->twig
->expects($this->once())
->method('render')
->with(
'@FOSOAuthServer/Authorize/authorize.html.twig',
[
'form' => $this->formView,
'client' => $this->client,
]
)
->willReturn($response)
->willReturn($responseBody = 'response')
;

$this->assertSame($response, $this->instance->authorizeAction($this->request));
$this->assertSame($responseBody, $this->instance->authorizeAction($this->request)->getContent());
}

public function testAuthorizeActionWillFinishClientAuthorization()
Expand Down Expand Up @@ -466,22 +457,20 @@ public function testAuthorizeActionWillEnsureLogout()
->willReturn($this->formView)
;

$response = new Response();

$this->templateEngine
->expects($this->at(0))
->method('renderResponse')
$this->twig
->expects($this->once())
->method('render')
->with(
'@FOSOAuthServer/Authorize/authorize.html.twig',
[
'form' => $this->formView,
'client' => $this->client,
]
)
->willReturn($response)
->willReturn($responseBody = 'response')
;

$this->assertSame($response, $this->instance->authorizeAction($this->request));
$this->assertSame($responseBody, $this->instance->authorizeAction($this->request)->getContent());
}

public function testAuthorizeActionWillProcessAuthorizationForm()
Expand Down
6 changes: 4 additions & 2 deletions Tests/Functional/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
framework:
templating:
engines: ["twig"]
form: ~
secret: test
router:
resource: "%kernel.root_dir%/config/routing.yml"

twig:
exception_controller: null
strict_variables: '%kernel.debug%'

fos_oauth_server:

security:
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"friendsofsymfony/oauth2-php": "~1.1",
"symfony/dependency-injection": "^4.4",
"symfony/framework-bundle": "^4.4",
"symfony/security-bundle": "^4.4"
"symfony/security-bundle": "^4.4",
"symfony/twig-bundle": "^4.4"
},
"conflict": {
"twig/twig": "<1.40 || >=2.0,<2.9"
Expand All @@ -43,8 +44,6 @@
"symfony/console": "~3.0 || ~4.0",
"symfony/form": "~3.0 || ~4.0",
"symfony/phpunit-bridge": "~3.0 || ~4.0",
"symfony/templating": "~3.0 || ~4.0",
"symfony/twig-bundle": "~3.0 || ^4.0",
"symfony/yaml": "~3.0 || ~4.0",
"willdurand/propel-typehintable-behavior": "~1.0"
},
Expand Down

0 comments on commit d8f41fe

Please sign in to comment.