示例#1
0
 /**
  * @test
  */
 public function it_applies_premap_callback_to_payload_collection()
 {
     $stringCollection = ["a string", 100, "yet another string"];
     $collection = StringCollection::fromNativeValue($stringCollection);
     $string_cast = Func::prepare('premap', null, function ($item, $key, \Iterator $collection) {
         return (string) $item;
     });
     $collection = $string_cast($collection);
     $this->assertEquals(["a string", "100", "yet another string"], iterator_to_array(new MapIterator($collection, function (string $string) {
         return $string->value();
     })));
 }
示例#2
0
 /**
  * Initializes method of Func passed as first argument with provided arguments.
  * Pass null for arguments that you wanna skip even when you wanna skip the last argument!
  *
  * @example
  * <code>
  *   $doAnythingWithEach = Func::prepare('map', null, function($value) { return do_anything_with($value); });
  *   $result = $doAnythingWithEach([1,2,3]);
  * </code>
  */
 public static function prepare()
 {
     $orgArgs = func_get_args();
     if (count($orgArgs) < 2) {
         throw new \BadMethodCallException('Func::prepare requires at least two arguments. First should be the function to prepare followed by arguments for that function');
     }
     $func = array_shift($orgArgs);
     return function () use($func, $orgArgs) {
         $additionalArgs = func_get_args();
         foreach ($orgArgs as $index => $arg) {
             if (is_null($arg)) {
                 $additionalArg = array_shift($additionalArgs);
                 if (is_null($additionalArg)) {
                     throw new \BadMethodCallException('Parameter mismatch detected for prepared function: ' . (string) $func);
                 }
                 $orgArgs[$index] = $additionalArg;
             }
         }
         return Func::call($func, $orgArgs);
     };
 }
 /**
  * @test
  */
 public function it_pipes_middleware_and_executes_the_pipeline()
 {
     $chapterCommand = $this->prophesize(Message::class);
     $chapterCommand->metadata()->willReturn([Metadata::STORY_CHAPTER => Uuid::uuid4()->toString() . '____1', Metadata::STORY_NAME => 'Test Story']);
     $chapterCommand->messageName()->willReturn('Chapter Command');
     $chapterCommand->uuid()->willReturn(Uuid::uuid4());
     $doneBackend = $this->prophesize(DoneBackend::class);
     $chapterLogger = ChapterLogger::fromChapterCommand($chapterCommand->reveal(), $doneBackend->reveal());
     $workflow = new Workflow();
     $workflow->pipe(function (Message $chapterCommand, array $userData, ChapterLogger $chapterLogger, callable $next) {
         $user = UserDictionary::fromNativeValue($userData);
         return $next($chapterCommand, $user, $chapterLogger);
     });
     $workflow->pipe(function (Message $chapterCommand, UserDictionary $user, ChapterLogger $chapterLogger, callable $next) {
         return $next($chapterCommand, $user->property('address')->type(), $chapterLogger);
     });
     $userData = ['id' => 1, 'name' => 'Alex', 'address' => ['street' => 'Main Street', 'streetNumber' => 10, 'zip' => '12345', 'city' => 'Test City']];
     $addressData = $workflow($chapterCommand->reveal(), $userData, $chapterLogger, function (Message $chapterCommand, AddressDictionary $address, ChapterLogger $chapterLogger) {
         return Func::to_data($address);
     });
     $this->assertEquals(['street' => 'Main Street', 'streetNumber' => 10, 'zip' => '12345', 'city' => 'Test City'], $addressData);
 }