client = $this->getClientAuthenticated(); } public function personProvider(): array { self::bootKernel(); $em = self::$container->get(EntityManagerInterface::class); $countPersons = $em->createQueryBuilder() ->select('count(p)') ->from(Person::class, 'p') ->join('p.center', 'c') ->where('c.name LIKE :name') ->setParameter('name', 'Center A') ->getQuery() ->getSingleScalarResult(); $person = $em->createQueryBuilder() ->select('p') ->from(Person::class, 'p') ->join('p.center', 'c') ->where('c.name LIKE :name') ->setParameter('name', 'Center A') ->getQuery() ->setMaxResults(1) ->setFirstResult(random_int(0, $countPersons - 1)) ->getSingleResult(); return [ [$person->getId()], ]; } public function relationProvider(): array { self::bootKernel(); $em = self::$container->get(EntityManagerInterface::class); $countPersons = $em->createQueryBuilder() ->select('count(p)') ->from(Person::class, 'p') ->join('p.center', 'c') ->where('c.name LIKE :name') ->setParameter('name', 'Center A') ->getQuery() ->getSingleScalarResult(); $persons = $em->createQueryBuilder() ->select('p') ->from(Person::class, 'p') ->join('p.center', 'c') ->where('c.name LIKE :name') ->setParameter('name', 'Center A') ->getQuery() ->setMaxResults(2) ->setFirstResult(random_int(0, $countPersons - 1)) ->getResult(); return [ [$persons[0]->getId(), $persons[1]->getId(), $this->getRandomRelation($em)->getId(), true], ]; } /** * @dataProvider personProvider * * @param mixed $personId */ public function testGetRelationshipByPerson($personId) { $this->client->request(Request::METHOD_GET, sprintf('/api/1.0/relations/relationship/by-person/%d.json', $personId)); $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode(), 'Test to see that API response returns a status code 200'); } /** * @dataProvider relationProvider * * @param mixed $fromPersonId * @param mixed $toPersonId * @param mixed $relationId * @param mixed $isReverse */ public function testPostRelationship($fromPersonId, $toPersonId, $relationId, $isReverse): void { $this->client->request( Request::METHOD_POST, '/api/1.0/relations/relationship.json', [], [], [], json_encode([ 'type' => 'relationship', 'fromPerson' => ['id' => $fromPersonId, 'type' => 'person'], 'toPerson' => ['id' => $toPersonId, 'type' => 'person'], 'relation' => ['id' => $relationId, 'type' => 'relation'], 'reverse' => $isReverse, ]) ); $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); } private function getRandomRelation(EntityManagerInterface $em): Relation { if (null === $this->relations) { $this->relations = $em->getRepository(Relation::class) ->findAll(); } return $this->relations[array_rand($this->relations)]; } }