5
5
namespace EventListener ;
6
6
7
7
use PHPUnit \Framework \Attributes \CoversClass ;
8
+ use PHPUnit \Framework \Attributes \DataProvider ;
8
9
use PHPUnit \Framework \MockObject \MockObject ;
9
10
use PHPUnit \Framework \TestCase ;
10
11
use Psr \Http \Message \RequestInterface ;
11
12
use Shopware \App \SDK \Event \BeforeRegistrationStartsEvent ;
12
13
use Shopware \App \SDK \Shop \ShopInterface ;
13
14
use Shopware \AppBundle \EventListener \BeforeRegistrationStartsListener ;
14
15
use Shopware \AppBundle \Exception \ShopURLIsNotReachableException ;
16
+ use Symfony \Component \HttpClient \Exception \ClientException ;
17
+ use Symfony \Component \HttpClient \Exception \TransportException ;
18
+ use Symfony \Component \HttpClient \Response \MockResponse ;
19
+ use Symfony \Component \HttpFoundation \Response ;
20
+ use Symfony \Component \HttpKernel \Exception \UnauthorizedHttpException ;
15
21
use Symfony \Contracts \HttpClient \HttpClientInterface ;
16
22
17
23
#[CoversClass(BeforeRegistrationStartsListener::class)]
@@ -95,7 +101,7 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable():
95
101
'timeout ' => 10 ,
96
102
'max_redirects ' => 0 ,
97
103
])
98
- ->willThrowException (new \ Exception ('Shop url is not reachable ' ));
104
+ ->willThrowException (new TransportException ('Shop is not reachable ' ));
99
105
100
106
$ listener = new BeforeRegistrationStartsListener (
101
107
$ this ->httpClient ,
@@ -109,4 +115,48 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable():
109
115
)
110
116
);
111
117
}
118
+
119
+ #[DataProvider('unauthorizedHttpExceptionProvider ' )]
120
+ public function testListenerDoesNotThrowExceptionWhenTheExceptionCodeIsHTTPUnauthorized ($ exception ): void
121
+ {
122
+ $ shop = $ this ->createMock (ShopInterface::class);
123
+ $ shop
124
+ ->expects (self ::once ())
125
+ ->method ('getShopUrl ' )
126
+ ->willReturn ('https://shop-url.com ' );
127
+
128
+ $ this ->httpClient
129
+ ->expects (self ::once ())
130
+ ->method ('request ' )
131
+ ->with ('HEAD ' , 'https://shop-url.com/api/_info/config ' , [
132
+ 'timeout ' => 10 ,
133
+ 'max_redirects ' => 0 ,
134
+ ])
135
+ ->willThrowException ($ exception );
136
+
137
+ $ listener = new BeforeRegistrationStartsListener (
138
+ $ this ->httpClient ,
139
+ true
140
+ );
141
+
142
+ $ listener ->__invoke (
143
+ new BeforeRegistrationStartsEvent (
144
+ $ this ->createMock (RequestInterface::class),
145
+ $ shop
146
+ )
147
+ );
148
+ }
149
+
150
+ public static function unauthorizedHttpExceptionProvider (): \Generator
151
+ {
152
+ yield 'HttpException ' => [
153
+ new UnauthorizedHttpException ('Unauthorized ' )
154
+ ];
155
+
156
+ yield 'HttpExceptionInterface ' => [
157
+ new ClientException (new MockResponse ('' , [
158
+ 'http_code ' => Response::HTTP_UNAUTHORIZED ,
159
+ ]))
160
+ ];
161
+ }
112
162
}
0 commit comments