@@ -17,40 +17,109 @@ class ArrayBackendTest extends TestCase
17
17
/**
18
18
* @var ArrayBackend
19
19
*/
20
- protected $ _model ;
20
+ private $ _model ;
21
21
22
22
/**
23
23
* @var Attribute
24
24
*/
25
- protected $ _attribute ;
25
+ private $ _attribute ;
26
26
27
27
protected function setUp (): void
28
28
{
29
29
$ this ->_attribute = $ this ->createPartialMock (
30
30
Attribute::class,
31
- ['getAttributeCode ' , '__wakeup ' ]
31
+ ['getAttributeCode ' , 'getDefaultValue ' , ' __wakeup ' ]
32
32
);
33
33
$ this ->_model = new ArrayBackend ();
34
34
$ this ->_model ->setAttribute ($ this ->_attribute );
35
35
}
36
36
37
37
/**
38
- * @dataProvider attributeValueDataProvider
38
+ * @dataProvider validateDataProvider
39
+ * @param array $productData
40
+ * @param bool $hasData
41
+ * @param string|int|float|null $expectedValue
39
42
*/
40
- public function testValidate ($ data )
43
+ public function testValidate (array $ productData , bool $ hasData , $ expectedValue )
41
44
{
42
- $ this ->_attribute ->expects ($ this ->atLeastOnce ())->method ('getAttributeCode ' )->willReturn ('code ' );
43
- $ product = new DataObject (['code ' => $ data , 'empty ' => null ]);
45
+ $ this ->_attribute ->expects ($ this ->atLeastOnce ())
46
+ ->method ('getAttributeCode ' )
47
+ ->willReturn ('attr ' );
48
+
49
+ $ product = new DataObject ($ productData );
44
50
$ this ->_model ->validate ($ product );
45
- $ this ->assertEquals ('1,2,3 ' , $ product ->getCode ());
46
- $ this ->assertNull ($ product ->getEmpty ());
51
+ $ this ->assertEquals ($ hasData , $ product ->hasData ('attr ' ));
52
+ $ this ->assertEquals ($ expectedValue , $ product ->getAttr ());
53
+ }
54
+
55
+ /**
56
+ * @return array
57
+ */
58
+ public static function validateDataProvider (): array
59
+ {
60
+ return [
61
+ [
62
+ ['sku ' => 'test1 ' , 'attr ' => [1 , 2 , 3 ]],
63
+ true ,
64
+ '1,2,3 ' ,
65
+ ],
66
+ [
67
+ ['sku ' => 'test1 ' , 'attr ' => '1,2,3 ' ],
68
+ true ,
69
+ '1,2,3 ' ,
70
+ ],
71
+ [
72
+ ['sku ' => 'test1 ' , 'attr ' => null ],
73
+ true ,
74
+ null ,
75
+ ],
76
+ [
77
+ ['sku ' => 'test1 ' ],
78
+ false ,
79
+ null ,
80
+ ],
81
+ ];
82
+ }
83
+
84
+ /**
85
+ * @dataProvider beforeSaveDataProvider
86
+ * @param array $productData
87
+ * @param string $defaultValue
88
+ * @param string $expectedValue
89
+ */
90
+ public function testBeforeSave (
91
+ array $ productData ,
92
+ string $ defaultValue ,
93
+ string $ expectedValue
94
+ ) {
95
+ $ this ->_attribute ->expects ($ this ->atLeastOnce ())
96
+ ->method ('getAttributeCode ' )
97
+ ->willReturn ('attr ' );
98
+ $ this ->_attribute ->expects ($ this ->any ())
99
+ ->method ('getDefaultValue ' )
100
+ ->willReturn ($ defaultValue );
101
+
102
+ $ product = new DataObject ($ productData );
103
+ $ this ->_model ->beforeSave ($ product );
104
+ $ this ->assertEquals ($ expectedValue , $ product ->getAttr ());
47
105
}
48
106
49
107
/**
50
108
* @return array
51
109
*/
52
- public static function attributeValueDataProvider ()
110
+ public function beforeSaveDataProvider (): array
53
111
{
54
- return [[[1 , 2 , 3 ]], ['1,2,3 ' ]];
112
+ return [
113
+ [
114
+ ['sku ' => 'test1 ' , 'attr ' => 'Value 2 ' ],
115
+ 'Default value 1 ' ,
116
+ 'Value 2 ' ,
117
+ ],
118
+ [
119
+ ['sku ' => 'test1 ' ],
120
+ 'Default value 1 ' ,
121
+ 'Default value 1 ' ,
122
+ ],
123
+ ];
55
124
}
56
125
}
0 commit comments