8000 [Tests] [Propel] Added some tests for the ModelChoiceList class · helmer/symfony@007de8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 007de8c

Browse files
committed
[Tests] [Propel] Added some tests for the ModelChoiceList class
1 parent 8257efd commit 007de8c

File tree

5 files changed

+293
-0
lines changed

5 files changed

+293
-0
lines changed

autoload.php.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ if (is_file(__DIR__.'/vendor/swiftmailer/lib/classes/Swift.php')) {
3434
require_once __DIR__.'/vendor/swiftmailer/lib/classes/Swift.php';
3535
Swift::registerAutoload(__DIR__.'/vendor/swiftmailer/lib/swift_init.php');
3636
}
37+
38+
if (is_file($file = __DIR__.'/vendor/propel/runtime/lib/Propel.php')) {
39+
require_once $file;
40+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* this file is part of the symfony package.
5+
*
6+
* (c) fabien potencier <fabien@symfony.com>
7+
*
8+
* for the full copyright and license information, please view the license
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Bridge\Propel1\Fixtures;
13+
14+
use \PropelPDO;
15+
16+
class Item implements \Persistent
17+
{
18+
private $id;
19+
20+
private $value;
21+
22+
private $groupName;
23+
24+
public function __construct($id = null, $value = null, $groupName = null)
25+
{
26+
$this->id = $id;
27+
$this->value = $value;
28+
$this->groupName = $groupName;
29+
}
30+
31+
public function getId()
32+
{
33+
return $this->id;
34+
}
35+
36+
public function setId($id)
37+
{
38+
$this->id = $id;
39+
}
40+
41+
public function getValue()
42+
{
43+
return $this->value;
44+
}
45+
46+
public function getGroupName()
47+
{
48+
return $this->groupName;
49+
}
50+
51+
public function getPrimaryKey()
52+
{
53+
return $this->getId();
54+
}
55+
56+
public function setPrimaryKey($primaryKey)
57+
{
58+
$this->setId($primaryKey);
59+
}
60+
61+
public function isModified()
62+
{
63+
return false;
64+
}
65+
66+
public function isColumnModified($col)
67+
{
68+
return false;
69+
}
70+
71+
public function isNew()
72+
{
73+
return false;
74+
}
75+
76+
public function setNew($b)
77+
{
78+
}
79+
80+
public function resetModified()
81+
{
82+
}
83+
84+
public function isDeleted()
85+
{
86+
return false;
87+
}
88+
89+
public function setDeleted($b)
90+
{
91+
}
92+
93+
public function delete(PropelPDO $con = null)
94+
{
95+
}
96+
97+
public function save(PropelPDO $con = null)
98+
{
99+
}
100+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* this file is part of the symfony package.
5+
*
6+
* (c) fabien potencier <fabien@symfony.com>
7+
*
8+
* for the full copyright and license information, please view the license
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Bridge\Propel1\Fixtures;
13+
14+
class ItemQuery
15+
{
16+
public function getTableMap()
17+
{
18+
// Allows to define methods in this class
19+
// to avoid a lot of mock classes
20+
return $this;
21+
}
22+
23+
public function getPrimaryKeys()
24+
{
25+
return array('id');
26+
}
27+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Bridge\Propel1\Form\ChoiceList;
13+
14+
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
15+
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
16+
17+
use Symfony\Tests\Bridge\Propel1\Fixtures\Item;
18+
use Symfony\Tests\Bridge\Propel1\Propel1TestCase;
19+
20+
class ModelChoiceListTest extends Propel1TestCase
21+
{
22+
const ITEM_CLASS = '\Symfony\Tests\Bridge\Propel1\Fixtures\Item';
23+
24+
public function testEmptyChoicesReturnsEmpty()
25+
{
26+
$choiceList = new ModelChoiceList(
27+
self::ITEM_CLASS,
28+
'value',
29+
array()
30+
);
31+
32+
$this->assertSame(array(), $choiceList->getChoices());
33+
}
34+
35+
public function testFlattenedChoices()
36+
{
37+
$item1 = new Item(1, 'Foo');
38+
$item2 = new Item(2, 'Bar');
39+
40+
$choiceList = new ModelChoiceList(
41+
self::ITEM_CLASS,
42+
'value',
43+
array(
44+
$item1,
45+
$item2,
46+
)
47+
);
48+
49+
$this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
50+
}
51+
52+
public function testNestedChoices()
53+
{
54+
$item1 = new Item(1, 'Foo');
55+
$item2 = new Item(2, 'Bar');
56+
57+
$choiceList = new ModelChoiceList(
58+
self::ITEM_CLASS,
59+
'value',
60+
array(
61+
'group1' => array($item1),
62+
'group2' => array($item2),
63+
)
64+
);
65+
66+
$this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
67+
$this->assertEquals(array(
68+
'group1' => array(1 => new ChoiceView('1', 'Foo')),
69+
'group2' => array(2 => new ChoiceView('2', 'Bar'))
70+
), $choiceList->getRemainingViews());
71+
}
72+
73+
public function testGroupBySupportsString()
74+
{
75+
$item1 = new Item(1, 'Foo', 'Group1');
76+
$item2 = new Item(2, 'Bar', 'Group1');
77+
$item3 = new Item(3, 'Baz', 'Group2');
78+
$item4 = new Item(4, 'Boo!', null);
79+
80+
$choiceList = new ModelChoiceList(
81+
self::ITEM_CLASS,
82+
'value',
83+
array(
84+
$item1,
85+
$item2,
86+
$item3,
87+
$item4,
88+
),
89+
null,
90+
'groupName'
91+
);
92+
93+
$this->assertEquals(array(1 => $item1, 2 => $item2, 3 => $item3, 4 => $item4), $choiceList->getChoices());
94+
$this->assertEquals(array(
95+
'Group1' => array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')),
96+
'Group2' => array(3 => new ChoiceView('3', 'Baz')),
97+
4 => new ChoiceView('4', 'Boo!')
98+
), $choiceList->getRemainingViews());
99+
}
100+
101+
public function testGroupByInvalidPropertyPathReturnsFlatChoices()
102+
{
103+
$item1 = new Item(1, 'Foo', 'Group1');
104+
$item2 = new Item(2, 'Bar', 'Group1');
105+
106+
$choiceList = new ModelChoiceList(
107+
self::ITEM_CLASS,
108+
'value',
109+
array(
110+
$item1,
111+
$item2,
112+
),
113+
null,
114+
'child.that.does.not.exist'
115+
);
116+
117+
$this->assertEquals(array(
118+
1 => $item1,
119+
2 => $item2
120+
), $choiceList->getChoices());
121+
}
122+
123+
public function testGetValuesForChoices()
124+
{
125+
$item1 = new Item(1, 'Foo');
126+
$item2 = new Item(2, 'Bar');
127+
128+
$choiceList = new ModelChoiceList(
129+
self::ITEM_CLASS,
130+
'value',
131+
null,
132+
null,
133+
null,
134+
null
135+
);
136+
137+
$this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2)));
138+
$this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2)));
139+
}
140+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Bridge\Propel1;
13+
14+
abstract class Propel1TestCase extends \PHPUnit_Framework_TestCase
15+
{
16+
public function setUp()
17+
{
18+
if (!class_exists('\Propel')) {
19+
$this->markTestSkipped('Propel is not available.');
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)
0