15
15
use PHPUnit \Framework \MockObject \MockObject ;
16
16
use PHPUnit \Framework \TestCase ;
17
17
use Psr \Http \Message \ResponseInterface ;
18
+ use Psr \Http \Message \ServerRequestInterface ;
18
19
use Zend \Diactoros \ResponseFactory ;
19
20
use Zend \Diactoros \ServerRequest ;
20
21
@@ -65,15 +66,6 @@ public function createDependencies(): void
65
66
*/
66
67
public function handleShouldExecuteTheCommandAndReturnAnEmptyResponse (): void
67
68
{
68
- $ handler = new CreateAndFetch (
69
- new ExecuteCommand ($ this ->bus , $ this ->creator , 'command ' ),
70
- new ExecuteQuery ($ this ->bus , $ this ->creator , 'query ' ),
71
- new ResponseFactory (),
72
- 'info ' ,
73
- $ this ->uriGenerator ,
74
- $ this ->idGenerator
75
- );
76
-
77
69
$ request = new ServerRequest ();
78
70
$ command = (object ) ['a ' => 'b ' ];
79
71
$ query = (object ) ['c ' => 'd ' ];
@@ -96,12 +88,68 @@ public function handleShouldExecuteTheCommandAndReturnAnEmptyResponse(): void
96
88
->willReturn ('/testing/1 ' );
97
89
98
90
/** @var ResponseInterface|UnformattedResponse $response */
99
- $ response = $ handler -> handle ($ request );
91
+ $ response = $ this -> handleRequest ($ request );
100
92
101
93
self ::assertInstanceOf (UnformattedResponse::class, $ response );
102
94
self ::assertSame (StatusCodeInterface::STATUS_CREATED , $ response ->getStatusCode ());
103
95
self ::assertSame ('/testing/1 ' , $ response ->getHeaderLine ('Location ' ));
104
96
self ::assertSame ([ExecuteQuery::class => 'query ' ], $ response ->getAttributes ());
105
97
self ::assertSame ('result ' , $ response ->getUnformattedContent ());
106
98
}
99
+
100
+ /**
101
+ * @test
102
+ *
103
+ * @covers ::__construct()
104
+ * @covers ::handle()
105
+ * @covers ::generateResponse()
106
+ *
107
+ * @uses \Chimera\Routing\HttpRequest
108
+ */
109
+ public function handleShouldPreserveTheRequestGeneratedIdIfAlreadyPresent (): void
110
+ {
111
+ $ request = (new ServerRequest ())->withAttribute (IdentifierGenerator::class, 2 );
112
+ $ command = (object ) ['a ' => 'b ' ];
113
+ $ query = (object ) ['c ' => 'd ' ];
114
+
115
+ $ this ->creator ->expects (self ::exactly (2 ))
116
+ ->method ('create ' )
117
+ ->willReturn ($ command , $ query );
118
+
119
+ $ this ->bus ->expects (self ::exactly (2 ))
120
+ ->method ('handle ' )
121
+ ->withConsecutive ([$ command ], [$ query ])
122
+ ->willReturn (null , 'result ' );
123
+
124
+ $ this ->idGenerator ->method ('generate ' )
125
+ ->willReturn (1 );
126
+
127
+ $ this ->uriGenerator ->expects (self ::once ())
128
+ ->method ('generateRelativePath ' )
129
+ ->with ($ request , 'info ' )
130
+ ->willReturn ('/testing/2 ' );
131
+
132
+ /** @var ResponseInterface|UnformattedResponse $response */
133
+ $ response = $ this ->handleRequest ($ request );
134
+
135
+ self ::assertInstanceOf (UnformattedResponse::class, $ response );
136
+ self ::assertSame (StatusCodeInterface::STATUS_CREATED , $ response ->getStatusCode ());
137
+ self ::assertSame ('/testing/2 ' , $ response ->getHeaderLine ('Location ' ));
138
+ self ::assertSame ([ExecuteQuery::class => 'query ' ], $ response ->getAttributes ());
139
+ self ::assertSame ('result ' , $ response ->getUnformattedContent ());
140
+ }
141
+
142
+ private function handleRequest (ServerRequestInterface $ request ): ResponseInterface
143
+ {
144
+ $ handler = new CreateAndFetch (
145
+ new ExecuteCommand ($ this ->bus , $ this ->creator , 'command ' ),
146
+ new ExecuteQuery ($ this ->bus , $ this ->creator , 'query ' ),
147
+ new ResponseFactory (),
148
+ 'info ' ,
149
+ $ this ->uriGenerator ,
150
+ $ this ->idGenerator
151
+ );
152
+
153
+ return $ handler ->handle ($ request );
154
+ }
107
155
}
0 commit comments