Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-rolland committed Sep 13, 2024
1 parent 9e32f9f commit e77dec3
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Core/Module/ModuleOverrideChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function hasOverrideConflict(string $moduleOverridePath): bool
$finder = new Finder();
$finder->files()->in($moduleOverridePath)->name('*.php');

// module doesn't have overrides, return false
// The module's override folder doesn't contain any override, return false
if (!$finder->hasResults()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ public function testInstall(): void
$resource_path = dirname(__DIR__, 4) . '/Resources/modules_tests/override/';

$actual_override_cart = file_get_contents(_PS_ROOT_DIR_ . '/override/classes/Cart.php');
$expected_override_cart = file_get_contents($resource_path . '/Cart.php');
$expected_override_cart = file_get_contents($resource_path . '/classes/Cart.php');

$actual_override_cart = $this->cleanup($actual_override_cart);
$expected_override_cart = $this->cleanup($expected_override_cart);

$this->assertEquals($expected_override_cart, $actual_override_cart);

$actual_override_admin_product = file_get_contents(_PS_ROOT_DIR_ . '/override/controllers/admin/AdminProductsController.php');
$expected_override_admin_product = file_get_contents($resource_path . '/AdminProductsController.php');
$expected_override_admin_product = file_get_contents($resource_path . '/controllers/admin/AdminProductsController.php');

$this->assertEquals(
$this->cleanup($expected_override_admin_product),
Expand Down
12 changes: 12 additions & 0 deletions tests/Resources/modules_tests/testnoconflict/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>testnoconflict</name>
<displayName><![CDATA[test no conflict]]></displayName>
<version><![CDATA[1]]></version>
<description><![CDATA[A module to test no override conflict]]></description>
<author><![CDATA[fake_author]]></author>
<tab><![CDATA[front_office_features]]></tab>
<is_configurable>0</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class Cart extends CartCore
{
/**
* test for override that should trigger no conflict
*/
public function isCarrierInRange($id_carrier, $id_zone)
{
return true;
}
}
43 changes: 43 additions & 0 deletions tests/Resources/modules_tests/testnoconflict/testnoconflict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}

class testnoconflict extends Module
{
public function __construct()
{
$this->name = 'testnoconflict';
$this->tab = 'front_office_features';
$this->version = 1.0;
$this->author = 'fake author';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('no conflict');
$this->description = $this->l('A module to test no override conflict');
}
}
12 changes: 12 additions & 0 deletions tests/Resources/modules_tests/testnooverride/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>testnooverride</name>
<displayName><![CDATA[test no override]]></displayName>
<version><![CDATA[1]]></version>
<description><![CDATA[A module to test case when there is no override]]></description>
<author><![CDATA[fake_author]]></author>
<tab><![CDATA[front_office_features]]></tab>
<is_configurable>0</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
43 changes: 43 additions & 0 deletions tests/Resources/modules_tests/testnooverride/testnooverride.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}

class testnooverride extends Module
{
public function __construct()
{
$this->name = 'testnooverride';
$this->tab = 'front_office_features';
$this->version = 1.0;
$this->author = 'fake author';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('no override module');
$this->description = $this->l('A module to test override check when there is no override');
}
}
Binary file not shown.
92 changes: 92 additions & 0 deletions tests/Unit/Core/Module/ModuleOverrideCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace Tests\Unit\Core\Module;

use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Core\Module\ModuleOverrideChecker;
use Symfony\Contracts\Translation\TranslatorInterface;

class ModuleOverrideCheckerTest extends TestCase
{
/**
* @var string
*/
protected $psOverrideDir;

/**
* @var string
*/
protected $modulesTestsDir;

protected function setUp(): void
{
$this->psOverrideDir = dirname(__DIR__, 3) . '/Resources/modules_tests/override/';
$this->modulesTestsDir = dirname(__DIR__, 3) . '/Resources/modules_tests';
}

/**
* @dataProvider provideTestData
*/
public function testHasOverrideConflict(string $moduleName, bool $expectedResult): void
{
$moduleOverrideChecker = $this->getModuleOverrideChecker();

$moduleOverridePath = sprintf('%s/%s/override', $this->modulesTestsDir, $moduleName);

$this->assertEquals($expectedResult, $moduleOverrideChecker->hasOverrideConflict($moduleOverridePath));
}

private function getModuleOverrideChecker(): ModuleOverrideChecker
{
$translatorMock = $this->createMock(TranslatorInterface::class);
$translatorMock->method('trans')->willReturnArgument(0);

return new ModuleOverrideChecker($translatorMock, $this->psOverrideDir);
}

public function provideTestData(): array
{
return [
[
'testnoconflict',
false,
],
[
'testnooverride',
false,
],
[
'testbasicconflict',
true,
],
[
'testtrickyconflict',
true,
],
];
}
}

0 comments on commit e77dec3

Please sign in to comment.