UpdateTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Cron;
  7. use Magento\Analytics\Cron\Update;
  8. use Magento\Analytics\Model\AnalyticsToken;
  9. use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
  10. use Magento\Analytics\Model\Connector;
  11. use Magento\Framework\App\Config\ReinitableConfigInterface;
  12. use Magento\Framework\App\Config\Storage\WriterInterface;
  13. use Magento\Framework\FlagManager;
  14. class UpdateTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var Connector|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $connectorMock;
  20. /**
  21. * @var WriterInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $configWriterMock;
  24. /**
  25. * @var FlagManager|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $flagManagerMock;
  28. /**
  29. * @var ReinitableConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $reinitableConfigMock;
  32. /**
  33. * @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $analyticsTokenMock;
  36. /**
  37. * @var Update
  38. */
  39. private $update;
  40. protected function setUp()
  41. {
  42. $this->connectorMock = $this->getMockBuilder(Connector::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->configWriterMock = $this->getMockBuilder(WriterInterface::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->flagManagerMock = $this->getMockBuilder(FlagManager::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->reinitableConfigMock = $this->getMockBuilder(ReinitableConfigInterface::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->update = new Update(
  58. $this->connectorMock,
  59. $this->configWriterMock,
  60. $this->reinitableConfigMock,
  61. $this->flagManagerMock,
  62. $this->analyticsTokenMock
  63. );
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testExecuteWithoutToken()
  69. {
  70. $this->flagManagerMock
  71. ->method('getFlagData')
  72. ->with(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE)
  73. ->willReturn(10);
  74. $this->connectorMock
  75. ->expects($this->once())
  76. ->method('execute')
  77. ->with('update')
  78. ->willReturn(false);
  79. $this->analyticsTokenMock
  80. ->expects($this->once())
  81. ->method('isTokenExist')
  82. ->willReturn(false);
  83. $this->addFinalOutputAsserts();
  84. $this->assertFalse($this->update->execute());
  85. }
  86. /**
  87. * @param bool $isExecuted
  88. */
  89. private function addFinalOutputAsserts(bool $isExecuted = true)
  90. {
  91. $this->flagManagerMock
  92. ->expects($this->exactly(2 * $isExecuted))
  93. ->method('deleteFlag')
  94. ->withConsecutive(
  95. [SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE],
  96. [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE]
  97. );
  98. $this->configWriterMock
  99. ->expects($this->exactly((int)$isExecuted))
  100. ->method('delete')
  101. ->with(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH);
  102. $this->reinitableConfigMock
  103. ->expects($this->exactly((int)$isExecuted))
  104. ->method('reinit')
  105. ->with();
  106. }
  107. /**
  108. * @param $counterData
  109. * @return void
  110. * @dataProvider executeWithEmptyReverseCounterDataProvider
  111. */
  112. public function testExecuteWithEmptyReverseCounter($counterData)
  113. {
  114. $this->flagManagerMock
  115. ->method('getFlagData')
  116. ->with(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE)
  117. ->willReturn($counterData);
  118. $this->connectorMock
  119. ->expects($this->never())
  120. ->method('execute')
  121. ->with('update')
  122. ->willReturn(false);
  123. $this->analyticsTokenMock
  124. ->method('isTokenExist')
  125. ->willReturn(true);
  126. $this->addFinalOutputAsserts();
  127. $this->assertFalse($this->update->execute());
  128. }
  129. /**
  130. * Provides empty states of the reverse counter.
  131. *
  132. * @return array
  133. */
  134. public function executeWithEmptyReverseCounterDataProvider()
  135. {
  136. return [
  137. [null],
  138. [0]
  139. ];
  140. }
  141. /**
  142. * @param int $reverseCount
  143. * @param bool $commandResult
  144. * @param bool $finalConditionsIsExpected
  145. * @param bool $functionResult
  146. * @return void
  147. * @dataProvider executeRegularScenarioDataProvider
  148. */
  149. public function testExecuteRegularScenario(
  150. int $reverseCount,
  151. bool $commandResult,
  152. bool $finalConditionsIsExpected,
  153. bool $functionResult
  154. ) {
  155. $this->flagManagerMock
  156. ->method('getFlagData')
  157. ->with(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE)
  158. ->willReturn($reverseCount);
  159. $this->connectorMock
  160. ->expects($this->once())
  161. ->method('execute')
  162. ->with('update')
  163. ->willReturn($commandResult);
  164. $this->analyticsTokenMock
  165. ->method('isTokenExist')
  166. ->willReturn(true);
  167. $this->addFinalOutputAsserts($finalConditionsIsExpected);
  168. $this->assertSame($functionResult, $this->update->execute());
  169. }
  170. /**
  171. * @return array
  172. */
  173. public function executeRegularScenarioDataProvider()
  174. {
  175. return [
  176. 'The last attempt with command execution result False' => [
  177. 'Reverse count' => 1,
  178. 'Command result' => false,
  179. 'Executed final output conditions' => true,
  180. 'Function result' => false,
  181. ],
  182. 'Not the last attempt with command execution result False' => [
  183. 'Reverse count' => 10,
  184. 'Command result' => false,
  185. 'Executed final output conditions' => false,
  186. 'Function result' => false,
  187. ],
  188. 'Command execution result True' => [
  189. 'Reverse count' => 10,
  190. 'Command result' => true,
  191. 'Executed final output conditions' => true,
  192. 'Function result' => true,
  193. ],
  194. ];
  195. }
  196. }