5
5
*/
6
6
namespace Magento \Bundle \Test \Unit \Model \Product \CopyConstructor ;
7
7
8
+ use Magento \Bundle \Api \Data \BundleOptionInterface ;
9
+ use Magento \Bundle \Model \Product \CopyConstructor \Bundle ;
10
+ use Magento \Catalog \Api \Data \ProductExtensionInterface ;
11
+ use Magento \Catalog \Model \Product ;
12
+ use Magento \Catalog \Model \Product \Type ;
13
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+
8
15
class BundleTest extends \PHPUnit_Framework_TestCase
9
16
{
10
17
/**
11
- * @var PHPUnit_Framework_MockObject_MockObject
12
- */
13
- protected $ product ;
14
-
15
- /**
16
- * @var PHPUnit_Framework_MockObject_MockObject
17
- */
18
- protected $ duplicate ;
19
-
20
- /**
21
- * @var \Magento\Bundle\Model\Product\CopyConstructor\Bundle
18
+ * @var Bundle
22
19
*/
23
20
protected $ model ;
24
21
25
- /**
26
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
27
- */
28
- protected $ objectManager ;
29
-
30
22
protected function setUp ()
31
23
{
32
- // Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate
33
- $ this ->product = $ this ->getMock ('Magento\Catalog\Model\Product ' , [], [], '' , false );
34
- $ this ->duplicate = $ this ->getMock (
35
- 'Magento\Catalog\Model\Product ' ,
36
- ['setBundleOptionsData ' , 'setBundleSelectionsData ' , '__wakeup ' ],
37
- [],
38
- '' ,
39
- false
40
- );
41
- $ this ->model = new \Magento \Bundle \Model \Product \CopyConstructor \Bundle ();
42
- $ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
24
+ $ objectManager = new ObjectManager ($ this );
25
+ $ this ->model = $ objectManager ->getObject (Bundle::class);
43
26
}
44
27
45
28
public function testBuildNegative ()
46
29
{
47
- $ this ->product ->expects ($ this ->once ())->method ('getTypeId ' )->will ($ this ->returnValue ('other product ' ));
48
- $ this ->product ->expects ($ this ->never ())->method ('getTypeInstance ' );
49
- $ this ->model ->build ($ this ->product , $ this ->duplicate );
30
+ $ product = $ this ->getMockBuilder (Product::class)
31
+ ->disableOriginalConstructor ()
32
+ ->getMock ();
33
+ $ duplicate = $ this ->getMockBuilder (Product::class)
34
+ ->disableOriginalConstructor ()
35
+ ->getMock ();
36
+ $ product ->expects ($ this ->once ())
37
+ ->method ('getTypeId ' )
38
+ ->willReturn ('other product type ' );
39
+ $ this ->model ->build ($ product , $ duplicate );
50
40
}
51
41
52
42
/**
@@ -55,112 +45,48 @@ public function testBuildNegative()
55
45
*/
56
46
public function testBuildPositive ()
57
47
{
58
- //prepare mocks and data samples
59
- $ instance = $ this ->getMock (
60
- 'Magento\Bundle\Model\Product\Type ' ,
61
- ['setStoreFilter ' , 'getOptionsCollection ' , 'getSelectionsCollection ' , 'getOptionsIds ' ],
62
- [],
63
- '' ,
64
- false
65
- );
66
- $ option = $ this ->getMock (
67
- 'Magento\Bundle\Model\Option ' ,
68
- ['getSelections ' , '__wakeup ' , 'getData ' ],
69
- [],
70
- '' ,
71
- false
72
- );
73
- $ options = [$ option ];
74
- $ optionCollection = $ this ->objectManager ->getCollectionMock (
75
- 'Magento\Bundle\Model\ResourceModel\Option\Collection ' ,
76
- $ options
77
- );
78
- $ optionRawData = [
79
- ['required ' => true , 'position ' => 100 , 'type ' => 'someType ' , 'title ' => 'title ' , 'delete ' => '' ],
80
- ];
81
- $ selectionRawData = [
82
- [
83
- [
84
- 'product_id ' => 123 ,
85
- 'position ' => 500 ,
86
- 'is_default ' => false ,
87
- 'selection_price_type ' => 'priceType ' ,
88
- 'selection_price_value ' => 'priceVal ' ,
89
- 'selection_qty ' => 21 ,
90
- 'selection_can_change_qty ' => 11 ,
91
- 'delete ' => '' ,
92
- ],
93
- ],
94
- ];
95
-
96
- $ selection = $ this ->getMock (
97
- 'Magento\Bundle\Model\Selection ' ,
98
- [
99
- 'getProductId ' ,
100
- 'getPosition ' ,
101
- 'getIsDefault ' ,
102
- 'getSelectionPriceType ' ,
103
- 'getSelectionPriceValue ' ,
104
- 'getSelectionQty ' ,
105
- 'getSelectionCanChangeQty ' ,
106
- '__wakeup '
107
- ],
108
- [],
109
- '' ,
110
- false
111
- );
112
- $ selections = [$ selection ];
113
- $ selectionCollection = $ this ->getMock (
114
- 'Magento\Bundle\Model\ResourceModel\Selection\Collection ' ,
115
- [],
116
- [],
117
- '' ,
118
- false
119
- );
48
+ $ product = $ this ->getMockBuilder (Product::class)
49
+ ->disableOriginalConstructor ()
50
+ ->getMock ();
51
+ $ extensionAttributesProduct = $ this ->getMockBuilder (ProductExtensionInterface::class)
52
+ ->setMethods (['getBundleProductOptions ' ])
53
+ ->disableOriginalConstructor ()
54
+ ->getMockForAbstractClass ();
120
55
121
- // method flow
122
- $ this ->product ->expects ($ this ->once ())->method ('getTypeId ' )->will ($ this ->returnValue ('bundle ' ));
123
- $ this ->product ->expects ($ this ->any ())->method ('getTypeInstance ' )->will ($ this ->returnValue ($ instance ));
124
- $ instance ->expects ($ this ->once ())->method ('setStoreFilter ' )->with (null , $ this ->product );
125
- $ instance ->expects (
126
- $ this ->once ()
127
- )->method (
128
- 'getOptionsCollection '
129
- )->with (
130
- $ this ->product
131
- )->will (
132
- $ this ->returnValue ($ optionCollection )
133
- );
134
- $ instance ->expects (
135
- $ this ->once ()
136
- )->method (
137
- 'getSelectionsCollection '
138
- )->with (
139
- null ,
140
- $ this ->product
141
- )->will (
142
- $ this ->returnValue ($ selectionCollection )
143
- );
144
- $ optionCollection ->expects ($ this ->once ())->method ('appendSelections ' )->with ($ selectionCollection );
145
- $ option ->expects ($ this ->any ())->method ('getSelections ' )->will ($ this ->returnValue ($ selections ));
56
+ $ product ->expects ($ this ->once ())
57
+ ->method ('getTypeId ' )
58
+ ->willReturn (Type::TYPE_BUNDLE );
59
+ $ product ->expects ($ this ->once ())
60
+ ->method ('getExtensionAttributes ' )
61
+ ->willReturn ($ extensionAttributesProduct );
146
62
147
- $ option ->expects ($ this ->at (0 ))->method ('getData ' )->with ('required ' )->will ($ this ->returnValue (true ));
148
- $ option ->expects ($ this ->at (1 ))->method ('getData ' )->with ('position ' )->will ($ this ->returnValue (100 ));
149
- $ option ->expects ($ this ->at (2 ))->method ('getData ' )->with ('type ' )->will ($ this ->returnValue ('someType ' ));
150
- $ option ->expects ($ this ->at (3 ))->method ('getData ' )->with ('title ' )->will ($ this ->returnValue ('title ' ));
151
- $ option ->expects ($ this ->at (4 ))->method ('getData ' )->with ('title ' )->will ($ this ->returnValue ('title ' ));
63
+ $ bundleOptions = [
64
+ $ this ->getMockBuilder (BundleOptionInterface::class)
65
+ ->disableOriginalConstructor ()
66
+ ->getMockForAbstractClass (),
67
+ $ this ->getMockBuilder (BundleOptionInterface::class)
68
+ ->disableOriginalConstructor ()
69
+ ->getMockForAbstractClass ()
70
+ ];
71
+ $ extensionAttributesProduct ->expects ($ this ->once ())
72
+ ->method ('getBundleProductOptions ' )
73
+ ->willReturn ($ bundleOptions );
152
74
153
- $ selection -> expects ( $ this ->once ())-> method ( ' getProductId ' )-> will ( $ this -> returnValue ( 123 ));
154
- $ selection -> expects ( $ this -> once ())-> method ( ' getPosition ' )-> will ( $ this -> returnValue ( 500 ));
155
- $ selection -> expects ( $ this -> once ())-> method ( ' getIsDefault ' )-> will ( $ this -> returnValue ( false ) );
156
- $ selection -> expects ( $ this ->once ())-> method ( ' getSelectionPriceType ' )-> will ( $ this -> returnValue ( ' priceType ' ));
157
- $ selection -> expects ( $ this -> once ())-> method ( ' getSelectionPriceValue ' )-> will ( $ this -> returnValue ( ' priceVal ' ));
158
- $ selection -> expects ( $ this -> once ())-> method ( ' getSelectionQty ' )-> will ( $ this -> returnValue ( 21 ));
159
- $ selection -> expects ( $ this -> once ())-> method ( ' getSelectionCanChangeQty ' )-> will ( $ this -> returnValue ( 11 ) );
75
+ $ duplicate = $ this ->getMockBuilder (Product::class)
76
+ -> disableOriginalConstructor ()
77
+ -> getMock ( );
78
+ $ extensionAttributesDuplicate = $ this ->getMockBuilder (ProductExtensionInterface::class)
79
+ -> setMethods ([ ' setBundleProductOptions ' ])
80
+ -> disableOriginalConstructor ()
81
+ -> getMockForAbstractClass ( );
160
82
161
- $ this ->duplicate ->expects ($ this ->once ())->method ('setBundleOptionsData ' )->with ($ optionRawData );
162
- $ this ->duplicate ->expects ($ this ->once ())->method ('setBundleSelectionsData ' )->with ($ selectionRawData );
83
+ $ duplicate ->expects ($ this ->once ())
84
+ ->method ('getExtensionAttributes ' )
85
+ ->willReturn ($ extensionAttributesDuplicate );
86
+ $ extensionAttributesDuplicate ->expects ($ this ->once ())
87
+ ->method ('setBundleProductOptions ' )
88
+ ->withConsecutive ([$ bundleOptions ]);
163
89
164
- $ this ->model ->build ($ this -> product , $ this -> duplicate );
90
+ $ this ->model ->build ($ product , $ duplicate );
165
91
}
166
92
}
0 commit comments