diff --git a/src/Bundle/ChillMainBundle/Tests/Export/AccompanyingCourseExportHelperTest.php b/src/Bundle/ChillMainBundle/Tests/Export/AccompanyingCourseExportHelperTest.php new file mode 100644 index 000000000..bc7f413d7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Export/AccompanyingCourseExportHelperTest.php @@ -0,0 +1,114 @@ + + */ + private static array $entitiesToDelete = []; + + private EntityManagerInterface $em; + + private EntityRepository $repository; + + private AccompanyingCourseExportHelper $accompanyingCourseExportHelper; + + protected function setUp(): void + { + self::bootKernel(); + $this->em = self::$container->get(EntityManagerInterface::class); + $this->repository = self::$container->get($this->em->getRepository(AccompanyingPeriod::class)); + $this->accompanyingPeriodExportHelper = self::$container->get(AccompanyingCourseExportHelper::class); + } + + public static function tearDownAfterClass(): void + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + + foreach (self::$entitiesToDelete as [$class, $id]) { + $entity = $em->find($class, $id); + + if (null !== $entity) { + $em->remove($entity); + } + } + + $em->flush(); + } + + /** + * @dataProvider dataProviderExclusionOnClosingMotive + */ + public function testExclusionOnClosingMotive( + AccompanyingPeriod $periodA, //canceled + AccompanyingPeriod $periodB, // not canceled + AccompanyingPeriod $periodC // canceled (child cm of canceled cm) + ): void + { + $qb = $this->repository->createQueryBuilder('acp'); + $qb->select('acp.id')->from(AccompanyingPeriod::class, 'acp'); + $this->accompanyingCourseExportHelper::addClosingMotiveExclusionClause($qb); + + $periodIdsFound = $qb->getQuery()->getResult(); + + self::assertContains($periodB->getId(), $periodIdsFound, 'Period without canceled closing motive has been found'); + self::assertNotContains($periodA->getId(), $periodIdsFound, 'Period with canceled closing motive has not been found'); + self::assertNotContains($periodC->getId(), $periodIdsFound, 'Period with child canceled closing motive has not been found'); + } + + public function dataProviderExclusionOnClosingMotive(): iterable + { + $this->setUp(); + + $cmA = new ClosingMotive(); + $cmA->setIsCanceledAccompanyingPeriod(true); + + $cmB = new ClosingMotive(); + $cmB->setIsCanceledAccompanyingPeriod(false); + + $cmChild = new ClosingMotive(); + $cmChild->setParent($cmA); + + $this->em->persist($cmA); + $this->em->persist($cmB); + $this->em->persist($cmChild); + + $periodA = new AccompanyingPeriod(); + $periodB = new AccompanyingPeriod(); + $periodC = new AccompanyingPeriod(); + + $periodA->setClosingMotive($cmA); + $periodB->setClosingMotive($cmB); + $periodC->setClosingMotive($cmChild); + + $this->em->persist($periodA); + $this->em->persist($periodB); + $this->em->persist($periodC); + + self::$entitiesToDelete[] = [ClosingMotive::class, $cmA]; + self::$entitiesToDelete[] = [ClosingMotive::class, $cmB]; + self::$entitiesToDelete[] = [ClosingMotive::class, $cmChild]; + + self::$entitiesToDelete[] = [AccompanyingPeriod::class, $periodA]; + self::$entitiesToDelete[] = [AccompanyingPeriod::class, $periodB]; + self::$entitiesToDelete[] = [AccompanyingPeriod::class, $periodC]; + + yield [$periodA, $periodB, $periodC]; + + $this->em->flush(); + $this->em->clear(); + } + +}