2012-07-12 18:17:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-10-13 15:51:08 +01:00
|
|
|
namespace Symfony\Component\Form\Tests;
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2012-10-13 15:51:08 +01:00
|
|
|
use Symfony\Component\Form\FormRegistry;
|
|
|
|
use Symfony\Component\Form\FormTypeGuesserChain;
|
2015-06-23 17:42:21 +01:00
|
|
|
use Symfony\Component\Form\ResolvedFormType;
|
|
|
|
use Symfony\Component\Form\ResolvedFormTypeFactoryInterface;
|
2012-07-12 18:17:14 +01:00
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooSubType;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooType;
|
2015-06-23 17:42:21 +01:00
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooTypeBarExtension;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooTypeBazExtension;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\TestExtension;
|
2012-07-12 18:17:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
2017-02-08 07:24:27 +00:00
|
|
|
class FormRegistryTest extends TestCase
|
2012-07-12 18:17:14 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var FormRegistry
|
|
|
|
*/
|
|
|
|
private $registry;
|
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
/**
|
2015-06-23 17:42:21 +01:00
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResolvedFormTypeFactoryInterface
|
2012-07-29 18:10:47 +01:00
|
|
|
*/
|
|
|
|
private $resolvedTypeFactory;
|
|
|
|
|
2012-07-12 18:17:14 +01:00
|
|
|
/**
|
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $guesser1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $guesser2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TestExtension
|
|
|
|
*/
|
|
|
|
private $extension1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TestExtension
|
|
|
|
*/
|
|
|
|
private $extension2;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2016-12-19 15:48:05 +00:00
|
|
|
$this->resolvedTypeFactory = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeFactory')->getMock();
|
|
|
|
$this->guesser1 = $this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock();
|
|
|
|
$this->guesser2 = $this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock();
|
2012-07-12 18:17:14 +01:00
|
|
|
$this->extension1 = new TestExtension($this->guesser1);
|
|
|
|
$this->extension2 = new TestExtension($this->guesser2);
|
|
|
|
$this->registry = new FormRegistry(array(
|
|
|
|
$this->extension1,
|
|
|
|
$this->extension2,
|
2012-07-29 18:10:47 +01:00
|
|
|
), $this->resolvedTypeFactory);
|
2012-07-12 18:17:14 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
public function testGetTypeFromExtension()
|
2012-07-12 18:17:14 +01:00
|
|
|
{
|
2012-07-29 18:10:47 +01:00
|
|
|
$type = new FooType();
|
2015-06-23 17:42:21 +01:00
|
|
|
$resolvedType = new ResolvedFormType($type);
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->extension2->addType($type);
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->resolvedTypeFactory->expects($this->once())
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type)
|
2015-06-23 17:42:21 +01:00
|
|
|
->willReturn($resolvedType);
|
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType(get_class($type)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoadUnregisteredType()
|
|
|
|
{
|
|
|
|
$type = new FooType();
|
|
|
|
$resolvedType = new ResolvedFormType($type);
|
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->once())
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type)
|
|
|
|
->willReturn($resolvedType);
|
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType('Symfony\Component\Form\Tests\Fixtures\FooType'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testFailIfUnregisteredTypeNoClass()
|
|
|
|
{
|
|
|
|
$this->registry->getType('Symfony\Blubb');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testFailIfUnregisteredTypeNoFormType()
|
|
|
|
{
|
|
|
|
$this->registry->getType('stdClass');
|
|
|
|
}
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
public function testGetTypeWithTypeExtensions()
|
2012-07-12 18:17:14 +01:00
|
|
|
{
|
|
|
|
$type = new FooType();
|
2012-07-29 18:10:47 +01:00
|
|
|
$ext1 = new FooTypeBarExtension();
|
|
|
|
$ext2 = new FooTypeBazExtension();
|
2015-06-23 17:42:21 +01:00
|
|
|
$resolvedType = new ResolvedFormType($type, array($ext1, $ext2));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->extension2->addType($type);
|
|
|
|
$this->extension1->addTypeExtension($ext1);
|
|
|
|
$this->extension2->addTypeExtension($ext2);
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->resolvedTypeFactory->expects($this->once())
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type, array($ext1, $ext2))
|
2015-06-23 17:42:21 +01:00
|
|
|
->willReturn($resolvedType);
|
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType(get_class($type)));
|
|
|
|
}
|
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
public function testGetTypeConnectsParent()
|
2012-07-12 18:17:14 +01:00
|
|
|
{
|
2012-07-29 18:10:47 +01:00
|
|
|
$parentType = new FooType();
|
|
|
|
$type = new FooSubType();
|
2015-06-23 17:42:21 +01:00
|
|
|
$parentResolvedType = new ResolvedFormType($parentType);
|
|
|
|
$resolvedType = new ResolvedFormType($type);
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->extension1->addType($parentType);
|
2012-07-12 18:17:14 +01:00
|
|
|
$this->extension2->addType($type);
|
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->resolvedTypeFactory->expects($this->at(0))
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($parentType)
|
2015-06-23 17:42:21 +01:00
|
|
|
->willReturn($parentResolvedType);
|
2012-07-29 18:10:47 +01:00
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->at(1))
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type, array(), $parentResolvedType)
|
2015-06-23 17:42:21 +01:00
|
|
|
->willReturn($resolvedType);
|
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType(get_class($type)));
|
|
|
|
}
|
|
|
|
|
2012-07-12 18:17:14 +01:00
|
|
|
/**
|
2013-04-15 21:11:02 +01:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
|
2012-07-12 18:17:14 +01:00
|
|
|
*/
|
|
|
|
public function testGetTypeThrowsExceptionIfTypeNotFound()
|
|
|
|
{
|
|
|
|
$this->registry->getType('bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasTypeAfterLoadingFromExtension()
|
|
|
|
{
|
|
|
|
$type = new FooType();
|
2015-06-23 17:42:21 +01:00
|
|
|
$resolvedType = new ResolvedFormType($type);
|
2012-07-29 18:10:47 +01:00
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->once())
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type)
|
2015-06-23 17:42:21 +01:00
|
|
|
->willReturn($resolvedType);
|
2012-07-29 18:10:47 +01:00
|
|
|
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->extension2->addType($type);
|
|
|
|
|
|
|
|
$this->assertTrue($this->registry->hasType(get_class($type)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasTypeIfFQCN()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->registry->hasType('Symfony\Component\Form\Tests\Fixtures\FooType'));
|
|
|
|
}
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2015-06-23 17:42:21 +01:00
|
|
|
public function testDoesNotHaveTypeIfNonExistingClass()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->registry->hasType('Symfony\Blubb'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesNotHaveTypeIfNoFormType()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->registry->hasType('stdClass'));
|
|
|
|
}
|
|
|
|
|
2012-07-12 18:17:14 +01:00
|
|
|
public function testGetTypeGuesser()
|
|
|
|
{
|
|
|
|
$expectedGuesser = new FormTypeGuesserChain(array($this->guesser1, $this->guesser2));
|
|
|
|
|
|
|
|
$this->assertEquals($expectedGuesser, $this->registry->getTypeGuesser());
|
2012-11-08 18:49:46 +00:00
|
|
|
|
|
|
|
$registry = new FormRegistry(
|
2016-12-19 15:48:05 +00:00
|
|
|
array($this->getMockBuilder('Symfony\Component\Form\FormExtensionInterface')->getMock()),
|
2015-06-23 17:42:21 +01:00
|
|
|
$this->resolvedTypeFactory
|
|
|
|
);
|
2012-11-08 18:49:46 +00:00
|
|
|
|
|
|
|
$this->assertNull($registry->getTypeGuesser());
|
2012-07-12 18:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetExtensions()
|
|
|
|
{
|
|
|
|
$expectedExtensions = array($this->extension1, $this->extension2);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedExtensions, $this->registry->getExtensions());
|
|
|
|
}
|
|
|
|
}
|