yaroslavbr
11/11/2016 - 3:49 PM

AttributeGroupCollectionTypeTest.php

<?php

namespace Oro\Bundle\EntityExtendBundle\Tests\Unit\Form\Type;

use Oro\Bundle\EntityExtendBundle\Entity\AttributeGroup;
use Oro\Bundle\EntityExtendBundle\Form\Type\AttributeGroupCollectionType;
use Oro\Bundle\EntityExtendBundle\Form\Type\AttributeGroupType;
use Oro\Bundle\FormBundle\Form\Type\CollectionType;
use Oro\Bundle\LocaleBundle\Entity\LocalizedFallbackValue;
use Oro\Bundle\LocaleBundle\Form\Type\LocalizedFallbackValueCollectionType;
use Oro\Bundle\LocaleBundle\Tests\Unit\Form\Type\Stub\LocalizedFallbackValueCollectionTypeStub;
use Oro\Component\Testing\Unit\FormIntegrationTestCase;

use Symfony\Component\Form\PreloadedExtension;

class AttributeGroupCollectionTypeTest extends FormIntegrationTestCase
{
    /**
     * @var AttributeGroupCollectionType
     */
    protected $formType;

    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        parent::setUp();
        $this->formType = new AttributeGroupCollectionType();
    }

    /**
     * @return array
     */
    public function getExtensions()
    {
        return [
            new PreloadedExtension([
                CollectionType::NAME => new CollectionType(),
                AttributeGroupType::NAME => new AttributeGroupType(),
                LocalizedFallbackValueCollectionType::NAME => new LocalizedFallbackValueCollectionTypeStub()
            ], []),

        ];
    }

    /**
     * @param string|null $string
     * @param string|null $text
     * @return LocalizedFallbackValue
     */
    protected function createLocalizedValue($string = null, $text = null)
    {
        $value = new LocalizedFallbackValue();
        $value->setString($string)->setText($text);

        return $value;
    }

    public function testSubmit()
    {
        $existingEntity = new AttributeGroup();
        $existingEntity->addLabel($this->createLocalizedValue('Group1 Label 1'));
        $form = $this->factory->create($this->formType, [$existingEntity]);

        $submittedData = [
            [
                'labels' => [
                    ['string' => 'Group1 Label 1'],
                    ['string' => 'Group1 Label 2'],
                ]

            ],
            [
                'labels' => [
                    ['string' => 'Group2 Label 3'],
                    ['string' => 'Group2 Label 4'],
                ]

            ]
        ];

        $form->submit($submittedData, [$existingEntity]);
        $this->assertTrue($form->isValid());

        $existingEntity->addLabel($this->createLocalizedValue('Group1 Label 2'));
        $entity = new AttributeGroup();
        $entity->addLabel($this->createLocalizedValue('Group2 Label 3'));
        $entity->addLabel($this->createLocalizedValue('Group2 Label 4'));

        $formData = $form->getData();
        $this->assertEquals([$existingEntity, $entity], $formData);
    }

    public function testGetName()
    {
        $this->assertSame(AttributeGroupCollectionType::NAME, $this->formType->getName());
    }

    public function testGetParent()
    {
        $this->assertSame(CollectionType::NAME, $this->formType->getParent());
    }
}