private function populate()
 {
     // engines
     $v8 = new Engine();
     $v8->setType('V8');
     $v8->setValves(8);
     $this->em->persist($v8);
     $v6 = new Engine();
     $v6->setType('V6');
     $v6->setValves(8);
     $this->em->persist($v6);
     $this->em->flush();
     // cars
     $audi80 = new Car();
     $audi80->setEngine($v8);
     $audi80->setTitle('Audi-80');
     $this->em->persist($audi80);
     $audi80s = new Car();
     $audi80s->setParent($audi80);
     $audi80s->setTitle('Audi-80s');
     $audi80s->setEngine($v8);
     $this->em->persist($audi80s);
     $icarus = new Bus();
     $icarus->setEngine($v8);
     $icarus->setTitle('Icarus');
     $this->em->persist($icarus);
     $audiJet = new Car();
     $audiJet->setParent($audi80);
     $audiJet->setTitle('Audi-jet');
     $audiJet->setEngine($v6);
     $this->em->persist($audiJet);
     $today = \DateTime::createFromFormat(self::TRAVEL_DATE_FORMAT, self::TODAY);
     $tomorrow = \DateTime::createFromFormat(self::TRAVEL_DATE_FORMAT, self::TOMORROW);
     for ($i = 0; $i < self::SEATS; $i++) {
         $reservationBratislava = new Reservation();
         $reservationBratislava->setBus($icarus);
         $reservationBratislava->setDestination('Bratislava');
         $reservationBratislava->setTravelDate($today);
         $reservationBratislava->setName('Bratislava Today ' . $i);
         $this->em->persist($reservationBratislava);
         $reservationBratislava = new Reservation();
         $reservationBratislava->setBus($icarus);
         $reservationBratislava->setDestination('Bratislava');
         $reservationBratislava->setTravelDate($tomorrow);
         $reservationBratislava->setName('Bratislava Tomorrow ' . $i);
         $this->em->persist($reservationBratislava);
         $reservationPrague = new Reservation();
         $reservationPrague->setBus($icarus);
         $reservationPrague->setDestination('Prague');
         $reservationPrague->setTravelDate($today);
         $reservationPrague->setName('Prague Today ' . $i);
         $this->em->persist($reservationPrague);
     }
     $categoryVehicle = new Category();
     $categoryVehicle->setName('Vehicle');
     $this->em->persist($categoryVehicle);
     $categoryAccessory = new Category();
     $categoryAccessory->setName('Accessory');
     $this->em->persist($categoryAccessory);
     for ($i = 1; $i <= 60; $i++) {
         $item = new Item();
         $item->setName('Item ' . $i);
         if ($i <= 30) {
             $item->setCategory($categoryVehicle);
             $item->setPosition($i);
         } else {
             $item->setCategory($categoryAccessory);
             $item->setPosition($i - 30);
         }
         $this->em->persist($item);
     }
     $this->em->flush();
 }
 /**
  * @test
  */
 public function shouldGroupByNewAssociation()
 {
     $category1 = new Category();
     $category1->setName("Category1");
     $item1 = new Item();
     $item1->setName("Item1");
     $item1->setPosition(0);
     $item1->setCategory($category1);
     $this->em->persist($item1);
     $this->em->persist($category1);
     $this->em->flush();
     $repo = $this->em->getRepository(self::CATEGORY);
     $category1 = $repo->findOneByName('Category1');
     $repo = $this->em->getRepository(self::ITEM);
     $items = $repo->getBySortableGroups(array('category' => $category1));
     $this->assertEquals("Item1", $items[0]->getName());
     $this->assertEquals("Category1", $items[0]->getCategory()->getName());
 }
 public function testGroupByAssociation()
 {
     $category1 = new Category();
     $category1->setName("Category1");
     $this->em->persist($category1);
     $category2 = new Category();
     $category2->setName("Category2");
     $this->em->persist($category2);
     $this->em->flush();
     $item3 = new Item();
     $item3->setName("Item3");
     $item3->setCategory($category1);
     $this->em->persist($item3);
     $item4 = new Item();
     $item4->setName("Item4");
     $item4->setCategory($category1);
     $this->em->persist($item4);
     $this->em->flush();
     $item1 = new Item();
     $item1->setName("Item1");
     $item1->setPosition(0);
     $item1->setCategory($category1);
     $this->em->persist($item1);
     $item2 = new Item();
     $item2->setName("Item2");
     $item2->setPosition(0);
     $item2->setCategory($category1);
     $this->em->persist($item2);
     $item2 = new Item();
     $item2->setName("Item2_2");
     $item2->setPosition(0);
     $item2->setCategory($category2);
     $this->em->persist($item2);
     $this->em->flush();
     $item1 = new Item();
     $item1->setName("Item1_2");
     $item1->setPosition(0);
     $item1->setCategory($category2);
     $this->em->persist($item1);
     $this->em->flush();
     $this->em->clear();
     $repo = $this->em->getRepository('Sortable\\Fixture\\Category');
     $category1 = $repo->findOneByName('Category1');
     $category2 = $repo->findOneByName('Category2');
     $repo = $this->em->getRepository('Sortable\\Fixture\\Item');
     $items = $repo->getBySortableGroups(array('category' => $category1));
     $this->assertEquals("Item1", $items[0]->getName());
     $this->assertEquals("Category1", $items[0]->getCategory()->getName());
     $this->assertEquals("Item2", $items[1]->getName());
     $this->assertEquals("Category1", $items[1]->getCategory()->getName());
     $this->assertEquals("Item3", $items[2]->getName());
     $this->assertEquals("Category1", $items[2]->getCategory()->getName());
     $this->assertEquals("Item4", $items[3]->getName());
     $this->assertEquals("Category1", $items[3]->getCategory()->getName());
     $items = $repo->getBySortableGroups(array('category' => $category2));
     $this->assertEquals("Item1_2", $items[0]->getName());
     $this->assertEquals("Category2", $items[0]->getCategory()->getName());
     $this->assertEquals("Item2_2", $items[1]->getName());
     $this->assertEquals("Category2", $items[1]->getCategory()->getName());
 }