Skip to content

Commit

Permalink
feat(chore): implement missing test unit again
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisdugue committed Nov 10, 2024
1 parent 2de2597 commit a23aed3
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 30 deletions.
20 changes: 13 additions & 7 deletions Core/H5PIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,37 @@ class H5PIntegration extends H5PUtils
/**
* @var EntityManager
*/
private $entityManager;
private EntityManager $entityManager;

/**
* @var \H5PCore
*/
private $core;
private \H5PCore $core;

/**
* @var RouterInterface
*/
private $router;
private RouterInterface $router;

/**
* @var H5POptions
*/
private $options;
private H5POptions $options;

/**
* @var RequestStack
*/
private $requestStack;
private RequestStack $requestStack;

/**
* @var Packages
*/
private $assetsPaths;
private Packages $assetsPaths;

/**
* @var \H5PContentValidator
*/
private $contentValidator;
private \H5PContentValidator $contentValidator;

/**
* H5PContent constructor.
Expand Down
9 changes: 4 additions & 5 deletions Core/H5POptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class H5POptions
/**
* @var array
*/
private $config;
private array|null $config;
/**
* @var array
*/
private $storedConfig = null;
private array|null $storedConfig = null;

private $h5pPath;
private $folderPath;
private $projectRootDir;
/**
* @var EntityManagerInterface
*/
private $manager;
private EntityManagerInterface $manager;

/**
* H5POptions constructor.
Expand Down Expand Up @@ -139,8 +139,7 @@ public function getAbsoluteH5PPath(): string
{
$dir = $this->getOption('storage_dir');
$dir = $dir[0] === '/' ? $dir : "/{$dir}";

return $this->getAbsoluteWebPath() . $dir;
return rtrim($this->getAbsoluteWebPath(), '/') . $dir;
}

public function getAbsoluteWebPath(): string
Expand Down
3 changes: 2 additions & 1 deletion Event/H5PEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class H5PEvents extends \H5PEventBase
/**
* @var EntityManagerInterface $em
*/
private $em;
private EntityManagerInterface $em;

/**
* H5PEvents constructor.
* @param $type
Expand Down
39 changes: 22 additions & 17 deletions Event/LibrarySemanticsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,54 @@

class LibrarySemanticsEvent extends Event
{
private $semantics;
private $name;
private $majorVersion;
private $minorVersion;
private array $semantics;
private string $name;
private int $majorVersion;
private int $minorVersion;

/**
* LibrarySemanticsEvent constructor.
* @param $semantics
* @param $name
* @param $majorVersion
* @param $minorVersion
* @param array $semantics array of semantics
* @param string $name Nom of package
* @param int $majorVersion number of major version
* @param int $minorVersion number of minor version
*/
public function __construct($semantics, $name, $majorVersion, $minorVersion)
public function __construct(array $semantics, string $name, int $majorVersion, int $minorVersion)
{
$this->semantics = $semantics;
$this->name = $name;
$this->majorVersion = $majorVersion;
$this->minorVersion = $minorVersion;
}

/**
* @return mixed
* @return array
*/
public function getSemantics()
public function getSemantics(): array
{
return $this->semantics;
}

/**
* @return mixed
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* @return mixed
* @return int
*/
public function getMajorVersion()
public function getMajorVersion(): int
{
return $this->majorVersion;
}

/**
* @return mixed
* @return int
*/
public function getMinorVersion()
public function getMinorVersion(): int
{
return $this->minorVersion;
}
Expand Down
116 changes: 116 additions & 0 deletions Tests/Core/H5PIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Studit\H5PBundle\Tests\Core;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use H5PCore;
use H5peditor;
use H5PContentValidator;
use H5PFrameworkInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Studit\H5PBundle\Core\H5PIntegration;
use Studit\H5PBundle\Core\H5POptions;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class H5PIntegrationTest extends TestCase
{
private H5PIntegration|MockObject $h5pIntegration;
private H5POptions|MockObject $options;
private H5PCore|MockObject $core;
private EntityManagerInterface|MockObject $entityManager;
private RouterInterface|MockObject $router;
private RequestStack|MockObject $requestStack;
private Packages|MockObject $assetsPaths;
private H5PContentValidator|MockObject $contentValidator;

protected function setUp(): void
{
$this->core = $this->createMock(H5PCore::class);
$this->options = $this->createMock(H5POptions::class);
$tokenStorage = $this->createMock(TokenStorageInterface::class);
$this->entityManager = $this->createMock(EntityManager::class);
$this->router = $this->createMock(RouterInterface::class);
$this->requestStack = $this->createMock(RequestStack::class);
$this->assetsPaths = $this->createMock(Packages::class);
$this->contentValidator = $this->createMock(H5PContentValidator::class);

// Création de l'instance de H5PIntegration pour les tests
$this->h5pIntegration = new H5PIntegration(
$this->core,
$this->options,
$tokenStorage,
$this->entityManager,
$this->router,
$this->requestStack,
$this->assetsPaths,
$this->contentValidator
);
}

public function testGetGenericH5PIntegrationSettings()
{
$request = new Request();
$this->requestStack->method('getMainRequest')->willReturn($request);

$this->options->method('getOption')->willReturnMap([
['save_content_state', false, true],
['save_content_frequency', 30, 30],
['hub_is_enabled', true, true]
]);
$h5pFrameworkMock = $this->createMock(H5PFrameworkInterface::class);
$h5pFrameworkMock->method('getLibraryConfig')->willReturn(['someKey' => 'someValue']);

// Injectez le mock H5PFramework dans H5PCore
$this->core->h5pF = $h5pFrameworkMock;

$settings = $this->h5pIntegration->getGenericH5PIntegrationSettings();

$this->assertIsArray($settings);
$this->assertArrayHasKey('baseUrl', $settings);
$this->assertArrayHasKey('ajax', $settings);
$this->assertArrayHasKey('l10n', $settings);
}

public function testGetCoreAssets()
{
$this->options->method('getH5PAssetPath')->willReturn('/assets/h5p');
H5PCore::$scripts = ['script1.js', 'script2.js'];
H5PCore::$styles = ['style1.css', 'style2.css'];

$assets = $this->h5pIntegration->getCoreAssets();

$this->assertIsArray($assets);
$this->assertArrayHasKey('scripts', $assets);
$this->assertArrayHasKey('styles', $assets);
$this->assertCount(2, $assets['scripts']);
$this->assertCount(2, $assets['styles']);
}

public function testGetCacheBuster()
{
H5PCore::$coreApi = ['majorVersion' => 1, 'minorVersion' => 2];
$cacheBuster = $this->h5pIntegration->getCacheBuster();

$this->assertEquals('?=1.2', $cacheBuster);
}

public function testGetTranslationFilePath()
{
$request = new Request();
$request->setLocale('en');
$this->requestStack->method('getCurrentRequest')->willReturn($request);

$this->options->method('getAbsoluteWebPath')->willReturn('/web');

$translationFilePath = $this->h5pIntegration->getTranslationFilePath();

$this->assertStringContainsString('/h5p-editor/language/en.js', $translationFilePath);
}

}
111 changes: 111 additions & 0 deletions Tests/Core/H5POptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Studit\H5PBundle\Tests\Core;

