AbstractTest.php 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class AbstractTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Test protected method to reduce testing complexity, which would be too high in case of testing a public method
  14. * without completing a huge refactoring of the class.
  15. */
  16. public function testInsertTotals()
  17. {
  18. // Setup parameters, that will be passed to the tested model method
  19. $page = $this->createMock(\Zend_Pdf_Page::class);
  20. $order = new \stdClass();
  21. $source = $this->createMock(\Magento\Sales\Model\Order\Invoice::class);
  22. $source->expects($this->any())->method('getOrder')->will($this->returnValue($order));
  23. // Setup most constructor dependencies
  24. $paymentData = $this->createMock(\Magento\Payment\Helper\Data::class);
  25. $addressRenderer = $this->createMock(\Magento\Sales\Model\Order\Address\Renderer::class);
  26. $string = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
  27. $scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  28. $translate = $this->createMock(\Magento\Framework\Translate\Inline\StateInterface::class);
  29. $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  30. $pdfItemsFactory = $this->createMock(\Magento\Sales\Model\Order\Pdf\ItemsFactory::class);
  31. $localeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  32. // Setup config file totals
  33. $configTotals = ['item1' => [''], 'item2' => ['model' => 'custom_class']];
  34. $pdfConfig = $this->createMock(\Magento\Sales\Model\Order\Pdf\Config::class);
  35. $pdfConfig->expects($this->once())->method('getTotals')->will($this->returnValue($configTotals));
  36. // Setup total factory
  37. $total1 = $this->createPartialMock(
  38. \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal::class,
  39. ['setSource', 'setOrder', 'canDisplay', 'getTotalsForDisplay']
  40. );
  41. $total1->expects($this->once())->method('setOrder')->with($order)->will($this->returnSelf());
  42. $total1->expects($this->once())->method('setSource')->with($source)->will($this->returnSelf());
  43. $total1->expects($this->once())->method('canDisplay')->will($this->returnValue(true));
  44. $total1->expects($this->once())
  45. ->method('getTotalsForDisplay')
  46. ->will($this->returnValue([['label' => 'label1', 'font_size' => 1, 'amount' => '$1']]));
  47. $total2 = $this->createPartialMock(
  48. \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal::class,
  49. ['setSource', 'setOrder', 'canDisplay', 'getTotalsForDisplay']
  50. );
  51. $total2->expects($this->once())->method('setOrder')->with($order)->will($this->returnSelf());
  52. $total2->expects($this->once())->method('setSource')->with($source)->will($this->returnSelf());
  53. $total2->expects($this->once())->method('canDisplay')->will($this->returnValue(true));
  54. $total2->expects($this->once())
  55. ->method('getTotalsForDisplay')
  56. ->will($this->returnValue([['label' => 'label2', 'font_size' => 2, 'amount' => '$2']]));
  57. $valueMap = [[null, [], $total1], ['custom_class', [], $total2]];
  58. $pdfTotalFactory = $this->createMock(\Magento\Sales\Model\Order\Pdf\Total\Factory::class);
  59. $pdfTotalFactory->expects($this->exactly(2))->method('create')->will($this->returnValueMap($valueMap));
  60. // Test model
  61. /** @var \Magento\Sales\Model\Order\Pdf\AbstractPdf $model */
  62. $model = $this->getMockForAbstractClass(
  63. \Magento\Sales\Model\Order\Pdf\AbstractPdf::class,
  64. [
  65. $paymentData,
  66. $string,
  67. $scopeConfig,
  68. $filesystem,
  69. $pdfConfig,
  70. $pdfTotalFactory,
  71. $pdfItemsFactory,
  72. $localeMock,
  73. $translate,
  74. $addressRenderer
  75. ],
  76. '',
  77. true,
  78. false,
  79. true,
  80. ['drawLineBlocks']
  81. );
  82. $model->expects($this->once())->method('drawLineBlocks')->will($this->returnValue($page));
  83. $reflectionMethod = new \ReflectionMethod(\Magento\Sales\Model\Order\Pdf\AbstractPdf::class, 'insertTotals');
  84. $reflectionMethod->setAccessible(true);
  85. $actual = $reflectionMethod->invoke($model, $page, $source);
  86. $this->assertSame($page, $actual);
  87. }
  88. }