Tests: Add phpunit tests

pull/3965/head
Julio 4 years ago
parent 0dad0d454f
commit 9ab747d142
  1. 15
      src/CourseBundle/Entity/CCourseDescription.php
  2. 14
      src/CourseBundle/Entity/CCourseSetting.php
  3. 19
      tests/ChamiloTestTrait.php
  4. 4
      tests/CoreBundle/Controller/AccountControllerTest.php
  5. 3
      tests/CoreBundle/Controller/Admin/AdminControllerTest.php
  6. 4
      tests/CoreBundle/Controller/Admin/SettingsControllerTest.php
  7. 2
      tests/CoreBundle/Controller/AssetControllerTest.php
  8. 31
      tests/CoreBundle/Controller/CourseControllerTest.php
  9. 5
      tests/CoreBundle/Controller/IndexControllerTest.php
  10. 3
      tests/CoreBundle/Repository/AssetRepositoryTest.php
  11. 3
      tests/CoreBundle/Repository/CourseCategoryRepositoryTest.php

@ -49,6 +49,7 @@ class CCourseDescription extends AbstractResource implements ResourceInterface
/**
* @ORM\Column(name="description_type", type="integer", nullable=false)
*/
#[Assert\Choice(callback: 'getTypes')]
protected int $descriptionType;
/**
@ -68,6 +69,20 @@ class CCourseDescription extends AbstractResource implements ResourceInterface
return $this->getTitle();
}
public static function getTypes(): array
{
return [
self::TYPE_DESCRIPTION,
self::TYPE_OBJECTIVES,
self::TYPE_TOPICS,
self::TYPE_METHODOLOGY,
self::TYPE_COURSE_MATERIAL,
self::TYPE_RESOURCES,
self::TYPE_ASSESSMENT,
self::TYPE_CUSTOM,
];
}
public function setTitle(string $title): self
{
$this->title = $title;

@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* CCourseSetting.
* Course settings.
*
* @ORM\Table(
* name="c_course_setting",
@ -32,13 +32,13 @@ class CCourseSetting
/**
* @ORM\Column(name="c_id", type="integer")
*/
#[Assert\NotBlank]
protected int $cId;
/**
* @Assert\NotBlank()
*
* @ORM\Column(name="variable", type="string", length=255, nullable=false)
*/
#[Assert\NotBlank]
protected string $variable;
/**
@ -64,6 +64,7 @@ class CCourseSetting
/**
* @ORM\Column(name="title", type="string", length=255, nullable=false)
*/
#[Assert\NotBlank]
protected ?string $title = null;
/**
@ -83,12 +84,7 @@ class CCourseSetting
return $this;
}
/**
* Get variable.
*
* @return string
*/
public function getVariable()
public function getVariable(): string
{
return $this->variable;
}

@ -54,17 +54,24 @@ trait ChamiloTestTrait
/** @var UserRepository $repo */
$repo = static::getContainer()->get(UserRepository::class);
// retrieve user
return $repo->findByUsername($username);
}
public function createCourse($title): ?Course
public function getCourse($courseId): ?Course
{
$repo = self::getContainer()->get(CourseRepository::class);
$repo = static::getContainer()->get(CourseRepository::class);
return $repo->find($courseId);
}
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);
@ -72,9 +79,9 @@ trait ChamiloTestTrait
return $course;
}
public function createSession($title): ?Session
public function createSession(string $title): ?Session
{
$repo = self::getContainer()->get(SessionRepository::class);
$repo = static::getContainer()->get(SessionRepository::class);
$session = (new Session())
->setName($title)
@ -140,6 +147,6 @@ trait ChamiloTestTrait
public function getEntityManager(): EntityManager
{
return self::getContainer()->get('doctrine')->getManager();
return static::getContainer()->get('doctrine')->getManager();
}
}

