Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
not test Exporter if not Sonata\CoreBundle\Exporter\Exporter

not test ChoiceTypeExtension if no SonataCoreBundle

remove SonataCoreBundle from AppKernel if it is not exists

register SonataFormBundle and SonataTwigBundle in AppKernel

add ChoiceTypeExtensionTest::$factory property

fix ChoiceTypeExtensionTest
  • Loading branch information
peter-gribanov committed Jun 26, 2020
1 parent 86905e6 commit 1e40f92
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 40 deletions.
13 changes: 11 additions & 2 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Sonata\BlockBundle\SonataBlockBundle;
use Sonata\CoreBundle\SonataCoreBundle;
use Sonata\Doctrine\Bridge\Symfony\Bundle\SonataDoctrineBundle;
use Sonata\Form\Bridge\Symfony\SonataFormBundle;
use Sonata\Twig\Bridge\Symfony\SonataTwigBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
Expand All @@ -39,17 +41,24 @@ public function __construct()

public function registerBundles()
{
return [
$bundles = [
new FrameworkBundle(),
new TwigBundle(),
new TwigExtraBundle(),
new SecurityBundle(),
new KnpMenuBundle(),
new SonataBlockBundle(),
new SonataCoreBundle(),
new SonataDoctrineBundle(),
new SonataAdminBundle(),
new SonataTwigBundle(),
new SonataFormBundle(),
];

if (class_exists(SonataCoreBundle::class)) {
$bundles[] = new SonataCoreBundle();
}

return $bundles;
}

public function getCacheDir(): string
Expand Down
6 changes: 6 additions & 0 deletions tests/Export/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Export\Exporter;
use Sonata\CoreBundle\Exporter\Exporter as CoreExporter;
use Sonata\Exporter\Source\ArraySourceIterator;
use Sonata\Exporter\Source\SourceIteratorInterface;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -41,6 +42,11 @@ public function testFilter(): void
*/
public function testGetResponse(string $format, string $filename, string $contentType): void
{
if (!class_exists(CoreExporter::class)) {
$this->markTestSkipped('Not test Exporter from removed SonataCoreBundle.');
}

$this->markTestSkipped();
$source = new ArraySourceIterator([
['foo' => 'bar'],
]);
Expand Down
15 changes: 10 additions & 5 deletions tests/Fixtures/TestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Fixtures;

use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\FormExtensionInterface;
use Symfony\Component\Form\FormTypeExtensionInterface;
use Symfony\Component\Form\FormTypeGuesserInterface;
Expand All @@ -26,27 +27,31 @@ class TestExtension implements FormExtensionInterface

private $guesser;

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

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

public function getType($name): FormTypeInterface
{
return isset($this->types[$name]) ? $this->types[$name] : null;
if (!isset($this->types[$name])) {
throw new InvalidArgumentException(sprintf('Type "%s" is not supported.', $name));
}

return $this->types[$name];
}

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

public function addTypeExtension(FormTypeExtensionInterface $extension)
public function addTypeExtension(FormTypeExtensionInterface $extension): void
{
foreach ($extension::getExtendedTypes() as $type) {
if (!isset($this->extensions[$type])) {
Expand All @@ -59,7 +64,7 @@ public function addTypeExtension(FormTypeExtensionInterface $extension)

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

public function hasTypeExtensions($name): bool
Expand Down
68 changes: 40 additions & 28 deletions tests/Form/Extension/ChoiceTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,56 @@

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Form\Extension\ChoiceTypeExtension;
use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
use Sonata\CoreBundle\Form\Extension\DependencyInjectionExtension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Forms;

class ChoiceTypeExtensionTest extends TestCase
{
protected function setup(): void
/**
* @var FormFactoryInterface
*/
private $factory;

protected function setUp(): void
{
$container = $this->getMockForAbstractClass(ContainerInterface::class);
$container->method('has')->willReturn(true);
$container->method('get')
->with($this->equalTo('sonata.admin.form.choice_extension'))
->willReturn(new ChoiceTypeExtension());

$typeServiceIds = [];
$typeExtensionServiceIds = [];
$guesserServiceIds = [];
$mappingTypes = [
'choice' => ChoiceType::class,
];
$extensionTypes = [
'choice' => [
'sonata.admin.form.choice_extension',
],
];

$dependency = new DependencyInjectionExtension(
$container,
$typeServiceIds,
$typeExtensionServiceIds,
$guesserServiceIds,
$mappingTypes,
$extensionTypes
);
if (class_exists(DependencyInjectionExtension::class)) {
$container = $this->getMockForAbstractClass(ContainerInterface::class);
$container->method('has')->willReturn(true);
$container->method('get')
->with($this->equalTo('sonata.admin.form.choice_extension'))
->willReturn(new ChoiceTypeExtension());

$typeServiceIds = [];
$typeExtensionServiceIds = [];
$guesserServiceIds = [];
$mappingTypes = [
'choice' => ChoiceType::class,
];
$extensionTypes = [
'choice' => [
'sonata.admin.form.choice_extension',
],
];

$extension = new DependencyInjectionExtension(
$container,
$typeServiceIds,
$typeExtensionServiceIds,
$guesserServiceIds,
$mappingTypes,
$extensionTypes
);
} else {
$extension = new TestExtension(null);
$extension->addTypeExtension(new ChoiceTypeExtension());
}

$this->factory = Forms::createFormFactoryBuilder()
->addExtension($dependency)
->addExtension($extension)
->getFormFactory();
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Form/Widget/BaseWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class BaseWidgetTest extends AbstractWidgetTestCase
/**
* {@inheritdoc}
*/
protected function getEnvironment()
protected function getEnvironment(): Environment
{
$environment = parent::getEnvironment();
$environment->addGlobal('sonata_admin', $this->getSonataAdmin());
Expand All @@ -67,7 +67,7 @@ protected function getEnvironment()
/**
* {@inheritdoc}
*/
protected function getRenderingEngine(?Environment $environment = null)
protected function getRenderingEngine(?Environment $environment = null): TwigRendererEngine
{
if (!\in_array($this->type, ['form', 'filter'], true)) {
throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
Expand All @@ -90,7 +90,7 @@ protected function getSonataAdmin()
/**
* {@inheritdoc}
*/
protected function getTemplatePaths()
protected function getTemplatePaths(): array
{
return array_merge(parent::getTemplatePaths(), [
__DIR__.'/../../../src/Resources/views/Form',
Expand Down
2 changes: 0 additions & 2 deletions tests/Resources/XliffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Sonata\AdminBundle\Tests\Resources;

use Sonata\CoreBundle\Test\XliffValidatorTestCase;

class XliffTest extends XliffValidatorTestCase
{
/**
Expand Down

0 comments on commit 1e40f92

Please sign in to comment.