/** * @test * * @group user-entity */ public function likesPost() { $user = new User(); $post = new Post(); $post2 = new Post(); $book = new Book(); $like = new Like($post); $like2 = new Like($post2); $like3 = new Like($book); $user->addLike($like)->addLike($like2)->addLike($like3); $this->assertTrue($user->likesPost($post)); }
/** * @param Post $post * @param User $user * @throws \Exception */ public function unlikePost(Post $post, User $user) { if (!$user->likesPost($post)) { throw new Exception("You don't like this post"); } $like = $this->likeService->getPostSpecificLikeByUser($post, $user); if (null === $like) { throw new ResourceNotFoundException('No like found for this post'); } $this->em->getConnection()->beginTransaction(); $this->em->remove($like); $this->em->flush(); $this->em->getConnection()->commit(); }