Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Dec 19, 2019
1 parent c70ac5f commit 52d543a
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 19 deletions.
22 changes: 17 additions & 5 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1469,12 +1469,16 @@ public function testDeleteActionWithDisabledCsrfProtection(): void
$this->admin->expects($this->once())
->method('toString')
->with($object)
->willReturn(\stdClass::class);
->willReturn('some class');

$this->admin->expects($this->once())
->method('delete')
->with($object);

$this->expectTranslate('flash_delete_success', [
'%name%' => 'some class',
], 'SonataAdminBundle');

$this->controller->deleteAction(1);
}

Expand Down Expand Up @@ -1875,14 +1879,18 @@ public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('createView')
->willReturn($formView);

$this->expectTranslate('flash_edit_error', [
'%name%' => '',
], 'SonataAdminBundle');

$this->assertInstanceOf(Response::class, $response = $this->controller->editAction(null));
$this->assertSame($this->admin, $this->parameters['admin']);
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
$this->assertSame($this->pool, $this->parameters['admin_pool']);
$this->assertSame('edit', $this->parameters['action']);
$this->assertInstanceOf(FormView::class, $this->parameters['form']);
$this->assertSame($object, $this->parameters['object']);
$this->assertSame(['sonata_flash_error' => [0 => null]], $this->session->getFlashBag()->all());
$this->assertSame(['sonata_flash_error' => [0 => 'flash_edit_error']], $this->session->getFlashBag()->all());
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
}

Expand Down Expand Up @@ -2645,14 +2653,18 @@ public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('createView')
->willReturn($formView);

$this->expectTranslate('flash_create_error', [
'%name%' => '',
], 'SonataAdminBundle');

$this->assertInstanceOf(Response::class, $response = $this->controller->createAction());
$this->assertSame($this->admin, $this->parameters['admin']);
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
$this->assertSame($this->pool, $this->parameters['admin_pool']);
$this->assertSame('create', $this->parameters['action']);
$this->assertInstanceOf(FormView::class, $this->parameters['form']);
$this->assertSame($object, $this->parameters['object']);
$this->assertSame(['sonata_flash_error' => [0 => null]], $this->session->getFlashBag()->all());
$this->assertSame(['sonata_flash_error' => [0 => 'flash_create_error']], $this->session->getFlashBag()->all());
$this->assertSame('@SonataAdmin/CRUD/edit.html.twig', $this->template);
}

Expand Down Expand Up @@ -3966,7 +3978,7 @@ public function testBatchActionNonRelevantAction2(): void
->method('getDatagrid')
->willReturn($datagrid);

$this->expectTranslate('flash_foo_error', [], 'SonataAdminBundle');
$this->expectTranslate('flash_batch_empty', [], 'SonataAdminBundle');

$this->request->setMethod('POST');
$this->request->request->set('action', 'foo');
Expand All @@ -3981,7 +3993,7 @@ public function testBatchActionNonRelevantAction2(): void
$result = $controller->batchAction();

$this->assertInstanceOf(RedirectResponse::class, $result);
$this->assertSame(['flash_foo_error'], $this->session->getFlashBag()->get('sonata_flash_info'));
$this->assertSame(['flash_batch_empty'], $this->session->getFlashBag()->get('sonata_flash_info'));
$this->assertSame('list', $result->getTargetUrl());
}

Expand Down
2 changes: 0 additions & 2 deletions tests/Fixtures/Admin/FieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function isIdentifier(): void

