@@ -60,6 +60,16 @@ class SubscriberTest extends \PHPUnit\Framework\TestCase
60
60
*/
61
61
protected $ objectManager ;
62
62
63
+ /**
64
+ * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
65
+ */
66
+ private $ dataObjectHelper ;
67
+
68
+ /**
69
+ * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
70
+ */
71
+ private $ customerFactory ;
72
+
63
73
/**
64
74
* @var \Magento\Newsletter\Model\Subscriber
65
75
*/
@@ -94,7 +104,13 @@ protected function setUp()
94
104
'received '
95
105
]);
96
106
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
97
-
107
+ $ this ->customerFactory = $ this ->getMockBuilder (\Magento \Customer \Api \Data \CustomerInterfaceFactory::class)
108
+ ->setMethods (['create ' ])
109
+ ->disableOriginalConstructor ()
110
+ ->getMock ();
111
+ $ this ->dataObjectHelper = $ this ->getMockBuilder (\Magento \Framework \Api \DataObjectHelper::class)
112
+ ->disableOriginalConstructor ()
113
+ ->getMock ();
98
114
$ this ->subscriber = $ this ->objectManager ->getObject (
99
115
\Magento \Newsletter \Model \Subscriber::class,
100
116
[
@@ -106,15 +122,31 @@ protected function setUp()
106
122
'customerRepository ' => $ this ->customerRepository ,
107
123
'customerAccountManagement ' => $ this ->customerAccountManagement ,
108
124
'inlineTranslation ' => $ this ->inlineTranslation ,
109
- 'resource ' => $ this ->resource
125
+ 'resource ' => $ this ->resource ,
126
+ 'customerFactory ' => $ this ->customerFactory ,
127
+ 'dataObjectHelper ' => $ this ->dataObjectHelper
110
128
]
111
129
);
112
130
}
113
131
114
132
public function testSubscribe ()
115
133
{
116
134
117
- $ this ->resource ->expects ($ this ->any ())->method ('loadByEmail ' )->willReturn (
135
+ $ storeId = 1 ;
136
+ $ customerData = ['store_id ' => $ storeId , 'email ' => $ email ];
137
+ $ storeModel = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
138
+ ->disableOriginalConstructor ()
139
+ ->getMock ();
140
+ $ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ storeModel );
141
+ $ storeModel ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ storeId );
142
+ $ customer = $ this ->createMock (\Magento \Customer \Api \Data \CustomerInterface::class);
143
+ $ this ->customerFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customer );
144
+ $ this ->dataObjectHelper ->expects ($ this ->once ())->method ('populateWithArray ' )->with (
145
+ $ customer ,
146
+ $ customerData ,
147
+ \Magento \Customer \Api \Data \CustomerInterface::class
148
+ );
149
+ $ this ->resource ->expects ($ this ->any ())->method ('loadByCustomerData ' )->with ($ customer )->willReturn (
118
150
[
119
151
'subscriber_status ' => 3 ,
120
152
'subscriber_email ' => $ email ,
@@ -128,7 +160,7 @@ public function testSubscribe()
128
160
$ this ->customerSession ->expects ($ this ->any ())->method ('getCustomerId ' )->willReturn (1 );
129
161
$ customerDataModel ->expects ($ this ->any ())->method ('getEmail ' )->willReturn ($ email );
130
162
$ this ->customerRepository ->expects ($ this ->any ())->method ('getById ' )->willReturn ($ customerDataModel );
131
- $ customerDataModel ->expects ($ this ->any ())->method ('getStoreId ' )->willReturn (1 );
163
+ $ customerDataModel ->expects ($ this ->any ())->method ('getStoreId ' )->willReturn ($ storeId );
132
164
$ customerDataModel ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
133
165
$ this ->sendEmailCheck ();
134
166
$ this ->resource ->expects ($ this ->atLeastOnce ())->method ('save ' )->willReturnSelf ();
@@ -139,7 +171,21 @@ public function testSubscribe()
139
171
public function testSubscribeNotLoggedIn ()
140
172
{
141
173
142
- $ this ->resource ->expects ($ this ->any ())->method ('loadByEmail ' )->willReturn (
174
+ $ storeId = 1 ;
175
+ $ customerData = ['store_id ' => $ storeId , 'email ' => $ email ];
176
+ $ storeModel = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)
177
+ ->disableOriginalConstructor ()
178
+ ->getMock ();
179
+ $ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ storeModel );
180
+ $ storeModel ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ storeId );
181
+ $ customer = $ this ->createMock (\Magento \Customer \Api \Data \CustomerInterface::class);
182
+ $ this ->customerFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customer );
183
+ $ this ->dataObjectHelper ->expects ($ this ->once ())->method ('populateWithArray ' )->with (
184
+ $ customer ,
185
+ $ customerData ,
186
+ \Magento \Customer \Api \Data \CustomerInterface::class
187
+ );
188
+ $ this ->resource ->expects ($ this ->any ())->method ('loadByCustomerData ' )->with ($ customer )->willReturn (
143
189
[
144
190
'subscriber_status ' => 3 ,
145
191
'subscriber_email ' => $ email ,
@@ -153,7 +199,7 @@ public function testSubscribeNotLoggedIn()
153
199
$ this ->customerSession ->expects ($ this ->any ())->method ('getCustomerId ' )->willReturn (1 );
154
200
$ customerDataModel ->expects ($ this ->any ())->method ('getEmail ' )->willReturn ($ email );
155
201
$ this ->customerRepository ->expects ($ this ->any ())->method ('getById ' )->willReturn ($ customerDataModel );
156
- $ customerDataModel ->expects ($ this ->any ())->method ('getStoreId ' )->willReturn (1 );
202
+ $ customerDataModel ->expects ($ this ->any ())->method ('getStoreId ' )->willReturn ($ storeId );
157
203
$ customerDataModel ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
158
204
$ this ->sendEmailCheck ();
159
205
$ this ->resource ->expects ($ this ->atLeastOnce ())->method ('save ' )->willReturnSelf ();
0 commit comments