44
55namespace Yiisoft \Queue \Tests \Unit \Middleware \Consume ;
66
7- use PHPUnit \Framework \Attributes \DataProvider ;
87use PHPUnit \Framework \TestCase ;
98use Psr \Container \ContainerInterface ;
109use Yiisoft \Test \Support \Container \SimpleContainer ;
@@ -24,13 +23,11 @@ public function testCallableMiddlewareCalled(): void
2423 {
2524 $ request = $ this ->getConsumeRequest ();
2625
27- $ dispatcher = $ this ->createDispatcher ()->withMiddlewares (
28- [
29- static function (ConsumeRequest $ request ): ConsumeRequest {
30- return $ request ->withMessage (new GenericMessage ('test ' , 'New closure test data ' ))->withQueueName ('other-queue ' );
31- },
32- ],
33- );
26+ $ dispatcher = $ this ->createDispatcher (middlewareDefinitions: [
27+ static function (ConsumeRequest $ request ): ConsumeRequest {
28+ return $ request ->withMessage (new GenericMessage ('test ' , 'New closure test data ' ))->withQueueName ('other-queue ' );
29+ },
30+ ]);
3431
3532 $ request = $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
3633 $ this ->assertSame ('New closure test data ' , $ request ->getMessage ()->getPayload ());
@@ -44,7 +41,7 @@ public function testArrayMiddlewareCallableDefinition(): void
4441 TestCallableMiddleware::class => new TestCallableMiddleware (),
4542 ],
4643 );
47- $ dispatcher = $ this ->createDispatcher ($ container)-> withMiddlewares ( [[TestCallableMiddleware::class, 'index ' ]]);
44+ $ dispatcher = $ this ->createDispatcher ($ container, [[TestCallableMiddleware::class, 'index ' ]]);
4845 $ request = $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
4946 $ this ->assertSame ('New test data ' , $ request ->getMessage ()->getPayload ());
5047 }
@@ -57,7 +54,7 @@ public function testFactoryArrayDefinition(): void
5754 'class ' => TestMiddleware::class,
5855 '__construct() ' => ['message ' => 'New test data from the definition ' ],
5956 ];
60- $ dispatcher = $ this ->createDispatcher ($ container)-> withMiddlewares ( [$ definition ]);
57+ $ dispatcher = $ this ->createDispatcher ($ container, [$ definition ]);
6158 $ request = $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
6259 $ this ->assertSame ('New test data from the definition ' , $ request ->getMessage ()->getPayload ());
6360 }
@@ -77,7 +74,7 @@ public function testMiddlewareFullStackCalled(): void
7774 return $ handler ->handleConsume ($ request );
7875 };
7976
80- $ dispatcher = $ this ->createDispatcher ()-> withMiddlewares ( [$ middleware1 , $ middleware2 ]);
77+ $ dispatcher = $ this ->createDispatcher (middlewareDefinitions: [$ middleware1 , $ middleware2 ]);
8178
8279 $ request = $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
8380 $ this ->assertSame ('new test data ' , $ request ->getMessage ()->getPayload ());
@@ -95,56 +92,12 @@ public function testMiddlewareStackInterrupted(): void
9592 return $ request ->withMessage (new GenericMessage ($ request ->getMessage ()->getType (), 'second ' ));
9693 };
9794
98- $ dispatcher = $ this ->createDispatcher ()-> withMiddlewares ( [$ middleware1 , $ middleware2 ]);
95+ $ dispatcher = $ this ->createDispatcher (middlewareDefinitions: [$ middleware1 , $ middleware2 ]);
9996
10097 $ request = $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
10198 $ this ->assertSame ('first ' , $ request ->getMessage ()->getPayload ());
10299 }
103100
104- public static function dataHasMiddlewares (): array
105- {
106- return [
107- [[], false ],
108- [[[TestCallableMiddleware::class, 'index ' ]], true ],
109- ];
110- }
111-
112- #[DataProvider('dataHasMiddlewares ' )]
113- public function testHasMiddlewares (array $ definitions , bool $ expected ): void
114- {
115- self ::assertSame (
116- $ expected ,
117- $ this ->createDispatcher ()->withMiddlewares ($ definitions )->hasMiddlewares (),
118- );
119- }
120-
121- public function testImmutability (): void
122- {
123- $ dispatcher = $ this ->createDispatcher ();
124- self ::assertNotSame ($ dispatcher , $ dispatcher ->withMiddlewares ([]));
125- }
126-
127- public function testResetStackOnWithMiddlewares (): void
128- {
129- $ request = $ this ->getConsumeRequest ();
130- $ container = $ this ->createContainer (
131- [
132- TestCallableMiddleware::class => new TestCallableMiddleware (),
133- TestMiddleware::class => new TestMiddleware (),
134- ],
135- );
136-
137- $ dispatcher = $ this
138- ->createDispatcher ($ container )
139- ->withMiddlewares ([[TestCallableMiddleware::class, 'index ' ]]);
140- $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
141-
142- $ dispatcher = $ dispatcher ->withMiddlewares ([TestMiddleware::class]);
143- $ request = $ dispatcher ->dispatch ($ request , $ this ->getRequestHandler ());
144-
145- self ::assertSame ('New middleware test data ' , $ request ->getMessage ()->getPayload ());
146- }
147-
148101 private function getRequestHandler (): ConsumeHandlerInterface
149102 {
150103 return new class implements ConsumeHandlerInterface {
@@ -157,12 +110,11 @@ public function handleConsume(ConsumeRequest $request): ConsumeRequest
157110
158111 private function createDispatcher (
159112 ?ContainerInterface $ container = null ,
113+ array $ middlewareDefinitions = [],
160114 ): ConsumeMiddlewareDispatcher {
161115 $ container ??= $ this ->createContainer ([AdapterInterface::class => new InMemoryAdapter ()]);
162116
163- return new ConsumeMiddlewareDispatcher (
164- new ConsumeMiddlewareFactory ($ container ),
165- );
117+ return new ConsumeMiddlewareDispatcher (new ConsumeMiddlewareFactory ($ container ), ...$ middlewareDefinitions );
166118 }
167119
168120 private function createContainer (array $ instances = []): ContainerInterface
0 commit comments