use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Studit\H5PBundle\Entity\Option;
use Studit\H5PBundle\Core\H5POptions;
use Doctrine\DBAL\Exception\DriverException;
use PHPUnit\Framework\MockObject\MockObject;

class H5POptionsTest extends TestCase
{
private H5POptions $h5pOptions;
private EntityManagerInterface|MockObject $entityManager;
private EntityRepository|MockObject $repository;

protected function setUp(): void
{
// Créez un mock pour l'EntityManager
$this->entityManager = $this->createMock(EntityManagerInterface::class);
// Créez un mock pour l'EntityRepository
$this->repository = $this->createMock(EntityRepository::class);

// Configurez l'EntityManager pour retourner un mock de repository
$this->entityManager->method('getRepository')->willReturn($this->repository);

// Initialisez H5POptions avec les dépendances mockées
$this->h5pOptions = new H5POptions(
['storage_dir' => '/var/www/html'], // Config de test
'/var/www', // projectRootDir de test
$this->entityManager
);
}

public function testGetOptionReturnsStoredConfigValue()
{
// Simule la méthode findAll() du repository pour retourner une option
$option = $this->createMock(Option::class);
$option->method('getName')->willReturn('storage_dir');
$option->method('getValue')->willReturn('/tmp/h5p');

// Configurez le repository pour retourner cette option
$this->repository->method('findAll')->willReturn([$option]);

// Testez la méthode getOption
$result = $this->h5pOptions->getOption('storage_dir');
$this->assertEquals('/tmp/h5p', $result);
}

public function testSetOptionStoresNewOptionValue()
{
// Créez un mock de l'option à persister
$option = $this->createMock(Option::class);
$option->method('getName')->willReturn('storage_dir');

// Simulez la recherche de l'option dans le repository
$this->repository->method('find')->willReturn(null); // Aucun option trouvée, il faut créer une nouvelle

// Configurez l'EntityManager pour simuler les méthodes persist et flush
$this->entityManager->expects($this->once())->method('persist')->with($this->isInstanceOf(Option::class));
$this->entityManager->expects($this->once())->method('flush');

// Appelez setOption et vérifiez que persist et flush sont bien appelées
$this->h5pOptions->setOption('storage_dir', '/new/path/h5p');
}

public function testGetOptionReturnsDefaultIfOptionNotFound()
{
// Configurez le mock pour retourner une liste vide d'options
$this->repository->method('findAll')->willReturn([]);

// Testez la méthode getOption avec une option qui n'existe pas
$result = $this->h5pOptions->getOption('non_existent_option', 'default_value');
$this->assertEquals('default_value', $result);
}

public function testRetrieveStoredConfigHandlesDriverException()
{
// Testez la gestion de l'exception dans la méthode retrieveStoredConfig
$this->expectNotToPerformAssertions();
try {
$this->h5pOptions->getOption('storage_dir');
} catch (DriverException $e) {
// Vérifiez que l'exception est bien attrapée
$this->assertEquals('Database error', $e->getMessage());
}
}

public function testGetUploadedH5pFolderPath()
{
// Testez le getter et setter de folderPath
$this->h5pOptions->getUploadedH5pFolderPath('/custom/folder');
$this->assertEquals('/custom/folder', $this->h5pOptions->getUploadedH5pFolderPath());
}

public function testGetRelativeH5PPath()
{
// Testez la méthode getRelativeH5PPath pour obtenir le chemin relatif
$this->h5pOptions->setOption('storage_dir', 'var/h5p');
$this->assertEquals('/var/h5p', $this->h5pOptions->getRelativeH5PPath());
}

public function testGetAbsoluteH5PPath()
{
// Testez la méthode getAbsoluteH5PPath pour obtenir le chemin absolu
$this->h5pOptions->setOption('storage_dir', 'var/h5p');
$this->assertStringContainsString('/var/www/var/h5p', $this->h5pOptions->getAbsoluteH5PPath());
}
}
Loading

0 comments on commit a23aed3

Please sign in to comment.