@ -8,7 +8,6 @@ namespace Chamilo\Tests\CoreBundle\Controller;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
class AccountControllerTest extends WebTestCase
{
@ -25,8 +24,7 @@ class AccountControllerTest extends WebTestCase
$client->loginUser($admin);
$client->request('GET', '/account/edit');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
$client->submitForm('Update profile', [
'profile[firstname]' => 'admin firstname',

@ -8,7 +8,6 @@ namespace Chamilo\Tests\CoreBundle\Controller\Admin;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
class AdminControllerTest extends WebTestCase
{
@ -25,6 +24,6 @@ class AdminControllerTest extends WebTestCase
$client->loginUser($admin);
$client->request('GET', '/admin/settings/platform');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
}
}

@ -17,10 +17,7 @@ class SettingsControllerTest extends WebTestCase
public function testIndex(): void
{
$client = static::createClient();
// retrieve the admin
$admin = $this->getUser('admin');
$client->loginUser($admin);
$client->request('GET', '/admin/settings/admin');
@ -34,7 +31,6 @@ class SettingsControllerTest extends WebTestCase
{
$client = static::createClient();
// retrieve the admin
$admin = $this->getUser('admin');
$client->loginUser($admin);

@ -36,7 +36,7 @@ class AssetControllerTest extends WebTestCase
$url = $assetRepo->getAssetUrl($asset);
$client->request('GET', $url);
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
$client->request('GET', $url.'not-existed');
$this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $client->getResponse()->getStatusCode());

@ -9,7 +9,6 @@ namespace Chamilo\Tests\CoreBundle\Controller;
use Chamilo\CourseBundle\Entity\CCourseDescription;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
class CourseControllerTest extends WebTestCase
{
@ -38,9 +37,7 @@ class CourseControllerTest extends WebTestCase
$client->loginUser($admin);
$client->request('GET', '/course/'.$course->getId().'/welcome');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
$this->assertStringContainsString('new course', $client->getResponse()->getContent());
}
@ -57,22 +54,24 @@ class CourseControllerTest extends WebTestCase
$course->addTeacher($teacher);
$em = $this->getEntityManager();
$item = (new CCourseDescription())
->setTitle('title')
->setContent('content')
->setDescriptionType(1)
->setProgress(100)
->setParent($course)
->setCreator($teacher)
->addCourseLink($course)
;
$em->persist($item);
$types = CCourseDescription::getTypes();
foreach ($types as $type) {
$item = (new CCourseDescription())
->setTitle('title')
->setContent('content')
->setDescriptionType($type)
->setProgress(100)
->setParent($course)
->setCreator($teacher)
->addCourseLink($course)
;
$em->persist($item);
}
$em->persist($course);
$em->flush();
$client->request('GET', '/course/'.$course->getId().'/about');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
$this->assertStringContainsString('new course', $client->getResponse()->getContent());
}
}

@ -33,8 +33,7 @@ class IndexControllerTest extends WebTestCase
$client->loginUser($admin);
$client->request('GET', '/toggle_student_view');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
}
public function testLogout(): void
@ -50,7 +49,7 @@ class IndexControllerTest extends WebTestCase
$client->loginUser($admin);
$client->request('GET', '/account/home');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
$this->assertResponseIsSuccessful();
$client->request('GET', '/logout');
$this->assertResponseRedirects($defaultUrl);

@ -90,8 +90,7 @@ class AssetRepositoryTest extends AbstractApiTest
$this->assertNotEmpty($url);
$client = static::createClient();
$response = $client->request('GET', $url);
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
$asset = $assetRepo->find($asset->getId());

@ -12,7 +12,6 @@ use Chamilo\CoreBundle\Repository\AssetRepository;
use Chamilo\CoreBundle\Repository\CourseCategoryRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Component\HttpFoundation\Response;
class CourseCategoryRepositoryTest extends AbstractApiTest
{
@ -116,7 +115,7 @@ class CourseCategoryRepositoryTest extends AbstractApiTest
$this->assertNotEmpty($content);
$response = $client->request('GET', $url);
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
$this->assertResponseIsSuccessful();
$this->assertSame(1, $assetRepo->count([]));
$em->clear();

Loading…
Cancel
Save