ApplyBlockPersistentDataObserverTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Test\Unit\Observer;
  8. class ApplyBlockPersistentDataObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\ApplyBlockPersistentDataObserver
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $sessionMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $persistentHelperMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $customerSessionMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $observerMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $configMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $eventMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $blockMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $persistentConfigMock;
  46. protected function setUp()
  47. {
  48. $eventMethods = ['getConfigFilePath', 'getBlock', '__wakeUp'];
  49. $this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  50. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  51. $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  52. $this->configMock =
  53. $this->createPartialMock(\Magento\Persistent\Model\Persistent\ConfigFactory::class, ['create']);
  54. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  55. $this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
  56. $this->blockMock = $this->createMock(\Magento\Framework\View\Element\AbstractBlock::class);
  57. $this->persistentConfigMock = $this->createMock(\Magento\Persistent\Model\Persistent\Config::class);
  58. $this->model = new \Magento\Persistent\Observer\ApplyBlockPersistentDataObserver(
  59. $this->sessionMock,
  60. $this->persistentHelperMock,
  61. $this->customerSessionMock,
  62. $this->configMock
  63. );
  64. }
  65. public function testExecuteWhenSessionNotPersistent()
  66. {
  67. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
  68. $this->observerMock->expects($this->never())->method('getEvent');
  69. $this->model->execute($this->observerMock);
  70. }
  71. public function testExecuteForLoggedInAndPersistentCustomer()
  72. {
  73. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  74. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  75. $this->observerMock->expects($this->never())->method('getEvent');
  76. $this->model->execute($this->observerMock);
  77. }
  78. public function testExecuteWhenBlockDoesNotExist()
  79. {
  80. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  81. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  82. $this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($this->eventMock));
  83. $this->eventMock->expects($this->once())->method('getBlock')->will($this->returnValue(null));
  84. $this->eventMock->expects($this->never())->method('getConfigFilePath');
  85. $this->model->execute($this->observerMock);
  86. }
  87. public function testExecuteWhenConfigFilePathDoesNotExist()
  88. {
  89. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  90. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  91. $this->observerMock
  92. ->expects($this->any())
  93. ->method('getEvent')
  94. ->will($this->returnValue($this->eventMock));
  95. $this->eventMock->expects($this->once())->method('getBlock')->will($this->returnValue($this->blockMock));
  96. $this->eventMock->expects($this->once())->method('getConfigFilePath')->will($this->returnValue(false));
  97. $this->persistentHelperMock
  98. ->expects($this->once())
  99. ->method('getPersistentConfigFilePath')
  100. ->will($this->returnValue('path1/path2'));
  101. $this->configMock
  102. ->expects($this->once())
  103. ->method('create')
  104. ->will($this->returnValue($this->persistentConfigMock));
  105. $this->persistentConfigMock->expects($this->once())->method('setConfigFilePath')->with('path1/path2');
  106. $this->persistentConfigMock
  107. ->expects($this->once())
  108. ->method('getBlockConfigInfo')
  109. ->with(get_class($this->blockMock))
  110. ->will($this->returnValue(['persistentConfigInfo']));
  111. $this->persistentConfigMock
  112. ->expects($this->once())
  113. ->method('fireOne')
  114. ->with('persistentConfigInfo', $this->blockMock);
  115. $this->model->execute($this->observerMock);
  116. }
  117. public function testExecute()
  118. {
  119. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  120. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  121. $this->observerMock
  122. ->expects($this->any())
  123. ->method('getEvent')
  124. ->will($this->returnValue($this->eventMock));
  125. $this->eventMock->expects($this->once())->method('getBlock')->will($this->returnValue($this->blockMock));
  126. $this->eventMock->expects($this->once())->method('getConfigFilePath')->will($this->returnValue('path1/path2'));
  127. $this->persistentHelperMock
  128. ->expects($this->never())
  129. ->method('getPersistentConfigFilePath');
  130. $this->configMock
  131. ->expects($this->once())
  132. ->method('create')
  133. ->will($this->returnValue($this->persistentConfigMock));
  134. $this->persistentConfigMock->expects($this->once())->method('setConfigFilePath')->with('path1/path2');
  135. $this->persistentConfigMock
  136. ->expects($this->once())
  137. ->method('getBlockConfigInfo')
  138. ->with(get_class($this->blockMock))
  139. ->will($this->returnValue(['persistentConfigInfo']));
  140. $this->persistentConfigMock
  141. ->expects($this->once())
  142. ->method('fireOne')
  143. ->with('persistentConfigInfo', $this->blockMock);
  144. $this->model->execute($this->observerMock);
  145. }
  146. }