Ejemplo n.º 1
0
 /**
  * @covers ::add
  */
 public function testAdd()
 {
     $models = new RepoModels(Model::getRepo());
     $model = new Model();
     $models->add($model);
     $this->assertSame([$model], Objects::toArray($models->all()));
 }
Ejemplo n.º 2
0
 /**
  * @covers ::groupBy
  */
 public function testGroupBy()
 {
     $callback = function ($item) {
         return $item->id;
     };
     $group1 = new TestObject(1);
     $group2 = new TestObject(2);
     $obj1 = new TestObject($group1);
     $obj2 = new TestObject($group1);
     $obj3 = new TestObject($group2);
     $objects = new SplObjectStorage();
     $objects->attach($obj1);
     $objects->attach($obj2);
     $objects->attach($obj3);
     $expected = new SplObjectStorage();
     $expected[$group1] = new SplObjectStorage();
     $expected[$group1]->attach($obj1);
     $expected[$group1]->attach($obj2);
     $expected[$group2] = new SplObjectStorage();
     $expected[$group2]->attach($obj3);
     $this->assertEquals($expected, Objects::groupBy($objects, $callback));
 }
Ejemplo n.º 3
0
 /**
  * @covers ::byRepo
  */
 public function testByRepo()
 {
     $source = [0 => new Model(), 1 => new Model(), 2 => new SoftDeleteModel(), 3 => new Model(), 4 => new SoftDeleteModel()];
     $models = new Models($source);
     $expected = [[Model::getRepo(), [$source[0], $source[1], $source[3]]], [SoftDeleteModel::getRepo(), [$source[2], $source[4]]]];
     $i = 0;
     $models->byRepo(function ($repo, Models $repoModels) use($expected, &$i) {
         $this->assertSame($expected[$i][0], $repo);
         $this->assertSame($expected[$i][1], Objects::toArray($repoModels->all()));
         $i++;
     });
 }
Ejemplo n.º 4
0
 /**
  * @return array
  */
 public function getIds()
 {
     return Objects::invoke($this->models, 'getId');
 }
Ejemplo n.º 5
0
 /**
  * @covers ::getSerializers
  * @covers ::addSerializers
  */
 public function testSerializers()
 {
     $repo = new Repo(__NAMESPACE__ . '\\Model');
     $this->assertInstanceof('Harp\\Serializer\\Serializers', $repo->getSerializers());
     $serializers = [new Json('profile')];
     $repo->addSerializers($serializers);
     $this->assertSame($serializers, Objects::toArray($repo->getSerializers()->all()));
 }
Ejemplo n.º 6
0
 /**
  * @param  SplObjectStorage $storage
  * @param  Closure          $filter
  * @return SplObjectStorage
  */
 public static function sort(SplObjectStorage $storage, Closure $filter)
 {
     $array = iterator_to_array($storage);
     usort($array, $filter);
     return Objects::fromArray($array);
 }