Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropping support for symfony < 3.4 #479

Merged
merged 1 commit into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
"sonata-project/doctrine-extensions": "^1.1.3",
"sonata-project/doctrine-orm-admin-bundle": "^3.4",
"sonata-project/easy-extends-bundle": "^2.5",
"symfony/config": "^2.8 || ^3.2 || ^4.0",
"symfony/console": "^2.8 || ^3.2 || ^4.0",
"symfony/dependency-injection": "^2.8 || ^3.2 || ^4.0",
"symfony/form": "^2.8 || ^3.2 || ^4.0",
"symfony/http-foundation": "^2.8 || ^3.2 || ^4.0",
"symfony/http-kernel": "^2.8 || ^3.2 || ^4.0",
"symfony/options-resolver": "^2.8 || ^3.2 || ^4.0",
"symfony/config": "^3.4 || ^4.2",
"symfony/console": "^3.4 || ^4.2",
"symfony/dependency-injection": "^3.4 || ^4.2",
"symfony/form": "^3.4 || ^4.2",
"symfony/http-foundation": "^3.4 || ^4.2",
"symfony/http-kernel": "^3.4 || ^4.2",
"symfony/options-resolver": "^3.4 || ^4.2",
"twig/twig": "^1.35 || ^2.0"
},
"conflict": {
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Installation
Prerequisites
-------------

PHP 5.6 or 7.0 and Symfony 2.8, >=3.3 or 4 are needed to make this bundle work, there are
PHP 7.1 and Symfony >=3.4 or >= 4.2 are needed to make this bundle work, there are
also some Sonata dependencies that need to be installed and configured beforehand:

* `SonataEasyExtendsBundle <https://sonata-project.org/bundles/easy-extends>`_
Expand Down
18 changes: 0 additions & 18 deletions src/Controller/CategoryAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
namespace Sonata\ClassificationBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Command\DebugCommand;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -134,20 +130,6 @@ private function setFormTheme(FormView $formView, $theme)
{
$twig = $this->get('twig');

// BC for Symfony < 3.2 where this runtime does not exists
if (!method_exists(AppVariable::class, 'getToken')) {
$twig->getExtension(FormExtension::class)
->renderer->setTheme($formView, $theme);

return;
}

// BC for Symfony < 3.4 where runtime should be TwigRenderer
if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
$twig->getRuntime(TwigRenderer::class)->setTheme($formView, $theme);

return;
}
$twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
}
}
25 changes: 8 additions & 17 deletions src/Form/Type/CategorySelectorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
use Sonata\Doctrine\Model\ManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand All @@ -43,31 +41,19 @@ public function __construct(ManagerInterface $manager)
}

