loginUser($user); RestaurantFactory::createMany(5, [ 'createdBy' => $user, ]); $client->request( 'GET', '/api/subjects', server: ['CONTENT_TYPE' => 'application/json'], content: json_encode(['name' => 'Riviera']), ); $data = json_decode($client->getResponse()->getContent(), true); $this->assertResponseIsSuccessful(); $this->assertCount(5, $data['payload']['subjects']); $subject = $data['payload']['subjects'][0]; $this->assertNotNull($subject['latitude']); $this->assertNotNull($subject['longitude']); } public function testShow(): void { $client = static::createClient(); $user = UserFactory::createOne(); $client->loginUser($user); $subject = RestaurantFactory::createOne(['createdBy' => $user]); RatingFactory::createOne(['subject' => $subject, 'score' => 4, 'user' => $user]); RatingFactory::createOne(['subject' => $subject, 'score' => 2, 'user' => UserFactory::createOne()]); $client->request('GET', '/api/subjects/' . $subject->getId(), server: ['CONTENT_TYPE' => 'application/json']); $data = json_decode($client->getResponse()->getContent(), true); $this->assertResponseIsSuccessful(); $this->assertSame($subject->getId(), $data['payload']['subject']['id']); $this->assertSame($subject->getName(), $data['payload']['subject']['name']); $this->assertCount(2, $data['payload']['subject']['ratings']); $this->assertSame(2, $data['payload']['subject']['ratingCount']); $this->assertEquals(3.0, $data['payload']['subject']['averageScore']); $rating = $data['payload']['subject']['ratings'][0]; $this->assertArrayHasKey('id', $rating); $this->assertArrayHasKey('score', $rating); $this->assertArrayHasKey('note', $rating); $this->assertArrayHasKey('createdAt', $rating); $this->assertArrayHasKey('user', $rating); $this->assertArrayHasKey('id', $rating['user']); } public function testCreate(): void { $client = static::createClient(); $repo = static::getContainer()->get(SubjectRepository::class); $user = UserFactory::createOne(); $client->loginUser($user); $this->assertEmpty($repo->findAll()); $faker = \Zenstruck\Foundry\faker(); $name = $faker->company(); $latitude = $faker->latitude(); $longitude = $faker->longitude(); $client->request( 'POST', '/api/subjects', server: ['CONTENT_TYPE' => 'application/json'], content: json_encode([ 'name' => $name, 'latitude' => $latitude, 'longitude' => $longitude, ]), ); $this->assertResponseIsSuccessful(); $this->assertResponseStatusCodeSame(201); $this->assertCount(1, $repo->findAll()); $subject = static::getContainer() ->get(SubjectRepository::class) ->findOneBy(['name' => $name]); $this->assertNotNull($subject); $this->assertInstanceOf(Restaurant::class, $subject); $this->assertSame($user->getId(), $subject->getCreatedBy()->getId()); $this->assertEqualsWithDelta((float) $latitude, (float) $subject->getLatitude(), 0.0000001); $this->assertEqualsWithDelta((float) $longitude, (float) $subject->getLongitude(), 0.0000001); } public function testUpdate(): void { $client = static::createClient(); $user = UserFactory::createOne(); $client->loginUser($user); $nameOld = 'Old name'; $nameNew = 'New name'; $latitudeOld = '15.005'; $latitudeNew = '51.66'; $longitudeOld = '15.040'; $longitudeNew = '51.54'; $subject = RestaurantFactory::createOne([ 'name' => $nameOld, 'createdBy' => $user, 'latitude' => $latitudeOld, 'longitude' => $longitudeOld, ]); $client->request( 'PATCH', '/api/subjects/' . $subject->getId(), server: ['CONTENT_TYPE' => 'application/json'], content: json_encode([ 'name' => $nameNew, 'latitude' => $latitudeNew, 'longitude' => $longitudeNew, ]), ); $this->assertResponseIsSuccessful(); $this->assertResponseStatusCodeSame(202); $em = static::getContainer()->get(EntityManagerInterface::class); $em->clear(); $updated = $em->getRepository(Subject::class)->find($subject->getId()); $this->assertSame($nameNew, $updated->getName()); $this->assertEqualsWithDelta((float) $latitudeNew, (float) $subject->getLatitude(), 0.0000001); $this->assertEqualsWithDelta((float) $longitudeNew, (float) $subject->getLongitude(), 0.0000001); } public function testDelete(): void { $client = static::createClient(); $user = UserFactory::createOne(); $client->loginUser($user); $subject = RestaurantFactory::createOne([ 'name' => 'My Restaurant', 'createdBy' => $user, ]); $id = $subject->getId(); $client->request( 'DELETE', '/api/subjects/' . $id, server: ['CONTENT_TYPE' => 'application/json'], ); $this->assertResponseIsSuccessful(); $this->assertResponseStatusCodeSame(204); $em = static::getContainer()->get(EntityManagerInterface::class); $em->clear(); $this->assertNull( $em->getRepository(Subject::class)->find($id) ); } }