Easy_solution/tests/Functional/AbstractFunctional.php

124 lines
4.0 KiB
PHP

<?php
namespace App\Tests\Functional;
use App\Entity\Apps;
use App\Entity\Notification;
use App\Entity\Organizations;
use App\Entity\Roles;
use App\Entity\User;
use App\Entity\UserOrganizatonApp;
use App\Entity\UsersOrganizations;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
abstract class AbstractFunctional extends WebTestCase
{
protected KernelBrowser $client;
protected EntityManagerInterface $entityManager;
protected function setUp(): void
{
$this->client = static::createClient();
// Access the container to get the EntityManager
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
}
protected function createUser(string $email, array $roles = ['ROLE_USER']): User
{
$user = new User();
$user->setEmail($email);
$user->setRoles($roles);
$user->setName('Test');
$user->setSurname('User');
$user->setPassword('$2y$13$...'); // Dummy hash, logic typically bypassed by loginUser
$this->entityManager->persist($user);
$this->entityManager->flush();
return $user;
}
protected function createApp(string $name): Apps
{
// Based on your Entity, these fields are NOT nullable, so we must fill them
$app = new Apps();
$app->setName($name);
$app->setTitle($name . ' Title');
$app->setSubDomain(strtolower($name)); // Assuming valid subdomain logic
$app->setLogoUrl('https://example.com/logo.png');
// $app->setDescription() is nullable, so we can skip or set it
$this->entityManager->persist($app);
$this->entityManager->flush();
return $app;
}
protected function createOrganization(string $name): Organizations
{
// I am assuming the Organizations entity structure here based on context
$org = new Organizations();
$org->setName($name);
$org->setEmail('contact@' . strtolower($name) . '.com');
$org->setNumber(100 + rand(1, 900)); // Example number
$org->setAddress('123 ' . $name . ' St'); // Example address
$org->setLogoUrl('https://example.com/org_logo.png');
$this->entityManager->persist($org);
$this->entityManager->flush();
return $org;
}
protected function createUOLink(User $user, Organizations $organization): UsersOrganizations{
$uo = new UsersOrganizations();
$uo->setUsers($user);
$uo->setOrganization($organization);
$uo->setIsActive(true);
$uo->setStatut("ACCEPTED");
$this->entityManager->persist($uo);
$this->entityManager->flush();
return $uo;
}
protected function createUOALink(UsersOrganizations $uo, Apps $app, Roles $role): UserOrganizatonApp{
$uoa = new UserOrganizatonApp();
$uoa->setUserOrganization($uo);
$uoa->setApplication($app);
$uoa->setRole($role);
$this->entityManager->persist($uoa);
$this->entityManager->flush();
return $uoa;
}
protected function createRole(string $name): Roles{
$role = new Roles();
$role->setName($name);
$this->entityManager->persist($role);
$this->entityManager->flush();
return $role;
}
protected function createNotification($user, string $title, bool $isRead = false): Notification{
$notification = new Notification();
$notification->setUser($user);
$notification->setTitle($title);
$notification->setMessage('This is a test notification message.');
$notification->setType('info');
$notification->setIsRead($isRead);
$this->entityManager->persist($notification);
$this->entityManager->flush();
return $notification;
}
protected function countEntities(string $entityClass): int
{
return $this->entityManager->getRepository($entityClass)->count([]);
}
}