Skip to content

Instantly share code, notes, and snippets.

@podhy
Last active April 4, 2019 18:15
Show Gist options
  • Save podhy/48c525ea5e97477ceb4d6bcea21772f1 to your computer and use it in GitHub Desktop.
Save podhy/48c525ea5e97477ceb4d6bcea21772f1 to your computer and use it in GitHub Desktop.
Demo of testing form type with multiple entity types
<?php
declare(strict_types = 1);
namespace Tests\CommissionsBundle\Form\Type;
use CommissionsBundle\DTO\CatalogItemGroupDTO;
use CommissionsBundle\Entity\CatalogItem;
use CommissionsBundle\Entity\CatalogItemGroup;
use CommissionsBundle\Form\Type\CatalogItemGroupType;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\FormExtensionInterface;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
/**
* Class CatalogItemGroupTypeTest
*/
class CatalogItemGroupTypeTest extends TypeTestCase
{
private $itemRepositoryMock;
private $groupRepositoryMock;
private $managerMock;
/**
* Tests if form submit works
*/
public function testSubmitValidData()
{
$formData = [
'title' => 'test',
'parent' => null,
'items' => new ArrayCollection(),
];
$form = $this->factory->create(CatalogItemGroupType::class);
$object = new CatalogItemGroupDTO();
$object->setTitle($formData['title']);
$object->setParent($formData['parent']);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($object, $form->getData());
$view = $form->createView();
$children = $view->children;
foreach (array_keys($formData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
protected function getExtensions()
{
$mockEntityManager = $this->createMock(EntityManager::class);
$mockEntityManager->method('getClassMetadata')
->willReturn(new ClassMetadata(CatalogItemGroup::class))
;
$entityRepository = $this->createMock(EntityRepository::class);
$entityRepository->method('createQueryBuilder')
->willReturn(new QueryBuilder($mockEntityManager))
;
$mockEntityManager->method('getRepository')->willReturn($entityRepository);
$mockRegistry = $this->getMockBuilder(Registry::class)
->disableOriginalConstructor()
->setMethods(['getManagerForClass'])
->getMock()
;
$mockRegistry->method('getManagerForClass')
->willReturn($mockEntityManager)
;
/** @var EntityType|\PHPUnit_Framework_MockObject_MockObject $mockEntityType */
$mockEntityType = $this->getMockBuilder(EntityType::class)
->setConstructorArgs([$mockRegistry])
->setMethodsExcept(['configureOptions', 'getParent'])
->getMock()
;
$mockEntityType->method('getLoader')->willReturnCallback(function ($a, $b, $class) {
return new class($class) implements EntityLoaderInterface
{
/**
* @var
*/
private $class;
/**
* constructor.
*
* @param $class
*/
public function __construct($class)
{
$this->class = $class;
}
/**
* Returns an array of entities that are valid choices in the corresponding choice list.
*
* @return array The entities
*/
public function getEntities()
{
switch ($this->class) {
case CatalogItemGroup::class:
return [new CatalogItemGroup('asd')];
break;
case CatalogItem::class:
return [new CatalogItem('a', 'b', 'c')];
break;
}
}
/**
* Returns an array of entities matching the given identifiers.
*
* @param string $identifier The identifier field of the object. This method
* is not applicable for fields with multiple
* identifiers.
* @param array $values The values of the identifiers
*
* @return array The entities
*/
public function getEntitiesByIds($identifier, array $values)
{
// TODO: Implement getEntitiesByIds() method.
}
};
})
;
return [
new class($mockEntityType) implements FormExtensionInterface
{
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function getType($name)
{
return $this->type;
}
public function hasType($name)
{
return $name === EntityType::class;
}
public function getTypeExtensions($name)
{
return [];
}
public function hasTypeExtensions($name)
{
}
public function getTypeGuesser()
{
}
},
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment