Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
craigh committed Jun 19, 2022
1 parent 8689753 commit efd45cb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tests/Form/Type/FormSpecificationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ protected function getExtensions(): array
/**
* @covers \Zikula\Bundle\DynamicFormBundle\Entity\AbstractFormSpecification
*/
public function testNameFieldOnNew(): void
public function testFormCreation(): void
{
$formData = new class() extends AbstractFormSpecification {
};
$form = $this->factory->create(FormSpecificationType::class, $formData);

$this->assertTrue($form->has('name'));
$this->assertFalse($form->get('name')->isDisabled());

$this->assertInstanceOf(TextType::class, $form->get('labels')->getConfig()->getType()->getInnerType());
$this->assertInstanceOf(TextType::class, $form->get('groups')->getConfig()->getType()->getInnerType());
$this->assertCount(0, $form->get('labels'));
$this->assertCount(0, $form->get('groups'));
}

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/Form/Type/FormSpecificationTypeTranslatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\DynamicFormBundle\Tests\Form\Type;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\FormExtensionInterface;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Translation\IdentityTranslator;
use Zikula\Bundle\DynamicFormBundle\Entity\AbstractFormSpecification;
use Zikula\Bundle\DynamicFormBundle\EventSubscriber\FormTypeChoiceEventSubscriber;
use Zikula\Bundle\DynamicFormBundle\Form\Type\FormSpecificationType;
use Zikula\Bundle\DynamicFormBundle\Form\Type\TranslationCollectionType;
use Zikula\Bundle\DynamicFormBundle\Provider\LocaleProviderInterface;

class FormSpecificationTypeTranslatedTest extends TypeTestCase
{
/**
* @return FormExtensionInterface[]
*/
protected function getExtensions(): array
{
$dispatcher = new EventDispatcher();
$translator = new IdentityTranslator();
$localeProvider = new class() implements LocaleProviderInterface {
public function getSupportedLocales(): array
{
return ['en', 'de'];
}

public function getSupportedLocaleNames(string $displayLocale = null): array
{
return ['English' => 'en', 'German' => 'de'];
}
};

$dispatcher->addSubscriber(new FormTypeChoiceEventSubscriber($translator));
$formSpecificationType = new FormSpecificationType($dispatcher, true);
$translationCollectionType = new TranslationCollectionType($localeProvider);

return [
new PreloadedExtension([$formSpecificationType, $translationCollectionType], []),
];
}

/**
* @covers \Zikula\Bundle\DynamicFormBundle\Entity\AbstractFormSpecification
*/
public function testFormCreation(): void
{
$formData = new class() extends AbstractFormSpecification {
};
$form = $this->factory->create(FormSpecificationType::class, $formData);
$this->assertInstanceOf(TranslationCollectionType::class, $form->get('labels')->getConfig()->getType()->getInnerType());
$this->assertInstanceOf(TranslationCollectionType::class, $form->get('groups')->getConfig()->getType()->getInnerType());
$this->assertCount(2, $form->get('labels'));
$this->assertCount(2, $form->get('groups'));
$this->assertTrue($form->get('labels')->has('en'));
$this->assertTrue($form->get('labels')->has('de'));
$this->assertTrue($form->get('groups')->has('en'));
$this->assertTrue($form->get('groups')->has('de'));
}
}

0 comments on commit efd45cb

Please sign in to comment.