Ejemplo n.º 1
0
 public function testCombineMultiple()
 {
     /* Given */
     $data = [Data::build(['foo', 1], ['foo' => 'bar']), Data::build(['foo', 1], ['name' => 'bar2']), Data::build(['foo', 2], ['foo2' => 'bar']), Data::build(['foo', 2], ['name2' => 'bar2'])];
     /* When */
     $result = DataCombiner::combine($data);
     /* Then */
     assertThat(count($result), is(2));
     assertThat($result[0]->getValue(), is(['foo' => 'bar', 'name' => 'bar2']));
     assertThat($result[1]->getValue(), is(['foo2' => 'bar', 'name2' => 'bar2']));
 }
Ejemplo n.º 2
0
 /**
  * Combines all available data
  * @param Data[] $data
  * @return Data[]
  */
 public static function combine(array $data)
 {
     /** @var Data[] $result */
     $result = [];
     foreach ($data as $item) {
         $hash = self::toHash($item);
         if ($hash === null) {
             continue;
         }
         if (!isset($result[$hash])) {
             $result[$hash] = Data::build($item->getPath(), []);
         }
         $value = $result[$hash]->getValue();
         $value = self::merge($item, $value);
         $result[$hash]->setValue($value);
     }
     return array_values($result);
 }
Ejemplo n.º 3
0
 public function testMultipleReferencesShouldCallOnce()
 {
     /* Given */
     $dug = \Mockery::mock(Dug::class);
     $source = Source::build(['users', '/[0-9]+/'], function () {
     });
     $dug->shouldReceive('sourceForPath')->once()->andReturn($source);
     $user1 = Data::build(['users', 1], ['id' => 1, 'name' => 'Joris']);
     $user2 = Data::build(['users', 2], ['id' => 2, 'name' => 'Jisca']);
     $dug->shouldReceive('data')->once()->andReturn([$user1, $user2]);
     $input = [Data::build(['foo', 1], ['user' => new Reference(['users', 1])]), Data::build(['foo', 2], ['user' => new Reference(['users', 2])])];
     /* When */
     $referenceResolver = new ReferenceResolver($dug);
     $result = $referenceResolver->process($input);
     /* Then */
     assertThat(count($result), is(2));
     assertThat($result[0]->getValue()['user'], is([$user1]));
     assertThat($result[1]->getValue()['user'], is([$user2]));
 }
Ejemplo n.º 4
0
 public function testReferenceListToMultiple()
 {
     /* Given */
     $dug = new Dug();
     $source = Source::build(['pets', '/[0-9]+/'], function (array $path) {
         return [Data::build(['pets', 1], ['name' => 'Fluffy']), Data::build(['pets', 2], ['name' => 'Loesje'])];
     });
     $dug->register($source);
     $source = Source::build(['users', '/[0-9]+/'], function (array $path) {
         return [Data::build(['users', 123], ['id' => 123, 'pets' => new Reference(['pets', [1, 2]])])];
     });
     $dug->register($source);
     /* When */
     $result = $dug->fetch(['users', [123]]);
     /* Then */
     assertThat($result, is([['id' => 123, 'pets' => [['name' => 'Fluffy'], ['name' => 'Loesje']]]]));
 }
Ejemplo n.º 5
0
 /**
  * @param array $path
  * @return array
  */
 public function handle(array $path) : array
 {
     return [Data::build(['users', 1], ['id' => 1, 'source' => 'UserProvider'])];
 }