5
5
*/
6
6
namespace Magento \CustomerImportExport \Test \Unit \Model \ResourceModel \Import \Customer ;
7
7
8
- use Magento \Customer \Api \CustomerRepositoryInterface ;
9
8
use Magento \CustomerImportExport \Model \ResourceModel \Import \Customer \Storage ;
10
9
use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory ;
10
+ use Magento \Customer \Model \ResourceModel \Customer \Collection ;
11
11
use Magento \Framework \DataObject ;
12
- use Magento \Framework \Exception \ NoSuchEntityException ;
12
+ use Magento \Framework \DB \ Select ;
13
13
use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
14
+ use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIterator ;
14
15
15
16
class StorageTest extends \PHPUnit_Framework_TestCase
16
17
{
@@ -20,23 +21,59 @@ class StorageTest extends \PHPUnit_Framework_TestCase
20
21
private $ _model ;
21
22
22
23
/**
23
- * @var CustomerRepositoryInterface |\PHPUnit_Framework_MockObject_MockObject
24
+ * @var CollectionByPagesIterator |\PHPUnit_Framework_MockObject_MockObject
24
25
*/
25
- private $ customerRepositoryMock ;
26
+ private $ iteratorMock ;
27
+
28
+ /**
29
+ * @var Collection|\PHPUnit_Framework_MockObject_MockObject
30
+ */
31
+ private $ collectionMock ;
26
32
27
33
protected function setUp ()
28
34
{
35
+ $ this ->iteratorMock = $ this ->getMockBuilder (
36
+ CollectionByPagesIterator::class
37
+ )
38
+ ->disableOriginalConstructor ()
39
+ ->getMock ();
40
+ /** @var \PHPUnit_Framework_MockObject_MockObject|CollectionByPagesIteratorFactory $iteratorFactoryMock */
41
+ $ iteratorFactoryMock = $ this ->getMockBuilder (
42
+ CollectionByPagesIteratorFactory::class
43
+ )
44
+ ->disableOriginalConstructor ()
45
+ ->getMock ();
46
+ $ iteratorFactoryMock ->expects ($ this ->any ())
47
+ ->method ('create ' )
48
+ ->willReturn ($ this ->iteratorMock );
49
+ $ this ->collectionMock = $ this ->getMockBuilder (Collection::class)
50
+ ->disableOriginalConstructor ()
51
+ ->getMock ();
52
+ /** @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject $collectionFactoryMock */
53
+ $ collectionFactoryMock = $ this ->getMockBuilder (
54
+ CollectionFactory::class
55
+ )
56
+ ->disableOriginalConstructor ()
57
+ ->getMock ();
58
+ $ collectionFactoryMock ->expects ($ this ->any ())
59
+ ->method ('create ' )
60
+ ->willReturn ($ this ->collectionMock );
61
+ /** @var \PHPUnit_Framework_MockObject_MockObject $selectMock */
62
+ $ selectMock = $ this ->getMockBuilder (Select::class)
63
+ ->disableOriginalConstructor ()
64
+ ->getMock ();
65
+ $ selectMock ->expects ($ this ->any ())
66
+ ->method ('getPart ' )
67
+ ->with (Select::FROM )
68
+ ->willReturn (['e ' => []]);
69
+ $ this ->collectionMock ->expects ($ this ->any ())
70
+ ->method ('getSelect ' )
71
+ ->willReturn ($ selectMock );
72
+
29
73
$ this ->_model = new Storage (
30
- $ this ->getMockBuilder (CollectionFactory::class)
31
- ->disableOriginalConstructor ()
32
- ->getMock (),
33
- $ this ->getMockBuilder (CollectionByPagesIteratorFactory::class)
34
- ->disableOriginalConstructor ()
35
- ->getMock (),
36
- [],
37
- $ this ->customerRepositoryMock = $ this ->getMockBuilder (CustomerRepositoryInterface::class)
38
- ->disableOriginalConstructor ()
39
- ->getMock ()
74
+ $ collectionFactoryMock ,
75
+ $ iteratorFactoryMock ,
76
+ []
40
77
);
41
78
}
42
79
@@ -53,20 +90,26 @@ public function testGetCustomerId()
53
90
$ nonExistingEmail = 'test1@magento.com ' ;
54
91
$ nonExistingWebsiteId = 2 ;
55
92
56
- /** @var \PHPUnit_Framework_MockObject_MockObject $customerMock */
57
- $ customerMock = new DataObject ([
58
- 'id ' => $ existingId ,
59
- 'email ' => $ existingEmail ,
60
- 'website_id ' => $ existingWebsiteId
61
- ]);
62
- $ this ->customerRepositoryMock ->expects ($ this ->at (0 ))
63
- ->method ('get ' )
64
- ->with ($ existingEmail , $ existingWebsiteId )
65
- ->willReturn ($ customerMock );
66
- $ this ->customerRepositoryMock ->expects ($ this ->at (1 ))
67
- ->method ('get ' )
68
- ->with ($ nonExistingEmail , $ nonExistingWebsiteId )
69
- ->willThrowException (new NoSuchEntityException ());
93
+ $ this ->iteratorMock ->expects ($ this ->at (0 ))
94
+ ->method ('iterate ' )
95
+ ->willReturnCallback (
96
+ function (...$ args ) use (
97
+ $ existingId ,
98
+ $ existingEmail ,
99
+ $ existingWebsiteId
100
+ ) {
101
+ /** @var callable $callable */
102
+ foreach ($ args [2 ] as $ callable ) {
103
+ $ callable (
104
+ new DataObject ([
105
+ 'id ' => $ existingId ,
106
+ 'email ' => $ existingEmail ,
107
+ 'website_id ' => $ existingWebsiteId ,
108
+ ])
109
+ );
110
+ }
111
+ }
112
+ );
70
113
71
114
$ this ->assertEquals (
72
115
$ existingId ,
@@ -78,17 +121,5 @@ public function testGetCustomerId()
78
121
$ nonExistingWebsiteId
79
122
)
80
123
);
81
- //Checking one more time to see whether the repo will be used once
82
- //again.
83
- $ this ->assertEquals (
84
- $ existingId ,
85
- $ this ->_model ->getCustomerId ($ existingEmail , $ existingWebsiteId )
86
- );
87
- $ this ->assertFalse (
88
- $ this ->_model ->getCustomerId (
89
- $ nonExistingEmail ,
90
- $ nonExistingWebsiteId
91
- )
92
- );
93
124
}
94
125
}
0 commit comments