123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Admin;
- use Sonata\AdminBundle\Admin\BaseFieldDescription;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
- use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
- use Sonata\AdminBundle\Tests\Fixtures\Entity\FooBoolean;
- use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
- use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
- class BaseFieldDescriptionTest extends PHPUnit_Framework_TestCase
- {
- public function testSetName()
- {
- $description = new FieldDescription();
- $description->setName('foo');
- $this->assertSame('foo', $description->getFieldName());
- $this->assertSame('foo', $description->getName());
- }
- public function testOptions()
- {
- $description = new FieldDescription();
- $description->setOption('foo', 'bar');
- $this->assertNull($description->getOption('bar'));
- $this->assertSame('bar', $description->getOption('foo'));
- $description->mergeOptions(array('settings' => array('value_1', 'value_2')));
- $description->mergeOptions(array('settings' => array('value_1', 'value_3')));
- $this->assertSame(array('value_1', 'value_2', 'value_1', 'value_3'), $description->getOption('settings'));
- $description->mergeOption('settings', array('value_4'));
- $this->assertSame(array('value_1', 'value_2', 'value_1', 'value_3', 'value_4'), $description->getOption('settings'));
- $description->mergeOption('bar', array('hello'));
- $this->assertCount(1, $description->getOption('bar'));
- $description->setOption('label', 'trucmuche');
- $this->assertSame('trucmuche', $description->getLabel());
- $this->assertNull($description->getTemplate());
- $description->setOptions(array('type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp'));
- $this->assertSame('integer', $description->getType());
- $this->assertSame('foo.twig.html', $description->getTemplate());
- $this->assertSame('fooHelp', $description->getHelp());
- $this->assertCount(2, $description->getOptions());
- $description->setHelp('Please enter an integer');
- $this->assertSame('Please enter an integer', $description->getHelp());
- $description->setMappingType('int');
- $this->assertSame('int', $description->getMappingType());
- $this->assertSame('short_object_description_placeholder', $description->getOption('placeholder'));
- $description->setOptions(array('placeholder' => false));
- $this->assertFalse($description->getOption('placeholder'));
- $description->setOption('sortable', false);
- $this->assertFalse($description->isSortable());
- $description->setOption('sortable', 'field_name');
- $this->assertTrue($description->isSortable());
- }
- public function testAdmin()
- {
- $description = new FieldDescription();
- $admin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
- $description->setAdmin($admin);
- $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAdmin());
- $associationAdmin = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
- $associationAdmin->expects($this->once())->method('setParentFieldDescription');
- $this->assertFalse($description->hasAssociationAdmin());
- $description->setAssociationAdmin($associationAdmin);
- $this->assertTrue($description->hasAssociationAdmin());
- $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getAssociationAdmin());
- $parent = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\AdminInterface');
- $description->setParent($parent);
- $this->isInstanceOf('Sonata\AdminBundle\Admin\AdminInterface', $description->getParent());
- }
- public function testGetValue()
- {
- $description = new FieldDescription();
- $description->setOption('code', 'getFoo');
- $mock = $this->getMockBuilder('stdClass')
- ->setMethods(array('getFoo'))
- ->getMock();
- $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
- $this->assertSame(42, $description->getFieldValue($mock, 'fake'));
- /*
- * Test with One parameter int
- */
- $arg1 = 38;
- $oneParameter = array($arg1);
- $description1 = new FieldDescription();
- $description1->setOption('code', 'getWithOneParameter');
- $description1->setOption('parameters', $oneParameter);
- $mock1 = $this->getMockBuilder('stdClass')
- ->setMethods(array('getWithOneParameter'))
- ->getMock();
- $returnValue1 = $arg1 + 2;
- $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->will($this->returnValue($returnValue1));
- $this->assertSame(40, $description1->getFieldValue($mock1, 'fake'));
- /*
- * Test with Two parameters int
- */
- $arg2 = 4;
- $twoParameters = array($arg1, $arg2);
- $description2 = new FieldDescription();
- $description2->setOption('code', 'getWithTwoParameters');
- $description2->setOption('parameters', $twoParameters);
- $mock2 = $this->getMockBuilder('stdClass')
- ->setMethods(array('getWithTwoParameters'))
- ->getMock();
- $returnValue2 = $arg1 + $arg2;
- $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1), $this->equalTo($arg2))->will($this->returnValue($returnValue2));
- $this->assertSame(42, $description2->getFieldValue($mock2, 'fake'));
- /*
- * Test with underscored attribute name
- */
- foreach (array('getFake', 'isFake', 'hasFake') as $method) {
- $description3 = new FieldDescription();
- $mock3 = $this->getMockBuilder('stdClass')
- ->setMethods(array($method))
- ->getMock();
- $mock3->expects($this->once())->method($method)->will($this->returnValue(42));
- $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
- }
- }
- /**
- * @expectedException \Sonata\AdminBundle\Exception\NoValueException
- */
- public function testGetValueNoValueException()
- {
- $description = new FieldDescription();
- $mock = $this->getMockBuilder('stdClass')
- ->setMethods(array('getFoo'))
- ->getMock();
- $description->getFieldValue($mock, 'fake');
- }
- public function testGetVirtualValue()
- {
- $description = new FieldDescription();
- $mock = $this->getMockBuilder('stdClass')
- ->setMethods(array('getFoo'))
- ->getMock();
- $description->setOption('virtual_field', true);
- $description->getFieldValue($mock, 'fake');
- }
- /**
- * @expectedException \RuntimeException
- */
- public function testExceptionOnNonArrayOption()
- {
- $description = new FieldDescription();
- $description->setOption('bar', 'hello');
- $description->mergeOption('bar', array('exception'));
- }
- public function testGetTranslationDomain()
- {
- $description = new FieldDescription();
- $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $description->setAdmin($admin);
- $admin->expects($this->once())
- ->method('getTranslationDomain')
- ->will($this->returnValue('AdminDomain'));
- $this->assertSame('AdminDomain', $description->getTranslationDomain());
- $admin->expects($this->never())
- ->method('getTranslationDomain');
- $description->setOption('translation_domain', 'ExtensionDomain');
- $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
- }
- /**
- * @group legacy
- */
- public function testCamelize()
- {
- $this->assertSame('FooBar', BaseFieldDescription::camelize('foo_bar'));
- $this->assertSame('FooBar', BaseFieldDescription::camelize('foo bar'));
- $this->assertSame('FOoBar', BaseFieldDescription::camelize('fOo bar'));
- }
- public function testGetFieldValue()
- {
- $foo = new Foo();
- $foo->setBar('Bar');
- $description = new FieldDescription();
- $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
- $foo = new FooBoolean();
- $foo->setBar(true);
- $foo->setBaz(false);
- $description = new FieldDescription();
- $this->assertSame(true, $description->getFieldValue($foo, 'bar'));
- $this->assertSame(false, $description->getFieldValue($foo, 'baz'));
- $this->expectException('Sonata\AdminBundle\Exception\NoValueException');
- $description->getFieldValue($foo, 'inexistantMethod');
- }
- public function testGetFieldValueWithCodeOption()
- {
- $foo = new Foo();
- $foo->setBaz('Baz');
- $description = new FieldDescription();
- $description->setOption('code', 'getBaz');
- $this->assertSame('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
- $description->setOption('code', 'inexistantMethod');
- $this->expectException('Sonata\AdminBundle\Exception\NoValueException');
- $description->getFieldValue($foo, 'inexistantMethod');
- }
- public function testGetFieldValueMagicCall()
- {
- $parameters = array('foo', 'bar');
- $foo = new FooCall();
- $description = new FieldDescription();
- $description->setOption('parameters', $parameters);
- $this->assertSame(array('inexistantMethod', $parameters), $description->getFieldValue($foo, 'inexistantMethod'));
- }
- }
|