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

Fix unit tests #6168

Merged
merged 1 commit into from
Jun 27, 2020
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
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)) {
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved
$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());
}
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved

$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;
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved

class XliffTest extends XliffValidatorTestCase
{
/**
Expand Down