public function testIsNotEmpty() { $e1 = new Entity(['id' => 1]); $e2 = new Entity(['id' => 2]); $collection = new Collection([$e1, $e2]); $this->assertTrue($collection->isNotEmpty()); }
public function testGetRandom() { $collection = new Collection([new Entity(['id' => 1]), new Entity(['id' => 2]), new Entity(['id' => 3]), new Entity(['id' => 4]), new Entity(['id' => 5]), new Entity(['id' => 6]), new Entity(['id' => 7]), new Entity(['id' => 8]), new Entity(['id' => 9]), new Entity(['id' => 10]), new Entity(['id' => 11]), new Entity(['id' => 12]), new Entity(['id' => 13]), new Entity(['id' => 14]), new Entity(['id' => 15]), new Entity(['id' => 16]), new Entity(['id' => 17])]); $value1 = $collection->getRandom(); $value2 = $collection->getRandom(); $this->assertNotEquals($value1, $value2); }
public function testGettingNewCollection() { $collection = new TestCollection(); $this->assertInstanceOf(TestCollection::class, $collection->getNewCollection()); $collection = new Collection(); $this->assertInstanceOf(Collection::class, $collection->getNewCollection()); }
public function testSplit() { $collection = new Collection([$e1 = new Entity(['id' => 1]), $e2 = new Entity(['id' => 2]), $e3 = new Entity(['id' => 3]), $e4 = new Entity(['id' => 4]), $e5 = new Entity(['id' => 5])]); $splitted = $collection->split(2); $this->assertEquals([$e1, $e2, $e3], [$splitted[0][0], $splitted[0][1], $splitted[0][2]]); $this->assertEquals([$e4, $e5], [$splitted[1][0], $splitted[1][1]]); }
public function testAppendingMultipleElement() { $data = [new Entity(['id' => 1]), new Entity(['id' => 2])]; $collection = new Collection($data); $this->assertEquals(2, $collection->count()); $this->assertEquals([['id' => 1], ['id' => 2]], $collection->toArray()); }
public function test() { $e1 = new Entity(['id' => 1]); $collection = new Collection([$e2 = new Entity(['id' => 2]), $e3 = new Entity(['id' => 3])]); $collection->prepend($e1); $this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3]], $collection->toArray()); }
public function testGettingKeys() { $collection = new Collection([new Entity(['id' => 1]), new Entity(['id' => 2])]); $this->assertEquals([0, 1], $collection->getKeys()); $collection['name'] = new Entity(['id' => 3]); $this->assertEquals([0, 1, 'name'], $collection->getKeys()); $this->assertEquals([0, 1, 2], $collection->reindex()->getKeys()); }
public function test() { $collection = new Collection([new Entity(['id' => 1]), new Entity(['id' => 2]), new Entity(['id' => 3])]); $collection->walk(function ($entity) { $entity->id *= $entity->id; }); $this->assertEquals([['id' => 1], ['id' => 4], ['id' => 9]], $collection->toArray()); }
public function testCastingCollectionToArray() { $entity1 = new Entity(['one' => 1]); $entity2 = new Entity(['two' => 2]); $entity3 = new Entity(['three' => 3]); $collection = new Collection([$entity1, $entity2, $entity3]); $this->assertEquals([['one' => 1], ['two' => 2], ['three' => 3]], $collection->toArray()); }
public function testGetRandom() { $collection = new Collection([new Entity(['id' => 1]), new Entity(['id' => 2]), new Entity(['id' => 3]), new Entity(['id' => 4]), new Entity(['id' => 5]), new Entity(['id' => 6]), new Entity(['id' => 7]), new Entity(['id' => 8]), new Entity(['id' => 9]), new Entity(['id' => 10]), new Entity(['id' => 11]), new Entity(['id' => 12]), new Entity(['id' => 13]), new Entity(['id' => 14]), new Entity(['id' => 15]), new Entity(['id' => 16]), new Entity(['id' => 17])]); $v1 = $collection->getRandom(); $v2 = $collection->getRandom(); $v3 = $collection->getRandom(); $v4 = $collection->getRandom(); $this->assertFalse($v1 == $v2 && v2 == $v3 && v3 == $v4); }
public function test() { $collection = new Collection([$e1 = new Entity(['id' => 1]), $e2 = new Entity(['id' => 2]), $e3 = new Entity(['id' => 3])]); $collection->filter(function ($entity) { if ($entity->id == 2) { return false; } return true; }); $this->assertEquals([['id' => 1], ['id' => 3]], $collection->toArray()); }
public function testClear() { $e1 = new Entity(['id' => 1]); $e2 = new Entity(['id' => 2]); $e3 = new Entity(['id' => 3]); $e4 = new Entity(['id' => 4]); $collection = new Collection([$e1, $e2, $e3]); $collection['one'] = $e4; $collection->clear(); $this->assertTrue($collection->isEmpty()); }
public function test() { $one = new Collection([new Entity(['id' => 1]), new Entity(['id' => 2]), new Entity(['id' => 1]), new Entity(['id' => null]), new Entity(['noid' => 1])]); $two = new Collection([new Entity(['id2' => 1]), new Entity(['id2' => 5]), new Entity(['id2' => 1]), new Entity(['id2' => null])]); $one->bindCollection($two, ['id2' => 'id'], 'two'); $this->assertInstanceOf(Collection::class, $one[0]->two); $this->assertInstanceOf(Collection::class, $one[1]->two); $this->assertInstanceOf(Collection::class, $one[2]->two); $this->assertEquals(2, $one[0]->two->count()); $this->assertEquals(0, $one[1]->two->count()); $this->assertEquals(2, $one[2]->two->count()); }
public function testHas() { $entity1 = new Entity(['id' => 1]); $entity2 = new Entity(['id' => 2]); $entity3 = new Entity(['id' => 3]); $collection = new Collection([$entity1, $entity2, $entity3]); $this->assertTrue($collection->has('id', 1)); $this->assertTrue($collection->has('id', 1)); $this->assertTrue($collection->has('id', [1, 5])); $this->assertTrue($collection->has('id', 2)); $this->assertTrue($collection->has('id', 3)); $this->assertFalse($collection->has('id', [4, 5])); $this->assertFalse($collection->has('id', 4)); $this->assertFalse($collection->has('noid', 1)); $this->assertFalse($collection->has('id', '1', true)); }
/** * @expectedException \LogicException */ public function testMagicCallExceptionWhenEmptyCollection() { $collection = new Collection(); $collection->foo(); }
/** * @expectedException LogicException */ public function testCallingInvalidMethodFromEmptyCollection() { $collection = new Collection(); $collection->invalidMethod(); }
public function test() { $collection = new Collection([new Entity(['id' => 1]), new Entity(['id' => 1]), new Entity(['id' => 1]), new Entity(['id' => 2]), new Entity(['id' => 3])]); $collection->distinctOn('id'); $this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3]], $collection->toArray()); }