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