public function test_constructor_withProperParams_populateBodyAndPropertiesProperly() { $contentType = MessageInterface::CONTENT_TYPE_PLAIN_TEXT; $properties = [MessageInterface::PROPERTY_CONTENT_TYPE => $contentType]; $message = new RpcRequestMessage('', $properties); // timestamp $this->assertFalse(empty($message->getTimestamp()), 'Empty timestamp!'); $this->assertTrue(is_int($message->getTimestamp()), 'Timestamp is not int!'); // type $this->assertFalse(empty($message->getType()), 'Empty type!'); $this->assertEquals(MessageInterface::TYPE_RPC_REQUEST, $message->getType(), sprintf('Type is not "%s"!', MessageInterface::TYPE_RPC_REQUEST)); // correlation id $this->assertFalse(empty($message->getCorrelationId()), 'Empty correlation id!'); // content type $this->assertFalse(empty($message->getContentType()), 'Empty content type!'); $this->assertEquals(MessageInterface::CONTENT_TYPE_PLAIN_TEXT, $message->getContentType(), sprintf('Content type is not "%s"', $contentType)); // body $this->assertTrue(empty($message->getBody()), 'Body is not empty!'); }
/** * Strategy: preCallback() -> callback() -> postCallback() * * @param RpcRequestMessage $request */ protected final function onRequest(RpcRequestMessage $request) { // Reset possible previous values $this->currentRequest = null; try { $this->logger->notice('Message received!', ['body' => $request->getBody(), 'properties' => $request->getProperties()]); $this->currentRequest = $request; // Create an empty response $this->currentResponse = MessagesBuilder::emptyRpcResponse($this->getSerializer()->getSerializedContentType(), $this->currentRequest->getCorrelationId()); // Execute callback strategy $this->preCallback(); $this->callback(); $this->postCallback(); // Send response $this->sendResponse($this->currentResponse); // Send ACK for request $this->connector->basicAck($request); } catch (RemoteProcedureException $e) { $this->logger->warning($e->getMessage()); $responsePayload = RpcResponsePayload::create()->addError('Bad request! ' . $e->getMessage()); $this->currentResponse->setPayload($responsePayload); // Send response $this->sendResponse($this->currentResponse); // Reject and drop conflictive message $this->connector->basicReject($request); } catch (MessageException $e) { $this->logger->warning($e->getMessage()); $responsePayload = RpcResponsePayload::create()->addError('Bad request! ' . $e->getMessage()); $this->currentResponse->setPayload($responsePayload); // Send response $this->sendResponse($this->currentResponse); // Reject and drop conflictive message $this->connector->basicReject($request); } catch (RpcExecutionException $e) { $this->logger->warning($e->getMessage()); $responsePayload = RpcResponsePayload::create()->addError('Execution error! ' . $e->getMessage()); $this->currentResponse->setPayload($responsePayload); // Send response $this->sendResponse($this->currentResponse); // Reject and drop conflictive message $this->connector->basicReject($request); } catch (\Exception $e) { $this->logger->error(sprintf('Unexpected error! [File: %s, Line: %s]: %s ', $e->getFile(), $e->getLine(), $e->getMessage())); // Reject and drop conflictive message $this->connector->basicReject($request); } }