@@ -62,6 +62,16 @@ class SubscriberTest extends \PHPUnit\Framework\TestCase
6262 */
6363 protected $ objectManager ;
6464
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+
6575 /**
6676 * @var \Magento\Newsletter\Model\Subscriber
6777 */
@@ -97,6 +107,14 @@ protected function setUp()
97107 ]);
98108 $ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
99109
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+
100118 $ this ->subscriber = $ this ->objectManager ->getObject (
101119 \Magento \Newsletter \Model \Subscriber::class,
102120 [
@@ -108,15 +126,31 @@ protected function setUp()
108126 'customerRepository ' => $ this ->customerRepository ,
109127 'customerAccountManagement ' => $ this ->customerAccountManagement ,
110128 'inlineTranslation ' => $ this ->inlineTranslation ,
111- 'resource ' => $ this ->resource
129+ 'resource ' => $ this ->resource ,
130+ 'customerFactory ' => $ this ->customerFactory ,
131+ 'dataObjectHelper ' => $ this ->dataObjectHelper
112132 ]
113133 );
114134 }
115135
116136 public function testSubscribe ()
117137 {
118138119- $ 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 (
120154 [
121155 'subscriber_status ' => Subscriber::STATUS_UNSUBSCRIBED ,
122156 'subscriber_email ' => $ email ,
@@ -130,7 +164,7 @@ public function testSubscribe()
130164 $ this ->customerSession ->expects ($ this ->any ())->method ('getCustomerId ' )->willReturn (1 );
131165 $ customerDataModel ->expects ($ this ->any ())->method ('getEmail ' )->willReturn ($ email );
132166 $ 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 );
134168 $ customerDataModel ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
135169 $ this ->sendEmailCheck ();
136170 $ this ->resource ->expects ($ this ->atLeastOnce ())->method ('save ' )->willReturnSelf ();
@@ -141,7 +175,21 @@ public function testSubscribe()
141175 public function testSubscribeNotLoggedIn ()
142176 {
143177144- $ 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 (
145193 [
146194 'subscriber_status ' => Subscriber::STATUS_UNSUBSCRIBED ,
147195 'subscriber_email ' => $ email ,
@@ -155,7 +203,7 @@ public function testSubscribeNotLoggedIn()
155203 $ this ->customerSession ->expects ($ this ->any ())->method ('getCustomerId ' )->willReturn (1 );
156204 $ customerDataModel ->expects ($ this ->any ())->method ('getEmail ' )->willReturn ($ email );
157205 $ 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 );
159207 $ customerDataModel ->expects ($ this ->any ())->method ('getId ' )->willReturn (1 );
160208 $ this ->sendEmailCheck ();
161209 $ this ->resource ->expects ($ this ->atLeastOnce ())->method ('save ' )->willReturnSelf ();
0 commit comments