Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed methods if_true, if_false, if_end in BaseGroupedMapper to camelCase, added tests #2664

Merged
merged 1 commit into from Jan 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions Mapper/BaseGroupedMapper.php
Expand Up @@ -138,17 +138,18 @@ public function with($name, array $options = array())
}

/**
* Only nested add if the condition match FALSE
* Only nested add if the condition match true
*
* @param $bool
* @param boolean $bool
*
* @return BaseGroupedMapper
*
* @return $this
* @throws \RuntimeException
*/
public function if_true($bool)
public function ifTrue($bool)
{
if (!$this->apply == null) {
throw new \RuntimeException('Cannot nest ifTrue call');
if ($this->apply !== null) {
throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
}

$this->apply = ($bool === true);
Expand All @@ -157,17 +158,18 @@ public function if_true($bool)
}

/**
* Only nested add if the condition match FALSE
* Only nested add if the condition match false
*
* @param $bool
* @param boolean $bool
*
* @return BaseGroupedMapper
*
* @return $this
* @throws \RuntimeException
*/
public function if_false($bool)
public function ifFalse($bool)
{
if (!$this->apply == null) {
throw new \RuntimeException('Cannot nest ifTrue call');
if ($this->apply !== null) {
throw new \RuntimeException('Cannot nest ifTrue or ifFalse call');
}

$this->apply = ($bool === false);
Expand All @@ -178,7 +180,7 @@ public function if_false($bool)
/**
* @return $this
*/
public function end_if()
public function ifEnd()
{
$this->apply = null;

Expand Down
9 changes: 9 additions & 0 deletions Tests/Fixtures/Admin/CleanAdmin.php
@@ -0,0 +1,9 @@
<?php

namespace Sonata\AdminBundle\Tests\Fixtures\Admin;

use Sonata\AdminBundle\Admin\Admin;

class CleanAdmin extends Admin
{
}
254 changes: 199 additions & 55 deletions Tests/Form/FormMapperTest.php
Expand Up @@ -11,13 +11,9 @@

namespace Sonata\AdminBundle\Tests\Form;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;

class TestAdmin extends Admin
{

}
use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
use Symfony\Component\Form\FormBuilder;

class FormMapperTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -26,11 +22,6 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
*/
protected $contractor;

/**
* @var \Symfony\Component\Form\FormBuilder
*/
protected $builder;

/**
* @var \Sonata\AdminBundle\Admin\AdminInterface
*/
Expand All @@ -41,16 +32,6 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
*/
protected $modelManager;

/**
* @var \Sonata\AdminBundle\Admin\FieldDescriptionInterface
*/
protected $fieldDescription;

/**
* @var \Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface
*/
protected $labelTranslatorStrategy;

/**
* @var FormMapper
*/
Expand All @@ -59,19 +40,37 @@ class FormMapperTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
$this->builder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
->disableOriginalConstructor()
->getMock();

$this->admin = new TestAdmin('code', 'class', 'controller');
$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

$formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);

$this->admin = new CleanAdmin('code', 'class', 'controller');

$this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
$this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
$this->labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');

// php 5.3 BC
$fieldDescription = $this->getFieldDescriptionMock();

$this->modelManager->expects($this->any())
->method('getNewFieldDescriptionInstance')
->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
$fieldDescriptionClone = clone $fieldDescription;
$fieldDescriptionClone->setName($name);
$fieldDescriptionClone->setOptions($options);

return $fieldDescriptionClone;
}));

$this->admin->setModelManager($this->modelManager);

$labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
$this->admin->setLabelTranslatorStrategy($labelTranslatorStrategy);

$this->formMapper = new FormMapper(
$this->contractor,
$this->builder,
$formBuilder,
$this->admin
);
}
Expand Down Expand Up @@ -130,43 +129,23 @@ public function testWithOptions()

