Skip to content

Commit

Permalink
Tracking: replace mockery in status icons unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schmitz-ilias committed Oct 29, 2024
1 parent f3871bf commit 6df06c1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 57 deletions.
39 changes: 23 additions & 16 deletions components/ILIAS/Tracking/classes/status/class.ilLPStatusIcons.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*********************************************************************/

declare(strict_types=1);

/**
* Caches and supplies the paths to the learning progress status images.
* @author Tim Schmitz <[email protected]>
Expand Down Expand Up @@ -82,35 +84,40 @@ private function __construct(int $variant, \ILIAS\UI\Renderer $renderer, \ILIAS\

switch ($variant) {
case ilLPStatusIcons::ICON_VARIANT_SCORM:
$this->image_path_in_progress = ilUtil::getImagePath('scorm/incomplete.svg');
$this->image_path_completed = ilUtil::getImagePath('scorm/complete.svg');
$this->image_path_not_attempted = ilUtil::getImagePath('scorm/not_attempted.svg');
$this->image_path_failed = ilUtil::getImagePath('scorm/failed.svg');
$this->image_path_asset = ilUtil::getImagePath('scorm/asset.svg');
$this->image_path_running = ilUtil::getImagePath('scorm/running.svg');
$this->image_path_in_progress = $this->buildImagePath('scorm/incomplete.svg');
$this->image_path_completed = $this->buildImagePath('scorm/complete.svg');
$this->image_path_not_attempted = $this->buildImagePath('scorm/not_attempted.svg');
$this->image_path_failed = $this->buildImagePath('scorm/failed.svg');
$this->image_path_asset = $this->buildImagePath('scorm/asset.svg');
$this->image_path_running = $this->buildImagePath('scorm/running.svg');
break;

case ilLPStatusIcons::ICON_VARIANT_SHORT:
$this->image_path_in_progress = ilUtil::getImagePath('learning_progress/short/in_progress.svg');
$this->image_path_completed = ilUtil::getImagePath('learning_progress/short/completed.svg');
$this->image_path_not_attempted = ilUtil::getImagePath('learning_progress/short/not_attempted.svg');
$this->image_path_failed = ilUtil::getImagePath('learning_progress/short/failed.svg');
$this->image_path_asset = ilUtil::getImagePath('learning_progress/short/asset.svg');
$this->image_path_running = ilUtil::getImagePath('learning_progress/short/running.svg');
$this->image_path_in_progress = $this->buildImagePath('learning_progress/short/in_progress.svg');
$this->image_path_completed = $this->buildImagePath('learning_progress/short/completed.svg');
$this->image_path_not_attempted = $this->buildImagePath('learning_progress/short/not_attempted.svg');
$this->image_path_failed = $this->buildImagePath('learning_progress/short/failed.svg');
$this->image_path_asset = $this->buildImagePath('learning_progress/short/asset.svg');
$this->image_path_running = $this->buildImagePath('learning_progress/short/running.svg');
break;

case ilLPStatusIcons::ICON_VARIANT_LONG:
$this->image_path_in_progress = ilUtil::getImagePath('learning_progress/in_progress.svg');
$this->image_path_completed = ilUtil::getImagePath('learning_progress/completed.svg');
$this->image_path_not_attempted = ilUtil::getImagePath('learning_progress/not_attempted.svg');
$this->image_path_failed = ilUtil::getImagePath('learning_progress/failed.svg');
$this->image_path_in_progress = $this->buildImagePath('learning_progress/in_progress.svg');
$this->image_path_completed = $this->buildImagePath('learning_progress/completed.svg');
$this->image_path_not_attempted = $this->buildImagePath('learning_progress/not_attempted.svg');
$this->image_path_failed = $this->buildImagePath('learning_progress/failed.svg');
break;

default:
throw new ilLPException("No such variant of the LP icons exists.");
}
}

protected function buildImagePath(string $image_name): string
{
return ilUtil::getImagePath($image_name);
}

public function getImagePathInProgress(): string
{
return $this->image_path_in_progress;
Expand Down
106 changes: 65 additions & 41 deletions components/ILIAS/Tracking/tests/ilLPStatusIconsTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
Expand All @@ -18,11 +16,14 @@
*
*********************************************************************/

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use ILIAS\UI\Component\Symbol\Icon\Icon;
use ILIAS\UI\Renderer;
use ILIAS\UI\Factory;
use ILIAS\UI\Renderer as UIRenderer;
use ILIAS\UI\Factory as UIFactory;
use ILIAS\UI\Component\Symbol\Factory as SymbolFactory;
use ILIAS\UI\Component\Symbol\Icon\Factory as IconFactory;
use ILIAS\UI\Component\Symbol\Icon\Custom;

