Gd2Test.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Image\Test\Unit\Adapter;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * \Magento\Framework\Image\Adapter\Gd2 class test
  10. */
  11. class Gd2Test extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Value to mock ini_get('memory_limit')
  15. *
  16. * @var string
  17. */
  18. public static $memoryLimit;
  19. /**
  20. * @var array simulation of getimagesize()
  21. */
  22. public static $imageData = [];
  23. /**
  24. * Adapter for testing
  25. * @var \Magento\Framework\Image\Adapter\Gd2
  26. */
  27. protected $adapter;
  28. /**
  29. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  30. */
  31. protected $objectManager;
  32. /**
  33. * Setup testing object
  34. */
  35. protected function setUp()
  36. {
  37. require_once __DIR__ . '/_files/global_php_mock.php';
  38. $this->objectManager = new ObjectManager($this);
  39. $this->adapter = $this->objectManager->getObject(\Magento\Framework\Image\Adapter\Gd2::class);
  40. }
  41. /**
  42. * Test parent class
  43. */
  44. public function testParentClass()
  45. {
  46. $this->assertInstanceOf(\Magento\Framework\Image\Adapter\AbstractAdapter::class, $this->adapter);
  47. }
  48. /**
  49. * Test open() method
  50. *
  51. * @param array $fileData
  52. * @param string|bool|null $exception
  53. * @param string $limit
  54. * @dataProvider filesProvider
  55. */
  56. public function testOpen($fileData, $exception, $limit)
  57. {
  58. self::$memoryLimit = $limit;
  59. self::$imageData = $fileData;
  60. if (!empty($exception)) {
  61. $this->expectException($exception);
  62. }
  63. $this->adapter->open('file');
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function filesProvider()
  69. {
  70. $smallFile = [
  71. 0 => 480,
  72. 1 => 320,
  73. 2 => 2,
  74. 3 => 'width="480" height="320"',
  75. 'bits' => 8,
  76. 'channels' => 3,
  77. 'mime' => 'image/jpeg',
  78. ];
  79. $bigFile = [
  80. 0 => 3579,
  81. 1 => 2398,
  82. 2 => 2,
  83. 3 => 'width="3579" height="2398"',
  84. 'bits' => 8,
  85. 'channels' => 3,
  86. 'mime' => 'image/jpeg',
  87. ];
  88. return [
  89. 'positive_M' => [$smallFile, false, '2M'],
  90. 'positive_KB' => [$smallFile, false, '2048K'],
  91. 'negative_KB' => [$bigFile, 'OverflowException', '2048K'],
  92. 'negative_bytes' => [$bigFile, 'OverflowException', '2048000'],
  93. 'no_limit' => [$bigFile, false, '-1'],
  94. ];
  95. }
  96. /**
  97. * Test if open() method resets cached fileType
  98. *
  99. */
  100. public function testOpenDifferentTypes()
  101. {
  102. self::$imageData = [
  103. 0 => 480,
  104. 1 => 320,
  105. 2 => 2,
  106. 3 => 'width="480" height="320"',
  107. 'bits' => 8,
  108. 'channels' => 3,
  109. 'mime' => 'image/jpeg',
  110. ];
  111. $this->adapter->open('file');
  112. $type1 = $this->adapter->getImageType();
  113. self::$imageData = [
  114. 0 => 480,
  115. 1 => 320,
  116. 2 => 3,
  117. 3 => 'width="480" height="320"',
  118. 'bits' => 8,
  119. 'channels' => 3,
  120. 'mime' => 'image/png',
  121. ];
  122. $this->adapter->open('file');
  123. $type2 = $this->adapter->getImageType();
  124. $this->assertNotEquals($type1, $type2);
  125. }
  126. }