Exemplo n.º 1
0
 /**
  * @dataProvider mappedMiddlewaresProvider
  *
  * @param array|string $mappedMiddlewares
  * @param int          $expectedCounter
  */
 public function testDispatchesMappedMiddlewares($mappedMiddlewares, int $expectedCounter)
 {
     $container = $this->prophesize(ContainerInterface::class);
     $middleware = new WorkerMiddleware(['message-name' => $mappedMiddlewares], $container->reveal());
     $request = $this->createRequest();
     $response = new Response();
     if (is_string($mappedMiddlewares)) {
         $mappedMiddlewares = (array) $mappedMiddlewares;
     }
     foreach ($mappedMiddlewares as $mappedMiddleware) {
         $container->get($mappedMiddleware)->shouldBeCalled()->willReturn([$this, 'incrementMiddleware']);
     }
     $out = function ($request, ResponseInterface $response) use($expectedCounter) {
         $this->assertEquals('default-queue', $request->getAttribute(WorkerMiddleware::MATCHED_QUEUE_ATTRIBUTE));
         $this->assertEquals('123abc', $request->getAttribute(WorkerMiddleware::MESSAGE_ID_ATTRIBUTE));
         $this->assertEquals('message-name', $request->getAttribute(WorkerMiddleware::MESSAGE_NAME_ATTRIBUTE));
         $this->assertEquals(['id' => 123], $request->getAttribute(WorkerMiddleware::MESSAGE_PAYLOAD_ATTRIBUTE));
         $this->assertEquals($expectedCounter, $request->getAttribute('counter', 0));
         $this->assertEquals($expectedCounter, $response->hasHeader('counter') ? $response->getHeaderLine('counter') : 0);
         return $response->withAddedHeader('foo', 'bar');
     };
     /** @var ResponseInterface $returnedResponse */
     $returnedResponse = $middleware($request, $response, $out);
     $this->assertEquals('bar', $returnedResponse->getHeaderLine('foo'), 'Make sure that $out was called');
     $this->assertEquals('ZfrEbWorker', $returnedResponse->getHeaderLine('X-HANDLED-BY'), 'Make sure that it adds the X-HANDLED-BY header');
 }