Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FormTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class FormTest extends \PHPUnit\Framework\TestCase
11 {
13  protected $object;
14 
17 
21  protected $requestMock;
22 
24  protected $context;
25 
29  protected $reviewDataMock;
30 
32  protected $productRepository;
33 
35  protected $storeManager;
36 
38  protected $urlBuilder;
39 
41  private $serializerMock;
42 
43  protected function setUp()
44  {
45  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
46  $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
47  $this->reviewDataMock = $this->getMockBuilder(\Magento\Review\Helper\Data::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50 
51  $this->reviewDataMock->expects($this->once())
52  ->method('getIsGuestAllowToWrite')
53  ->willReturn(true);
54 
55  $this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)->getMockForAbstractClass();
56  $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
57  $this->context->expects(
58  $this->any()
59  )->method(
60  'getStoreManager'
61  )->will(
62  $this->returnValue($this->storeManager)
63  );
64  $this->context->expects($this->any())
65  ->method('getRequest')
66  ->willReturn($this->requestMock);
67  $this->context->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
68  $this->productRepository = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
69 
70  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
71 
72  $this->objectManagerHelper = new ObjectManagerHelper($this);
73  $this->object = $this->objectManagerHelper->getObject(
74  \Magento\Review\Block\Form::class,
75  [
76  'context' => $this->context,
77  'reviewData' => $this->reviewDataMock,
78  'productRepository' => $this->productRepository,
79  'data' => [
80  'jsLayout' => [
81  'some-layout' => 'layout information'
82  ]
83  ],
84  'serializer' => $this->serializerMock
85  ]
86  );
87  }
88 
89  public function testGetProductInfo()
90  {
91  $productId = 3;
92  $storeId = 1;
93 
94  $this->storeManager->expects(
95  $this->any()
96  )->method(
97  'getStore'
98  )->will(
99  $this->returnValue(new \Magento\Framework\DataObject(['id' => $storeId]))
100  );
101 
102  $this->requestMock->expects($this->once())
103  ->method('getParam')
104  ->with('id', false)
105  ->willReturn($productId);
106 
107  $productMock = $this->createMock(\Magento\Catalog\Api\Data\ProductInterface::class);
108  $this->productRepository->expects($this->once())
109  ->method('getById')
110  ->with($productId, false, $storeId)
111  ->willReturn($productMock);
112 
113  $this->assertSame($productMock, $this->object->getProductInfo());
114  }
115 
122  public function testGetAction($isSecure, $actionUrl, $productId)
123  {
124  $this->urlBuilder->expects($this->any())
125  ->method('getUrl')
126  ->with('review/product/post', ['_secure' => $isSecure, 'id' => $productId])
127  ->willReturn($actionUrl . '/id/' . $productId);
128  $this->requestMock->expects($this->any())
129  ->method('getParam')
130  ->with('id', false)
131  ->willReturn($productId);
132  $this->requestMock->expects($this->any())
133  ->method('isSecure')
134  ->willReturn($isSecure);
135 
136  $this->assertEquals($actionUrl . '/id/' . $productId, $this->object->getAction());
137  }
138 
142  public function getActionDataProvider()
143  {
144  return [
145  [false, 'http://localhost/review/product/post', 3],
146  [true, 'https://localhost/review/product/post' ,3],
147  ];
148  }
149 
150  public function testGetJsLayout()
151  {
152  $jsLayout = [
153  'some-layout' => 'layout information'
154  ];
155 
156  $this->serializerMock->expects($this->once())->method('serialize')
157  ->will($this->returnValue(json_encode($jsLayout)));
158  $this->assertEquals('{"some-layout":"layout information"}', $this->object->getJsLayout());
159  }
160 }
testGetAction($isSecure, $actionUrl, $productId)
Definition: FormTest.php:122