|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CatalogWidget\Test\Unit\Block\Product\Widget; |
| 7 | + |
| 8 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 9 | +use Magento\CatalogWidget\Block\Product\Widget\Conditions; |
| 10 | +use Magento\Framework\Registry; |
| 11 | +use Magento\Backend\Block\Template\Context; |
| 12 | +use Magento\CatalogWidget\Model\Rule; |
| 13 | +use Magento\Framework\View\LayoutInterface; |
| 14 | +use Magento\Framework\View\Element\BlockInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test class for \Magento\CatalogWidget\Block\Product\Widget\Conditions |
| 18 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 19 | + */ |
| 20 | +class ConditionsTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ObjectManagerHelper |
| 24 | + */ |
| 25 | + private $objectManagerHelper; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Registry|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $registryMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $contextMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Rule|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $ruleMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var LayoutInterface|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + private $layoutMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var BlockInterface|\PHPUnit_Framework_MockObject_MockObject |
| 49 | + */ |
| 50 | + private $blockMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * return void |
| 54 | + */ |
| 55 | + protected function setUp() |
| 56 | + { |
| 57 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 58 | + $this->ruleMock = $this->getMockBuilder(Rule::class) |
| 59 | + ->disableOriginalConstructor() |
| 60 | + ->getMock(); |
| 61 | + $this->registryMock = $this->getMockBuilder(Registry::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + $this->layoutMock = $this->getMockForAbstractClass(LayoutInterface::class); |
| 65 | + $this->blockMock = $this->getMockBuilder(BlockInterface::class) |
| 66 | + ->setMethods(['getWidgetValues']) |
| 67 | + ->getMockForAbstractClass(); |
| 68 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $this->contextMock->expects($this->once()) |
| 72 | + ->method('getLayout') |
| 73 | + ->willReturn($this->layoutMock); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testConstructWithEmptyData() |
| 80 | + { |
| 81 | + $this->registryMock->expects($this->once()) |
| 82 | + ->method('registry') |
| 83 | + ->with('current_widget_instance') |
| 84 | + ->willReturn(null); |
| 85 | + $this->layoutMock->expects($this->once()) |
| 86 | + ->method('getBlock') |
| 87 | + ->with('wysiwyg_widget.options') |
| 88 | + ->willReturn(null); |
| 89 | + $this->blockMock->expects($this->never()) |
| 90 | + ->method('getWidgetValues'); |
| 91 | + $this->ruleMock->expects($this->never()) |
| 92 | + ->method('loadPost'); |
| 93 | + |
| 94 | + $this->objectManagerHelper->getObject( |
| 95 | + Conditions::class, |
| 96 | + [ |
| 97 | + 'context' => $this->contextMock, |
| 98 | + 'registry' => $this->registryMock, |
| 99 | + 'rule' => $this->ruleMock, |
| 100 | + ] |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + public function testConstructWithWidgetInstance() |
| 108 | + { |
| 109 | + $widgetParams = ['conditions' => 'some conditions']; |
| 110 | + |
| 111 | + /** @var \Magento\Widget\Model\Widget\Instance|\PHPUnit_Framework_MockObject_MockObject $widgetMock */ |
| 112 | + $widgetMock = $this->getMockBuilder(\Magento\Widget\Model\Widget\Instance::class) |
| 113 | + ->disableOriginalConstructor() |
| 114 | + ->getMock(); |
| 115 | + $widgetMock->expects($this->once()) |
| 116 | + ->method('getWidgetParameters') |
| 117 | + ->willReturn($widgetParams); |
| 118 | + |
| 119 | + $this->layoutMock->expects($this->never()) |
| 120 | + ->method('getBlock'); |
| 121 | + $this->blockMock->expects($this->never()) |
| 122 | + ->method('getWidgetValues'); |
| 123 | + $this->registryMock->expects($this->once()) |
| 124 | + ->method('registry') |
| 125 | + ->with('current_widget_instance') |
| 126 | + ->willReturn($widgetMock); |
| 127 | + $this->ruleMock->expects($this->once()) |
| 128 | + ->method('loadPost') |
| 129 | + ->with($widgetParams) |
| 130 | + ->willReturnSelf(); |
| 131 | + |
| 132 | + $this->objectManagerHelper->getObject( |
| 133 | + Conditions::class, |
| 134 | + [ |
| 135 | + 'context' => $this->contextMock, |
| 136 | + 'registry' => $this->registryMock, |
| 137 | + 'rule' => $this->ruleMock, |
| 138 | + ] |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @return void |
| 144 | + */ |
| 145 | + public function testConstructWithParamsFromBlock() |
| 146 | + { |
| 147 | + $widgetParams = ['conditions' => 'some conditions']; |
| 148 | + |
| 149 | + $this->registryMock->expects($this->once()) |
| 150 | + ->method('registry') |
| 151 | + ->with('current_widget_instance') |
| 152 | + ->willReturn(null); |
| 153 | + $this->layoutMock->expects($this->once()) |
| 154 | + ->method('getBlock') |
| 155 | + ->with('wysiwyg_widget.options') |
| 156 | + ->willReturn($this->blockMock); |
| 157 | + $this->blockMock->expects($this->once()) |
| 158 | + ->method('getWidgetValues') |
| 159 | + ->willReturn($widgetParams); |
| 160 | + $this->ruleMock->expects($this->once()) |
| 161 | + ->method('loadPost') |
| 162 | + ->with($widgetParams) |
| 163 | + ->willReturnSelf(); |
| 164 | + |
| 165 | + $this->objectManagerHelper->getObject( |
| 166 | + Conditions::class, |
| 167 | + [ |
| 168 | + 'context' => $this->contextMock, |
| 169 | + 'registry' => $this->registryMock, |
| 170 | + 'rule' => $this->ruleMock, |
| 171 | + ] |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments