public function testItShould_fetchRandomExercise()
 {
     $previousExerciseId = rand(1, 100);
     $exercise = $this->createExercise();
     $this->exerciseRepository->method('fetchRandomExerciseOfLesson')->with($this->lesson->id, $this->user->id, $previousExerciseId)->willReturn($exercise);
     $this->assertEquals($exercise, $this->lesson->fetchRandomExercise($this->user->id, $previousExerciseId));
 }
 /**
  * @param Request $request
  * @return RedirectResponse
  */
 public function store(Request $request) : RedirectResponse
 {
     $this->validate($request, ['visibility' => 'required|in:public,private', 'name' => 'required|string']);
     $lesson = new Lesson($request->all());
     $lesson->owner_id = $this->user()->id;
     $lesson->save();
     return redirect('/lessons/' . $lesson->id);
 }
 private function assertExerciseCanWin(Lesson $lesson, int $userId, int $exerciseId, int $previousId = null)
 {
     // 1000 retries is more then enough
     $counter = 1000;
     do {
         if (!$counter--) {
             $this->fail('Unable to fetch random exercise.');
         }
         $result = $lesson->fetchRandomExercise($userId, $previousId);
         $this->assertInstanceOf(Exercise::class, $result);
     } while ($result->id != $exerciseId && $result->id && $previousId);
 }
 /**
  * Public lessons user does not own and does not subscribe.
  * @return EloquentCollection
  */
 public function availableLessons() : EloquentCollection
 {
     return Lesson::query()->where('lessons.owner_id', '!=', $this->id)->where('lessons.visibility', '=', 'public')->whereNotIn('lessons.id', $this->subscribedLessons()->pluck('id'))->with('exercises', 'subscribers')->get();
 }
 /**
  * @param FetchRandomExerciseOfLessonRequest $request
  * @param Lesson $lesson
  * @return JsonResponse
  * @throws NotEnoughExercisesException
  */
 public function fetchRandomExerciseOfLesson(FetchRandomExerciseOfLessonRequest $request, Lesson $lesson)
 {
     $exercise = $lesson->fetchRandomExercise($this->user()->id, $request->previous_exercise_id);
     return $this->response($exercise);
 }
 /**
  * Lesson must have certain number of exercises, and user must have access.
  *
  * @param User $user
  * @param Lesson $lesson
  * @return bool
  */
 public function learn(User $user, Lesson $lesson)
 {
     return $lesson->exercises()->count() >= config('app.min_exercises_to_learn_lesson') && $this->access($user, $lesson);
 }
 /**
  * @param PatchLessonRequest $request
  * @param Lesson $lesson
  * @return JsonResponse
  */
 public function updateLesson(PatchLessonRequest $request, Lesson $lesson)
 {
     $lesson->update($request->all());
     return $this->response($lesson);
 }