ValueTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Config;
  7. class ValueTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\App\Config\Value
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $eventManagerMock;
  17. /**
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $configMock;
  21. /**
  22. * @var \Magento\Framework\App\Cache\TypeListInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $cacheTypeListMock;
  25. /**
  26. * @return void
  27. */
  28. protected function setUp()
  29. {
  30. $this->configMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  31. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  32. $this->cacheTypeListMock = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->model = $objectManager->getObject(
  37. \Magento\Framework\App\Config\Value::class,
  38. [
  39. 'config' => $this->configMock,
  40. 'eventDispatcher' => $this->eventManagerMock,
  41. 'cacheTypeList' => $this->cacheTypeListMock,
  42. ]
  43. );
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function testGetOldValue()
  49. {
  50. $this->configMock->expects(
  51. $this->once()
  52. )->method(
  53. 'getValue'
  54. )->with(
  55. null,
  56. 'default'
  57. )->will(
  58. $this->returnValue('old_value')
  59. );
  60. $this->assertEquals('old_value', $this->model->getOldValue());
  61. }
  62. /**
  63. * @param string $oldValue
  64. * @param string $value
  65. * @param bool $result
  66. * @dataProvider dataIsValueChanged
  67. */
  68. public function testIsValueChanged($oldValue, $value, $result)
  69. {
  70. $this->configMock->expects(
  71. $this->once()
  72. )->method(
  73. 'getValue'
  74. )->with(
  75. null,
  76. 'default'
  77. )->will(
  78. $this->returnValue($oldValue)
  79. );
  80. $this->model->setValue($value);
  81. $this->assertEquals($result, $this->model->isValueChanged());
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function dataIsValueChanged()
  87. {
  88. return [
  89. ['value', 'value', false],
  90. ['value', 'new_value', true],
  91. ];
  92. }
  93. /**
  94. * @return void
  95. */
  96. public function testAfterLoad()
  97. {
  98. $this->eventManagerMock->expects(
  99. $this->at(0)
  100. )->method(
  101. 'dispatch'
  102. )->with(
  103. 'model_load_after',
  104. ['object' => $this->model]
  105. );
  106. $this->eventManagerMock->expects(
  107. $this->at(1)
  108. )->method(
  109. 'dispatch'
  110. )->with(
  111. 'config_data_load_after',
  112. [
  113. 'data_object' => $this->model,
  114. 'config_data' => $this->model,
  115. ]
  116. );
  117. $this->model->afterLoad();
  118. }
  119. /**
  120. * @param mixed $fieldsetData
  121. * @param string $key
  122. * @param string $result
  123. * @dataProvider dataProviderGetFieldsetDataValue
  124. * @return void
  125. */
  126. public function testGetFieldsetDataValue($fieldsetData, $key, $result)
  127. {
  128. $this->model->setData('fieldset_data', $fieldsetData);
  129. $this->assertEquals($result, $this->model->getFieldsetDataValue($key));
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function dataProviderGetFieldsetDataValue()
  135. {
  136. return [
  137. [
  138. ['key' => 'value'],
  139. 'key',
  140. 'value',
  141. ],
  142. [
  143. ['key' => 'value'],
  144. 'none',
  145. null,
  146. ],
  147. [
  148. 'value',
  149. 'key',
  150. null,
  151. ],
  152. ];
  153. }
  154. /**
  155. * @param int $callNumber
  156. * @param string $oldValue
  157. * @dataProvider afterSaveDataProvider
  158. */
  159. public function testAfterSave($callNumber, $oldValue)
  160. {
  161. $this->cacheTypeListMock->expects($this->exactly($callNumber))
  162. ->method('invalidate');
  163. $this->configMock->expects($this->any())
  164. ->method('getValue')
  165. ->willReturn($oldValue);
  166. $this->model->setValue('some_value');
  167. $this->assertInstanceOf(get_class($this->model), $this->model->afterSave());
  168. }
  169. /**
  170. * @return array
  171. */
  172. public function afterSaveDataProvider()
  173. {
  174. return [
  175. [0, 'some_value'],
  176. [1, 'other_value'],
  177. ];
  178. }
  179. /**
  180. * @return void;
  181. */
  182. public function testAfterDelete()
  183. {
  184. $this->cacheTypeListMock->expects($this->once())->method('invalidate');
  185. $this->assertInstanceOf(get_class($this->model), $this->model->afterDelete());
  186. }
  187. }