EmulateQuoteObserverTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Observer;
  8. /**
  9. * @magentoDataFixture Magento/Persistent/_files/persistent.php
  10. */
  11. class EmulateQuoteObserverTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  15. */
  16. protected $customerRepository;
  17. /**
  18. * @var \Magento\Persistent\Helper\Session
  19. */
  20. protected $_persistentSessionHelper;
  21. /**
  22. * @var \Magento\Framework\ObjectManagerInterface
  23. */
  24. protected $_objectManager;
  25. /**
  26. * @var \Magento\Persistent\Observer\EmulateQuoteObserver
  27. */
  28. protected $_observer;
  29. /**
  30. * @var \Magento\Customer\Model\Session
  31. */
  32. protected $_customerSession;
  33. /**
  34. * @var \Magento\Checkout\Model\Session | \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $_checkoutSession;
  37. public function setUp()
  38. {
  39. $this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  40. $this->_customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  41. $this->customerRepository = $this->_objectManager->create(
  42. \Magento\Customer\Api\CustomerRepositoryInterface::class
  43. );
  44. $this->_checkoutSession = $this->getMockBuilder(
  45. \Magento\Checkout\Model\Session::class
  46. )->disableOriginalConstructor()->setMethods([])->getMock();
  47. $this->_persistentSessionHelper = $this->_objectManager->create(\Magento\Persistent\Helper\Session::class);
  48. $this->_observer = $this->_objectManager->create(
  49. \Magento\Persistent\Observer\EmulateQuoteObserver::class,
  50. [
  51. 'customerRepository' => $this->customerRepository,
  52. 'checkoutSession' => $this->_checkoutSession,
  53. 'persistentSession' => $this->_persistentSessionHelper
  54. ]
  55. );
  56. }
  57. /**
  58. * @magentoConfigFixture current_store persistent/options/enabled 1
  59. * @magentoConfigFixture current_store persistent/options/remember_enabled 1
  60. * @magentoConfigFixture current_store persistent/options/remember_default 1
  61. * @magentoAppArea frontend
  62. * @magentoConfigFixture current_store persistent/options/shopping_cart 1
  63. * @magentoConfigFixture current_store persistent/options/logout_clear 0
  64. */
  65. public function testEmulateQuote()
  66. {
  67. $requestMock = $this->getMockBuilder(
  68. \Magento\Framework\App\Request\Http::class
  69. )->disableOriginalConstructor()->setMethods(
  70. []
  71. )->getMock();
  72. $requestMock->expects($this->once())->method('getFullActionName')->will($this->returnValue('valid_action'));
  73. $event = new \Magento\Framework\Event(['request' => $requestMock]);
  74. $observer = new \Magento\Framework\Event\Observer();
  75. $observer->setEvent($event);
  76. $this->_customerSession->loginById(1);
  77. $customer = $this->customerRepository->getById(
  78. $this->_persistentSessionHelper->getSession()->getCustomerId()
  79. );
  80. $this->_checkoutSession->expects($this->once())->method('setCustomerData')->with($customer);
  81. $this->_customerSession->logout();
  82. $this->_observer->execute($observer);
  83. }
  84. }