PreviewTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Test\Unit\Block\Adminhtml\Queue;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class PreviewTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  14. */
  15. protected $objectManager;
  16. /**
  17. * @var \Magento\Newsletter\Model\Template|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $template;
  20. /**
  21. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $request;
  24. /**
  25. * @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $subscriber;
  28. /**
  29. * @var \Magento\Newsletter\Model\Queue|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $queue;
  32. /**
  33. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $storeManager;
  36. /**
  37. * @var \Magento\Newsletter\Block\Adminhtml\Queue\Preview
  38. */
  39. protected $preview;
  40. protected function setUp()
  41. {
  42. $context = $this->createMock(\Magento\Backend\Block\Template\Context::class);
  43. $eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  44. $context->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager));
  45. $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  46. $context->expects($this->once())->method('getScopeConfig')->will($this->returnValue($scopeConfig));
  47. $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  48. $context->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
  49. $this->storeManager = $this->createPartialMock(
  50. \Magento\Store\Model\StoreManager::class,
  51. ['getStores', 'getDefaultStoreView']
  52. );
  53. $context->expects($this->once())->method('getStoreManager')->will($this->returnValue($this->storeManager));
  54. $appState = $this->createMock(\Magento\Framework\App\State::class);
  55. $context->expects($this->once())->method('getAppState')->will($this->returnValue($appState));
  56. $backendSession = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $context->expects($this->once())->method('getBackendSession')->willReturn($backendSession);
  60. $templateFactory = $this->createPartialMock(\Magento\Newsletter\Model\TemplateFactory::class, ['create']);
  61. $this->template = $this->createMock(\Magento\Newsletter\Model\Template::class);
  62. $templateFactory->expects($this->once())->method('create')->will($this->returnValue($this->template));
  63. $subscriberFactory = $this->createPartialMock(\Magento\Newsletter\Model\SubscriberFactory::class, ['create']);
  64. $this->subscriber = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
  65. $subscriberFactory->expects($this->once())->method('create')->will($this->returnValue($this->subscriber));
  66. $queueFactory = $this->createPartialMock(\Magento\Newsletter\Model\QueueFactory::class, ['create']);
  67. $this->queue = $this->createPartialMock(\Magento\Newsletter\Model\Queue::class, ['load']);
  68. $queueFactory->expects($this->any())->method('create')->will($this->returnValue($this->queue));
  69. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  70. $this->preview = $this->objectManager->getObject(
  71. \Magento\Newsletter\Block\Adminhtml\Queue\Preview::class,
  72. [
  73. 'context' => $context,
  74. 'templateFactory' => $templateFactory,
  75. 'subscriberFactory' => $subscriberFactory,
  76. 'queueFactory' => $queueFactory,
  77. ]
  78. );
  79. }
  80. public function testToHtmlEmpty()
  81. {
  82. /** @var \Magento\Store\Model\Store $store */
  83. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId']);
  84. $this->storeManager->expects($this->once())->method('getDefaultStoreView')->will($this->returnValue($store));
  85. $result = $this->preview->toHtml();
  86. $this->assertEquals('', $result);
  87. }
  88. public function testToHtmlWithId()
  89. {
  90. $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap(
  91. [
  92. ['id', null, 1],
  93. ['store_id', null, 0]
  94. ]
  95. ));
  96. $this->queue->expects($this->once())->method('load')->will($this->returnSelf());
  97. $this->template->expects($this->any())->method('isPlain')->will($this->returnValue(true));
  98. /** @var \Magento\Store\Model\Store $store */
  99. $this->storeManager->expects($this->once())->method('getDefaultStoreView')->will($this->returnValue(null));
  100. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId']);
  101. $this->storeManager->expects($this->once())->method('getStores')->will($this->returnValue([0 => $store]));
  102. $result = $this->preview->toHtml();
  103. $this->assertEquals('<pre></pre>', $result);
  104. }
  105. }