public function testWithFieldsCascadeTranslationDomain()
{
$formGroups = array(
'foobar' => array(
'collapsed' => false,
'fields' => array(),
'description' => false,
'translation_domain' => 'Foobar',
'class' => false,
'name' => 'foobar'
)
);

$this->admin->setModelManager($this->modelManager);

$this->modelManager->expects($this->once())
->method('getNewFieldDescriptionInstance')
->with(
'class',
'foo',
array(
'translation_domain' => 'Foobar',
'type' => 'bar',
)
)
->will($this->returnValue($this->fieldDescription));

$this->contractor->expects($this->once())
->method('getDefaultOptions')
->will($this->returnValue(array()));

$this->admin->setLabelTranslatorStrategy($this->labelTranslatorStrategy);

$this->formMapper->with('foobar', array(
'translation_domain' => 'Foobar'
))
->add('foo', 'bar')
->end();

$fieldDescription = $this->admin->getFormFieldDescription('foo');
$this->assertEquals('foo', $fieldDescription->getName());
$this->assertEquals('bar', $fieldDescription->getType());
$this->assertEquals('Foobar', $fieldDescription->getTranslationDomain());

$this->assertTrue($this->formMapper->has('foo'));

$this->assertEquals(array('default' => array (
'collapsed' => false,
'class' => false,
Expand Down Expand Up @@ -195,4 +174,169 @@ public function testRemoveCascadeRemoveFieldFromFormGroup()
$this->formMapper->with('foo');
$this->formMapper->remove('foo');
}

public function testIfTrueApply()
{
$this->contractor->expects($this->once())
->method('getDefaultOptions')
->will($this->returnValue(array()));

$this->formMapper
->ifTrue(true)
->add('foo', 'bar')
->ifEnd()
;

$this->assertTrue($this->formMapper->has('foo'));
}

public function testIfTrueNotApply()
{
$this->formMapper
->ifTrue(false)
->add('foo', 'bar')
->ifEnd()
;

$this->assertFalse($this->formMapper->has('foo'));
}

public function testIfTrueCombination()
{
$this->contractor->expects($this->once())
->method('getDefaultOptions')
->will($this->returnValue(array()));

$this->formMapper
->ifTrue(false)
->add('foo', 'bar')
->ifEnd()
->add('baz', 'foobaz')
;

$this->assertFalse($this->formMapper->has('foo'));
$this->assertTrue($this->formMapper->has('baz'));
}

public function testIfFalseApply()
{
$this->contractor->expects($this->once())
->method('getDefaultOptions')
->will($this->returnValue(array()));

$this->formMapper
->ifFalse(false)
->add('foo', 'bar')
->ifEnd()
;

$this->assertTrue($this->formMapper->has('foo'));
}

public function testIfFalseNotApply()
{
$this->formMapper
->ifFalse(true)
->add('foo', 'bar')
->ifEnd()
;

$this->assertFalse($this->formMapper->has('foo'));
}

public function testIfFalseCombination()
{
$this->contractor->expects($this->once())
->method('getDefaultOptions')
->will($this->returnValue(array()));

$this->formMapper
->ifFalse(true)
->add('foo', 'bar')
->ifEnd()
->add('baz', 'foobaz')
;

$this->assertFalse($this->formMapper->has('foo'));
$this->assertTrue($this->formMapper->has('baz'));
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
*/
public function testIfTrueNested()
{
$this->formMapper->ifTrue(true);
$this->formMapper->ifTrue(true);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
*/
public function testIfFalseNested()
{
$this->formMapper->ifFalse(false);
$this->formMapper->ifFalse(false);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
*/
public function testIfCombinationNested()
{
$this->formMapper->ifTrue(true);
$this->formMapper->ifFalse(false);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
*/
public function testIfFalseCombinationNested2()
{
$this->formMapper->ifFalse(false);
$this->formMapper->ifTrue(true);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
*/
public function testIfFalseCombinationNested3()
{
$this->formMapper->ifFalse(true);
$this->formMapper->ifTrue(false);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
*/
public function testIfFalseCombinationNested4()
{
$this->formMapper->ifTrue(false);
$this->formMapper->ifFalse(true);
}

private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
{
$fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');

if ($name !== null) {
$fieldDescription->setName($name);
}

if ($label !== null) {
$fieldDescription->setOption('label', $label);
}

if ($translationDomain !== null) {
$fieldDescription->setOption('translation_domain', $translationDomain);
}

return $fieldDescription;
}
}