123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?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\ChoiceList;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Form\ChoiceList\LazyChoiceList;
- /**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class LazyChoiceListTest extends TestCase
- {
- /**
- * @var LazyChoiceList
- */
- private $list;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $innerList;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $loader;
- private $value;
- protected function setUp()
- {
- $this->innerList = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
- $this->loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
- $this->value = function () {};
- $this->list = new LazyChoiceList($this->loader, $this->value);
- }
- public function testGetChoicesLoadsInnerListOnFirstCall()
- {
- $this->loader->expects($this->once())
- ->method('loadChoiceList')
- ->with($this->value)
- ->will($this->returnValue($this->innerList));
- $this->innerList->expects($this->exactly(2))
- ->method('getChoices')
- ->will($this->returnValue('RESULT'));
- $this->assertSame('RESULT', $this->list->getChoices());
- $this->assertSame('RESULT', $this->list->getChoices());
- }
- public function testGetValuesLoadsInnerListOnFirstCall()
- {
- $this->loader->expects($this->once())
- ->method('loadChoiceList')
- ->with($this->value)
- ->will($this->returnValue($this->innerList));
- $this->innerList->expects($this->exactly(2))
- ->method('getValues')
- ->will($this->returnValue('RESULT'));
- $this->assertSame('RESULT', $this->list->getValues());
- $this->assertSame('RESULT', $this->list->getValues());
- }
- public function testGetStructuredValuesLoadsInnerListOnFirstCall()
- {
- $this->loader->expects($this->once())
- ->method('loadChoiceList')
- ->with($this->value)
- ->will($this->returnValue($this->innerList));
- $this->innerList->expects($this->exactly(2))
- ->method('getStructuredValues')
- ->will($this->returnValue('RESULT'));
- $this->assertSame('RESULT', $this->list->getStructuredValues());
- $this->assertSame('RESULT', $this->list->getStructuredValues());
- }
- public function testGetOriginalKeysLoadsInnerListOnFirstCall()
- {
- $this->loader->expects($this->once())
- ->method('loadChoiceList')
- ->with($this->value)
- ->will($this->returnValue($this->innerList));
- $this->innerList->expects($this->exactly(2))
- ->method('getOriginalKeys')
- ->will($this->returnValue('RESULT'));
- $this->assertSame('RESULT', $this->list->getOriginalKeys());
- $this->assertSame('RESULT', $this->list->getOriginalKeys());
- }
- public function testGetChoicesForValuesForwardsCallIfListNotLoaded()
- {
- $this->loader->expects($this->exactly(2))
- ->method('loadChoicesForValues')
- ->with(array('a', 'b'))
- ->will($this->returnValue('RESULT'));
- $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
- $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
- }
- public function testGetChoicesForValuesUsesLoadedList()
- {
- $this->loader->expects($this->once())
- ->method('loadChoiceList')
- ->with($this->value)
- ->will($this->returnValue($this->innerList));
- $this->loader->expects($this->never())
- ->method('loadChoicesForValues');
- $this->innerList->expects($this->exactly(2))
- ->method('getChoicesForValues')
- ->with(array('a', 'b'))
- ->will($this->returnValue('RESULT'));
- // load choice list
- $this->list->getChoices();
- $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
- $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
- }
- public function testGetValuesForChoicesForwardsCallIfListNotLoaded()
- {
- $this->loader->expects($this->exactly(2))
- ->method('loadValuesForChoices')
- ->with(array('a', 'b'))
- ->will($this->returnValue('RESULT'));
- $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
- $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
- }
- public function testGetValuesForChoicesUsesLoadedList()
- {
- $this->loader->expects($this->once())
- ->method('loadChoiceList')
- ->with($this->value)
- ->will($this->returnValue($this->innerList));
- $this->loader->expects($this->never())
- ->method('loadValuesForChoices');
- $this->innerList->expects($this->exactly(2))
- ->method('getValuesForChoices')
- ->with(array('a', 'b'))
- ->will($this->returnValue('RESULT'));
- // load choice list
- $this->list->getChoices();
- $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
- $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
- }
- }
|