/** * Creates and returns the HTML used to display the provided history, including the exercises * for each workout, a comparison with previous workouts and the javascript data. * * @param History $history The history to render * @return string HTML string of the workouts */ public function renderHistory(History $history) { $workouts = $history->getWorkouts(); $renderedWorkouts = array(); $chartData = array('current' => array(), 'prev' => array()); $prevTimestamp = null; foreach ($workouts as $workout) { $date = $workout->getDate(); $timestamp = ($date->getTimestamp() + (int) $date->format('Z')) * 1000; $renderedWorkouts[] = sprintf('<h3 id="workout-%s">%s <small>(%s) %s</small></h3>%s', $workout->getId(), $workout->getLabel(), $this->renderRelativeDate($date), $this->renderType($workout->getType()), $this->renderExercises($workout->getExercises())); // create a break if the previous workout was more than 5 days ago if ($prevTimestamp && $timestamp - $prevTimestamp > 432000000) { $chartData['current'][] = array($timestamp - 1, null); if (count($chartData['prev'])) { $chartData['prev'][] = array($timestamp - 1, null); } } $prevWorkout = $workout->getPreviousWorkout(); if ($prevWorkout) { $chartData['prev'][] = array($timestamp, $prevWorkout->getTotalReps()); } $chartData['current'][] = array('name' => $workout->getLabel(), 'id' => $workout->getId(), 'x' => $timestamp, 'y' => $workout->getTotalReps()); $prevTimestamp = $timestamp; } $js = '<script type="text/javascript">var chartData = ' . json_encode($chartData) . ';</script>'; return '<div>' . join('', array_reverse($renderedWorkouts)) . '</div>' . $js; }
/** * Parse the log file from the YAYOG app and return workout objects. * * @param string $content Content of the log file * @return History */ public function parseTextFile($content) { $lines = array_reverse(array_filter(explode("\n\n<end>\n\n", $content))); $history = new History(); foreach ($lines as $line) { $history->addWorkout($this->getWorkout($line)); } return $history; }
/** * @covers YAYOG\HtmlFormatter::renderHistory */ public function testRenderHistory() { $formatter = $this->getMock('YAYOG\\HtmlFormatter', array('renderRelativeDate', 'renderType', 'renderExercises')); $history = new History(); for ($i = 0; $i < 4; $i++) { $workout = new Workout(); $workout->setDate(new DateTime()); $history->addWorkout($workout); } $formatter->expects($this->exactly(4))->method('renderRelativeDate'); $formatter->expects($this->exactly(4))->method('renderType'); $formatter->expects($this->exactly(4))->method('renderExercises'); $this->assertTag(array('tag' => 'div', 'children' => array('count' => 4, 'only' => array('tag' => 'h3'))), $formatter->renderHistory($history)); }
/** * @covers YAYOG\History::addWorkout */ public function testAddWorkoutSetsPreviousWorkout() { $workout1 = new Workout(); $workout1->setProgram('Basic')->setDay(4); $workout2 = new Workout(); $workout2->setProgram('Basic')->setDay(4); $workout3 = new Workout(); $workout3->setProgram('Basic')->setDay(6); $workouts = array($workout1, $workout2, $workout3); foreach ($workouts as $workout) { $this->history->addWorkout($workout); } $this->assertNull($workout1->getPreviousWorkout()); $this->assertNull($workout3->getPreviousWorkout()); $this->assertSame($workout1, $workout2->getPreviousWorkout()); }