/**
  * {@inheritdoc}
  */
 public function startTracking(User $user, Task $task)
 {
     $tracker = $this->trackerRepository->retrieveUserTracker($user);
     if ($tracker && $tracker->getTask()->getId() === $task->getId()) {
         return;
     }
     if ($tracker) {
         // stop previous tracker
         $this->processStopTracking($tracker);
     }
     $tracker = $this->trackerFactory->create($user, $task);
     $this->trackerRepository->save($tracker);
 }
 public function testStartTrackingWhenUserTrackingOtherTask()
 {
     $user = new User();
     $task = new Task();
     $task->setId(1);
     $otherTask = new Task();
     $otherTask->setId(2);
     $waitTimeInSeconds = 2;
     $trackerOfOtherTask = new Tracker($user, $otherTask);
     sleep($waitTimeInSeconds);
     $tracker = new Tracker($user, $task);
     $timeSpent = new TimeSpent($tracker->getSpentSeconds());
     $worklog = new Worklog($timeSpent, $trackerOfOtherTask->getDateStarted(), $trackerOfOtherTask->getTask(), $trackerOfOtherTask->getUser());
     $this->trackerRepository->expects($this->once())->method('retrieveUserTracker')->with($user)->will($this->returnValue($trackerOfOtherTask));
     $this->timeSpentFactory->expects($this->once())->method('create')->with($trackerOfOtherTask->getSpentSeconds())->will($this->returnValue($timeSpent));
     $this->worklogFactory->expects($this->once())->method('create')->with($timeSpent, $trackerOfOtherTask->getDateStarted(), $otherTask, $user)->will($this->returnValue($worklog));
     $this->trackerFactory->expects($this->once())->method('create')->with($user, $task)->will($this->returnValue($tracker));
     $this->trackerRepository->expects($this->once())->method('save')->with($tracker);
     $this->trackerRepository->expects($this->once())->method('removeTracker')->with($trackerOfOtherTask);
     $this->worklogRepository->expects($this->once())->method('save')->with($worklog);
     $this->service->startTracking($user, $task);
 }