loginUser($user); $subject = RestaurantFactory::createOne([ 'createdBy' => $user, ]); RatingFactory::createMany(5, [ 'subject' => $subject, ]); $client->request( 'GET', '/api/ratings', server: ['CONTENT_TYPE' => 'application/json'], content: json_encode(['name' => 'Riviera']), ); $data = json_decode($client->getResponse()->getContent(), true); $this->assertResponseIsSuccessful(); $this->assertCount(5, $data['payload']['ratings']); } public function testCreate(): void { $client = static::createClient(); $repo = static::getContainer()->get(RatingRepository::class); $user = UserFactory::createOne(); $client->loginUser($user); $subject = RestaurantFactory::createOne([ 'createdBy' => $user, ]); $client->request( 'POST', '/api/ratings', server: ['CONTENT_TYPE' => 'application/json'], content: json_encode([ 'subject_id' => $subject->getId(), 'score' => 8, ]), ); $this->assertResponseIsSuccessful(); $this->assertResponseStatusCodeSame(201); $this->assertCount(1, $repo->findAll()); $ratings = static::getContainer() ->get(RatingRepository::class) ->findAllBySubject($subject); $this->assertNotNull($ratings); $this->assertCount(1, $ratings); $rating = $ratings[0]; $this->assertInstanceOf(Rating::class, $rating); $this->assertSame($user->getId(), $rating->getUser()->getId()); } public function testUpdate(): void { $client = static::createClient(); $user = UserFactory::createOne(); $client->loginUser($user); $subject = RestaurantFactory::createOne([ 'createdBy' => $user, ]); $rating = RatingFactory::createOne([ 'subject' => $subject, 'user' => $user, 'score' => 0, 'note' => 'first note', ]); $client->request( 'PATCH', '/api/ratings/' . $rating->getId(), server: ['CONTENT_TYPE' => 'application/json'], content: json_encode([ 'score' => 8, 'note' => 'Other note', ]), ); $this->assertResponseIsSuccessful(); $this->assertResponseStatusCodeSame(202); $em = static::getContainer()->get(EntityManagerInterface::class); $em->clear(); $updated = $em->getRepository(Rating::class)->find($rating->getId()); $this->assertSame(8, $updated->getScore()); $this->assertSame('Other note', $updated->getNote()); } public function testDelete(): void { $client = static::createClient(); $user = UserFactory::createOne(); $client->loginUser($user); $subject = RestaurantFactory::createOne([ 'createdBy' => $user, ]); $rating = RatingFactory::createOne([ 'subject' => $subject, 'score' => 0, 'note' => 'first note', ]); $id = $rating->getId(); $client->request( 'DELETE', '/api/ratings/' . $id, server: ['CONTENT_TYPE' => 'application/json'], ); $this->assertResponseIsSuccessful(); $this->assertResponseStatusCodeSame(204); $em = static::getContainer()->get(EntityManagerInterface::class); $em->clear(); $this->assertNull( $em->getRepository(Rating::class)->find($id) ); } }