|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Sales\Test\Unit\Model\Order\Pdf; |
| 7 | + |
| 8 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 9 | +use Magento\MediaStorage\Helper\File\Storage\Database; |
| 10 | +use Magento\Sales\Model\Order; |
| 11 | +use Magento\Sales\Model\Order\Address; |
| 12 | +use Magento\Sales\Model\Order\Address\Renderer; |
| 13 | +use Magento\Sales\Model\Order\Creditmemo; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class CreditmemoTest |
| 19 | + * |
| 20 | + * Tests Sales Order Creditmemo PDF model |
| 21 | + * |
| 22 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 23 | + */ |
| 24 | +class CreditmemoTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var \Magento\Sales\Model\Order\Pdf\Invoice |
| 28 | + */ |
| 29 | + protected $_model; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Sales\Model\Order\Pdf\Config|MockObject |
| 33 | + */ |
| 34 | + protected $_pdfConfigMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Database|MockObject |
| 38 | + */ |
| 39 | + protected $databaseMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ScopeConfigInterface|MockObject |
| 43 | + */ |
| 44 | + protected $scopeConfigMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Framework\Filesystem\Directory\Write|MockObject |
| 48 | + */ |
| 49 | + protected $directoryMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Renderer|MockObject |
| 53 | + */ |
| 54 | + protected $addressRendererMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Magento\Payment\Helper\Data|MockObject |
| 58 | + */ |
| 59 | + protected $paymentDataMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var \Magento\Store\Model\App\Emulation |
| 63 | + */ |
| 64 | + private $appEmulation; |
| 65 | + |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + $this->_pdfConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Pdf\Config::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $this->directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class); |
| 72 | + $this->directoryMock->expects($this->any())->method('getAbsolutePath')->will( |
| 73 | + $this->returnCallback( |
| 74 | + function ($argument) { |
| 75 | + return BP . '/' . $argument; |
| 76 | + } |
| 77 | + ) |
| 78 | + ); |
| 79 | + $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class); |
| 80 | + $filesystemMock->expects($this->any()) |
| 81 | + ->method('getDirectoryRead') |
| 82 | + ->will($this->returnValue($this->directoryMock)); |
| 83 | + $filesystemMock->expects($this->any()) |
| 84 | + ->method('getDirectoryWrite') |
| 85 | + ->will($this->returnValue($this->directoryMock)); |
| 86 | + |
| 87 | + $this->databaseMock = $this->createMock(Database::class); |
| 88 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 89 | + $this->addressRendererMock = $this->createMock(Renderer::class); |
| 90 | + $this->paymentDataMock = $this->createMock(\Magento\Payment\Helper\Data::class); |
| 91 | + $this->appEmulation = $this->createMock(\Magento\Store\Model\App\Emulation::class); |
| 92 | + |
| 93 | + $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 94 | + $this->_model = $helper->getObject( |
| 95 | + \Magento\Sales\Model\Order\Pdf\Creditmemo::class, |
| 96 | + [ |
| 97 | + 'filesystem' => $filesystemMock, |
| 98 | + 'pdfConfig' => $this->_pdfConfigMock, |
| 99 | + 'fileStorageDatabase' => $this->databaseMock, |
| 100 | + 'scopeConfig' => $this->scopeConfigMock, |
| 101 | + 'addressRenderer' => $this->addressRendererMock, |
| 102 | + 'string' => new \Magento\Framework\Stdlib\StringUtils(), |
| 103 | + 'paymentData' => $this->paymentDataMock, |
| 104 | + 'appEmulation' => $this->appEmulation |
| 105 | + ] |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + public function testInsertLogoDatabaseMediaStorage() |
| 110 | + { |
| 111 | + $filename = 'image.jpg'; |
| 112 | + $path = '/sales/store/logo/'; |
| 113 | + $storeId = 1; |
| 114 | + |
| 115 | + $this->appEmulation->expects($this->once()) |
| 116 | + ->method('startEnvironmentEmulation') |
| 117 | + ->with( |
| 118 | + $storeId, |
| 119 | + \Magento\Framework\App\Area::AREA_FRONTEND, |
| 120 | + true |
| 121 | + ) |
| 122 | + ->willReturnSelf(); |
| 123 | + $this->appEmulation->expects($this->once()) |
| 124 | + ->method('stopEnvironmentEmulation') |
| 125 | + ->willReturnSelf(); |
| 126 | + $this->_pdfConfigMock->expects($this->once()) |
| 127 | + ->method('getRenderersPerProduct') |
| 128 | + ->with('creditmemo') |
| 129 | + ->will($this->returnValue(['product_type_one' => 'Renderer_Type_One_Product_One'])); |
| 130 | + $this->_pdfConfigMock->expects($this->any()) |
| 131 | + ->method('getTotals') |
| 132 | + ->will($this->returnValue([])); |
| 133 | + |
| 134 | + $block = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class) |
| 135 | + ->disableOriginalConstructor() |
| 136 | + ->setMethods(['setIsSecureMode','toPdf']) |
| 137 | + ->getMock(); |
| 138 | + $block->expects($this->any()) |
| 139 | + ->method('setIsSecureMode') |
| 140 | + ->willReturn($block); |
| 141 | + $block->expects($this->any()) |
| 142 | + ->method('toPdf') |
| 143 | + ->will($this->returnValue('')); |
| 144 | + $this->paymentDataMock->expects($this->any()) |
| 145 | + ->method('getInfoBlock') |
| 146 | + ->willReturn($block); |
| 147 | + |
| 148 | + $this->addressRendererMock->expects($this->any()) |
| 149 | + ->method('format') |
| 150 | + ->will($this->returnValue('')); |
| 151 | + |
| 152 | + $this->databaseMock->expects($this->any()) |
| 153 | + ->method('checkDbUsage') |
| 154 | + ->will($this->returnValue(true)); |
| 155 | + |
| 156 | + $creditmemoMock = $this->createMock(Creditmemo::class); |
| 157 | + $orderMock = $this->createMock(Order::class); |
| 158 | + $addressMock = $this->createMock(Address::class); |
| 159 | + $orderMock->expects($this->any()) |
| 160 | + ->method('getBillingAddress') |
| 161 | + ->willReturn($addressMock); |
| 162 | + $orderMock->expects($this->any()) |
| 163 | + ->method('getIsVirtual') |
| 164 | + ->will($this->returnValue(true)); |
| 165 | + $infoMock = $this->createMock(\Magento\Payment\Model\InfoInterface::class); |
| 166 | + $orderMock->expects($this->any()) |
| 167 | + ->method('getPayment') |
| 168 | + ->willReturn($infoMock); |
| 169 | + $creditmemoMock->expects($this->any()) |
| 170 | + ->method('getStoreId') |
| 171 | + ->willReturn($storeId); |
| 172 | + $creditmemoMock->expects($this->any()) |
| 173 | + ->method('getOrder') |
| 174 | + ->willReturn($orderMock); |
| 175 | + $creditmemoMock->expects($this->any()) |
| 176 | + ->method('getAllItems') |
| 177 | + ->willReturn([]); |
| 178 | + |
| 179 | + $this->scopeConfigMock->expects($this->at(0)) |
| 180 | + ->method('getValue') |
| 181 | + ->with('sales/identity/logo', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null) |
| 182 | + ->will($this->returnValue($filename)); |
| 183 | + $this->scopeConfigMock->expects($this->at(1)) |
| 184 | + ->method('getValue') |
| 185 | + ->with('sales/identity/address', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null) |
| 186 | + ->will($this->returnValue('')); |
| 187 | + |
| 188 | + $this->directoryMock->expects($this->any()) |
| 189 | + ->method('isFile') |
| 190 | + ->with($path . $filename) |
| 191 | + ->willReturnOnConsecutiveCalls( |
| 192 | + $this->returnValue(false), |
| 193 | + $this->returnValue(false) |
| 194 | + ); |
| 195 | + |
| 196 | + $this->databaseMock->expects($this->once()) |
| 197 | + ->method('saveFileToFilesystem') |
| 198 | + ->with($path . $filename); |
| 199 | + |
| 200 | + $this->_model->getPdf([$creditmemoMock]); |
| 201 | + } |
| 202 | +} |
0 commit comments