get(UserRepository::class); $admin = $this->getUser('admin'); if (empty($email)) { $email = "$username@example.com"; } if (empty($password)) { $password = $username; } $user = $repo->createUser() ->setLastname($username) ->setFirstname($username) ->setUsername($username) ->setStatus(1) ->setPlainPassword($password) ->setEmail($email) ->setCreator($admin) ->setCurrentUrl($this->getAccessUrl()) ; if (!empty($role)) { $user->addRole($role); } $repo->updateUser($user); return $user; } public function createCourse(string $title): ?Course { $repo = static::getContainer()->get(CourseRepository::class); $course = (new Course()) ->setTitle($title) ->addAccessUrl($this->getAccessUrl()) ->setCreator($this->getUser('admin')) ->setVisibility(Course::OPEN_PLATFORM) ; $repo->create($course); return $course; } public function createSession(string $title): ?Session { $repo = static::getContainer()->get(SessionRepository::class); $session = (new Session()) ->setTitle($title) ->addGeneralCoach($this->getUser('admin')) ->addAccessUrl($this->getAccessUrl()) ; $repo->update($session); return $session; } public function createGroup(string $title, Course $course): ?CGroup { $em = $this->getEntityManager(); $group = (new CGroup()) ->setTitle($title) ->setParent($course) ->setCreator($this->getUser('admin')) ->setMaxStudent(100) ; $em->persist($group); $em->flush(); return $group; } public function createUserGroup(string $title): ?Usergroup { $em = $this->getEntityManager(); $creator = $this->createUser('usergroup_creator'); $group = (new Usergroup()) ->setTitle($title) ->setDescription('desc') ->setCreator($creator) ->addAccessUrl($this->getAccessUrl()) ; $em->persist($group); $em->flush(); return $group; } /** * Finds a user registered in the test DB, added by the DataFixtures classes. */ public function getUser(string $username): ?User { /** @var UserRepository $repo */ $repo = static::getContainer()->get(UserRepository::class); return $repo->findByUsername($username); } public function getAdmin(): User { return $this->getUser('admin'); } public function getCourse($courseId): ?Course { $repo = static::getContainer()->get(CourseRepository::class); return $repo->find($courseId); } public function getAccessUrl(string $url = ''): ?AccessUrl { if (empty($url)) { $url = AccessUrl::DEFAULT_ACCESS_URL; } /** @var AccessUrlRepository $repo */ $repo = static::getContainer()->get(AccessUrlRepository::class); return $repo->findOneBy(['url' => $url]); } public function assertHasNoEntityViolations($entity) { /** @var ConstraintViolationList $errors */ $errors = $this->getViolations($entity); $message = []; foreach ($errors as $error) { $message[] = $error->getPropertyPath().': '.$error->getMessage(); } $this->assertEquals(0, $errors->count(), implode(', ', $message)); } public function getUploadedFileArray(): array { return [ 'tmp_name' => $this->getUploadedFile()->getRealPath(), 'name' => $this->getUploadedFile()->getFilename(), 'type' => $this->getUploadedFile()->getMimeType(), 'size' => $this->getUploadedFile()->getSize(), 'error' => UPLOAD_ERR_OK, ]; } public function getUploadedFile(): UploadedFile { $path = $this->getContainer()->get('kernel')->getProjectDir(); $filePath = $path.'/tests/fixtures/logo.png'; $fileName = basename($filePath); return new UploadedFile( $filePath, $fileName, 'image/png', ); } public function getUploadedZipFile(): UploadedFile { $path = $this->getContainer()->get('kernel')->getProjectDir(); $filePath = $path.'/tests/fixtures/logo.zip'; $fileName = basename($filePath); return new UploadedFile( $filePath, $fileName, 'image/png', ); } public function getViolations($entity) { /** @var ValidatorInterface $validator */ $validator = static::$kernel->getContainer()->get('validator'); /** @var ConstraintViolationList $errors */ return $validator->validate($entity); } public function convertToUTCAndFormat(\DateTime $localTime) : string { return $localTime->setTimezone(new \DateTimeZone('UTC'))->format('c'); } public function getEntityManager(): EntityManager { return static::getContainer()->get('doctrine')->getManager(); } }