* * 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\ArrayChoiceList; use Symfony\Component\Form\ChoiceList\LazyChoiceList; /** * @author Bernhard Schussek */ class LazyChoiceListTest extends TestCase { /** * @var LazyChoiceList */ private $list; /** * @var \PHPUnit_Framework_MockObject_MockObject */ private $loadedList; /** * @var \PHPUnit_Framework_MockObject_MockObject */ private $loader; private $value; protected function setUp() { $this->loadedList = $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 testGetChoiceLoadersLoadsLoadedListOnFirstCall() { $this->loader->expects($this->exactly(2)) ->method('loadChoiceList') ->with($this->value) ->will($this->returnValue($this->loadedList)); // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getChoices') ->will($this->returnValue('RESULT')); $this->assertSame('RESULT', $this->list->getChoices()); $this->assertSame('RESULT', $this->list->getChoices()); } /** * @group legacy */ public function testGetChoicesUsesLoadedListWhenLoaderDoesNotCacheChoiceListOnFirstCall() { $this->loader->expects($this->at(0)) ->method('loadChoiceList') ->with($this->value) ->willReturn($this->loadedList); $this->loader->expects($this->at(1)) ->method('loadChoiceList') ->with($this->value) ->willReturn(new ArrayChoiceList(array('a', 'b'))); // The same list is returned by the lazy choice list $this->loadedList->expects($this->exactly(2)) ->method('getChoices') ->will($this->returnValue('RESULT')); $this->assertSame('RESULT', $this->list->getChoices()); $this->assertSame('RESULT', $this->list->getChoices()); } public function testGetValuesLoadsLoadedListOnFirstCall() { $this->loader->expects($this->exactly(2)) ->method('loadChoiceList') ->with($this->value) ->will($this->returnValue($this->loadedList)); // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getValues') ->will($this->returnValue('RESULT')); $this->assertSame('RESULT', $this->list->getValues()); $this->assertSame('RESULT', $this->list->getValues()); } public function testGetStructuredValuesLoadsLoadedListOnFirstCall() { $this->loader->expects($this->exactly(2)) ->method('loadChoiceList') ->with($this->value) ->will($this->returnValue($this->loadedList)); // The same list is returned by the loader $this->loadedList->expects($this->exactly(2)) ->method('getStructuredValues') ->will($this->returnValue('RESULT')); $this->assertSame('RESULT', $this->list->getStructuredValues()); $this->assertSame('RESULT', $this->list->getStructuredValues()); } public function testGetOriginalKeysLoadsLoadedListOnFirstCall() { $this->loader->expects($this->exactly(2)) ->method('loadChoiceList') ->with($this->value) ->will($this->returnValue($this->loadedList)); // The same list is returned by the loader $this->loadedList->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->exactly(3)) ->method('loadChoiceList') ->with($this->value) // For BC, the same choice loaded list is returned 3 times // It should only twice in 4.0 ->will($this->returnValue($this->loadedList)); $this->loader->expects($this->never()) ->method('loadChoicesForValues'); $this->loadedList->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'))); } /** * @group legacy */ 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->exactly(3)) ->method('loadChoiceList') ->with($this->value) // For BC, the same choice loaded list is returned 3 times // It should only twice in 4.0 ->will($this->returnValue($this->loadedList)); $this->loader->expects($this->never()) ->method('loadValuesForChoices'); $this->loadedList->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'))); } } __halt_compiler();----SIGNATURE:----SfIRUfbyki+FwvAg/22kHbsysoy3IbajBlnkN7KWqReJ4FpVgadBUCLM54W8D3OPtr7Va3uFornWxcilXwtaNNSW+XkCg7xszT2GlhYFYgzVOegh+dRzx7jI0481tzYzN0I9zheDlCsY8fd1gzQ6IKKiQcN9UwgrkagdVpTde9CMMLTLKZokyTRK2vYe9oi69YcQfPcbYMzQLXIA4xhbC5syhmLmW5jE79TjwHJh6yIYrkUxI7t8VxPiDCli2lFYGIm7aMR2UJrCmg4k+0UnQBSibQT4gBWQNYfnP6swX6A/zfcv9WCu1o9XHS1T3w13pcgoY3YSHYfBYeczZO6Uqk4dtjHFc+U+Gxf4hS+dPM3EmgGCXyGt1qBmjEr/5D7NRDqV7kYETktEQv4JNmpVanPeVGbWakTkbKBREh50YNL4cttGezuejx573ga7vIfea2CURUbiwLYhsIs12KF5zOiMkpHRfj26tjO0nK2R3dKE73nB+iezGhlA8xssPl02umE4ifvmeUKauTAS3Ss5qNq/0ewRSTasmC5QTJG85SX0EweIiBbKU6Hi3WfFWhqPcH+zzZsouYARz0Jdd9RR8g5atGqUoQDyM8UrzI08oSLEI+qMGv0UlWxTwBwvDCDOoM4tt2WOoiKQwovHc80L37Od4/k8nCuMVD/s4SKRh9k=----ATTACHMENT:----MTE1NjIyMDA3NzgxMDcyMiA3MzM3NTAxODMwNTczNzc1IDE0OTQ3NzE1NjkwNzg0Ng==