static function find($search_id) { $found_habit = null; $habits = Habit::getAll(); foreach ($habits as $habit) { $habit_id = $habit->getId(); if ($habit_id == $search_id) { $found_habit = $habit; } } return $found_habit; }
function testDelete() { $name = "Meditate"; $motivation = "Clarity"; $interval_days = 3; $id = 4; $completed = False; $test_habit = new Habit($name, $motivation, $interval_days, $completed, $id); $test_habit->save(); $name2 = "Go running"; $motivation2 = "Fitness"; $interval_days2 = 35; $id2 = 5; $completed2 = False; $test_habit2 = new Habit($name2, $motivation2, $interval_days2, $completed2, $id2); $test_habit2->save(); $test_habit->delete(); $this->assertEquals([$test_habit2], Habit::getAll()); }
<?php $habit = $app['controllers_factory']; $habit->get('/current_habits', function () use($app) { $count = 0; return $app['twig']->render('habit/current_habits.html.twig', array('habits' => Habit::getAll())); }); $habit->get('/habits/{id}', function ($id) use($app) { $habit = Habit::find($id); return $app['twig']->render('habit/habit_edit.html.twig', array('habit' => $habit)); }); $habit->patch('/habits/{id}', function ($id) use($app) { $habit = Habit::find($id); $name = $_POST['name']; $motivation = $_POST['motivation']; $interval_days = $_POST['interval_days']; $habit->updateName($name); $habit->updateMotivation($motivation); $habit->updateIntervalDays($interval_days); return $app['twig']->render('habit/habit_edit.html.twig', array('habit' => $habit)); }); $habit->delete('/habits/{id}', function ($id) use($app) { $habit = Habit::find($id); $habit->delete(); return $app['twig']->render('habit/current_habits.html.twig', array('habits' => Habit::getAll())); }); // Place all urls in this file at /habit/* $app->mount('/habit', $habit);
** The Twig templates will be responsive to display only the aspects ** of an in-progress habit which have already been defined. */ $coach_new_habit->get('/name', function () use($app) { return $app['twig']->render('coach/new_habit/1new_habit_name.html.twig'); }); /* 2. Create a new project with name from last form and dummy values for other properties. ** We don't have an id yet, so can't use it in URL. ** Then, display project as is so far. */ $coach_new_habit->post('/motivation', function () use($app) { $name = $_POST['name']; $motivation = null; $interval_days = 0; $completed = false; $habit = new Habit($name, $motivation, $interval_days, $completed); $habit->save(); return $app['twig']->render('coach/new_habit/2new_habit_motivation.html.twig', array('habits' => Habit::getAll(), 'habit' => $habit)); }); /* 3. Add motivation from last form to habit. ** Show habit as is so far. ** Prompt user to brain dump pre-reqs. */ $coach_new_habit->post('/{id}/prereqs', function ($id) use($app) { $habit = Habit::find($id); if (!empty($_POST['motivation'])) { $habit->updateMotivation($_POST['motivation']); } else { // some kind of error here } return $app['twig']->render('coach/new_habit/3prereqs.html.twig', array('habit' => $habit)); }); /* 4. Add a new step if we have $_POST for it at the appropriate position. ** Display the user's braindump so they can refer to it when creating steps.