/** * @test */ function it_can_be_set_up_from_array() { $task = ManipulatePayload::with(__DIR__ . '/../../Mock/manipulation/append_world.php'); $taskData = $task->getArrayCopy(); $copiedTask = ManipulatePayload::reconstituteFromArray($taskData); $payload = Payload::fromType(String::fromNativeValue('Hello')); $copiedTask->performManipulationOn($payload); $this->assertEquals('Hello World', $payload->extractTypeData()); }
/** * Transforms current message to a data collected event and replaces payload data with collected data * * @param Type $collectedData * @param array $metadata * @throws \Prooph\Processing\Type\Exception\InvalidTypeException If answer type does not match with the previous requested type * @return WorkflowMessage */ public function answerWith(Type $collectedData, array $metadata = []) { $collectedPayload = Payload::fromType($collectedData); $collectedDataTypeClass = $collectedPayload->getTypeClass(); if ($this->payload->getTypeClass() !== $collectedPayload->getTypeClass()) { throw InvalidTypeException::fromInvalidArgumentExceptionAndPrototype(new \InvalidArgumentException(sprintf("Type %s of collected data does not match the type of requested data %s", $collectedPayload->getTypeClass(), $this->payload->getTypeClass())), $collectedDataTypeClass::prototype()); } $type = MessageNameUtils::getTypePartOfMessageName($this->messageName); $metadata = ArrayUtils::merge($this->metadata, $metadata); return new self($collectedPayload, MessageNameUtils::getDataCollectedEventName($type), $this->target, $this->origin, $metadata, $this->processTaskListPosition, $this->version + 1); }
/** * Applies callback to payload type if type is a collection * The callback is applied before the collection calls its own logic * on the current item. This allows you to prepare an item (change structure f.e.) * so that the collection can translate the item to its type representation * * @param Payload $payload * @param $callback * @return \Prooph\Processing\Message\Payload */ public static function premap(Payload $payload, $callback) { $type = $payload->toType(); if ($type instanceof \OuterIterator) { $payload->replaceType($type::fromNativeValue(new MapIterator($type->getInnerIterator(), $callback))); } return $payload; }
/** * @test */ public function it_applies_premap_callback_to_payload_collection() { $stringCollection = ["a string", 100, "yet another string"]; $collection = StringCollection::fromNativeValue($stringCollection); $payload = Payload::fromType($collection); $string_cast = Func::prepare('premap', null, function ($item, $key, \Iterator $collection) { return (string) $item; }); $string_cast($payload); $this->assertEquals(["a string", "100", "yet another string"], $payload->extractTypeData()); }
/** * @test */ public function it_encodes_and_decodes_payload_with_prototype_to_and_from_json() { $payload = Payload::fromPrototype(UserDictionary::prototype()); $jsonString = json_encode($payload); $jsonDecodedData = json_decode($jsonString, true); $decodedPayload = Payload::fromJsonDecodedData($jsonDecodedData); $this->assertEquals('Prooph\\ProcessingTest\\Mock\\UserDictionary', $decodedPayload->getTypeClass()); }