/** * @param Thread $thread * @param User $user * @return Thread */ public function create(Thread $thread, User $user) { $thread->setUser($user)->setLastPostedAt(null); $this->threadRepository->add($thread); $this->entityManager->flush(); return $thread; }
/** * @test */ public function failRemovingIfThreadOwnerIsDifferent() { $userId1 = 42; $userId2 = 27; $user1 = $this->getMock('Kumatch\\BBSAPI\\Entity\\User', array("getId")); $user1->expects($this->once())->method("getId")->will($this->returnValue($userId1)); $user2 = $this->getMock('Kumatch\\BBSAPI\\Entity\\User', array("getId")); $user2->expects($this->once())->method("getId")->will($this->returnValue($userId2)); /** @var \Kumatch\BBSAPI\Entity\User $user1 */ /** @var \Kumatch\BBSAPI\Entity\User $user2 */ $thread = new Thread(); $thread->setUser($user2); $threadRepo = $this->getMock("RepositoryMock", array("remove")); $threadRepo->expects($this->never())->method("remove"); $em = $this->getEntityManagerMock(array("getRepository", "flush")); $em->expects($this->once())->method("getRepository")->with($this->equalTo(EntityConstant::THREAD))->will($this->returnValue($threadRepo)); $em->expects($this->never())->method("flush"); /** @var EntityManager $em */ $useCase = new ThreadManagement($em); $result = $useCase->remove($thread, $user1); $this->assertFalse($result); }