/**
* NEXT_MAJOR: Remove method, when bumping requirements to SF 2.7+.
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/classification-bundle 3.x, to be removed in version 4.0.
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
franmomu marked this conversation as resolved.
Show resolved Hide resolved
{
$this->configureOptions($resolver);
}

/**
* NEXT_MAJOR: replace usage of deprecated 'choice_list' option, when bumping requirements to SF 2.7+.
*/
public function configureOptions(OptionsResolver $resolver)
{
$that = $this;

if (!interface_exists(ChoiceLoaderInterface::class)) {
$resolver->setDefaults([
'context' => null,
'category' => null,
'choice_list' => static function (Options $opts, $previousValue) use ($that) {
return new SimpleChoiceList($that->getChoices($opts));
},
]);

return;
}
$resolver->setDefaults([
'context' => null,
'category' => null,
Expand Down Expand Up @@ -113,6 +99,11 @@ public function getBlockPrefix()
return 'sonata_category_selector';
}

/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/classification-bundle 3.x, to be removed in version 4.0.
*/
public function getName()
{
return $this->getBlockPrefix();
Expand Down
100 changes: 18 additions & 82 deletions tests/Controller/CategoryAdminControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,15 @@
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
use Sonata\ClassificationBundle\Model\ContextInterface;
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Command\DebugCommand;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

Expand Down Expand Up @@ -85,7 +79,7 @@ class CategoryAdminControllerTest extends TestCase
private $template;

/**
* @var CsrfProviderInterface
* @var CsrfTokenManagerInterface
*/
private $csrfProvider;

Expand Down Expand Up @@ -156,83 +150,29 @@ protected function setUp()

$twig = $this->createMock(\Twig_Environment::class);

// BC for Symfony < 3.4 where runtime should be TwigRenderer
if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
$formRenderer = $this->createMock(TwigRenderer::class);
$formExtension = new FormExtension($formRenderer);
} else {
$formRenderer = $this->createMock(FormRenderer::class);
$formExtension = new FormExtension();
}

$twig->expects($this->any())
->method('getExtension')
->willReturnCallback(static function ($name) use ($formExtension) {
switch ($name) {
case 'form':
case FormExtension::class:
return $formExtension;
}
});
$formRenderer = $this->createMock(FormRenderer::class);

$twig->expects($this->any())
->method('getRuntime')
->willReturnCallback(static function ($name) use ($formRenderer) {
switch ($name) {
case TwigRenderer::class:
case FormRenderer::class:
if (method_exists(AppVariable::class, 'getToken')) {
return $formRenderer;
}
->willReturn($formRenderer);

throw new \Twig_Error_Runtime('This runtime exists when Symfony >= 3.2.');
}
});
$this->csrfProvider = $this->createMock(CsrfTokenManagerInterface::class);

// Prefer Symfony 2.x interfaces
if (interface_exists(CsrfProviderInterface::class)) {
$this->csrfProvider = $this->getMockBuilder(
CsrfProviderInterface::class
)
->getMock();

$this->csrfProvider->expects($this->any())
->method('generateCsrfToken')
->willReturnCallback(static function ($intention) {
return 'csrf-token-123_'.$intention;
});

$this->csrfProvider->expects($this->any())
->method('isCsrfTokenValid')
->willReturnCallback(static function ($intention, $token) {
if ($token === 'csrf-token-123_'.$intention) {
return true;
}
$this->csrfProvider->expects($this->any())
->method('getToken')
->willReturnCallback(static function ($intention) {
return new CsrfToken($intention, 'csrf-token-123_'.$intention);
});

return false;
});
} else {
$this->csrfProvider = $this->getMockBuilder(
CsrfTokenManagerInterface::class
)
->getMock();

$this->csrfProvider->expects($this->any())
->method('getToken')
->willReturnCallback(static function ($intention) {
return new CsrfToken($intention, 'csrf-token-123_'.$intention);
});

$this->csrfProvider->expects($this->any())
->method('isTokenValid')
->willReturnCallback(static function (CsrfToken $token) {
if ($token->getValue() === 'csrf-token-123_'.$token->getId()) {
return true;
}
$this->csrfProvider->expects($this->any())
->method('isTokenValid')
->willReturnCallback(static function (CsrfToken $token) {
if ($token->getValue() === 'csrf-token-123_'.$token->getId()) {
return true;
}

return false;
});
}
return false;
});

// php 5.3 BC
$csrfProvider = $this->csrfProvider;
Expand Down Expand Up @@ -295,11 +235,7 @@ protected function setUp()
$this->container->expects($this->any())
->method('has')
->willReturnCallback(static function ($id) use ($tthis) {
if ('form.csrf_provider' === $id && Kernel::MAJOR_VERSION === 2 && null !== $tthis->getCsrfProvider()) {
return true;
}

if ('security.csrf.token_manager' === $id && Kernel::MAJOR_VERSION >= 3 && null !== $tthis->getCsrfProvider()) {
if ('security.csrf.token_manager' === $id && null !== $tthis->getCsrfProvider()) {
return true;
}

Expand Down
8 changes: 0 additions & 8 deletions tests/Form/ChoiceList/CategoryChoiceLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@

use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Form\ChoiceList\CategoryChoiceLoader;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;

/**
* @author Anton Zlotnikov <exp.razor@gmail.com>
*/
class CategoryChoiceLoaderTest extends TestCase
{
protected function setUp()
{
if (!interface_exists(ChoiceLoaderInterface::class)) {
$this->markTestSkipped('Test only available for >= SF3.0');
}
}

public function testLoadChoiceList()
{
$choices = [
Expand Down
5 changes: 1 addition & 4 deletions tests/Form/Type/CategorySelectorTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public function testConfigureOptions()
$categorySelectorType = new CategorySelectorType($manager);
$optionsResolver = new OptionsResolver();
$categorySelectorType->configureOptions($optionsResolver);
//unable to get defined options on SF2.3
if (!method_exists($optionsResolver, 'getDefinedOptions')) {
return;
}

$definedOptions = $optionsResolver->getDefinedOptions();
$this->assertContains('category', $definedOptions);
$this->assertContains('context', $definedOptions);
Expand Down