FormTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Test\Unit\Block;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class FormTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Review\Block\Form */
  11. protected $object;
  12. /** @var ObjectManagerHelper */
  13. protected $objectManagerHelper;
  14. /**
  15. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $requestMock;
  18. /** @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $context;
  20. /**
  21. * @var \Magento\Review\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $reviewDataMock;
  24. /** @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $productRepository;
  26. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $storeManager;
  28. /** @var \Magento\Framework\UrlInterface|PHPUnit_Framework_MockObject_MockObject */
  29. protected $urlBuilder;
  30. /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
  31. private $serializerMock;
  32. protected function setUp()
  33. {
  34. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  35. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  36. $this->reviewDataMock = $this->getMockBuilder(\Magento\Review\Helper\Data::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->reviewDataMock->expects($this->once())
  40. ->method('getIsGuestAllowToWrite')
  41. ->willReturn(true);
  42. $this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)->getMockForAbstractClass();
  43. $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
  44. $this->context->expects(
  45. $this->any()
  46. )->method(
  47. 'getStoreManager'
  48. )->will(
  49. $this->returnValue($this->storeManager)
  50. );
  51. $this->context->expects($this->any())
  52. ->method('getRequest')
  53. ->willReturn($this->requestMock);
  54. $this->context->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
  55. $this->productRepository = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  56. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
  57. $this->objectManagerHelper = new ObjectManagerHelper($this);
  58. $this->object = $this->objectManagerHelper->getObject(
  59. \Magento\Review\Block\Form::class,
  60. [
  61. 'context' => $this->context,
  62. 'reviewData' => $this->reviewDataMock,
  63. 'productRepository' => $this->productRepository,
  64. 'data' => [
  65. 'jsLayout' => [
  66. 'some-layout' => 'layout information'
  67. ]
  68. ],
  69. 'serializer' => $this->serializerMock
  70. ]
  71. );
  72. }
  73. public function testGetProductInfo()
  74. {
  75. $productId = 3;
  76. $storeId = 1;
  77. $this->storeManager->expects(
  78. $this->any()
  79. )->method(
  80. 'getStore'
  81. )->will(
  82. $this->returnValue(new \Magento\Framework\DataObject(['id' => $storeId]))
  83. );
  84. $this->requestMock->expects($this->once())
  85. ->method('getParam')
  86. ->with('id', false)
  87. ->willReturn($productId);
  88. $productMock = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class);
  89. $this->productRepository->expects($this->once())
  90. ->method('getById')
  91. ->with($productId, false, $storeId)
  92. ->willReturn($productMock);
  93. $this->assertSame($productMock, $this->object->getProductInfo());
  94. }
  95. /**
  96. * @param bool $isSecure
  97. * @param string $actionUrl
  98. * @param int $productId
  99. * @dataProvider getActionDataProvider
  100. */
  101. public function testGetAction($isSecure, $actionUrl, $productId)
  102. {
  103. $this->urlBuilder->expects($this->any())
  104. ->method('getUrl')
  105. ->with('review/product/post', ['_secure' => $isSecure, 'id' => $productId])
  106. ->willReturn($actionUrl . '/id/' . $productId);
  107. $this->requestMock->expects($this->any())
  108. ->method('getParam')
  109. ->with('id', false)
  110. ->willReturn($productId);
  111. $this->requestMock->expects($this->any())
  112. ->method('isSecure')
  113. ->willReturn($isSecure);
  114. $this->assertEquals($actionUrl . '/id/' . $productId, $this->object->getAction());
  115. }
  116. /**
  117. * @return array
  118. */
  119. public function getActionDataProvider()
  120. {
  121. return [
  122. [false, 'http://localhost/review/product/post', 3],
  123. [true, 'https://localhost/review/product/post' ,3],
  124. ];
  125. }
  126. public function testGetJsLayout()
  127. {
  128. $jsLayout = [
  129. 'some-layout' => 'layout information'
  130. ];
  131. $this->serializerMock->expects($this->once())->method('serialize')
  132. ->will($this->returnValue(json_encode($jsLayout)));
  133. $this->assertEquals('{"some-layout":"layout information"}', $this->object->getJsLayout());
  134. }
  135. }