-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chore): implement missing test unit again
- Loading branch information
1 parent
2de2597
commit a23aed3
Showing
7 changed files
with
313 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.