Skip to content

Commit e68d4ea

Browse files
author
Allan Paiste
committed
test-coverage increased
1 parent 0887c01 commit e68d4ea

File tree

1 file changed

+224
-0
lines changed
  • app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend

1 file changed

+224
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Model\Category\Attribute\Backend;
7+
8+
use \Magento\Catalog\Model\Category\Attribute\Backend\Image as Model;
9+
10+
class ImageTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
14+
*/
15+
protected $attribute;
16+
17+
/**
18+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
19+
*/
20+
protected $objectManager;
21+
22+
/**
23+
* @var \Magento\Catalog\Model\ImageUploader
24+
*/
25+
protected $imageUploader;
26+
27+
protected function setUp()
28+
{
29+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
30+
31+
$this->attribute = $this->getMockForAbstractClass(
32+
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
33+
[],
34+
'TestAttribute',
35+
false,
36+
false,
37+
true,
38+
['getName']
39+
);
40+
41+
$this->attribute->expects($this->once())
42+
->method('getName')
43+
->will($this->returnValue('test_attribute'));
44+
45+
$this->imageUploader = $this->getMock(
46+
\Magento\Catalog\Model\ImageUploader::class,
47+
['moveFileFromTmp'],
48+
[],
49+
'',
50+
false
51+
);
52+
}
53+
54+
public function deletionValuesProvider()
55+
{
56+
return [
57+
[false],
58+
[['delete' => true]]
59+
];
60+
}
61+
62+
/**
63+
* @dataProvider deletionValuesProvider
64+
*
65+
* @param $value
66+
*/
67+
public function testBeforeSaveShouldSetAttributeValueToBlankWhenImageValueRequiresDeletion($value)
68+
{
69+
$model = $this->objectManager->getObject(Model::class);
70+
$model->setAttribute($this->attribute);
71+
72+
$object = new \Magento\Framework\DataObject([
73+
'test_attribute' => $value
74+
]);
75+
76+
$model->beforeSave($object);
77+
78+
$this->assertEquals('', $object->getTestAttribute());
79+
}
80+
81+
public function testBeforeSaveShouldSetAttributeValueToUploadedImageName()
82+
{
83+
$model = $this->objectManager->getObject(Model::class);
84+
$model->setAttribute($this->attribute);
85+
86+
$object = new \Magento\Framework\DataObject([
87+
'test_attribute' => [
88+
['name' => 'test123.jpg']
89+
]
90+
]);
91+
92+
$model->beforeSave($object);
93+
94+
$this->assertEquals('test123.jpg', $object->getTestAttribute());
95+
}
96+
97+
public function testBeforeSaveShouldSetAttributeUploadInformationToTemporaryAttribute()
98+
{
99+
$model = $this->objectManager->getObject(Model::class);
100+
$model->setAttribute($this->attribute);
101+
102+
$object = new \Magento\Framework\DataObject([
103+
'test_attribute' => [
104+
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.test.com/test123.jpg']
105+
]
106+
]);
107+
108+
$model->beforeSave($object);
109+
110+
$this->assertEquals([
111+
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.test.com/test123.jpg']
112+
], $object->getTestAttributeAdditionalData());
113+
}
114+
115+
public function stringValueProvider()
116+
{
117+
return [
118+
['test123'],
119+
[12345],
120+
[true],
121+
['some' => 'value']
122+
];
123+
}
124+
125+
/**
126+
* @dataProvider stringValueProvider
127+
*
128+
* @param $value
129+
*/
130+
public function testBeforeSaveShouldNotModifyAttributeValueWhenNotUploadData($value)
131+
{
132+
$model = $this->objectManager->getObject(Model::class);
133+
$model->setAttribute($this->attribute);
134+
135+
$object = new \Magento\Framework\DataObject([
136+
'test_attribute' => $value
137+
]);
138+
139+
$model->beforeSave($object);
140+
141+
$this->assertEquals($value, $object->getTestAttribute());
142+
}
143+
144+
/**
145+
* @dataProvider stringValueProvider
146+
*
147+
* @param $value
148+
*/
149+
public function testBeforeSaveShouldNotSetAdditionalDataWhenNotUploadData($value)
150+
{
151+
$model = $this->objectManager->getObject(Model::class);
152+
$model->setAttribute($this->attribute);
153+
154+
$object = new \Magento\Framework\DataObject([
155+
'test_attribute' => $value
156+
]);
157+
158+
$model->beforeSave($object);
159+
160+
$this->assertNull($object->getTestAttributeAdditionalData());
161+
}
162+
163+
protected function setUpModelForAfterSave()
164+
{
165+
$objectManagerMock = $this->getMock(
166+
\Magento\Framework\App\ObjectManager::class,
167+
['get'],
168+
[],
169+
'',
170+
false
171+
);
172+
173+
$imageUploaderMock = $this->imageUploader;
174+
175+
$objectManagerMock->expects($this->any())
176+
->method('get')
177+
->will($this->returnCallback(function($class, $params = []) use ($imageUploaderMock) {
178+
if ($class == \Magento\Catalog\CategoryImageUpload::class) {
179+
return $imageUploaderMock;
180+
}
181+
182+
return $this->objectManager->get($class, $params);
183+
}));
184+
185+
$model = $this->objectManager->getObject(Model::class, [
186+
'objectManager' => $objectManagerMock
187+
]);
188+
189+
return $model->setAttribute($this->attribute);
190+
}
191+
192+
public function testAfterSaveShouldUploadImageWhenAdditionalDataSet()
193+
{
194+
$model = $this->setUpModelForAfterSave();
195+
196+
$this->imageUploader->expects($this->once())
197+
->method('moveFileFromTmp')
198+
->with($this->equalTo('test1234.jpg'));
199+
200+
$object = new \Magento\Framework\DataObject([
201+
'test_attribute_additional_data' => [
202+
['name' => 'test1234.jpg']
203+
]
204+
]);
205+
206+
$model->afterSave($object);
207+
}
208+
209+
public function testAfterSaveShouldNotUploadImageWhenAdditionalDataNotSet()
210+
{
211+
$model = $this->setUpModelForAfterSave();
212+
213+
$this->imageUploader->expects($this->never())
214+
->method('moveFileFromTmp');
215+
216+
$object = new \Magento\Framework\DataObject([
217+
'test_attribute' => [
218+
['name' => 'test1234.jpg']
219+
]
220+
]);
221+
222+
$model->afterSave($object);
223+
}
224+
}

0 commit comments

Comments
 (0)