9
9
use BluePsyduck \FactorioModPortalClient \Entity \Release ;
10
10
use BluePsyduck \FactorioModPortalClient \Entity \Version ;
11
11
use BluePsyduck \FactorioModPortalClient \Exception \ClientException ;
12
+ use BluePsyduck \FactorioModPortalClient \Exception \ErrorResponseException ;
12
13
use BluePsyduck \FactorioModPortalClient \Request \FullModRequest ;
14
+ use FactorioItemBrowser \CombinationApi \Server \Exception \FailedModPortalRequestException ;
13
15
use FactorioItemBrowser \CombinationApi \Server \Service \ModPortalService ;
14
16
use GuzzleHttp \Promise \FulfilledPromise ;
17
+ use GuzzleHttp \Promise \RejectedPromise ;
15
18
use PHPUnit \Framework \TestCase ;
16
19
17
20
/**
23
26
*/
24
27
class ModPortalServiceTest extends TestCase
25
28
{
29
+ /**
30
+ * @throws FailedModPortalRequestException
31
+ */
26
32
public function testRequestMods (): void
27
33
{
28
34
$ modNames = ['abc ' , 'def ' , 'ghi ' ];
@@ -38,6 +44,7 @@ public function testRequestMods(): void
38
44
$ mod2 ->setName ('def ' );
39
45
$ promise1 = new FulfilledPromise ($ mod1 );
40
46
$ promise2 = new FulfilledPromise ($ mod2 );
47
+ $ promise3 = new RejectedPromise (new ErrorResponseException ('test ' , 404 , '' , '' ));
41
48
$ expectedResult = [
42
49
'abc ' => $ mod1 ,
43
50
'def ' => $ mod2 ,
@@ -54,7 +61,7 @@ public function testRequestMods(): void
54
61
->willReturnOnConsecutiveCalls (
55
62
$ promise1 ,
56
63
$ promise2 ,
57
- $ this -> throwException ( $ this -> createMock (ClientException::class)) ,
64
+ $ promise3 ,
58
65
);
59
66
60
67
$ instance = new ModPortalService ($ modPortalClient );
@@ -63,6 +70,65 @@ public function testRequestMods(): void
63
70
$ this ->assertEquals ($ expectedResult , $ result );
64
71
}
65
72
73
+ public function testRequestModsWithServerException (): void
74
+ {
75
+ $ modNames = ['abc ' , 'def ' ];
76
+ $ expectedRequest1 = new FullModRequest ();
77
+ $ expectedRequest1 ->setName ('abc ' );
78
+ $ expectedRequest2 = new FullModRequest ();
79
+ $ expectedRequest2 ->setName ('def ' );
80
+ $ mod1 = new Mod ();
81
+ $ mod1 ->setName ('abc ' );
82
+ $ promise1 = new FulfilledPromise ($ mod1 );
83
+ $ promise2 = new RejectedPromise (new ErrorResponseException ('test ' , 500 , '' , '' ));
84
+
85
+ $ modPortalClient = $ this ->createMock (ClientInterface::class);
86
+ $ modPortalClient ->expects ($ this ->exactly (2 ))
87
+ ->method ('sendRequest ' )
88
+ ->withConsecutive (
89
+ [$ this ->equalTo ($ expectedRequest1 )],
90
+ [$ this ->equalTo ($ expectedRequest2 )],
91
+ )
92
+ ->willReturnOnConsecutiveCalls (
93
+ $ promise1 ,
94
+ $ promise2 ,
95
+ );
96
+
97
+ $ this ->expectException (FailedModPortalRequestException::class);
98
+
99
+ $ instance = new ModPortalService ($ modPortalClient );
100
+ $ instance ->requestMods ($ modNames );
101
+ }
102
+
103
+ public function testRequestModsWithInitialException (): void
104
+ {
105
+ $ modNames = ['abc ' , 'def ' ];
106
+ $ expectedRequest1 = new FullModRequest ();
107
+ $ expectedRequest1 ->setName ('abc ' );
108
+ $ expectedRequest2 = new FullModRequest ();
109
+ $ expectedRequest2 ->setName ('def ' );
110
+ $ mod1 = new Mod ();
111
+ $ mod1 ->setName ('abc ' );
112
+ $ promise1 = new FulfilledPromise ($ mod1 );
113
+
114
+ $ modPortalClient = $ this ->createMock (ClientInterface::class);
115
+ $ modPortalClient ->expects ($ this ->exactly (2 ))
116
+ ->method ('sendRequest ' )
117
+ ->withConsecutive (
118
+ [$ this ->equalTo ($ expectedRequest1 )],
119
+ [$ this ->equalTo ($ expectedRequest2 )],
120
+ )
121
+ ->willReturnOnConsecutiveCalls (
122
+ $ promise1 ,
123
+ $ this ->throwException ($ this ->createMock (ClientException::class)),
124
+ );
125
+
126
+ $ this ->expectException (FailedModPortalRequestException::class);
127
+
128
+ $ instance = new ModPortalService ($ modPortalClient );
129
+ $ instance ->requestMods ($ modNames );
130
+ }
131
+
66
132
public function testSelectLatestReleases (): void
67
133
{
68
134
$ release1 = new Release ();
0 commit comments