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