LazyChoiceListTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Tests\ChoiceList;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\ChoiceList\LazyChoiceList;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class LazyChoiceListTest extends TestCase
  17. {
  18. /**
  19. * @var LazyChoiceList
  20. */
  21. private $list;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $innerList;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $loader;
  30. private $value;
  31. protected function setUp()
  32. {
  33. $this->innerList = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
  34. $this->loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
  35. $this->value = function () {};
  36. $this->list = new LazyChoiceList($this->loader, $this->value);
  37. }
  38. public function testGetChoicesLoadsInnerListOnFirstCall()
  39. {
  40. $this->loader->expects($this->once())
  41. ->method('loadChoiceList')
  42. ->with($this->value)
  43. ->will($this->returnValue($this->innerList));
  44. $this->innerList->expects($this->exactly(2))
  45. ->method('getChoices')
  46. ->will($this->returnValue('RESULT'));
  47. $this->assertSame('RESULT', $this->list->getChoices());
  48. $this->assertSame('RESULT', $this->list->getChoices());
  49. }
  50. public function testGetValuesLoadsInnerListOnFirstCall()
  51. {
  52. $this->loader->expects($this->once())
  53. ->method('loadChoiceList')
  54. ->with($this->value)
  55. ->will($this->returnValue($this->innerList));
  56. $this->innerList->expects($this->exactly(2))
  57. ->method('getValues')
  58. ->will($this->returnValue('RESULT'));
  59. $this->assertSame('RESULT', $this->list->getValues());
  60. $this->assertSame('RESULT', $this->list->getValues());
  61. }
  62. public function testGetStructuredValuesLoadsInnerListOnFirstCall()
  63. {
  64. $this->loader->expects($this->once())
  65. ->method('loadChoiceList')
  66. ->with($this->value)
  67. ->will($this->returnValue($this->innerList));
  68. $this->innerList->expects($this->exactly(2))
  69. ->method('getStructuredValues')
  70. ->will($this->returnValue('RESULT'));
  71. $this->assertSame('RESULT', $this->list->getStructuredValues());
  72. $this->assertSame('RESULT', $this->list->getStructuredValues());
  73. }
  74. public function testGetOriginalKeysLoadsInnerListOnFirstCall()
  75. {
  76. $this->loader->expects($this->once())
  77. ->method('loadChoiceList')
  78. ->with($this->value)
  79. ->will($this->returnValue($this->innerList));
  80. $this->innerList->expects($this->exactly(2))
  81. ->method('getOriginalKeys')
  82. ->will($this->returnValue('RESULT'));
  83. $this->assertSame('RESULT', $this->list->getOriginalKeys());
  84. $this->assertSame('RESULT', $this->list->getOriginalKeys());
  85. }
  86. public function testGetChoicesForValuesForwardsCallIfListNotLoaded()
  87. {
  88. $this->loader->expects($this->exactly(2))
  89. ->method('loadChoicesForValues')
  90. ->with(array('a', 'b'))
  91. ->will($this->returnValue('RESULT'));
  92. $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
  93. $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
  94. }
  95. public function testGetChoicesForValuesUsesLoadedList()
  96. {
  97. $this->loader->expects($this->once())
  98. ->method('loadChoiceList')
  99. ->with($this->value)
  100. ->will($this->returnValue($this->innerList));
  101. $this->loader->expects($this->never())
  102. ->method('loadChoicesForValues');
  103. $this->innerList->expects($this->exactly(2))
  104. ->method('getChoicesForValues')
  105. ->with(array('a', 'b'))
  106. ->will($this->returnValue('RESULT'));
  107. // load choice list
  108. $this->list->getChoices();
  109. $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
  110. $this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
  111. }
  112. public function testGetValuesForChoicesForwardsCallIfListNotLoaded()
  113. {
  114. $this->loader->expects($this->exactly(2))
  115. ->method('loadValuesForChoices')
  116. ->with(array('a', 'b'))
  117. ->will($this->returnValue('RESULT'));
  118. $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
  119. $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
  120. }
  121. public function testGetValuesForChoicesUsesLoadedList()
  122. {
  123. $this->loader->expects($this->once())
  124. ->method('loadChoiceList')
  125. ->with($this->value)
  126. ->will($this->returnValue($this->innerList));
  127. $this->loader->expects($this->never())
  128. ->method('loadValuesForChoices');
  129. $this->innerList->expects($this->exactly(2))
  130. ->method('getValuesForChoices')
  131. ->with(array('a', 'b'))
  132. ->will($this->returnValue('RESULT'));
  133. // load choice list
  134. $this->list->getChoices();
  135. $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
  136. $this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
  137. }
  138. }