Skip to content

Commit

Permalink
fix: add test in function refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Freoua-Alma committed Oct 29, 2024
1 parent 2c8eb85 commit fb115ce
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 10 deletions.
6 changes: 2 additions & 4 deletions alma/lib/Factories/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

namespace Alma\PrestaShop\Factories;

use Category;

if (!defined('_PS_VERSION_')) {
exit;
}
Expand All @@ -38,10 +36,10 @@ class CategoryFactory
/**
* @param int $id
*
* @return Category
* @return \Category
*/
public function create($id = null, $idLang = null)
{
return new Category($id, $idLang);
return new \Category($id, $idLang);
}
}
7 changes: 5 additions & 2 deletions alma/lib/Helpers/ModuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class ModuleHelper
*/
protected $moduleFactory;

public function __construct()
public function __construct($moduleFactory = null)
{
$moduleFactoryBuilder = new ModuleFactoryBuilder();
$this->moduleFactory = $moduleFactoryBuilder->getInstance();
if ($moduleFactory == null) {
$moduleFactory = $moduleFactoryBuilder->getInstance();
}
$this->moduleFactory = $moduleFactory;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion alma/lib/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ public function getCategoriesExcludedNames()
} catch (AlmaException $e) {
$category = $this->categoryFactory->create($id);
}
if ($this->validateHelper->isLoadedObject($category)) {
if ($this->validateHelper->isLoadedObject($category) && $category->name !== null) {
$categoriesNames[] = $category->name;
}
}
Expand Down
34 changes: 34 additions & 0 deletions alma/tests/Unit/Factories/ModuleFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,38 @@ public function testIsInstalledPsBefore17()
$moduleFactory->shouldNotReceive('isInstalledAfter17');
$moduleFactory->isInstalled('fakename');
}

/**
* @dataProvider moduleDataProvider
*
* @return void
*/
public function testGetModuleVersion($module, $expectedVersion)
{
if ($module) {
$module->version = $expectedVersion;
}
$moduleFactoryPartialMock = $this->getMockBuilder(ModuleFactory::class)
->setConstructorArgs([$this->toolsHelperMock])
->setMethods(['getModule'])
->getMock();
$moduleFactoryPartialMock->expects($this->once())
->method('getModule')
->willReturn($module);
$this->assertEquals($expectedVersion, $moduleFactoryPartialMock->getModuleVersion());
}

public function moduleDataProvider()
{
return [
'With module' => [
'module' => $this->createMock(\Module::class),
'expectedVersion' => '4.4.0',
],
'Without module' => [
'module' => false,
'expectedVersion' => '',
],
];
}
}
66 changes: 63 additions & 3 deletions alma/tests/Unit/Helper/ModuleHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,75 @@ class ModuleHelperTest extends TestCase
* @var ModuleHelper
*/
protected $moduleHelper;
/**
* @var ModuleFactory
*/
protected $moduleFactoryMock;

public function setUp()
{
$this->moduleFactoryMock = $this->createMock(ModuleFactory::class);
$this->moduleHelper = new ModuleHelper();
$this->moduleHelper = new ModuleHelper(
$this->moduleFactoryMock
);
}

/**
* @dataProvider modulesInstalledDataProvider
*
* @return void
*/
public function testGetModuleListWithListOfModules($moduleInstalled, $expectedModulesList)
{
$this->moduleFactoryMock->expects($this->once())
->method('getModulesInstalled')
->willReturn($moduleInstalled);
$this->assertEquals($expectedModulesList, $this->moduleHelper->getModuleList());
}

public function testGetModuleListWithListOfModules()
public function modulesInstalledDataProvider()
{
$this->assertEquals([], $this->moduleHelper->getModuleList());
return [
'With Modules installed' => [
'Modules installed' => [
[
'id_module' => '1',
'name' => 'blockwishlist',
'active' => '1',
'version' => '2.1.0',
],
[
'id_module' => '2',
'name' => 'contactform',
'active' => '1',
'version' => '4.3.0',
],
[
'id_module' => '3',
'name' => 'dashactivity',
'active' => '1',
'version' => '2.0.2',
],
],
'Modules list' => [
[
'name' => 'blockwishlist',
'version' => '2.1.0',
],
[
'name' => 'contactform',
'version' => '4.3.0',
],
[
'name' => 'dashactivity',
'version' => '2.0.2',
],
],
],
'With no module installed' => [
'Modules installed' => [],
'Modules list' => [],
],
];
}
}
97 changes: 97 additions & 0 deletions alma/tests/Unit/Helper/SettingsHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use Alma\API\Entities\FeePlan;
use Alma\PrestaShop\Builders\Helpers\SettingsHelperBuilder;
use Alma\PrestaShop\Exceptions\AlmaException;
use Alma\PrestaShop\Factories\CategoryFactory;
use Alma\PrestaShop\Factories\ContextFactory;
use Alma\PrestaShop\Forms\InpageAdminFormBuilder;
Expand Down Expand Up @@ -529,4 +530,100 @@ public function inPageSettingsDataProvider()
],
];
}

