IndexTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rss\Test\Unit\Controller\Adminhtml\Feed;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Zend\Feed\Writer\Exception\InvalidArgumentException;
  9. /**
  10. * Class IndexTest
  11. * @package Magento\Rss\Controller\Feed
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class IndexTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Rss\Controller\Feed\Index
  18. */
  19. protected $controller;
  20. /**
  21. * @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $context;
  24. /**
  25. * @var \Magento\Rss\Model\RssManager|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $rssManager;
  28. /**
  29. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $scopeConfigInterface;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $rssFactory;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $response;
  40. protected function setUp()
  41. {
  42. $this->rssManager = $this->createPartialMock(\Magento\Rss\Model\RssManager::class, ['getProvider']);
  43. $this->scopeConfigInterface = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  44. $this->rssFactory = $this->createPartialMock(\Magento\Rss\Model\RssFactory::class, ['create']);
  45. $request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  46. $request->expects($this->once())->method('getParam')->with('type')->will($this->returnValue('rss_feed'));
  47. $this->response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  48. ->setMethods(['setHeader', 'setBody', 'sendResponse'])
  49. ->disableOriginalConstructor()->getMock();
  50. $objectManagerHelper = new ObjectManagerHelper($this);
  51. $controllerArguments = $objectManagerHelper->getConstructArguments(
  52. \Magento\Rss\Controller\Adminhtml\Feed\Index::class,
  53. [
  54. 'rssManager' => $this->rssManager,
  55. 'scopeConfig' => $this->scopeConfigInterface,
  56. 'rssFactory' => $this->rssFactory,
  57. 'request' => $request,
  58. 'response' => $this->response
  59. ]
  60. );
  61. $objectManager = $controllerArguments['context']->getObjectManager();
  62. $urlInterface = $this->createMock(\Magento\Backend\Model\UrlInterface::class);
  63. $objectManager->expects($this->at(0))->method('get')->with(\Magento\Backend\Model\UrlInterface::class)
  64. ->will($this->returnValue($urlInterface));
  65. $this->controller = $objectManagerHelper->getObject(
  66. \Magento\Rss\Controller\Adminhtml\Feed\Index::class,
  67. $controllerArguments
  68. );
  69. }
  70. public function testExecute()
  71. {
  72. $this->scopeConfigInterface->expects($this->once())->method('getValue')->will($this->returnValue(true));
  73. $dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
  74. $dataProvider->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
  75. $rssModel = $this->createPartialMock(\Magento\Rss\Model\Rss::class, ['setDataProvider', 'createRssXml']);
  76. $rssModel->expects($this->once())->method('setDataProvider')->will($this->returnSelf());
  77. $rssModel->expects($this->once())->method('createRssXml')->will($this->returnValue(''));
  78. $this->response->expects($this->once())->method('setHeader')->will($this->returnSelf());
  79. $this->response->expects($this->once())->method('setBody')->will($this->returnSelf());
  80. $this->rssFactory->expects($this->once())->method('create')->will($this->returnValue($rssModel));
  81. $this->rssManager->expects($this->once())->method('getProvider')->will($this->returnValue($dataProvider));
  82. $this->controller->execute();
  83. }
  84. public function testExecuteWithException()
  85. {
  86. $this->scopeConfigInterface->expects($this->once())->method('getValue')->will($this->returnValue(true));
  87. $dataProvider = $this->createMock(\Magento\Framework\App\Rss\DataProviderInterface::class);
  88. $dataProvider->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
  89. $rssModel = $this->createPartialMock(\Magento\Rss\Model\Rss::class, ['setDataProvider', 'createRssXml']);
  90. $rssModel->expects($this->once())->method('setDataProvider')->will($this->returnSelf());
  91. $exceptionMock = new \Magento\Framework\Exception\RuntimeException(
  92. new \Magento\Framework\Phrase('Any message')
  93. );
  94. $rssModel->expects($this->once())->method('createRssXml')->will(
  95. $this->throwException($exceptionMock)
  96. );
  97. $this->response->expects($this->once())->method('setHeader')->will($this->returnSelf());
  98. $this->rssFactory->expects($this->once())->method('create')->will($this->returnValue($rssModel));
  99. $this->rssManager->expects($this->once())->method('getProvider')->will($this->returnValue($dataProvider));
  100. $this->expectException(\Magento\Framework\Exception\RuntimeException::class);
  101. $this->controller->execute();
  102. }
  103. }