public function testGetTopCouple() { $collection = new CandyCanePlantCollection([new CandyCanePlant(1), new CandyCanePlant(3), new CandyCanePlant(2)]); $couple = $collection->getTopCouple(); $this->assertInstanceOf(PlantCouple::class, $couple); $this->assertAttributeContainsOnly(PlantCouple::class, 'yields', $collection); $this->assertInstanceOf(CandyCanePlant::class, $couple->getFather()); $this->assertInstanceOf(CandyCanePlant::class, $couple->getMother()); $this->assertEquals(3, $couple->getFather()->getYield()); $this->assertEquals(2, $couple->getMother()->getYield()); }
* File index.php * * @author Edward Pfremmer <*****@*****.**> */ require_once 'vendor/autoload.php'; use PHPWeekly\Collection\CandyCanePlantCollection; use PHPWeekly\Service\CandyCanePlantBreederService; use PHPWeekly\Plant\CandyCanePlant; $breederService = new CandyCanePlantBreederService(); $plantCollection = new CandyCanePlantCollection([new CandyCanePlant(5), new CandyCanePlant(3), new CandyCanePlant(2)]); /* * To convert this to be able to calculate yield loss due to time this would * need to be called recursively. Each iteration representing 1 weeks time. * * Plant offspring would be pushed to a separate collection, each previous generation * plant's yield would be decremented by one with each iteration before mergine the * two collections back together for the next iteration. */ while ($couple = $plantCollection->getTopCouple()) { $offspring = $breederService->breed($couple->getFather(), $couple->getMother()); $plantCollection->push($offspring); } // calculate total yield $yield = $plantCollection->reduce(function ($result, CandyCanePlant $plant) { return $result + $plant->getYield(); }); // print results $plantCollection->each(function (CandyCanePlant $plant) { echo $plant . PHP_EOL; }); echo sprintf('Total Yield: %s', $yield) . PHP_EOL;