|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Bundle\Test\Unit\Model\Sales\Order\Pdf\Items; |
| 9 | + |
| 10 | +use Magento\Bundle\Model\Sales\Order\Pdf\Items\Creditmemo; |
| 11 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 12 | +use Magento\Framework\DataObject; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filter\FilterManager; |
| 15 | +use Magento\Framework\Model\Context; |
| 16 | +use Magento\Framework\Model\ResourceModel\AbstractResource; |
| 17 | +use Magento\Framework\Registry; |
| 18 | +use Magento\Framework\Serialize\Serializer\Json; |
| 19 | +use Magento\Framework\Stdlib\StringUtils; |
| 20 | +use Magento\Sales\Model\Order; |
| 21 | +use Magento\Sales\Model\Order\Pdf\Invoice as InvoicePdf; |
| 22 | +use Magento\Tax\Helper\Data; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Zend_Pdf_Page; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test for the vertical alignment of the printed elements of the creditmemo PDF |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class CreditmemoTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var Creditmemo|MockObject |
| 35 | + */ |
| 36 | + private $model; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Data|MockObject |
| 40 | + */ |
| 41 | + private $taxDataMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritDoc |
| 45 | + */ |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + $contextMock = $this->createMock(Context::class); |
| 49 | + $registryMock = $this->createMock(Registry::class); |
| 50 | + $this->taxDataMock = $this->createMock(Data::class); |
| 51 | + $directoryMock = $this->createMock(Filesystem\Directory\Read::class); |
| 52 | + $directoryMock->expects($this->any())->method('getAbsolutePath')->willReturn(''); |
| 53 | + $filesystemMock = $this->createMock(Filesystem::class); |
| 54 | + $filesystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($directoryMock); |
| 55 | + $filterManagerMock = $this->createMock(FilterManager::class); |
| 56 | + $stringUtils = new StringUtils(); |
| 57 | + $resourceMock = $this->createMock(AbstractResource::class); |
| 58 | + $collectionMock = $this->createMock(AbstractDb::class); |
| 59 | + $serializerMock = $this->createMock(Json::class); |
| 60 | + |
| 61 | + $this->model = $this->getMockBuilder(Creditmemo::class) |
| 62 | + ->setConstructorArgs( |
| 63 | + [ |
| 64 | + $contextMock, |
| 65 | + $registryMock, |
| 66 | + $this->taxDataMock, |
| 67 | + $filesystemMock, |
| 68 | + $filterManagerMock, |
| 69 | + $serializerMock, |
| 70 | + $stringUtils, |
| 71 | + $resourceMock, |
| 72 | + $collectionMock, |
| 73 | + [] |
| 74 | + ] |
| 75 | + ) |
| 76 | + ->onlyMethods( |
| 77 | + [ |
| 78 | + '_setFontRegular', |
| 79 | + 'getChildren', |
| 80 | + 'isShipmentSeparately', |
| 81 | + 'isChildCalculated', |
| 82 | + 'getValueHtml', |
| 83 | + 'getSelectionAttributes' |
| 84 | + ] |
| 85 | + ) |
| 86 | + ->getMock(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param array $expected |
| 91 | + * @param string $method |
| 92 | + * |
| 93 | + * @return void |
| 94 | + * @dataProvider \Magento\Bundle\Test\Unit\Model\Sales\Order\Pdf\Items\CreditmemoTestProvider::getData |
| 95 | + */ |
| 96 | + public function testDrawPrice(array $expected): void |
| 97 | + { |
| 98 | + $pageMock = $this->createMock(Zend_Pdf_Page::class); |
| 99 | + $this->model->setPage($pageMock); |
| 100 | + $pdfMock = $this->createMock(InvoicePdf::class); |
| 101 | + $pdfMock->expects($this->any())->method('drawLineBlocks')->with( |
| 102 | + $pageMock, |
| 103 | + $expected, |
| 104 | + ['table_header' => true] |
| 105 | + )->willReturn($pageMock); |
| 106 | + $this->model->setPdf($pdfMock); |
| 107 | + |
| 108 | + $this->prepareModel(); |
| 109 | + $this->model->draw(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Prepare invoice draw model for test execution |
| 114 | + * |
| 115 | + * @return void |
| 116 | + */ |
| 117 | + private function prepareModel(): void |
| 118 | + { |
| 119 | + $parentItem = new DataObject( |
| 120 | + [ |
| 121 | + 'sku' => 'bundle-simple', |
| 122 | + 'name' => 'Bundle', |
| 123 | + 'order_item' => new DataObject( |
| 124 | + [ |
| 125 | + 'sku' => 'bundle-simple', |
| 126 | + 'name' => 'Bundle', |
| 127 | + 'product_options' => [] |
| 128 | + ] |
| 129 | + ) |
| 130 | + ] |
| 131 | + ); |
| 132 | + |
| 133 | + $items = [ |
| 134 | + new DataObject( |
| 135 | + [ |
| 136 | + 'name' => 'Simple1', |
| 137 | + 'sku' => 'simple1', |
| 138 | + 'price' => '10.00', |
| 139 | + 'price_incl_tax' => '10.83', |
| 140 | + 'row_total' => '20.00', |
| 141 | + 'row_total_incl_tax' => '21.66', |
| 142 | + 'qty' => '2', |
| 143 | + 'tax_amount' => '1.66', |
| 144 | + 'order_item' => new DataObject( |
| 145 | + [ |
| 146 | + 'parent_item' => $parentItem |
| 147 | + ] |
| 148 | + ) |
| 149 | + ] |
| 150 | + ), |
| 151 | + new DataObject( |
| 152 | + [ |
| 153 | + 'name' => 'Simple2', |
| 154 | + 'sku' => 'simple2', |
| 155 | + 'price' => '5.00', |
| 156 | + 'price_incl_tax' => '5.41', |
| 157 | + 'row_total' => '10.00', |
| 158 | + 'row_total_incl_tax' => '10.83', |
| 159 | + 'qty' => '2', |
| 160 | + 'tax_amount' => '0.83', |
| 161 | + 'order_item' => new DataObject( |
| 162 | + [ |
| 163 | + 'parent_item' => $parentItem |
| 164 | + ] |
| 165 | + ) |
| 166 | + ] |
| 167 | + ) |
| 168 | + ]; |
| 169 | + |
| 170 | + $parentItem['order_item']['children_items'] = $items; |
| 171 | + |
| 172 | + $orderMock = $this->createMock(Order::class); |
| 173 | + |
| 174 | + $this->model->expects($this->any())->method('getChildren')->willReturn($items); |
| 175 | + $this->model->expects($this->any())->method('isShipmentSeparately')->willReturn(false); |
| 176 | + $this->model->expects($this->any())->method('isChildCalculated')->willReturn(true); |
| 177 | + $this->model |
| 178 | + ->method('getSelectionAttributes') |
| 179 | + ->willReturnOnConsecutiveCalls( |
| 180 | + ['option_id' => 1, 'option_label' => 'test option'], |
| 181 | + ['option_id' => 1, 'option_label' => 'second option'] |
| 182 | + ); |
| 183 | + $this->model |
| 184 | + ->method('getValueHtml') |
| 185 | + ->willReturnOnConsecutiveCalls( |
| 186 | + $items[0]->getName(), |
| 187 | + $items[1]->getName() |
| 188 | + ); |
| 189 | + |
| 190 | + $orderMock->expects($this->any())->method('formatPriceTxt')->willReturnArgument(0); |
| 191 | + $this->model->setOrder($orderMock); |
| 192 | + $this->model->setItem($parentItem); |
| 193 | + } |
| 194 | +} |
0 commit comments