66namespace Magento \CustomerImportExport \Test \Unit \Model \ResourceModel \Import \Customer ;
77
88use Magento \CustomerImportExport \Model \ResourceModel \Import \Customer \Storage ;
9+ use Magento \Framework \DB \Adapter \AdapterInterface ;
10+ use Magento \Customer \Model \ResourceModel \Customer \Collection ;
11+ use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory ;
12+ use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
913
1014class StorageTest extends \PHPUnit \Framework \TestCase
1115{
@@ -26,74 +30,63 @@ class StorageTest extends \PHPUnit\Framework\TestCase
2630
2731 protected function setUp ()
2832 {
29- $ this ->_model = new \Magento \CustomerImportExport \Model \ResourceModel \Import \Customer \Storage (
30- $ this ->getMockBuilder (\Magento \Customer \Model \ResourceModel \Customer \CollectionFactory::class)
31- ->disableOriginalConstructor ()
32- ->getMock (),
33- $ this ->getMockBuilder (\Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory::class)
34- ->disableOriginalConstructor ()
35- ->getMock (),
36- $ this ->_getModelDependencies ()
37- );
38- $ this ->_model ->load ();
39- }
40-
41- protected function tearDown ()
42- {
43- unset($ this ->_model );
44- }
45-
46- /**
47- * Retrieve all necessary objects mocks which used inside customer storage
48- *
49- * @return array
50- */
51- protected function _getModelDependencies ()
52- {
53- $ select = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
33+ /** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject $selectMock */
34+ $ selectMock = $ this ->getMockBuilder (\Magento \Framework \DB \Select::class)
5435 ->disableOriginalConstructor ()
5536 ->setMethods (['from ' ])
5637 ->getMock ();
57- $ select ->expects ($ this ->any ())->method ('from ' )->will ($ this ->returnCallback ([$ this , 'validateFrom ' ]));
58- $ customerCollection = $ this ->getMockBuilder (\Magento \Customer \Model \ResourceModel \Customer \Collection::class)
38+ $ selectMock ->expects ($ this ->any ())->method ('from ' )->will ($ this ->returnSelf ());
39+
40+ /** @var $connectionMock AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
41+ $ connectionMock = $ this ->getMockBuilder (\Magento \Framework \DB \Adapter \Pdo \Mysql::class)
42+ ->disableOriginalConstructor ()
43+ ->setMethods (['select ' , 'fetchAll ' ])
44+ ->getMock ();
45+ $ connectionMock ->expects ($ this ->any ())
46+ ->method ('select ' )
47+ ->will ($ this ->returnValue ($ selectMock ));
48+ $ connectionMock ->expects ($ this ->any ())
49+ ->method ('fetchAll ' )
50+ ->will ($ this ->returnValue ([]));
51+
52+ /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $customerCollection */
53+ $ customerCollection = $ this ->getMockBuilder (Collection::class)
5954 ->disableOriginalConstructor ()
60- ->setMethods (['load ' , ' removeAttributeToSelect ' , ' getResource ' , ' getSelect ' ])
55+ ->setMethods (['getConnection ' , ' getMainTable ' ])
6156 ->getMock ();
57+ $ customerCollection ->expects ($ this ->any ())
58+ ->method ('getConnection ' )
59+ ->will ($ this ->returnValue ($ connectionMock ));
6260
63- $ resourceStub = new \ Magento \ Framework \ DataObject ();
64- $ resourceStub -> setEntityTable ( $ this -> _entityTable );
65- $ customerCollection -> expects ( $ this -> once ())-> method ( ' getResource ' )-> will ( $ this -> returnValue ( $ resourceStub ) );
61+ $ customerCollection -> expects ( $ this -> any ())
62+ -> method ( ' getMainTable ' )
63+ -> willReturn ( ' customer_entity ' );
6664
67- $ customerCollection ->expects ($ this ->once ())->method ('getSelect ' )->will ($ this ->returnValue ($ select ));
65+ /** @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject $collectionFactory */
66+ $ collectionFactory = $ this ->getMockBuilder (CollectionFactory::class)
67+ ->disableOriginalConstructor ()
68+ ->setMethods (['create ' ])
69+ ->getMock ();
70+ $ collectionFactory ->expects ($ this ->any ())
71+ ->method ('create ' )
72+ ->willReturn ($ customerCollection );
6873
69- $ byPagesIterator = $ this ->createPartialMock (\stdClass::class, ['iterate ' ]);
70- $ byPagesIterator ->expects ($ this ->once ())
71- ->method ('iterate ' )
72- ->will ($ this ->returnCallback ([$ this , 'iterate ' ]));
74+ /** @var CollectionByPagesIteratorFactory|\PHPUnit_Framework_MockObject_MockObject $byPagesIteratorFactory */
75+ $ byPagesIteratorFactory = $ this ->getMockBuilder (CollectionByPagesIteratorFactory::class)
76+ ->disableOriginalConstructor ()
77+ ->setMethods (['create ' ])
78+ ->getMock ();
7379
74- return [
75- ' customer_collection ' => $ customerCollection ,
76- ' collection_by_pages_iterator ' => $ byPagesIterator ,
77- ' page_size ' => 10
78- ] ;
80+ $ this -> _model = new Storage (
81+ $ collectionFactory ,
82+ $ byPagesIteratorFactory
83+ );
84+ $ this -> _model -> load () ;
7985 }
8086
81- /**
82- * Iterate stub
83- *
84- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
85- *
86- * @param \Magento\Framework\Data\Collection $collection
87- * @param int $pageSize
88- * @param array $callbacks
89- */
90- public function iterate (\Magento \Framework \Data \Collection $ collection , $ pageSize , array $ callbacks )
87+ protected function tearDown ()
9188 {
92- foreach ($ collection as $ customer ) {
93- foreach ($ callbacks as $ callback ) {
94- call_user_func ($ callback , $ customer );
95- }
96- }
89+ unset($ this ->_model );
9790 }
9891
9992 /**
@@ -112,13 +105,26 @@ public function testLoad()
112105 }
113106
114107 public function testAddCustomer ()
108+ {
109+ $ customer =
new \
Magento \
Framework \
DataObject ([
'id ' =>
1 ,
'website_id ' =>
1 ,
'email ' =>
'[email protected] ' ]);
110+ $ this ->_model ->addCustomer ($ customer );
111+
112+ $ propertyName = '_customerIds ' ;
113+ $ this ->assertAttributeCount (1 , $ propertyName , $ this ->_model );
114+ $ this ->assertAttributeContains ([$ customer ->getWebsiteId () => $ customer ->getId ()], $ propertyName , $ this ->_model );
115+ $ this ->assertEquals (
116+ $ customer ->getId (),
117+ $ this ->_model ->getCustomerId ($ customer ->getEmail (), $ customer ->getWebsiteId ())
118+ );
119+ }
120+
121+ public function testAddCustomerByArray ()
115122 {
116123 $ propertyName = '_customerIds ' ;
117124 $ customer = $ this ->_addCustomerToStorage ();
118125
119126 $ this ->assertAttributeCount (1 , $ propertyName , $ this ->_model );
120-
121- $ expectedCustomerData = [$ customer ->getWebsiteId () => $ customer ->getId ()];
127+ $ expectedCustomerData = [$ customer ['website_id ' ] => $ customer ['entity_id ' ]];
122128 $ this ->assertAttributeContains ($ expectedCustomerData , $ propertyName , $ this ->_model );
123129 }
124130
@@ -127,19 +133,19 @@ public function testGetCustomerId()
127133 $ customer = $ this ->_addCustomerToStorage ();
128134
129135 $ this ->assertEquals (
130- $ customer-> getId () ,
131- $ this ->_model ->getCustomerId ($ customer-> getEmail () , $ customer-> getWebsiteId () )
136+ $ customer[ ' entity_id ' ] ,
137+ $ this ->_model ->getCustomerId ($ customer[ ' email ' ] , $ customer[ ' website_id ' ] )
132138 );
133- $ this ->
assertFalse (
$ this ->
_model ->
getCustomerId (
'[email protected] ' ,
$ customer-> getWebsiteId () ));
139+ $ this ->
assertFalse (
$ this ->
_model ->
getCustomerId (
'[email protected] ' ,
$ customer[ ' website_id ' ] ));
134140 }
135141
136142 /**
137- * @return \Magento\Framework\DataObject
143+ * @return array
138144 */
139145 protected function _addCustomerToStorage ()
140146 {
141- $ customer =
new \ Magento \ Framework \ DataObject ([ ' id ' =>
1 ,
'website_id ' =>
1 ,
'email ' =>
'[email protected] ' ]
) ;
142- $ this ->_model ->addCustomer ($ customer );
147+ $ customer =
[ ' entity_id ' =>
1 ,
'website_id ' =>
1 ,
'email ' =>
'[email protected] ' ];
148+ $ this ->_model ->addCustomerByArray ($ customer );
143149
144150 return $ customer ;
145151 }
0 commit comments