/** * At original repo */ public function testIssue4() { // Has many -> Belongs to // Should be the same for Has many -> Has one $loader = new Loader(Robot::findFirstById(1), 'Bugs.Robot'); $this->assertEquals($loader->execute()->get()->bugs, array()); }
/** * @requires PHP 5.4 */ public function testManyEagerLoadsAndConstraints() { $manufacturers = Manufacturer::with(array('Robots' => function ($builder) { $builder->where('id < 25'); }, 'Robots.Bugs' => function ($builder) { $builder->limit(2); }, 'Robots.Parts'), array('id < 50')); $this->assertEquals(array_sum(array_map(function ($o) { return count($o->robots); }, $manufacturers)), Robot::count(array('id < 25 AND manufacturer_id < 50'))); $this->assertEquals(array_sum(array_map(function ($o) { $c = 0; foreach ($o->robots as $r) { $c += count($r->bugs); } return $c; }, $manufacturers)), 2); $manufacturers = Manufacturer::with(array('Robots.Bugs' => function ($builder) { $builder->where('id > 10000'); }), array('limit' => 5, 'order' => 'id ASC')); $this->assertEquals(array_sum(array_map(function ($o) { return count($o->robots); }, $manufacturers)), Robot::count(array('manufacturer_id < 6'))); $robots = array(); foreach ($manufacturers as $m) { $robots = array_merge($robots, $m->robots); } $this->assertEquals(array_sum(array_map(function ($o) { return count($o->bugs); }, $robots)), 0); }