|
|
@@ -2,33 +2,106 @@
|
|
|
|
|
|
namespace App\Tests\Application;
|
|
|
|
|
|
+use App\Entity\Access\Access;
|
|
|
+use App\Entity\Person\Person;
|
|
|
use App\Entity\Public\FederationStructure;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
+use Doctrine\Common\DataFixtures\Purger\ORMPurger;
|
|
|
+use Symfony\Bundle\SecurityBundle\Security;
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
use ApiPlatform\Symfony\Bundle\Test\Client;
|
|
|
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
+use Zenstruck\Foundry\Proxy;
|
|
|
|
|
|
/**
|
|
|
* Base class for applicative tests
|
|
|
*/
|
|
|
abstract class OtWebTestCase extends ApiTestCase
|
|
|
{
|
|
|
+ protected EntityManagerInterface $em;
|
|
|
protected Client $client;
|
|
|
+ protected Access | Proxy | null $user = null;
|
|
|
+ protected ?string $securityToken = null;
|
|
|
|
|
|
+ /**
|
|
|
+ * Executed before each test
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
public function setup(): void {
|
|
|
+
|
|
|
+ // Boot le kernel symfony et récupère l'entity manager
|
|
|
+ // @see https://symfony.com/doc/current/testing.html#retrieving-services-in-the-test
|
|
|
+ self::bootKernel();
|
|
|
+ $this->em = static::getContainer()->get(EntityManagerInterface::class);
|
|
|
+
|
|
|
+ // Purge DB before populating new fixtures
|
|
|
+ $this->purgeDb();
|
|
|
+
|
|
|
+ // Définit les fixtures et flush
|
|
|
+ $this->setFixtures();
|
|
|
+ $this->em->flush();
|
|
|
+
|
|
|
+ // Instancie le client qui exécutera les requêtes à l'api
|
|
|
+ // @see https://symfony.com/doc/current/testing.html#making-requests
|
|
|
$this->client = static::createClient();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Delete all DB records before populating fixtures.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws \Doctrine\DBAL\Exception
|
|
|
+ */
|
|
|
+ private function purgeDb() {
|
|
|
+ if (!preg_match('/.*test.*/', $this->em->getConnection()->getDatabase())) {
|
|
|
+ throw new \RuntimeException("The DB name shall contain 'test' in its name to be purge");
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->em->getConnection()->exec('SET FOREIGN_KEY_CHECKS = 0;');
|
|
|
+ $purger = new ORMPurger($this->em);
|
|
|
+ $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE);
|
|
|
+ $purger->purge();
|
|
|
+ $this->em->getConnection()->exec('SET FOREIGN_KEY_CHECKS = 1;');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create and persist the fixtures (no need to flush, the setup will perform it later)
|
|
|
+ *
|
|
|
+ * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#same-entities-used-in-these-docs
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function setFixtures(): void {
|
|
|
+ // TODO: préparer un jeu de fixtures par défaut
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Send a requests, parse the hydra response and return an object or a Collection
|
|
|
*
|
|
|
* @param string $method
|
|
|
* @param string $route
|
|
|
- * @param array<mixed> $parameters
|
|
|
+ * @param array<mixed> $data
|
|
|
+ * @param array<mixed> $headers
|
|
|
* @return ResponseInterface
|
|
|
*/
|
|
|
- protected function request(string $method, string $route, array $parameters = []): ResponseInterface {
|
|
|
+ protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface {
|
|
|
+ if ($this->user) {
|
|
|
+ $headers = array_merge(
|
|
|
+ ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
|
|
|
+ $headers
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ $parameters = ['headers' => $headers];
|
|
|
+
|
|
|
+ if ($data) {
|
|
|
+ $parameters['json'] = $data;
|
|
|
+ }
|
|
|
+
|
|
|
return $this->client->request(
|
|
|
$method,
|
|
|
$route,
|
|
|
@@ -43,11 +116,28 @@ abstract class OtWebTestCase extends ApiTestCase
|
|
|
* @param array<mixed> $parameters
|
|
|
* @return ResponseInterface
|
|
|
*/
|
|
|
- protected function get(string $route, array $parameters = []): ResponseInterface {
|
|
|
+ protected function get(string $route, array $headers = []): ResponseInterface {
|
|
|
return $this->request(
|
|
|
Request::METHOD_GET,
|
|
|
$route,
|
|
|
- $parameters
|
|
|
+ null,
|
|
|
+ $headers
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Send a POST request and return the response parsed content
|
|
|
+ *
|
|
|
+ * @param string $route
|
|
|
+ * @param array<mixed> $parameters
|
|
|
+ * @return ResponseInterface
|
|
|
+ */
|
|
|
+ protected function post(string $route, array $data, array $headers = []): ResponseInterface {
|
|
|
+ return $this->request(
|
|
|
+ Request::METHOD_POST,
|
|
|
+ $route,
|
|
|
+ $data,
|
|
|
+ $headers
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -62,4 +152,16 @@ abstract class OtWebTestCase extends ApiTestCase
|
|
|
// $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
|
|
|
}
|
|
|
|
|
|
+ protected function loginAs(Proxy | Access $access): void {
|
|
|
+ $person = $access->getPerson();
|
|
|
+
|
|
|
+ $response = $this->post(
|
|
|
+ '/login_check',
|
|
|
+ ['username' => $person->getUsername(), 'password' => $person->getPassword()]
|
|
|
+ );
|
|
|
+ $content = $response->getContent();
|
|
|
+
|
|
|
+ $this->securityToken = json_decode($content)->token;
|
|
|
+ $this->user = $access;
|
|
|
+ }
|
|
|
}
|