6
6
namespace Magento \CustomerImportExport \Test \Unit \Model \ResourceModel \Import \Customer ;
7
7
8
8
use 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 ;
9
13
10
14
class StorageTest extends \PHPUnit \Framework \TestCase
11
15
{
@@ -26,74 +30,63 @@ class StorageTest extends \PHPUnit\Framework\TestCase
26
30
27
31
protected function setUp ()
28
32
{
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)
54
35
->disableOriginalConstructor ()
55
36
->setMethods (['from ' ])
56
37
->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)
59
54
->disableOriginalConstructor ()
60
- ->setMethods (['load ' , ' removeAttributeToSelect ' , ' getResource ' , ' getSelect ' ])
55
+ ->setMethods (['getConnection ' , ' getMainTable ' ])
61
56
->getMock ();
57
+ $ customerCollection ->expects ($ this ->any ())
58
+ ->method ('getConnection ' )
59
+ ->will ($ this ->returnValue ($ connectionMock ));
62
60
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 ' );
66
64
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 );
68
73
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 ();
73
79
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 () ;
79
85
}
80
86
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 ()
91
88
{
92
- foreach ($ collection as $ customer ) {
93
- foreach ($ callbacks as $ callback ) {
94
- call_user_func ($ callback , $ customer );
95
- }
96
- }
89
+ unset($ this ->_model );
97
90
}
98
91
99
92
/**
@@ -112,13 +105,26 @@ public function testLoad()
112
105
}
113
106
114
107
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 ()
115
122
{
116
123
$ propertyName = '_customerIds ' ;
117
124
$ customer = $ this ->_addCustomerToStorage ();
118
125
119
126
$ this ->assertAttributeCount (1 , $ propertyName , $ this ->_model );
120
-
121
- $ expectedCustomerData = [$ customer ->getWebsiteId () => $ customer ->getId ()];
127
+ $ expectedCustomerData = [$ customer ['website_id ' ] => $ customer ['entity_id ' ]];
122
128
$ this ->assertAttributeContains ($ expectedCustomerData , $ propertyName , $ this ->_model );
123
129
}
124
130
@@ -127,19 +133,19 @@ public function testGetCustomerId()
127
133
$ customer = $ this ->_addCustomerToStorage ();
128
134
129
135
$ 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 ' ] )
132
138
);
133
- $ this ->
assertFalse (
$ this ->
_model ->
getCustomerId (
'[email protected] ' ,
$ customer-> getWebsiteId () ));
139
+ $ this ->
assertFalse (
$ this ->
_model ->
getCustomerId (
'[email protected] ' ,
$ customer[ ' website_id ' ] ));
134
140
}
135
141
136
142
/**
137
- * @return \Magento\Framework\DataObject
143
+ * @return array
138
144
*/
139
145
protected function _addCustomerToStorage ()
140
146
{
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 );
143
149
144
150
return $ customer ;
145
151
}
0 commit comments