[Form] Improved test coverage of ChoiceList classes

This commit is contained in:
Bernhard Schussek 2013-09-10 12:41:39 +02:00
parent 6283b0e93d
commit 31e5ce5de1
5 changed files with 486 additions and 316 deletions

View File

@ -0,0 +1,297 @@
<?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.
*/
namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class AbstractChoiceListTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected $list;
/**
* @var array
*/
protected $choices;
/**
* @var array
*/
protected $values;
/**
* @var array
*/
protected $indices;
/**
* @var array
*/
protected $labels;
/**
* @var mixed
*/
protected $choice1;
/**
* @var mixed
*/
protected $choice2;
/**
* @var mixed
*/
protected $choice3;
/**
* @var mixed
*/
protected $choice4;
/**
* @var string
*/
protected $value1;
/**
* @var string
*/
protected $value2;
/**
* @var string
*/
protected $value3;
/**
* @var string
*/
protected $value4;
/**
* @var int|string
*/
protected $index1;
/**
* @var int|string
*/
protected $index2;
/**
* @var int|string
*/
protected $index3;
/**
* @var int|string
*/
protected $index4;
/**
* @var string
*/
protected $label1;
/**
* @var string
*/
protected $label2;
/**
* @var string
*/
protected $label3;
/**
* @var string
*/
protected $label4;
protected function setUp()
{
parent::setUp();
$this->list = $this->createChoiceList();
$this->choices = $this->getChoices();
$this->indices = $this->getIndices();
$this->values = $this->getValues();
$this->labels = $this->getLabels();
// allow access to the individual entries without relying on their indices
reset($this->choices);
reset($this->indices);
reset($this->values);
reset($this->labels);
for ($i = 1; $i <= 4; ++$i) {
$this->{'choice'.$i} = current($this->choices);
$this->{'index'.$i} = current($this->indices);
$this->{'value'.$i} = current($this->values);
$this->{'label'.$i} = current($this->labels);
next($this->choices);
next($this->indices);
next($this->values);
next($this->labels);
}
}
public function testGetChoices()
{
$this->assertSame($this->choices, $this->list->getChoices());
}
public function testGetValues()
{
$this->assertSame($this->values, $this->list->getValues());
}
public function testGetIndicesForChoices()
{
$choices = array($this->choice1, $this->choice2);
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesKeys()
{
$choices = array(5 => $this->choice1, 8 => $this->choice2);
$this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesOrder()
{
$choices = array($this->choice2, $this->choice1);
$this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->choice1, $this->choice2, 'foobar');
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesEmpty()
{
$this->assertSame(array(), $this->list->getIndicesForChoices(array()));
}
public function testGetIndicesForValues()
{
// values and indices are always the same
$values = array($this->value1, $this->value2);
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesKeys()
{
// values and indices are always the same
$values = array(5 => $this->value1, 8 => $this->value2);
$this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesOrder()
{
$values = array($this->value2, $this->value1);
$this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array($this->value1, $this->value2, 'foobar');
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesEmpty()
{
$this->assertSame(array(), $this->list->getIndicesForValues(array()));
}
public function testGetChoicesForValues()
{
$values = array($this->value1, $this->value2);
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => $this->value1, 8 => $this->value2);
$this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array($this->value2, $this->value1);
$this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array($this->value1, $this->value2, 'foobar');
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
}
// https://github.com/symfony/symfony/issues/3446
public function testGetChoicesForValuesEmpty()
{
$this->assertSame(array(), $this->list->getChoicesForValues(array()));
}
public function testGetValuesForChoices()
{
$choices = array($this->choice1, $this->choice2);
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => $this->choice1, 8 => $this->choice2);
$this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array($this->choice2, $this->choice1);
$this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->choice1, $this->choice2, 'foobar');
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesEmpty()
{
$this->assertSame(array(), $this->list->getValuesForChoices(array()));
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
abstract protected function createChoiceList();
abstract protected function getChoices();
abstract protected function getLabels();
abstract protected function getValues();
abstract protected function getIndices();
}

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class ChoiceListTest extends \PHPUnit_Framework_TestCase
class ChoiceListTest extends AbstractChoiceListTest
{
private $obj1;
@ -24,39 +24,14 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
private $obj4;
private $list;
protected function setUp()
{
parent::setUp();
$this->obj1 = new \stdClass();
$this->obj2 = new \stdClass();
$this->obj3 = new \stdClass();
$this->obj4 = new \stdClass();
$this->list = new ChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
array(
'Group 1' => array('A', 'B'),
'Group 2' => array('C', 'D'),
),
array($this->obj2, $this->obj3)
);
}
protected function tearDown()
{
parent::tearDown();
$this->obj1 = null;
$this->obj2 = null;
$this->obj3 = null;
$this->obj4 = null;
$this->list = null;
parent::setUp();
}
public function testInitArray()
@ -119,111 +94,10 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
), $this->list->getRemainingViews());
}
public function testGetIndicesForChoices()
{
$choices = array($this->obj2, $this->obj3);
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesKeys()
{
$choices = array(5 => $this->obj2, 8 => $this->obj3);
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesOrder()
{
$choices = array($this->obj3, $this->obj2);
$this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->obj2, $this->obj3, 'foobar');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForValues()
{
// values and indices are always the same
$values = array('1', '2');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesKeys()
{
// values and indices are always the same
$values = array(5 => '1', 8 => '2');
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesOrder()
{
// values and indices are always the same
$values = array('2', '1');
$this->assertSame(array(2, 1), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array('1', '2', '5');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetChoicesForValues()
{
$values = array('1', '2');
$this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => '1', 8 => '2');
$this->assertSame(array(5 => $this->obj2, 8 => $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array('2', '1');
$this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array('1', '2', '5');
$this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
}
public function testGetValuesForChoices()
{
$choices = array($this->obj2, $this->obj3);
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => $this->obj2, 8 => $this->obj3);
$this->assertSame(array(5 => '1', 8 => '2'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array($this->obj3, $this->obj2);
$this->assertSame(array('2', '1'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->obj2, $this->obj3, 'foobar');
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testNonMatchingLabels()
public function testInitWithInsufficientLabels()
{
$this->list = new ChoiceList(
array($this->obj1, $this->obj2),
@ -231,7 +105,7 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
);
}
public function testLabelsContainingNull()
public function testInitWithLabelsContainingNull()
{
$this->list = new ChoiceList(
array($this->obj1, $this->obj2),
@ -243,4 +117,42 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
$this->list->getRemainingViews()
);
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new ChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
array(
'Group 1' => array('A', 'B'),
'Group 2' => array('C', 'D'),
),
array($this->obj2, $this->obj3)
);
}
protected function getChoices()
{
return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4);
}
protected function getLabels()
{
return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D');
}
protected function getValues()
{
return array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
}
protected function getIndices()
{
return array(0, 1, 2, 3);
}
}

View File

@ -29,7 +29,7 @@ class ObjectChoiceListTest_EntityWithToString
}
}
class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
class ObjectChoiceListTest extends AbstractChoiceListTest
{
private $obj1;
@ -39,39 +39,14 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
private $obj4;
/**
* @var ObjectChoiceList
*/
private $list;
protected function setUp()
{
parent::setUp();
$this->obj1 = (object) array('name' => 'A');
$this->obj2 = (object) array('name' => 'B');
$this->obj3 = (object) array('name' => 'C');
$this->obj4 = (object) array('name' => 'D');
$this->list = new ObjectChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
'name',
array($this->obj2, $this->obj3)
);
}
protected function tearDown()
{
parent::tearDown();
$this->obj1 = null;
$this->obj2 = null;
$this->obj3 = null;
$this->obj4 = null;
$this->list = null;
parent::setUp();
}
public function testInitArray()
@ -209,4 +184,39 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
array($this->obj1, $this->obj2, $this->obj3, $this->obj4)
);
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new ObjectChoiceList(
array(
'Group 1' => array($this->obj1, $this->obj2),
'Group 2' => array($this->obj3, $this->obj4),
),
'name',
array($this->obj2, $this->obj3)
);
}
protected function getChoices()
{
return array(0 => $this->obj1, 1 => $this->obj2, 2 => $this->obj3, 3 => $this->obj4);
}
protected function getLabels()
{
return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D');
}
protected function getValues()
{
return array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
}
protected function getIndices()
{
return array(0, 1, 2, 3);
}
}

View File

@ -11,43 +11,11 @@
namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
class SimpleChoiceListTest extends AbstractChoiceListTest
{
private $list;
private $numericList;
protected function setUp()
{
parent::setUp();
$choices = array(
'Group 1' => array('a' => 'A', 'b' => 'B'),
'Group 2' => array('c' => 'C', 'd' => 'D'),
);
$numericChoices = array(
'Group 1' => array(0 => 'A', 1 => 'B'),
'Group 2' => array(2 => 'C', 3 => 'D'),
);
$this->list = new SimpleChoiceList($choices, array('b', 'c'));
// Use COPY_CHOICE strategy to test for the various associated problems
$this->numericList = new SimpleChoiceList($numericChoices, array(1, 2));
}
protected function tearDown()
{
parent::tearDown();
$this->list = null;
$this->numericList = null;
}
public function testInitArray()
{
$choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
@ -73,131 +41,6 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
), $this->list->getRemainingViews());
}
public function testGetIndicesForChoices()
{
$choices = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesKeys()
{
$choices = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesPreservesOrder()
{
$choices = array('c', 'b');
$this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
{
$choices = array('b', 'c', 'foobar');
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForChoicesDealsWithNumericChoices()
{
// Pass choices as strings although they are integers
$choices = array('0', '1');
$this->assertSame(array(0, 1), $this->numericList->getIndicesForChoices($choices));
}
public function testGetIndicesForValues()
{
$values = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesKeys()
{
$values = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesPreservesOrder()
{
$values = array('c', 'b');
$this->assertSame(array(2, 1), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
$values = array('b', 'c', '100');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->numericList->getIndicesForValues($values));
}
public function testGetChoicesForValues()
{
$values = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array('c', 'b');
$this->assertSame(array('c', 'b'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array('b', 'c', '100');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->numericList->getChoicesForValues($values));
}
public function testGetValuesForChoices()
{
$choices = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => 'b', 8 => 'c');
$this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array('c', 'b');
$this->assertSame(array('c', 'b'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingValues()
{
$choices = array('b', 'c', 'foobar');
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array('0', '1'), $this->numericList->getValuesForChoices($values));
}
/**
* @dataProvider dirtyValuesProvider
*/
@ -212,7 +55,6 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
'foo10' => 'Foo 10',
);
// use COPY_CHOICE strategy to test the problems
$this->list = new SimpleChoiceList($choices, array());
$this->assertSame(array($value), $this->list->getValuesForChoices(array($choice)));
@ -233,4 +75,35 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
array('foo10', 'foo10'),
);
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new SimpleChoiceList(array(
'Group 1' => array('a' => 'A', 'b' => 'B'),
'Group 2' => array('c' => 'C', 'd' => 'D'),
), array('b', 'c'));
}
protected function getChoices()
{
return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
}
protected function getLabels()
{
return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D');
}
protected function getValues()
{
return array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
}
protected function getIndices()
{
return array(0, 1, 2, 3);
}
}

View File

@ -0,0 +1,78 @@
<?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.
*/
namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class SimpleNumericChoiceListTest extends AbstractChoiceListTest
{
public function testGetIndicesForChoicesDealsWithNumericChoices()
{
// Pass choices as strings although they are integers
$choices = array('0', '1');
$this->assertSame(array(0, 1), $this->list->getIndicesForChoices($choices));
}
public function testGetIndicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->list->getIndicesForValues($values));
}
public function testGetChoicesForValuesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array(0, 1), $this->list->getChoicesForValues($values));
}
public function testGetValuesForChoicesDealsWithNumericValues()
{
// Pass values as strings although they are integers
$values = array('0', '1');
$this->assertSame(array('0', '1'), $this->list->getValuesForChoices($values));
}
/**
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*/
protected function createChoiceList()
{
return new SimpleChoiceList(array(
'Group 1' => array(0 => 'A', 1 => 'B'),
'Group 2' => array(2 => 'C', 3 => 'D'),
), array(1, 2));
}
protected function getChoices()
{
return array(0 => 0, 1 => 1, 2 => 2, 3 => 3);
}
protected function getLabels()
{
return array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D');
}
protected function getValues()
{
return array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
}
protected function getIndices()
{
return array(0, 1, 2, 3);
}
}