public function testGetCategoriesExcludedNamesWithNoCategories()
{
$this->configurationHelperMock->expects($this->once())
->method('get')
->with(SettingsHelper::ALMA_EXCLUDED_CATEGORIES)
->willReturn(false);
$this->assertEquals([], $this->settingsHelper->getCategoriesExcludedNames());
}

/**
* @return void
*/
public function testGetCategoriesExcludedNamesWithCategoriesDontExist()
{
$categories = [
10 => $this->createMock(\Category::class),
11 => $this->createMock(\Category::class),
];

$this->configurationHelperMock->expects($this->once())
->method('get')
->with(SettingsHelper::ALMA_EXCLUDED_CATEGORIES)
->willReturn('[10, 11]');
$this->categoryFactoryMock->expects($this->exactly(2))
->method('create')
->withConsecutive([10, 1], [11, 1])
->willReturnOnConsecutiveCalls($categories[10], $categories[11]);
$this->contextFactoryMock->expects($this->exactly(2))
->method('getContextLanguageId')
->willReturn(1);
$this->validateHelperMock->expects($this->exactly(2))
->method('isLoadedObject')
->withConsecutive([$categories[10]], [$categories[11]])
->willReturnOnConsecutiveCalls(false, false);
$this->assertEquals([], $this->settingsHelper->getCategoriesExcludedNames());
}

public function testGetCategoriesExcludedNamesWithReturnOnlyCategoryExisting()
{
/** @var \Category[] $categories */
$categories = [
10 => $this->createMock(\Category::class),
11 => $this->createMock(\Category::class),
];

$categories[10]->id = 10;
$categories[10]->name = 'category 10';

$this->configurationHelperMock->expects($this->once())
->method('get')
->with(SettingsHelper::ALMA_EXCLUDED_CATEGORIES)
->willReturn('[10, 11]');
$this->categoryFactoryMock->expects($this->exactly(2))
->method('create')
->withConsecutive([10, 1], [11, 1])
->willReturnOnConsecutiveCalls($categories[10], $categories[11]);
$this->contextFactoryMock->expects($this->exactly(2))
->method('getContextLanguageId')
->willReturn(1);
$this->validateHelperMock->expects($this->exactly(2))
->method('isLoadedObject')
->withConsecutive([$categories[10]], [$categories[11]])
->willReturnOnConsecutiveCalls(true, false);
$this->assertEquals(['category 10'], $this->settingsHelper->getCategoriesExcludedNames());
}

public function testGetCategoriesExcludedNamesWithGetContextLanguageIdThrowException()
{
/** @var \Category[] $categories */
$categories = [
10 => $this->createMock(\Category::class),
11 => $this->createMock(\Category::class),
];

$categories[10]->id = 10;
$categories[10]->name = 'category 10';

$this->configurationHelperMock->expects($this->once())
->method('get')
->with(SettingsHelper::ALMA_EXCLUDED_CATEGORIES)
->willReturn('[10, 11]');

$this->contextFactoryMock->expects($this->exactly(2))
->method('getContextLanguageId')
->willThrowException(new AlmaException('Context language is null'));
$this->categoryFactoryMock->expects($this->exactly(2))
->method('create')
->withConsecutive([10], [11])
->willReturnOnConsecutiveCalls($categories[10], $categories[11]);
$this->validateHelperMock->expects($this->exactly(2))
->method('isLoadedObject')
->withConsecutive([$categories[10]], [$categories[11]])
->willReturnOnConsecutiveCalls(true, false);
$this->assertEquals(['category 10'], $this->settingsHelper->getCategoriesExcludedNames());
}
}

0 comments on commit fb115ce

Please sign in to comment.