@@ -43,7 +43,7 @@ class EntityTest extends TestCase
43
43
protected function setUp (): void
44
44
{
45
45
$ this ->urlFactoryMock = $ this ->createMock (UrlFactory::class);
46
- $ this ->urlFinderMock = $ this ->getMockForAbstractClass (UrlFinderInterface::class);
46
+ $ this ->urlFinderMock = $ this ->createMock (UrlFinderInterface::class);
47
47
$ this ->urlFixerMock = $ this ->createMock (UrlFixer::class);
48
48
}
49
49
@@ -60,66 +60,59 @@ public function testGetEmptyStores()
60
60
public function testGet ()
61
61
{
62
62
$ storeMock1 = $ this ->createMock (Store::class);
63
- $ storeMock1 ->expects ($ this ->exactly (2 ))
64
- ->method ('getId ' )
65
- ->willReturn ('store1 ' );
66
63
$ storeMock2 = $ this ->createMock (Store::class);
67
- $ storeMock2 ->expects ($ this ->exactly (2 ))
68
- ->method ('getId ' )
69
- ->willReturn ('store2 ' );
70
64
71
- $ urlMock1 = $ this ->getMockForAbstractClass (UrlInterface::class);
72
- $ urlMock1 ->expects ($ this ->once ())
73
- ->method ('getUrl ' )
74
- ->with ('/path1 ' )
75
- ->willReturn ('http://site1.com/path1 ' );
76
- $ urlMock1 ->expects ($ this ->once ())
77
- ->method ('setScope ' )
78
- ->with ('store1 ' )
79
- ->willReturnSelf ();
80
- $ urlMock2 = $ this ->getMockForAbstractClass (UrlInterface::class);
81
- $ urlMock2 ->expects ($ this ->once ())
82
- ->method ('getUrl ' )
83
- ->with ('/path2 ' )
84
- ->willReturn ('http://site2.com/path2 ' );
85
- $ urlMock2 ->expects ($ this ->once ())
86
- ->method ('setScope ' )
87
- ->with ('store2 ' )
88
- ->willReturnSelf ();
65
+ $ urlMock1 = $ this ->createMock (UrlInterface::class);
66
+ $ urlMock2 = $ this ->createMock (UrlInterface::class);
89
67
90
68
$ urlRewriteMock1 = $ this ->createMock (UrlRewrite::class);
91
- $ urlRewriteMock1 ->expects ($ this ->once ())
92
- ->method ('getRequestPath ' )
93
- ->willReturn ('/path1 ' );
94
69
$ urlRewriteMock2 = $ this ->createMock (UrlRewrite::class);
95
- $ urlRewriteMock2 ->expects ($ this ->once ())
96
- ->method ('getRequestPath ' )
97
- ->willReturn ('/path2 ' );
98
70
99
- $ this ->urlFactoryMock ->expects ($ this ->exactly (2 ))
100
- ->method ('create ' )
101
- ->willReturnOnConsecutiveCalls ($ urlMock1 , $ urlMock2 );
71
+ $ this ->setupUrlMocks ($ urlMock1 , '/path1 ' , 'http://site1.com/path1 ' , 'store1 ' );
72
+ $ this ->setupUrlMocks ($ urlMock2 , '/path2 ' , 'http://site2.com/path2 ' , 'store2 ' );
73
+
74
+ $ this ->setupStoreMocks ($ storeMock1 , 'store1 ' , 2 );
75
+ $ this ->setupStoreMocks ($ storeMock2 , 'store2 ' , 2 );
76
+
77
+ $ this ->setupUrlRewriteMocks ($ urlRewriteMock1 , '/path1 ' );
78
+ $ this ->setupUrlRewriteMocks ($ urlRewriteMock2 , '/path2 ' );
79
+
80
+ $ this ->setupUrlFactoryMock ($ urlMock1 , $ urlMock2 );
102
81
$ this ->urlFinderMock ->expects ($ this ->exactly (2 ))
103
82
->method ('findAllByData ' )
104
- ->withConsecutive (
105
- [['store_id ' => 'store1 ' , 'entity_type ' => 'category ' ]],
106
- [['store_id ' => 'store2 ' , 'entity_type ' => 'category ' ]]
107
- )
108
- ->willReturnOnConsecutiveCalls (
109
- [$ urlRewriteMock1 ],
110
- [$ urlRewriteMock2 ]
111
- );
83
+ ->willReturnCallback (function ($ data ) use ($ urlRewriteMock1 , $ urlRewriteMock2 ) {
84
+ $ expected1 = ['store_id ' => 'store1 ' , 'entity_type ' => 'category ' ];
85
+ $ expected2 = ['store_id ' => 'store2 ' , 'entity_type ' => 'category ' ];
86
+
87
+ if (array_intersect_assoc ($ expected1 , $ data ) == $ expected1 ) {
88
+ return [$ urlRewriteMock1 ];
89
+ }
90
+
91
+ if (array_intersect_assoc ($ expected2 , $ data ) == $ expected2 ) {
92
+ return [$ urlRewriteMock2 ];
93
+ }
94
+
95
+ return [];
96
+ });
112
97
113
98
$ this ->urlFixerMock ->expects ($ this ->exactly (2 ))
114
99
->method ('run ' )
115
- ->withConsecutive (
116
- [$ storeMock1 , 'http://site1.com/path1 ' ],
117
- [$ storeMock2 , 'http://site2.com/path2 ' ]
118
- )
119
- ->willReturnOnConsecutiveCalls ('http://site1.com/fixed/path1 ' , 'http://site2.com/fixed/path2 ' );
100
+ ->willReturnCallback (function ($ store , $ url ) use ($ storeMock1 , $ storeMock2 ) {
101
+ static $ callCount = 0 ;
102
+ $ callCount ++;
120
103
121
- $ entity = $ this ->createEntity ('category ' , [$ storeMock1 , $ storeMock2 ]);
104
+ if ($ callCount === 1 && $ store === $ storeMock1 && $ url === '/path1 ' ) {
105
+ return 'http://site1.com/fixed/path1 ' ;
106
+ }
107
+
108
+ if ($ callCount === 2 && $ store === $ storeMock2 && $ url === '/path2 ' ) {
109
+ return 'http://site2.com/fixed/path2 ' ;
110
+ }
122
111
112
+ return '' ;
113
+ });
114
+
115
+ $ entity = $ this ->createEntity ('category ' , [$ storeMock1 , $ storeMock2 ]);
123
116
$ this ->assertEquals (
124
117
[
125
118
'http://site1.com/fixed/path1 ' ,
@@ -129,6 +122,44 @@ public function testGet()
129
122
);
130
123
}
131
124
125
+ private function setupStoreMocks ($ storeMock , $ storeId , $ times )
126
+ {
127
+ $ storeMock ->expects ($ this ->exactly ($ times ))
128
+ ->method ('getId ' )
129
+ ->willReturn ($ storeId );
130
+ }
131
+
132
+ private function setupUrlMocks ($ urlMock , $ requestPath , $ returnUrl , $ storeId )
133
+ {
134
+ $ urlMock ->expects ($ this ->any ())
135
+ ->method ('setScope ' )
136
+ ->with ($ storeId )
137
+ ->willReturnSelf ();
138
+ $ urlMock ->expects ($ this ->any ())
139
+ ->method ('getUrl ' )
140
+ ->with ($ requestPath )
141
+ ->willReturn ($ returnUrl );
142
+ }
143
+
144
+ private function setupUrlRewriteMocks ($ urlRewriteMock , $ requestPath )
145
+ {
146
+ $ urlRewriteMock ->expects ($ this ->once ())
147
+ ->method ('getRequestPath ' )
148
+ ->willReturn ($ requestPath );
149
+ }
150
+
151
+ private function setupUrlFactoryMock ($ urlMock1 , $ urlMock2 )
152
+ {
153
+ $ this ->urlFactoryMock ->expects ($ this ->exactly (2 ))
154
+ ->method ('create ' )
155
+ ->willReturnCallback (function () use ($ urlMock1 , $ urlMock2 ) {
156
+ static $ callCount = 0 ;
157
+ $ callCount ++;
158
+
159
+ return $ callCount === 1 ? $ urlMock1 : $ urlMock2 ;
160
+ });
161
+ }
162
+
132
163
/**
133
164
* @param string $entityType
134
165
* @param array $stores
0 commit comments