/** * @expectedException \AppBundle\Service\HistoryException * @expectedExceptionCode \AppBundle\Service\HistoryException::NON_FAST_FORWARD */ public function testCommit_non_fastforward() { // Simulation d'une branche en bdd sur le commit 1 $user = new User(); $user->setId(1); $pageAnimalBranch = new PageAnimalBranch(); $pageAnimalBranch->setOwner($user); $commit = new PageAnimalCommit(null, 'Joey', null, null, PageAnimal::DISPONIBLE, PageAnimal::MALE, null); $commit->setId(1); $pageAnimalBranch->setCommit($commit); $this->pageAnimalBranchRepository->method('find')->with(10)->willReturn($pageAnimalBranch); // Simulation commit à partir d'un head sur le commit 2 $pageAnimal = new PageAnimal(); $pageAnimal->setHead(2); $pageAnimal->setId(10); $this->pageAnimalCommitRepository->method('find')->with(2)->willReturn(new PageAnimalCommit(null, 'Joey', null, null, PageAnimal::DISPONIBLE, PageAnimal::MALE, null)); $this->pageAnimalService->commit($user, $pageAnimal); }
/** * @param User $user * @param PageAnimal $pageAnimal * @throws HistoryException * @throws ValidationException */ public function commit(User $user, PageAnimal $pageAnimal) { /** @var PageAnimalBranch $pageAnimalBranch */ $pageAnimalBranch = $this->pageAnimalBranchRepository->find($pageAnimal->getId()); if ($pageAnimalBranch == null) { throw new HistoryException(HistoryException::BRANCHE_INCONNUE); } if ($user->getId() !== $pageAnimalBranch->getOwner()->getId()) { throw new HistoryException(HistoryException::DROIT_REFUSE); } /** @var PageAnimalCommit $clientHead */ $clientHead = $this->pageAnimalCommitRepository->find($pageAnimal->getHead()); if ($clientHead->getId() !== $pageAnimalBranch->getCommit()->getId()) { throw new HistoryException(HistoryException::NON_FAST_FORWARD); } if (empty($pageAnimal->getNom())) { throw new ValidationException(ValidationException::EMPTY_NOM); } if (empty($pageAnimal->getDateNaissance())) { throw new ValidationException(ValidationException::EMPTY_DATE_NAISSANCE); } $commit = new PageAnimalCommit($clientHead, $pageAnimal->getNom(), $pageAnimal->getDateNaissance(), $pageAnimal->getDescription(), $pageAnimal->getStatut(), $pageAnimal->getSexe(), $pageAnimal->getPhotos()); $this->doctrine->persist($commit); $pageAnimalBranch->setCommit($commit); $this->doctrine->flush([$commit, $pageAnimalBranch]); $pageAnimal->setHead($commit->getId()); }
public function testCommit_BrancheInconnue() { $this->testUtils->createUser(); $fakePageEleveur = new PageAnimal(); $fakePageEleveur->setId(-1); $fakePageEleveur->setHead(-1); $this->client->request('POST', '/animal/1', array(), array(), array(), $this->serializer->serialize($fakePageEleveur, 'json')); $this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); }