/**
Expand All @@ -35,48 +36,60 @@ class ilLPStatusIconsTest extends TestCase
protected $alt = 'alt';
protected $size = Icon::SMALL;

/**
* @return array<string, ilLPStatusIcons>
*/
public function testTripleton(): array
protected function getUIFactory(): UIFactory
{
$this->markTestSkipped('Data Provider needs to be revisited.');

$utilMock = Mockery::mock('alias:' . ilUtil::class);
$utilMock->shouldReceive('getImagePath')
->with(Mockery::type('string'))
->andReturnUsing(function ($arg) {
return 'test/' . $arg;
});
$custom_icon = $this->createMock(Custom::class);
$custom_icon->method('getIconPath')
->willReturn($this->path);
$custom_icon->method('getSize')
->willReturn($this->size);
$custom_icon->method('getLabel')
->willReturn($this->alt);

$icon_factory = $this->createMock(IconFactory::class);
$icon_factory->method('custom')
->willReturn($custom_icon);

$symbol_factory = $this->createMock(SymbolFactory::class);
$symbol_factory->method('icon')
->willReturn($icon_factory);

$factory = $this->createMock(UIFactory::class);
$factory->method('symbol')
->willReturn($symbol_factory);

return $factory;
}

$renderer = Mockery::mock(Renderer::class);
$renderer->shouldReceive('render')
->andReturnUsing(function ($arg1) {
return 'rendered: path(' . $arg1->getIconPath() .
'), alt(' . $arg1->getLabel() .
'), size(' . $arg1->getSize() . ')';
protected function getUIRenderer(): UIRenderer
{
$renderer = $this->createMock(UIRenderer::class);
$renderer->method('render')
->willReturnCallback(function ($arg) {
return 'rendered: path(' . $arg->getIconPath() .
'), alt(' . $arg->getLabel() .
'), size(' . $arg->getSize() . ')';
});

$custom_icon = Mockery::mock(Custom::class);
$custom_icon->shouldReceive('getIconPath')
->andReturn($this->path);
$custom_icon->shouldReceive('getSize')
->andReturn($this->size);
$custom_icon->shouldReceive('getLabel')
->andReturn($this->alt);
return $renderer;
}

$factory = Mockery::mock(Factory::class);
$factory->shouldReceive('symbol->icon->custom')
->andReturn($custom_icon);
/**
* @return array<string, ilLPStatusIcons>
*/
public function testTripleton(): array
{
$factory = $this->getUIFactory();
$renderer = $this->getUIRenderer();

$long1 = ilLPStatusIcons::getInstance(ilLPStatusIcons::ICON_VARIANT_LONG, $renderer, $factory);
$long2 = ilLPStatusIcons::getInstance(ilLPStatusIcons::ICON_VARIANT_LONG, $renderer, $factory);
$long1 = ilLPStatusIconsMock::getInstance(ilLPStatusIcons::ICON_VARIANT_LONG, $renderer, $factory);
$long2 = ilLPStatusIconsMock::getInstance(ilLPStatusIcons::ICON_VARIANT_LONG, $renderer, $factory);

$short1 = ilLPStatusIcons::getInstance(ilLPStatusIcons::ICON_VARIANT_SHORT, $renderer, $factory);
$short2 = ilLPStatusIcons::getInstance(ilLPStatusIcons::ICON_VARIANT_SHORT, $renderer, $factory);
$short1 = ilLPStatusIconsMock::getInstance(ilLPStatusIcons::ICON_VARIANT_SHORT, $renderer, $factory);
$short2 = ilLPStatusIconsMock::getInstance(ilLPStatusIcons::ICON_VARIANT_SHORT, $renderer, $factory);

$scorm1 = ilLPStatusIcons::getInstance(ilLPStatusIcons::ICON_VARIANT_SCORM, $renderer, $factory);
$scorm2 = ilLPStatusIcons::getInstance(ilLPStatusIcons::ICON_VARIANT_SCORM, $renderer, $factory);
$scorm1 = ilLPStatusIconsMock::getInstance(ilLPStatusIcons::ICON_VARIANT_SCORM, $renderer, $factory);
$scorm2 = ilLPStatusIconsMock::getInstance(ilLPStatusIcons::ICON_VARIANT_SCORM, $renderer, $factory);

$this->assertSame($short1, $short2);
$this->assertSame($long1, $long2);
Expand All @@ -91,11 +104,11 @@ public function testTripleton(): array

public function testGetInstanceForInvalidVariant(): void
{
$renderer = $this->getMockBuilder(Renderer::class)
$renderer = $this->getMockBuilder(UIRenderer::class)
->disableOriginalConstructor()
->getMock();

$factory = $this->getMockBuilder(Factory::class)
$factory = $this->getMockBuilder(UIFactory::class)
->disableOriginalConstructor()
->getMock();

Expand Down Expand Up @@ -174,3 +187,14 @@ public function testRenderScormIcons(array $instances): void
$instances['scorm']->renderIcon('path', 'alt');
}
}

/**
* Mocks out calls to ilUtil::getImagePath
*/
class ilLPStatusIconsMock extends ilLPStatusIcons
{
protected function buildImagePath(string $image_name): string
{
return 'test/' . $image_name;
}
}

0 comments on commit 6df06c1

Please sign in to comment.