/**
* set the parent association mappings information.
*
* @param array $parentAssociationMappings
*/
public function setParentAssociationMappings(array $parentAssociationMappings): void
{
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/Admin/ModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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\AdminBundle\Tests\Fixtures\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Controller/BatchAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class BatchAdminController extends CRUDController
*/
public function batchActionFooIsRelevant(array $idx, $allElements)
{
if (isset($idx[0], $idx[1]) && 123 == $idx[0] && 456 == $idx[1]) {
if (isset($idx[0], $idx[1]) && 123 === $idx[0] && 456 === $idx[1]) {
return true;
}

if (isset($idx[0]) && 999 == $idx[0]) {
if (isset($idx[0]) && 999 === $idx[0]) {
return 'flash_foo_error';
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/Controller/ModelAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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\AdminBundle\Tests\Fixtures\Controller;

use Sonata\AdminBundle\Controller\CRUDController;
Expand Down
48 changes: 48 additions & 0 deletions tests/Fixtures/StubTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\AdminBundle\Tests\Fixtures;

use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

if (interface_exists(TranslatorInterface::class)) {
final class StubTranslator implements TranslatorInterface
{
public function trans($id, array $parameters = [], $domain = null, $locale = null): string
{
return '[trans]'.strtr($id, $parameters).'[/trans]';
}
}
} else {
final class StubTranslator implements LegacyTranslatorInterface
{
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
return '[trans]'.$id.'[/trans]';
}

public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
return '[trans]'.$id.'[/trans]';
}

public function setLocale($locale)
{
}

public function getLocale()
{
}
}
}
71 changes: 71 additions & 0 deletions tests/Fixtures/TestExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\AdminBundle\Tests\Fixtures;

use Symfony\Component\Form\FormExtensionInterface;
use Symfony\Component\Form\FormTypeExtensionInterface;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\FormTypeInterface;

class TestExtension implements FormExtensionInterface
{
private $types = [];
private $extensions = [];
private $guesser;

public function __construct(FormTypeGuesserInterface $guesser)
{
$this->guesser = $guesser;
}

public function addType(FormTypeInterface $type)
{
$this->types[\get_class($type)] = $type;
}

public function getType($name): FormTypeInterface
{
return isset($this->types[$name]) ? $this->types[$name] : null;
}

public function hasType($name): bool
{
return isset($this->types[$name]);
}

public function addTypeExtension(FormTypeExtensionInterface $extension)
{
foreach ($extension::getExtendedTypes() as $type) {
if (!isset($this->extensions[$type])) {
$this->extensions[$type] = [];
}
$this->extensions[$type][] = $extension;
}
}

public function getTypeExtensions($name): array
{
return isset($this->extensions[$name]) ? $this->extensions[$name] : [];
}

public function hasTypeExtensions($name): bool
{
return isset($this->extensions[$name]);
}

public function getTypeGuesser(): ?FormTypeGuesserInterface
{
return $this->guesser;
}
}
2 changes: 1 addition & 1 deletion tests/Form/Type/AdminTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use Sonata\AdminBundle\Form\Type\AdminType;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Tests\Fixtures\TestExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;

Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Widget/BaseWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Sonata\AdminBundle\Tests\Form\Widget;

use Sonata\AdminBundle\Tests\Fixtures\StubTranslator;
use Sonata\Form\Test\AbstractWidgetTestCase;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Twig\Environment;

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Widget/FormSonataFilterChoiceWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
namespace Sonata\AdminBundle\Tests\Form\Widget;

use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Tests\Fixtures\TestExtension;
use Symfony\Component\Translation\TranslatorInterface;

class FormSonataFilterChoiceWidgetTest extends BaseWidgetTest
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Widget/FormSonataNativeCollectionWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension;
use Sonata\AdminBundle\Form\Type\CollectionType;
use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Tests\Fixtures\TestExtension;

class FormSonataNativeCollectionWidgetTest extends BaseWidgetTest
{
Expand Down
1 change: 0 additions & 1 deletion tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ protected function configureContainer(ContainerBuilder $containerBuilder, Loader
{
$containerBuilder->loadFromExtension('framework', [
'secret' => 'MySecret',
'trusted_proxies' => [],
'fragments' => ['enabled' => true],
'form' => ['enabled' => true],
'session' => ['handler_id' => null, 'storage_id' => 'session.storage.mock_file', 'name' => 'MOCKSESSID'],
Expand Down
6 changes: 3 additions & 3 deletions tests/Menu/Integration/BaseMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
use Knp\Menu\Matcher\MatcherInterface;
use Knp\Menu\Renderer\TwigRenderer;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Tests\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

/**
* Base class for tests checking rendering of twig templates.
Expand All @@ -40,7 +40,7 @@ public function setUp(): void
__DIR__.'/../../../src/Resources/views',
], 'is_dir');

$loader = new StubFilesystemLoader($twigPaths);
$loader = new FilesystemLoader($twigPaths);
$this->environment = new Environment($loader, ['strict_variables' => true]);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -40,6 +39,7 @@
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Extensions\TextExtension;
use Twig\Loader\FilesystemLoader;

/**
* Test for SonataAdminExtension.
Expand Down Expand Up @@ -169,7 +169,7 @@ public function setUp(): void
$request = $this->createMock(Request::class);
$request->method('get')->with('_sonata_admin')->willReturn('sonata_admin_foo_service');

$loader = new StubFilesystemLoader([
$loader = new FilesystemLoader([
__DIR__.'/../../../src/Resources/views/CRUD',
]);
$loader->addPath(__DIR__.'/../../../src/Resources/views/', 'SonataAdmin');
Expand Down

0 comments on commit 52d543a

Please sign in to comment.