99use Http \Client \Exception \HttpException ;
1010use Http \HttplugBundle \Collector \Collector ;
1111use Http \HttplugBundle \Collector \Formatter ;
12- use Http \HttplugBundle \Collector \Stack ;
1312use Http \HttplugBundle \Collector \StackPlugin ;
13+ use Http \Message \Formatter as MessageFormatter ;
1414use Http \Promise \FulfilledPromise ;
1515use Http \Promise \RejectedPromise ;
1616use PHPUnit \Framework \Error \Warning ;
@@ -52,75 +52,43 @@ class StackPluginTest extends TestCase
5252
5353 public function setUp (): void
5454 {
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));
5758 $ this ->request = new Request ('GET ' , '/ ' );
5859 $ this ->response = new Response ();
5960 $ this ->exception = new HttpException ('' , $ this ->request , $ this ->response );
6061
61- $ this -> formatter
62+ $ messageFormatter
6263 ->method ('formatRequest ' )
6364 ->with ($ this ->request )
6465 ->willReturn ('FormattedRequest ' )
6566 ;
6667
67- $ this -> formatter
68+ $ messageFormatter
6869 ->method ('formatResponse ' )
6970 ->with ($ this ->response )
7071 ->willReturn ('FormattedResponse ' )
7172 ;
7273
73- $ this ->formatter
74- ->method ('formatException ' )
75- ->with ($ this ->exception )
76- ->willReturn ('FormattedException ' )
77- ;
78-
7974 $ this ->subject = new StackPlugin ($ this ->collector , $ this ->formatter , 'default ' );
8075 }
8176
8277 public function testStackIsInitialized (): void
8378 {
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-
9979 $ next = function () {
10080 return new FulfilledPromise ($ this ->response );
10181 };
10282
10383 $ this ->subject ->handleRequest ($ this ->request , $ next , function (): void {
10484 });
85+ $ stack = $ this ->collector ->getActiveStack ();
86+ $ this ->assertEquals ('default ' , $ stack ->getClient ());
87+ $ this ->assertEquals ('FormattedRequest ' , $ stack ->getRequest ());
10588 }
10689
10790 public function testOnFulfilled (): void
10891 {
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-
12492 $ next = function () {
12593 return new FulfilledPromise ($ this ->response );
12694 };
@@ -129,45 +97,27 @@ public function testOnFulfilled(): void
12997 });
13098
13199 $ this ->assertEquals ($ this ->response , $ promise ->wait ());
132- $ this ->assertInstanceOf (Stack::class, $ currentStack );
100+ $ currentStack = $ this ->collector -> getActiveStack ( );
133101 $ this ->assertEquals ('FormattedResponse ' , $ currentStack ->getResponse ());
134102 }
135103
136104 public function testOnRejected (): void
137105 {
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-
153106 $ next = function () {
154107 return new RejectedPromise ($ this ->exception );
155108 };
156109
157110 $ promise = $ this ->subject ->handleRequest ($ this ->request , $ next , function (): void {
158111 });
159112
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 ());
162117 }
163118
164119 public function testOnException (): void
165120 {
166- $ this ->collector
167- ->expects ($ this ->once ())
168- ->method ('deactivateStack ' )
169- ;
170-
171121 $ next = function (): void {
172122 throw new \Exception ();
173123 };
@@ -185,11 +135,6 @@ public function testOnError(): void
185135 $ this ->expectException (Warning::class);
186136 }
187137
188- $ this ->collector
189- ->expects ($ this ->once ())
190- ->method ('deactivateStack ' )
191- ;
192-
193138 $ next = function () {
194139 return 2 / 0 ;
195140 };
0 commit comments