/**
  * @covers ::onDispatchError
  */
 public function testOnDispatchErrorStoreAndStreamImage()
 {
     $id = 'someId';
     $resource = 'someResource';
     $this->event->setError(Application::ERROR_ROUTER_NO_MATCH);
     $image = $this->getMockBuilder(ImageEntity::class)->setMethods(['getLength', 'getResource'])->getMock();
     $image->setId($id);
     $image->setType('image/jpeg');
     $image->setName('image.jpg');
     $image->method('getLength')->willReturn(1024);
     $image->method('getResource')->willReturn($resource);
     $this->manager->expects($this->once())->method('matchUri')->willReturn($id);
     $this->repository->expects($this->once())->method('find')->with($this->equalTo($id))->willReturn($image);
     $this->manager->expects($this->once())->method('store')->with($this->identicalTo($image));
     $this->listener->onDispatchError($this->event);
     $response = $this->event->getResponse();
     $this->assertInstanceOf(Stream::class, $response);
     $this->assertEquals(Response::STATUS_CODE_200, $response->getStatusCode());
     $this->assertEquals($image->getName(), $response->getStreamName());
     $this->assertEquals($image->getResource(), $response->getStream());
     $headers = $response->getHeaders();
     $this->assertInstanceOf(Headers::class, $headers);
     $this->assertTrue($headers->has('Content-Type'));
     $this->assertEquals($image->getType(), $headers->get('Content-Type')->getFieldValue());
     $this->assertTrue($headers->has('Content-Length'));
     $this->assertEquals($image->getLength(), $headers->get('Content-Length')->getFieldValue());
 }
Ejemplo n.º 2
-1
 /**
  * @param MvcEvent $event
  */
 public function onDispatchError(MvcEvent $event)
 {
     if (Application::ERROR_ROUTER_NO_MATCH != $event->getError()) {
         // ignore other than 'no route' errors
         return;
     }
     // get URI stripped of a base URL
     $request = $event->getRequest();
     $uri = str_replace($request->getBaseUrl(), '', $request->getRequestUri());
     // try get image ID from URI
     $id = $this->manager->matchUri($uri);
     if (!$id) {
         // abort if URI does not match
         return;
     }
     // try get image from repository
     $image = $this->repository->find($id);
     if (!$image) {
         // abort if image does not exist
         return;
     }
     // store image
     $this->manager->store($image);
     // return image in response as a stream
     $headers = new Headers();
     $headers->addHeaders(['Content-Type' => $image->getType(), 'Content-Length' => $image->getLength()]);
     $response = new Stream();
     $response->setStatusCode(Response::STATUS_CODE_200);
     $response->setStream($image->getResource());
     $response->setStreamName($image->getName());
     $response->setHeaders($headers);
     $event->setResponse($response);
 }