Create a page which list events

This commit is contained in:
2023-11-27 20:30:50 +01:00
parent d8bf6a195f
commit e902b6d409
16 changed files with 716 additions and 125 deletions

View File

@@ -11,6 +11,11 @@ declare(strict_types=1);
namespace Chill\EventBundle\Tests\Controller;
use Chill\EventBundle\Entity\Event;
use Chill\EventBundle\Repository\EventRepository;
use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\DataFixtures\Helper\PersonRandomHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use function count;
@@ -23,15 +28,12 @@ use function count;
*/
final class ParticipationControllerTest extends WebTestCase
{
/**
* @var \Symfony\Component\BrowserKit\AbstractBrowser
*/
protected $client;
use PersonRandomHelper;
use PrepareClientTrait;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $em;
private EntityManagerInterface $em;
private EventRepository $eventRepository;
/**
* Keep a cache for each person id given by the function getRandomPerson.
@@ -44,23 +46,21 @@ final class ParticipationControllerTest extends WebTestCase
*/
private array $personsIdsCache = [];
protected function setUp(): void
protected function prepareDI(): void
{
self::bootKernel();
$this->client = self::createClient([], [
'PHP_AUTH_USER' => 'center a_social',
'PHP_AUTH_PW' => 'password',
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR',
]);
$container = self::$kernel->getContainer();
$this->em = $container->get('doctrine.orm.entity_manager');
$this->em = self::$container->get(EntityManagerInterface::class);
$this->eventRepository = self::$container->get(EventRepository::class);
$this->personsIdsCache = [];
}
protected function tearDown(): void
{
parent::tearDown();
self::ensureKernelShutdown();
}
/**
* This method test participation creation with wrong parameters.
*
@@ -68,11 +68,13 @@ final class ParticipationControllerTest extends WebTestCase
*/
public function testCreateActionWrongParameters()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
$event = $this->getRandomEvent();
$person = $this->getRandomPerson();
$person = $this->getRandomPerson($this->em);
// missing person_id or persons_ids
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/create',
[
@@ -81,33 +83,33 @@ final class ParticipationControllerTest extends WebTestCase
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'Test that /fr/event/participation/create fail if '
.'both person_id and persons_ids are missing'
);
// having both person_id and persons_ids
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/create',
[
'event_id' => $event->getId(),
'persons_ids' => implode(',', [
$this->getRandomPerson()->getId(),
$this->getRandomPerson()->getId(),
$this->getRandomPerson($this->em)->getId(),
$this->getRandomPerson($this->em)->getId(),
]),
'person_id' => $person->getId(),
]
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'test that /fr/event/participation/create fail if both person_id and '
.'persons_ids are set'
);
// missing event_id
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/create',
[
@@ -116,12 +118,12 @@ final class ParticipationControllerTest extends WebTestCase
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'Test that /fr/event/participation/create fails if event_id is missing'
);
// persons_ids with wrong content
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/create',
[
@@ -131,42 +133,47 @@ final class ParticipationControllerTest extends WebTestCase
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'Test that /fr/event/participation/create fails if persons_ids has wrong content'
);
}
public function testEditMultipleAction()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
/** @var \Chill\EventBundle\Entity\Event $event */
$event = $this->getRandomEventWithMultipleParticipations();
$crawler = $this->client->request('GET', '/fr/event/participation/'.$event->getId().
$crawler = $client->request('GET', '/fr/event/participation/'.$event->getId().
'/edit_multiple');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$button = $crawler->selectButton('Mettre à jour');
$this->assertEquals(1, $button->count(), "test the form with button 'mettre à jour' exists ");
$this->client->submit($button->form(), [
$client->submit($button->form(), [
'form[participations][0][role]' => $event->getType()->getRoles()->first()->getId(),
'form[participations][0][status]' => $event->getType()->getStatuses()->first()->getId(),
'form[participations][1][role]' => $event->getType()->getRoles()->last()->getId(),
'form[participations][1][status]' => $event->getType()->getStatuses()->last()->getId(),
]);
$this->assertTrue($this->client->getResponse()
$this->assertTrue($client->getResponse()
->isRedirect('/fr/event/event/'.$event->getId().'/show'));
}
public function testNewActionWrongParameters()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
$event = $this->getRandomEvent();
$person = $this->getRandomPerson();
$person = $this->getRandomPerson($this->em);
// missing person_id or persons_ids
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/new',
[
@@ -175,33 +182,33 @@ final class ParticipationControllerTest extends WebTestCase
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'Test that /fr/event/participation/new fail if '
.'both person_id and persons_ids are missing'
);
// having both person_id and persons_ids
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/new',
[
'event_id' => $event->getId(),
'persons_ids' => implode(',', [
$this->getRandomPerson()->getId(),
$this->getRandomPerson()->getId(),
$this->getRandomPerson($this->em)->getId(),
$this->getRandomPerson($this->em)->getId(),
]),
'person_id' => $person->getId(),
]
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'test that /fr/event/participation/new fail if both person_id and '
.'persons_ids are set'
);
// missing event_id
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/new',
[
@@ -210,12 +217,12 @@ final class ParticipationControllerTest extends WebTestCase
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'Test that /fr/event/participation/new fails if event_id is missing'
);
// persons_ids with wrong content
$this->client->request(
$client->request(
'GET',
'/fr/event/participation/new',
[
@@ -225,13 +232,15 @@ final class ParticipationControllerTest extends WebTestCase
);
$this->assertEquals(
400,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'Test that /fr/event/participation/new fails if persons_ids has wrong content'
);
}
public function testNewMultipleAction()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
$event = $this->getRandomEvent();
// record the number of participation for the event (used later in this test)
$nbParticipations = $event->getParticipations()->count();
@@ -244,10 +253,10 @@ final class ParticipationControllerTest extends WebTestCase
->toArray()
);
// get some random people
$person1 = $this->getRandomPerson();
$person2 = $this->getRandomPerson();
$person1 = $this->getRandomPerson($this->em);
$person2 = $this->getRandomPerson($this->em);
$crawler = $this->client->request(
$crawler = $client->request(
'GET',
'/fr/event/participation/new',
[
@@ -258,7 +267,7 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertEquals(
200,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'test that /fr/event/participation/new is successful'
);
@@ -266,7 +275,7 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertNotNull($button, "test the form with button 'Créer' exists");
$this->client->submit($button->form(), [
$client->submit($button->form(), [
'form' => [
'participations' => [
0 => [
@@ -281,8 +290,8 @@ final class ParticipationControllerTest extends WebTestCase
],
]);
$this->assertTrue($this->client->getResponse()->isRedirect());
$crawler = $this->client->followRedirect();
$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect();
$span1 = $crawler->filter('table td span.entity-person a:contains("'
.$person1->getFirstName().'"):contains("'.$person1->getLastname().'")');
@@ -300,13 +309,15 @@ final class ParticipationControllerTest extends WebTestCase
public function testNewMultipleWithAllPeopleParticipating()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
$event = $this->getRandomEventWithMultipleParticipations();
$persons_id = implode(',', $event->getParticipations()->map(
static fn ($p) => $p->getPerson()->getId()
)->toArray());
$crawler = $this->client->request(
$crawler = $client->request(
'GET',
'/fr/event/participation/new',
[
@@ -317,13 +328,15 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertEquals(
302,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'test that /fr/event/participation/new is redirecting'
);
}
public function testNewMultipleWithSomePeopleParticipating()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
$event = $this->getRandomEventWithMultipleParticipations();
// record the number of participation for the event (used later in this test)
$nbParticipations = $event->getParticipations()->count();
@@ -335,12 +348,12 @@ final class ParticipationControllerTest extends WebTestCase
$this->personsIdsCache = array_merge($this->personsIdsCache, $persons_id);
// get a random person
$newPerson = $this->getRandomPerson();
$newPerson = $this->getRandomPerson($this->em);
// build the `persons_ids` parameter
$persons_ids_string = implode(',', [...$persons_id, $newPerson->getId()]);
$crawler = $this->client->request(
$crawler = $client->request(
'GET',
'/fr/event/participation/new',
[
@@ -351,7 +364,7 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertEquals(
200,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'test that /fr/event/participation/new is successful'
);
@@ -377,12 +390,12 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertNotNull($button, "test the form with button 'Créer' exists");
// submit the form
$this->client->submit($button->form(), [
$client->submit($button->form(), [
'participation[role]' => $event->getType()->getRoles()->first()->getId(),
'participation[status]' => $event->getType()->getStatuses()->first()->getId(),
]);
$this->assertTrue($this->client->getResponse()->isRedirect());
$this->assertTrue($client->getResponse()->isRedirect());
// reload the event and test there is a new participation
$event = $this->em->getRepository(\Chill\EventBundle\Entity\Event::class)
@@ -398,12 +411,14 @@ final class ParticipationControllerTest extends WebTestCase
public function testNewSingleAction()
{
$client = $this->getClientAuthenticated();
$this->prepareDI();
$event = $this->getRandomEvent();
// record the number of participation for the event
$nbParticipations = $event->getParticipations()->count();
$person = $this->getRandomPerson();
$person = $this->getRandomPerson($this->em);
$crawler = $this->client->request(
$crawler = $client->request(
'GET',
'/fr/event/participation/new',
[
@@ -414,7 +429,7 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertEquals(
200,
$this->client->getResponse()->getStatusCode(),
$client->getResponse()->getStatusCode(),
'test that /fr/event/participation/new is successful'
);
@@ -422,13 +437,13 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertNotNull($button, "test the form with button 'Créer' exists");
$this->client->submit($button->form(), [
$client->submit($button->form(), [
'participation[role]' => $event->getType()->getRoles()->first()->getId(),
'participation[status]' => $event->getType()->getStatuses()->first()->getId(),
]);
$this->assertTrue($this->client->getResponse()->isRedirect());
$crawler = $this->client->followRedirect();
$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect();
$span = $crawler->filter('table td span.entity-person a:contains("'
.$person->getFirstName().'"):contains("'.$person->getLastname().'")');
@@ -442,23 +457,17 @@ final class ParticipationControllerTest extends WebTestCase
$this->assertEquals($nbParticipations + 1, $event->getParticipations()->count());
}
/**
* @return \Chill\EventBundle\Entity\Event
*/
protected function getRandomEvent(mixed $centerName = 'Center A', mixed $circleName = 'social')
private function getRandomEvent(string $centerName = 'Center A', string $circleName = 'social'): Event
{
$center = $this->em->getRepository(\Chill\MainBundle\Entity\Center::class)
->findByName($centerName);
$dql = 'FROM '.Event::class.' e JOIN e.center center JOIN e.circle scope WHERE center.name LIKE :cname AND JSON_EXTRACT(scope.name, \'fr\') LIKE :sname';
$circles = $this->em->getRepository(\Chill\MainBundle\Entity\Scope::class)
->findAll();
array_filter($circles, static fn ($circle) => \in_array($circleName, $circle->getName(), true));
$circle = $circles[0];
$ids = $this->em->createQuery(
'SELECT DISTINCT e.id '.$dql
)
->setParameters(['cname' => $centerName, 'sname' => $circleName])
->getResult();
$events = $this->em->getRepository(\Chill\EventBundle\Entity\Event::class)
->findBy(['center' => $center, 'circle' => $circle]);
return $events[array_rand($events)];
return $this->eventRepository->find($ids[array_rand($ids)]['id']);
}
/**
@@ -479,35 +488,4 @@ final class ParticipationControllerTest extends WebTestCase
$event :
$this->getRandomEventWithMultipleParticipations($centerName, $circleName);
}
/**
* Returns a person randomly.
*
* This function does not give the same person twice
* for each test.
*
* You may ask to ignore some people by adding their id to the property
* `$this->personsIdsCache`
*
* @param string $centerName
*
* @return \Chill\PersonBundle\Entity\Person
*/
protected function getRandomPerson($centerName = 'Center A')
{
$center = $this->em->getRepository(\Chill\MainBundle\Entity\Center::class)
->findByName($centerName);
$persons = $this->em->getRepository(\Chill\PersonBundle\Entity\Person::class)
->findBy(['center' => $center]);
$person = $persons[array_rand($persons)];
if (\in_array($person->getId(), $this->personsIdsCache, true)) {
return $this->getRandomPerson($centerName); // we try another time
}
$this->personsIdsCache[] = $person->getId();
return $person;
}
}