9
9
use Http \Client \Exception \HttpException ;
10
10
use Http \HttplugBundle \Collector \Collector ;
11
11
use Http \HttplugBundle \Collector \Formatter ;
12
- use Http \HttplugBundle \Collector \Stack ;
13
12
use Http \HttplugBundle \Collector \StackPlugin ;
13
+ use Http \Message \Formatter as MessageFormatter ;
14
14
use Http \Promise \FulfilledPromise ;
15
15
use Http \Promise \RejectedPromise ;
16
16
use PHPUnit \Framework \Error \Warning ;
@@ -52,75 +52,43 @@ class StackPluginTest extends TestCase
52
52
53
53
public function setUp (): void
54
54
{
55
- $ this ->collector = $ this ->getMockBuilder (Collector::class)->disableOriginalConstructor ()->getMock ();
56
- $ this ->formatter = $ this ->getMockBuilder (Formatter::class)->disableOriginalConstructor ()->getMock ();
55
+ $ this ->collector = new Collector ();
56
+ $ messageFormatter = $ this ->createMock (MessageFormatter::class);
57
+ $ this ->formatter = new Formatter ($ messageFormatter , $ this ->createMock (MessageFormatter::class));
57
58
$ this ->request = new Request ('GET ' , '/ ' );
58
59
$ this ->response = new Response ();
59
60
$ this ->exception = new HttpException ('' , $ this ->request , $ this ->response );
60
61
61
- $ this -> formatter
62
+ $ messageFormatter
62
63
->method ('formatRequest ' )
63
64
->with ($ this ->request )
64
65
->willReturn ('FormattedRequest ' )
65
66
;
66
67
67
- $ this -> formatter
68
+ $ messageFormatter
68
69
->method ('formatResponse ' )
69
70
->with ($ this ->response )
70
71
->willReturn ('FormattedResponse ' )
71
72
;
72
73
73
- $ this ->formatter
74
- ->method ('formatException ' )
75
- ->with ($ this ->exception )
76
- ->willReturn ('FormattedException ' )
77
- ;
78
-
79
74
$ this ->subject = new StackPlugin ($ this ->collector , $ this ->formatter , 'default ' );
80
75
}
81
76
82
77
public function testStackIsInitialized (): void
83
78
{
84
- $ this ->collector
85
- ->expects ($ this ->once ())
86
- ->method ('addStack ' )
87
- ->with ($ this ->callback (function (Stack $ stack ) {
88
- $ this ->assertEquals ('default ' , $ stack ->getClient ());
89
- $ this ->assertEquals ('FormattedRequest ' , $ stack ->getRequest ());
90
-
91
- return true ;
92
- }))
93
- ;
94
- $ this ->collector
95
- ->expects ($ this ->once ())
96
- ->method ('activateStack ' )
97
- ;
98
-
99
79
$ next = function () {
100
80
return new FulfilledPromise ($ this ->response );
101
81
};
102
82
103
83
$ this ->subject ->handleRequest ($ this ->request , $ next , function (): void {
104
84
});
85
+ $ stack = $ this ->collector ->getActiveStack ();
86
+ $ this ->assertEquals ('default ' , $ stack ->getClient ());
87
+ $ this ->assertEquals ('FormattedRequest ' , $ stack ->getRequest ());
105
88
}
106
89
107
90
public function testOnFulfilled (): void
108
91
{
109
- //Capture the current stack
110
- $ currentStack = null ;
111
- $ this ->collector
112
- ->method ('addStack ' )
113
- ->with ($ this ->callback (function (Stack $ stack ) use (&$ currentStack ) {
114
- $ currentStack = $ stack ;
115
-
116
- return true ;
117
- }))
118
- ;
119
- $ this ->collector
120
- ->expects ($ this ->once ())
121
- ->method ('deactivateStack ' )
122
- ;
123
-
124
92
$ next = function () {
125
93
return new FulfilledPromise ($ this ->response );
126
94
};
@@ -129,45 +97,27 @@ public function testOnFulfilled(): void
129
97
});
130
98
131
99
$ this ->assertEquals ($ this ->response , $ promise ->wait ());
132
- $ this ->assertInstanceOf (Stack::class, $ currentStack );
100
+ $ currentStack = $ this ->collector -> getActiveStack ( );
133
101
$ this ->assertEquals ('FormattedResponse ' , $ currentStack ->getResponse ());
134
102
}
135
103
136
104
public function testOnRejected (): void
137
105
{
138
- //Capture the current stack
139
- $ currentStack = null ;
140
- $ this ->collector
141
- ->method ('addStack ' )
142
- ->with ($ this ->callback (function (Stack $ stack ) use (&$ currentStack ) {
143
- $ currentStack = $ stack ;
144
-
145
- return true ;
146
- }))
147
- ;
148
- $ this ->collector
149
- ->expects ($ this ->once ())
150
- ->method ('deactivateStack ' )
151
- ;
152
-
153
106
$ next = function () {
154
107
return new RejectedPromise ($ this ->exception );
155
108
};
156
109
157
110
$ promise = $ this ->subject ->handleRequest ($ this ->request , $ next , function (): void {
158
111
});
159
112
160
- $ this ->expectException (\Exception::class);
161
- $ promise ->wait ();
113
+ $ this ->assertEquals ($ this ->exception , $ promise ->wait ());
114
+ $ currentStack = $ this ->collector ->getActiveStack ();
115
+ $ this ->assertEquals ('FormattedResponse ' , $ currentStack ->getResponse ());
116
+ $ this ->assertTrue ($ currentStack ->isFailed ());
162
117
}
163
118
164
119
public function testOnException (): void
165
120
{
166
- $ this ->collector
167
- ->expects ($ this ->once ())
168
- ->method ('deactivateStack ' )
169
- ;
170
-
171
121
$ next = function (): void {
172
122
throw new \Exception ();
173
123
};
@@ -185,11 +135,6 @@ public function testOnError(): void
185
135
$ this ->expectException (Warning::class);
186
136
}
187
137
188
- $ this ->collector
189
- ->expects ($ this ->once ())
190
- ->method ('deactivateStack ' )
191
- ;
192
-
193
138
$ next = function () {
194
139
return 2 / 0 ;
195
140
};
0 commit comments