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
|
|
|
|
2012-10-13 15:51:08 +01:00
|
|
|
use Symfony\Component\Form\FormRegistry;
|
|
|
|
use Symfony\Component\Form\FormTypeGuesserChain;
|
2012-07-12 18:17:14 +01:00
|
|
|
use Symfony\Component\Form\Tests\Fixtures\TestExtension;
|
2012-08-22 15:00:43 +01:00
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooSubTypeWithParentInstance;
|
2012-07-12 18:17:14 +01:00
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooSubType;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooTypeBazExtension;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooTypeBarExtension;
|
|
|
|
use Symfony\Component\Form\Tests\Fixtures\FooType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
|
|
|
class FormRegistryTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var FormRegistry
|
|
|
|
*/
|
|
|
|
private $registry;
|
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
/**
|
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
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()
|
|
|
|
{
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->resolvedTypeFactory = $this->getMock('Symfony\Component\Form\ResolvedFormTypeFactory');
|
2012-07-12 18:17:14 +01:00
|
|
|
$this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
|
|
|
$this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
|
|
|
|
$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 testGetTypeReturnsAddedType()
|
2012-07-12 18:17:14 +01:00
|
|
|
{
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-12-16 08:38:51 +00:00
|
|
|
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->registry->addType($resolvedType);
|
2012-12-14 06:31:09 +00:00
|
|
|
restore_error_handler();
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->assertSame($resolvedType, $this->registry->getType('foo'));
|
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();
|
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
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)
|
|
|
|
->will($this->returnValue($resolvedType));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType = $this->registry->getType('foo');
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$this->assertSame($resolvedType, $this->registry->getType('foo'));
|
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();
|
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
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))
|
|
|
|
->will($this->returnValue($resolvedType));
|
|
|
|
|
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType('foo'));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
$parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
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)
|
|
|
|
->will($this->returnValue($parentResolvedType));
|
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->at(1))
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type, array(), $parentResolvedType)
|
|
|
|
->will($this->returnValue($resolvedType));
|
|
|
|
|
|
|
|
$parentResolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo_sub_type'));
|
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType('foo_sub_type'));
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:00:43 +01:00
|
|
|
public function testGetTypeConnectsParentIfGetParentReturnsInstance()
|
|
|
|
{
|
|
|
|
$type = new FooSubTypeWithParentInstance();
|
|
|
|
$parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
|
|
|
|
|
|
|
$this->extension1->addType($type);
|
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->at(0))
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($this->isInstanceOf('Symfony\Component\Form\Tests\Fixtures\FooType'))
|
|
|
|
->will($this->returnValue($parentResolvedType));
|
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->at(1))
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type, array(), $parentResolvedType)
|
|
|
|
->will($this->returnValue($resolvedType));
|
|
|
|
|
|
|
|
$parentResolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
|
|
|
|
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo_sub_type_parent_instance'));
|
|
|
|
|
|
|
|
$this->assertSame($resolvedType, $this->registry->getType('foo_sub_type_parent_instance'));
|
|
|
|
}
|
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
/**
|
2013-01-05 17:31:34 +00:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\Exception
|
2012-07-29 18:10:47 +01:00
|
|
|
*/
|
|
|
|
public function testGetTypeThrowsExceptionIfParentNotFound()
|
|
|
|
{
|
|
|
|
$type = new FooSubType();
|
|
|
|
|
|
|
|
$this->extension1->addType($type);
|
|
|
|
|
|
|
|
$this->registry->getType($type);
|
2012-07-12 18:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-05 17:31:34 +00:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\Exception
|
2012-07-12 18:17:14 +01:00
|
|
|
*/
|
|
|
|
public function testGetTypeThrowsExceptionIfTypeNotFound()
|
|
|
|
{
|
|
|
|
$this->registry->getType('bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-05 17:52:40 +00:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
|
2012-07-12 18:17:14 +01:00
|
|
|
*/
|
|
|
|
public function testGetTypeThrowsExceptionIfNoString()
|
|
|
|
{
|
|
|
|
$this->registry->getType(array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasTypeAfterAdding()
|
|
|
|
{
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
2012-07-12 18:17:14 +01:00
|
|
|
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
|
|
|
$this->assertFalse($this->registry->hasType('foo'));
|
|
|
|
|
2012-12-16 08:38:51 +00:00
|
|
|
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
|
2012-07-12 18:17:14 +01:00
|
|
|
$this->registry->addType($resolvedType);
|
2012-12-14 06:31:09 +00:00
|
|
|
restore_error_handler();
|
2012-07-12 18:17:14 +01:00
|
|
|
|
|
|
|
$this->assertTrue($this->registry->hasType('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasTypeAfterLoadingFromExtension()
|
|
|
|
{
|
|
|
|
$type = new FooType();
|
2012-07-29 18:10:47 +01:00
|
|
|
$resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
|
|
|
|
|
|
|
|
$this->resolvedTypeFactory->expects($this->once())
|
|
|
|
->method('createResolvedType')
|
|
|
|
->with($type)
|
|
|
|
->will($this->returnValue($resolvedType));
|
|
|
|
|
|
|
|
$resolvedType->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('foo'));
|
2012-07-12 18:17:14 +01:00
|
|
|
|
|
|
|
$this->assertFalse($this->registry->hasType('foo'));
|
|
|
|
|
|
|
|
$this->extension2->addType($type);
|
|
|
|
|
|
|
|
$this->assertTrue($this->registry->hasType('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
array($this->getMock('Symfony\Component\Form\FormExtensionInterface')),
|
|
|
|
$this->resolvedTypeFactory);
|
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
}
|