LoadCustomerQuoteObserverTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Test\Unit\Observer;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class LoadCustomerQuoteObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Checkout\Observer\LoadCustomerQuoteObserver */
  11. protected $object;
  12. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  13. protected $objectManager;
  14. /** @var \PHPUnit_Framework_MockObject_MockObject */
  15. protected $checkoutSession;
  16. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $messageManager;
  18. protected function setUp()
  19. {
  20. $this->objectManager = new ObjectManager($this);
  21. $this->checkoutSession = $this->createMock(\Magento\Checkout\Model\Session::class);
  22. $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  23. $this->object = $this->objectManager->getObject(
  24. \Magento\Checkout\Observer\LoadCustomerQuoteObserver::class,
  25. [
  26. 'checkoutSession' => $this->checkoutSession,
  27. 'messageManager' => $this->messageManager
  28. ]
  29. );
  30. }
  31. public function testLoadCustomerQuoteThrowingCoreException()
  32. {
  33. $this->checkoutSession->expects($this->once())->method('loadCustomerQuote')->willThrowException(
  34. new \Magento\Framework\Exception\LocalizedException(__('Message'))
  35. );
  36. $this->messageManager->expects($this->once())->method('addErrorMessage')->with('Message');
  37. $observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->object->execute($observerMock);
  41. }
  42. public function testLoadCustomerQuoteThrowingException()
  43. {
  44. $exception = new \Exception('Message');
  45. $this->checkoutSession->expects($this->once())->method('loadCustomerQuote')->will(
  46. $this->throwException($exception)
  47. );
  48. $this->messageManager->expects($this->once())->method('addExceptionMessage')
  49. ->with($exception, 'Load customer quote error');
  50. $observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->object->execute($observerMock);
  54. }
  55. }