1313namespace CloudCreativity \Modules \Tests \Unit \Application \Bus \Middleware ;
1414
1515use CloudCreativity \Modules \Application \Bus \Middleware \ValidateCommand ;
16+ use CloudCreativity \Modules \Contracts \Application \Bus \Bail ;
1617use CloudCreativity \Modules \Contracts \Application \Bus \Validator ;
1718use CloudCreativity \Modules \Contracts \Toolkit \Messages \Command ;
1819use CloudCreativity \Modules \Contracts \Toolkit \Result \Result ;
@@ -62,6 +63,11 @@ public function testItSucceeds(): void
6263 }))
6364 ->willReturnSelf ();
6465
66+ $ this ->validator
67+ ->expects ($ this ->once ())
68+ ->method ('stopOnFirstFailure ' )
69+ ->willReturnSelf ();
70+
6571 $ this ->validator
6672 ->expects ($ this ->once ())
6773 ->method ('validate ' )
@@ -85,9 +91,55 @@ public function testItSucceeds(): void
8591 public function testItFails (): void
8692 {
8793 $ this ->validator
94+ ->expects ($ this ->once ())
8895 ->method ('using ' )
8996 ->willReturnSelf ();
9097
98+ $ this ->validator
99+ ->expects ($ this ->once ())
100+ ->method ('stopOnFirstFailure ' )
101+ ->with (false )
102+ ->willReturnSelf ();
103+
104+ $ this ->validator
105+ ->expects ($ this ->once ())
106+ ->method ('validate ' )
107+ ->with ($ command = $ this ->createMock (Command::class))
108+ ->willReturn ($ errors = new ListOfErrors (new Error (null , 'Something went wrong. ' )));
109+
110+ $ next = function () {
111+ throw new \LogicException ('Not expecting next closure to be called. ' );
112+ };
113+
114+ $ result = ($ this ->middleware )($ command , $ next );
115+
116+ $ this ->assertTrue ($ result ->didFail ());
117+ $ this ->assertSame ($ errors , $ result ->errors ());
118+ }
119+
120+ public function testItStopsOnFirstFailureViaBail (): void
121+ {
122+ $ this ->middleware = new class ($ this ->validator ) extends ValidateCommand implements Bail {
123+ /**
124+ * @return iterable<string>
125+ */
126+ protected function rules (): iterable
127+ {
128+ return ['foo ' , 'bar ' ];
129+ }
130+ };
131+
132+ $ this ->validator
133+ ->expects ($ this ->once ())
134+ ->method ('using ' )
135+ ->willReturnSelf ();
136+
137+ $ this ->validator
138+ ->expects ($ this ->once ())
139+ ->method ('stopOnFirstFailure ' )
140+ ->with (true )
141+ ->willReturnSelf ();
142+
91143 $ this ->validator
92144 ->expects ($ this ->once ())
93145 ->method ('validate ' )
@@ -103,4 +155,55 @@ public function testItFails(): void
103155 $ this ->assertTrue ($ result ->didFail ());
104156 $ this ->assertSame ($ errors , $ result ->errors ());
105157 }
158+
159+ public function testItStopsOnFirstFailure (): void
160+ {
161+ $ command = $ this ->createMock (Command::class);
162+
163+ $ this ->middleware = new class ($ command , $ this ->validator ) extends ValidateCommand {
164+ public function __construct (private Command $ command , Validator $ validator )
165+ {
166+ parent ::__construct ($ validator );
167+ }
168+
169+ /**
170+ * @return iterable<string>
171+ */
172+ protected function rules (): iterable
173+ {
174+ return ['foo ' , 'bar ' ];
175+ }
176+
177+ protected function stopOnFirstFailure (Command $ command ): bool
178+ {
179+ return $ this ->command === $ command ;
180+ }
181+ };
182+
183+ $ this ->validator
184+ ->expects ($ this ->once ())
185+ ->method ('using ' )
186+ ->willReturnSelf ();
187+
188+ $ this ->validator
189+ ->expects ($ this ->once ())
190+ ->method ('stopOnFirstFailure ' )
191+ ->with (true )
192+ ->willReturnSelf ();
193+
194+ $ this ->validator
195+ ->expects ($ this ->once ())
196+ ->method ('validate ' )
197+ ->with ($ command )
198+ ->willReturn ($ errors = new ListOfErrors (new Error (null , 'Something went wrong. ' )));
199+
200+ $ next = function () {
201+ throw new \LogicException ('Not expecting next closure to be called. ' );
202+ };
203+
204+ $ result = ($ this ->middleware )($ command , $ next );
205+
206+ $ this ->assertTrue ($ result ->didFail ());
207+ $ this ->assertSame ($ errors , $ result ->errors ());
208+ }
106209}
0 commit comments