ApplyPersistentDataObserverTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ApplyPersistentDataObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\ApplyPersistentDataObserver
  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 $persistentConfigMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $observerMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $configMock;
  38. protected function setUp()
  39. {
  40. $this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  41. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  42. $this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  43. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  44. $this->persistentConfigMock = $this->createMock(\Magento\Persistent\Model\Persistent\Config::class);
  45. $this->configMock =
  46. $this->createPartialMock(\Magento\Persistent\Model\Persistent\ConfigFactory::class, ['create']);
  47. $this->model = new \Magento\Persistent\Observer\ApplyPersistentDataObserver(
  48. $this->sessionMock,
  49. $this->persistentHelperMock,
  50. $this->customerSessionMock,
  51. $this->configMock
  52. );
  53. }
  54. public function testExecuteWhenCanNotApplyPersistentData()
  55. {
  56. $this->persistentHelperMock
  57. ->expects($this->once())
  58. ->method('canProcess')
  59. ->with($this->observerMock)
  60. ->will($this->returnValue(false));
  61. $this->configMock->expects($this->never())->method('create');
  62. $this->model->execute($this->observerMock);
  63. }
  64. public function testExecuteWhenCustomerIsNotPersistent()
  65. {
  66. $this->persistentHelperMock
  67. ->expects($this->once())
  68. ->method('canProcess')
  69. ->with($this->observerMock)
  70. ->will($this->returnValue(true));
  71. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
  72. $this->configMock->expects($this->never())->method('create');
  73. $this->model->execute($this->observerMock);
  74. }
  75. public function testExecuteWhenCustomerIsLoggedIn()
  76. {
  77. $this->persistentHelperMock
  78. ->expects($this->once())
  79. ->method('canProcess')
  80. ->with($this->observerMock)
  81. ->will($this->returnValue(true));
  82. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  83. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  84. $this->configMock->expects($this->never())->method('create');
  85. $this->model->execute($this->observerMock);
  86. }
  87. public function testExecute()
  88. {
  89. $this->persistentHelperMock
  90. ->expects($this->once())
  91. ->method('canProcess')
  92. ->with($this->observerMock)
  93. ->will($this->returnValue(true));
  94. $this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  95. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  96. $this->configMock
  97. ->expects($this->once())
  98. ->method('create')
  99. ->will($this->returnValue($this->persistentConfigMock));
  100. $this->persistentHelperMock
  101. ->expects($this->once())
  102. ->method('getPersistentConfigFilePath')
  103. ->will($this->returnValue('path/path1'));
  104. $this->persistentConfigMock
  105. ->expects($this->once())
  106. ->method('setConfigFilePath')
  107. ->with('path/path1')
  108. ->will($this->returnSelf());
  109. $this->persistentConfigMock->expects($this->once())->method('fire');
  110. $this->model->execute($this->observerMock);
  111. }
  112. }