|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Reports\Block\Adminhtml\Sales\Invoiced; |
| 9 | + |
| 10 | +use Magento\Backend\Block\Template\Context; |
| 11 | +use Magento\Directory\Model\Currency; |
| 12 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\Framework\DB\Select; |
| 15 | +use Magento\Framework\Event\ManagerInterface; |
| 16 | +use Magento\Framework\Filesystem; |
| 17 | +use Magento\Framework\Math\Random; |
| 18 | +use Magento\Framework\Phrase; |
| 19 | +use Magento\Framework\Test\Unit\Helper\RequestInterfaceTestHelper; |
| 20 | +use Magento\Framework\UrlInterface; |
| 21 | +use Magento\Framework\View\Element\AbstractBlock; |
| 22 | +use Magento\Framework\View\LayoutInterface; |
| 23 | +use Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date; |
| 24 | +use Magento\Reports\Helper\Data; |
| 25 | +use Magento\Reports\Model\Grouped\CollectionFactory; |
| 26 | +use Magento\Reports\Model\ResourceModel\Report\Collection\Factory; |
| 27 | +use Magento\Store\Model\StoreManagerInterface; |
| 28 | +use PHPUnit\Framework\MockObject\Exception; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | +use PHPUnit\Framework\MockObject\MockObject; |
| 31 | + |
| 32 | +class GridTest extends TestCase |
| 33 | +{ |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Context|MockObject |
| 37 | + */ |
| 38 | + private Context $context; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Backend\Helper\Data|MockObject |
| 42 | + */ |
| 43 | + private \Magento\Backend\Helper\Data $backendHelper; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Factory|MockObject |
| 47 | + */ |
| 48 | + private Factory $resourceFactory; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var CollectionFactory|MockObject |
| 52 | + */ |
| 53 | + private CollectionFactory $collectionFactory; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var Data|MockObject |
| 57 | + */ |
| 58 | + private Data $reportsData; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var LayoutInterface|MockObject |
| 62 | + */ |
| 63 | + private LayoutInterface $layout; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var StoreManagerInterface|MockObject |
| 67 | + */ |
| 68 | + private StoreManagerInterface $storeManager; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var UrlInterface|MockObject |
| 72 | + */ |
| 73 | + private UrlInterface $urlBuilder; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var ManagerInterface|MockObject |
| 77 | + */ |
| 78 | + private ManagerInterface $eventManager; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var Random|MockObject |
| 82 | + */ |
| 83 | + private Random $mathRandom; |
| 84 | + |
| 85 | + /** |
| 86 | + * @var RequestInterfaceTestHelper|MockObject |
| 87 | + */ |
| 88 | + private RequestInterfaceTestHelper $request; |
| 89 | + |
| 90 | + /** |
| 91 | + * @var Grid |
| 92 | + */ |
| 93 | + private Grid $invoicedGrid; |
| 94 | + |
| 95 | + protected function setUp(): void |
| 96 | + { |
| 97 | + parent::setUp(); |
| 98 | + |
| 99 | + $this->request = $this->createMock(RequestInterfaceTestHelper::class); |
| 100 | + $this->mathRandom = $this->createMock(Random::class); |
| 101 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 102 | + $this->context = $this->createMock(Context::class); |
| 103 | + $this->eventManager = $this->createMock(ManagerInterface::class); |
| 104 | + $this->context->method('getEventManager')->willReturn($this->eventManager); |
| 105 | + $this->urlBuilder = $this->createMock(UrlInterface::class); |
| 106 | + $this->context->method('getUrlBuilder')->willReturn($this->urlBuilder); |
| 107 | + $filesystem = $this->createMock(Filesystem::class); |
| 108 | + $this->context->method('getFilesystem')->willReturn($filesystem); |
| 109 | + $this->layout = $this->createMock(LayoutInterface::class); |
| 110 | + $this->context->method('getLayout')->willReturn($this->layout); |
| 111 | + $this->context->method('getStoreManager')->willReturn($this->storeManager); |
| 112 | + $this->context->method('getMathRandom')->willReturn($this->mathRandom); |
| 113 | + $this->context->method('getRequest')->willReturn($this->request); |
| 114 | + |
| 115 | + $this->backendHelper = $this->createMock(\Magento\Backend\Helper\Data::class); |
| 116 | + $this->resourceFactory = $this->createMock(Factory::class); |
| 117 | + $this->collectionFactory = $this->createMock(CollectionFactory::class); |
| 118 | + $this->reportsData = $this->createMock(Data::class); |
| 119 | + |
| 120 | + $filterData = new DataObject(); |
| 121 | + $this->invoicedGrid = new Grid( |
| 122 | + $this->context, |
| 123 | + $this->backendHelper, |
| 124 | + $this->resourceFactory, |
| 125 | + $this->collectionFactory, |
| 126 | + $this->reportsData, |
| 127 | + [ |
| 128 | + 'filter_data' => $filterData, |
| 129 | + 'id' => 'test' |
| 130 | + ] |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @return void |
| 136 | + * @throws Exception |
| 137 | + */ |
| 138 | + public function testColumnsRenderer(): void |
| 139 | + { |
| 140 | + $currencyCode = 'USD'; |
| 141 | + $rate = 0.5; |
| 142 | + $this->layout->method('getChildName')->willReturn('columns'); |
| 143 | + $block = $this->getMockBuilder(AbstractBlock::class) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->addMethods(['getColumns', 'isAvailable']) |
| 146 | + ->onlyMethods(['getChildBlock', 'getChildNames', 'setChild']) |
| 147 | + ->getMock(); |
| 148 | + $block->method('getColumns')->willReturn([]); |
| 149 | + $this->layout->method('getBlock')->willReturn($block); |
| 150 | + $extendedBlock = $this->getMockBuilder(DataObject::class) |
| 151 | + ->disableOriginalConstructor() |
| 152 | + ->addMethods(['setId', 'setGrid', 'setDataAttribute']) |
| 153 | + ->onlyMethods(['setData']) |
| 154 | + ->getMock(); |
| 155 | + |
| 156 | + $expectedData = [ |
| 157 | + [ |
| 158 | + 'header' => __('Interval'), |
| 159 | + 'index' => 'period', |
| 160 | + 'sortable' => false, |
| 161 | + 'period_type' => null, |
| 162 | + 'renderer' => Date::class, |
| 163 | + 'totals_label' => __('Total'), |
| 164 | + 'html_decorators' => ['nobr'], |
| 165 | + 'header_css_class' => 'col-period', |
| 166 | + 'column_css_class' => 'col-period' |
| 167 | + ], |
| 168 | + [ |
| 169 | + 'header' => __('Orders'), |
| 170 | + 'index' => 'orders_count', |
| 171 | + 'type' => 'number', |
| 172 | + 'total' => 'sum', |
| 173 | + 'sortable' => false, |
| 174 | + 'header_css_class' => 'col-qty', |
| 175 | + 'column_css_class' => 'col-qty' |
| 176 | + ], |
| 177 | + [ |
| 178 | + 'header' => __('Invoiced Orders'), |
| 179 | + 'index' => 'orders_invoiced', |
| 180 | + 'type' => 'number', |
| 181 | + 'total' => 'sum', |
| 182 | + 'sortable' => false, |
| 183 | + 'header_css_class' => 'col-invoiced', |
| 184 | + 'column_css_class' => 'col-invoiced' |
| 185 | + ], |
| 186 | + [ |
| 187 | + 'header' => __('Total Invoiced'), |
| 188 | + 'type' => 'currency', |
| 189 | + 'currency_code' => $currencyCode, |
| 190 | + 'index' => 'invoiced', |
| 191 | + 'total' => 'sum', |
| 192 | + 'sortable' => false, |
| 193 | + 'rate' => $rate, |
| 194 | + 'header_css_class' => 'col-total-invoiced', |
| 195 | + 'column_css_class' => 'col-total-invoiced', |
| 196 | + 'renderer' => \Magento\Reports\Block\Adminhtml\Grid\Column\Renderer\Currency::class |
| 197 | + ], |
| 198 | + [ |
| 199 | + 'header' => __('Paid Invoices'), |
| 200 | + 'type' => 'currency', |
| 201 | + 'currency_code' => $currencyCode, |
| 202 | + 'index' => 'invoiced_captured', |
| 203 | + 'total' => 'sum', |
| 204 | + 'sortable' => false, |
| 205 | + 'rate' => $rate, |
| 206 | + 'header_css_class' => 'col-total-invoiced-paid', |
| 207 | + 'column_css_class' => 'col-total-invoiced-paid', |
| 208 | + 'renderer' => \Magento\Reports\Block\Adminhtml\Grid\Column\Renderer\Currency::class |
| 209 | + ], |
| 210 | + [ |
| 211 | + 'header' => __('Unpaid Invoices'), |
| 212 | + 'type' => 'currency', |
| 213 | + 'currency_code' => $currencyCode, |
| 214 | + 'index' => 'invoiced_not_captured', |
| 215 | + 'total' => 'sum', |
| 216 | + 'sortable' => false, |
| 217 | + 'rate' => $rate, |
| 218 | + 'header_css_class' => 'col-total-invoiced-not-paid', |
| 219 | + 'column_css_class' => 'col-total-invoiced-not-paid', |
| 220 | + 'renderer' => \Magento\Reports\Block\Adminhtml\Grid\Column\Renderer\Currency::class |
| 221 | + ], |
| 222 | + [ |
| 223 | + 'label' => __('Reset Filter'), |
| 224 | + 'onclick' => 'testJsObject.resetFilter()', |
| 225 | + 'class' => 'action-reset action-tertiary' |
| 226 | + ] |
| 227 | + ]; |
| 228 | + $callIndex = 0; |
| 229 | + $extendedBlock |
| 230 | + ->method('setData') |
| 231 | + ->willReturnCallback(function (array $data) use (&$callIndex, $expectedData, $extendedBlock) { |
| 232 | + if (!isset($expectedData[$callIndex])) { |
| 233 | + return $extendedBlock; |
| 234 | + } |
| 235 | + $expected = $this->normalizeDataArray($expectedData[$callIndex]); |
| 236 | + $actual = $this->normalizeDataArray($data); |
| 237 | + |
| 238 | + self::assertSame( |
| 239 | + $expected, |
| 240 | + $actual, |
| 241 | + sprintf('Unexpected data passed to setData() at call #%d', $callIndex + 1) |
| 242 | + ); |
| 243 | + |
| 244 | + $callIndex++; |
| 245 | + |
| 246 | + return $extendedBlock; |
| 247 | + }); |
| 248 | + |
| 249 | + $extendedBlock->method('setId')->willReturnSelf(); |
| 250 | + $extendedBlock->method('setGrid')->willReturnSelf(); |
| 251 | + $this->layout->method('createBlock')->willReturn($extendedBlock); |
| 252 | + $block->method('getChildBlock')->willReturn($extendedBlock); |
| 253 | + $block->method('getChildNames')->willReturn([]); |
| 254 | + |
| 255 | + $this->storeManager->method('getStores')->willReturn([]); |
| 256 | + $store = $this->createMock(\Magento\Store\Model\Store::class); |
| 257 | + $store->method('getBaseCurrencyCode')->willReturn($currencyCode); |
| 258 | + $currency = $this->createMock(Currency::class); |
| 259 | + $currency->method('getRate')->willReturn($rate); |
| 260 | + $store->method('getBaseCurrency')->willReturn($currency); |
| 261 | + $this->storeManager->method('getStore')->willReturn($store); |
| 262 | + |
| 263 | + $collection = $this->createMock(AbstractDb::class); |
| 264 | + $select = $this->createMock(Select::class); |
| 265 | + $collection->method('getSelect')->willReturn($select); |
| 266 | + $collection |
| 267 | + ->method('getIterator') |
| 268 | + ->willReturn(new \ArrayIterator([new DataObject([])])); |
| 269 | + |
| 270 | + $this->invoicedGrid->setTotals(new DataObject()); |
| 271 | + $this->invoicedGrid->setCollection($collection); |
| 272 | + $this->invoicedGrid->getXml(); |
| 273 | + } |
| 274 | + |
| 275 | + /** |
| 276 | + * Prepare data for assertion |
| 277 | + * |
| 278 | + * @param array $data |
| 279 | + * @return array |
| 280 | + */ |
| 281 | + private function normalizeDataArray(array $data): array |
| 282 | + { |
| 283 | + array_walk_recursive($data, function (&$value) { |
| 284 | + if ($value instanceof Phrase) { |
| 285 | + $value = (string)$value; |
| 286 | + } |
| 287 | + }); |
| 288 | + |
| 289 | + return $data; |
| 290 | + } |
| 291 | +} |
0 commit comments