Skip to content

Commit

Permalink
Added unit tests to checks (#8)
Browse files Browse the repository at this point in the history
* Added unit tests to checks

* PHP-CS-Fixer

* Sonar fixes

* Fixes namespace
  • Loading branch information
sreichel authored May 28, 2024
1 parent 28bbb75 commit 8198718
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 81 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
PhpCsFixer\Finder::create()
->in([
'app/code/core/Mage/',
'dev/tests/unit/',
'lib/Mage/',
'lib/Magento/',
'lib/Varien/',
Expand Down
1 change: 1 addition & 0 deletions .phpcs.php.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<file>install.php</file>
<file>app/Mage.php</file>
<file>app/code/core/Mage/</file>
<file>dev/tests/unit/</file>
<file>lib/Mage/</file>
<file>lib/Magento/</file>
<file>lib/Varien/</file>
Expand Down
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<file>install.php</file>
<file>app/Mage.php</file>
<file>app/code/core/Mage/</file>
<file>dev/tests/unit/</file>
<file>lib/Mage/</file>
<file>lib/Magento/</file>
<file>lib/Varien/</file>
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ext-pdo": "*",
"ext-simplexml": "*",
"ext-soap": "*",
"ext-xmlreader": "*",
"ext-zlib": "*",
"colinmollenhour/cache-backend-redis": "^1.14",
"colinmollenhour/magento-redis-session": "^3.2.0",
Expand Down
11 changes: 6 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dev/tests/unit/Base/ClassLoadingTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace OpenMage\Tests\Unit\Base;
Expand All @@ -19,7 +20,7 @@ public function testClassExists(bool $expectedResult, string $class): void
}

/**
* @return string[][]
* @return array<string, array<int, bool|string>>
*/
public function provideClassExistsData(): array
{
Expand Down
14 changes: 9 additions & 5 deletions dev/tests/unit/Base/XmlFileLoadingTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

declare(strict_types=1);

namespace OpenMage\Tests\Unit\Base;

use PHPUnit\Framework\TestCase;
use SimpleXMLElement;
use XMLReader;

class XmlFileLoadingTest extends TestCase
class XmlFileLoadingTest extends TestCase
{
/**
*
Expand All @@ -15,10 +18,10 @@ class XmlFileLoadingTest extends TestCase
*/
public function testFileLoading(string $filepath): void
{
//$simplexml = new \SimpleXMLElement(file_get_contents($filepath));
/** @var SimpleXMLElement $simplexml */
$simplexml = simplexml_load_file(
$filepath,
null,
SimpleXMLElement::class,
LIBXML_PEDANTIC //not needed by OpenMage, but good to test more strictly
);
self::assertNotEmpty($simplexml->asXML());
Expand All @@ -32,8 +35,9 @@ public function testFileLoading(string $filepath): void
*/
public function testXmlReaderIsValid(string $filepath): void
{
$xml = \XMLReader::open($filepath);
$xml->setParserProperty(\XMLReader::VALIDATE, true);
/** @var XMLReader $xml */
$xml = XMLReader::open($filepath);
$xml->setParserProperty(XMLReader::VALIDATE, true);
self::assertTrue($xml->isValid());
}

Expand Down
112 changes: 61 additions & 51 deletions dev/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
<?php

declare(strict_types=1);

namespace OpenMage\Tests\Unit\Core\Helper;
namespace OpenMage\Tests\Unit\Mage\Core\Helper;

use PHPUnit\Framework\TestCase;
use Mage;
use Mage_Core_Exception;
use Mage_Core_Helper_EnvironmentConfigLoader;
use Mage_Core_Model_Config;

class TestEnvLoaderHelper extends Mage_Core_Helper_EnvironmentConfigLoader {
public function exposedBuildPath(string $section, string $group, string $field): string
{
return $this->buildPath($section, $group, $field);
}

public function exposedBuildNodePath(string $scope, string $path): string
{
return $this->buildNodePath($scope, $path);
}
}
use PHPUnit\Framework\TestCase;
use Varien_Simplexml_Config;

class EnvironmentConfigLoaderTest extends TestCase
{
private const XML_PATH_GENERAL = 'general/store_information/name';

private const XML_PATH_DEFAULT = 'default/general/store_information/name';

private const XML_PATH_WEBSITE = 'websites/base/general/store_information/name';

private const XML_PATH_STORE = 'stores/german/general/store_information/name';

/**
* @throws Mage_Core_Exception
*/
public function setup(): void
{
\Mage::setRoot('');
Mage::setRoot();
}

public function testBuildPath()
public function testBuildPath(): void
{
$environmentConfigLoaderHelper = new TestEnvLoaderHelper();
$environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
$path = $environmentConfigLoaderHelper->exposedBuildPath('GENERAL', 'STORE_INFORMATION', 'NAME');
$this->assertEquals('general/store_information/name', $path);
self::assertEquals(self::XML_PATH_GENERAL, $path);
}

public function testBuildNodePath()
public function testBuildNodePath(): void
{
$environmentConfigLoaderHelper = new TestEnvLoaderHelper();
$nodePath = $environmentConfigLoaderHelper->exposedBuildNodePath('DEFAULT', 'general/store_information/name');
$this->assertEquals('default/general/store_information/name', $nodePath);
$environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper();
$nodePath = $environmentConfigLoaderHelper->exposedBuildNodePath('DEFAULT', self::XML_PATH_GENERAL);
self::assertEquals(self::XML_PATH_DEFAULT, $nodePath);
}

public function test_xml_has_test_strings()
public function testXmlHasTestStrings(): void
{
$xmlStruct = $this->getTestXml();
$xml = new \Varien_Simplexml_Config();
$xml = new Varien_Simplexml_Config();
$xml->loadString($xmlStruct);
$this->assertEquals('test_default', (string)$xml->getNode('default/general/store_information/name'));
$this->assertEquals('test_website', (string)$xml->getNode('websites/base/general/store_information/name'));
$this->assertEquals('test_store', (string)$xml->getNode('stores/german/general/store_information/name'));
self::assertEquals('test_default', (string)$xml->getNode(self::XML_PATH_DEFAULT));
self::assertEquals('test_website', (string)$xml->getNode(self::XML_PATH_WEBSITE));
self::assertEquals('test_store', (string)$xml->getNode(self::XML_PATH_STORE));
}

/**
* @dataProvider env_overrides_correct_config_keys
* @test
* @dataProvider envOverridesCorrectConfigKeysDataProvider
* @param array<string, string> $config
*/
public function env_overrides_for_valid_config_keys(array $config)
public function testEnvOverridesForValidConfigKeys(array $config): void
{
$xmlStruct = $this->getTestXml();

$xmlDefault = new \Varien_Simplexml_Config();
$xmlDefault = new Varien_Simplexml_Config();
$xmlDefault->loadString($xmlStruct);
$xml = new \Varien_Simplexml_Config();
$xml = new Varien_Simplexml_Config();
$xml->loadString($xmlStruct);

// act
Expand All @@ -75,10 +77,13 @@ public function env_overrides_for_valid_config_keys(array $config)
$valueAfterOverride = $xml->getNode($configPath);

// assert
$this->assertNotEquals((string)$defaultValue, (string)$valueAfterOverride, 'Default value was not overridden.');
self::assertNotEquals((string)$defaultValue, (string)$valueAfterOverride, 'Default value was not overridden.');
}

public function env_overrides_correct_config_keys(): array
/**
* @return array<array<string, array<string, string>>>
*/
public function envOverridesCorrectConfigKeysDataProvider(): array
{
$defaultPath = 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME';
$defaultPathWithDash = 'OPENMAGE_CONFIG__DEFAULT__GENERAL__FOO-BAR__NAME';
Expand All @@ -96,7 +101,7 @@ public function env_overrides_correct_config_keys(): array
[
'Case DEFAULT overrides.' => [
'case' => 'DEFAULT',
'xml_path' => 'default/general/store_information/name',
'xml_path' => self::XML_PATH_DEFAULT,
'env_path' => $defaultPath,
'value' => 'default_new_value'
]
Expand All @@ -120,7 +125,7 @@ public function env_overrides_correct_config_keys(): array
[
'Case STORE overrides.' => [
'case' => 'STORE',
'xml_path' => 'stores/german/general/store_information/name',
'xml_path' => self::XML_PATH_STORE,
'env_path' => $storePath,
'value' => 'store_new_value'
]
Expand All @@ -144,7 +149,7 @@ public function env_overrides_correct_config_keys(): array
[
'Case WEBSITE overrides.' => [
'case' => 'WEBSITE',
'xml_path' => 'websites/base/general/store_information/name',
'xml_path' => self::XML_PATH_WEBSITE,
'env_path' => $websitePath,
'value' => 'website_new_value'
]
Expand All @@ -169,24 +174,24 @@ public function env_overrides_correct_config_keys(): array
}

/**
* @dataProvider env_does_not_override_on_wrong_config_keys
* @test
* @dataProvider envDoesNotOverrideOnWrongConfigKeysDataProvider
* @param array<string, string> $config
*/
public function env_does_not_override_for_invalid_config_keys(array $config)
public function testEnvDoesNotOverrideForInvalidConfigKeys(array $config): void
{
$xmlStruct = $this->getTestXml();

$xmlDefault = new \Varien_Simplexml_Config();
$xmlDefault = new Varien_Simplexml_Config();
$xmlDefault->loadString($xmlStruct);
$xml = new \Varien_Simplexml_Config();
$xml = new Varien_Simplexml_Config();
$xml->loadString($xmlStruct);

$defaultValue = 'test_default';
$this->assertEquals($defaultValue, (string)$xml->getNode('default/general/store_information/name'));
$this->assertEquals($defaultValue, (string)$xml->getNode(self::XML_PATH_DEFAULT));
$defaultWebsiteValue = 'test_website';
$this->assertEquals($defaultWebsiteValue, (string)$xml->getNode('websites/base/general/store_information/name'));
$this->assertEquals($defaultWebsiteValue, (string)$xml->getNode(self::XML_PATH_WEBSITE));
$defaultStoreValue = 'test_store';
$this->assertEquals($defaultStoreValue, (string)$xml->getNode('stores/german/general/store_information/name'));
$this->assertEquals($defaultStoreValue, (string)$xml->getNode(self::XML_PATH_STORE));

// act
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
Expand All @@ -195,27 +200,32 @@ public function env_does_not_override_for_invalid_config_keys(array $config)
]);
$loader->overrideEnvironment($xml);

$valueAfterCheck = '';
switch ($config['case']) {
case 'DEFAULT':
$valueAfterCheck = $xml->getNode('default/general/store_information/name');
$valueAfterCheck = $xml->getNode(self::XML_PATH_DEFAULT);
break;
case 'STORE':
$valueAfterCheck = $xml->getNode('stores/german/general/store_information/name');
$valueAfterCheck = $xml->getNode(self::XML_PATH_STORE);
break;
case 'WEBSITE':
$valueAfterCheck = $xml->getNode('websites/base/general/store_information/name');
$valueAfterCheck = $xml->getNode(self::XML_PATH_WEBSITE);
break;
}

// assert
$this->assertTrue(!str_contains('value_will_not_be_changed', (string)$valueAfterCheck), 'Default value was wrongfully overridden.');
self::assertTrue(!str_contains('value_will_not_be_changed', (string)$valueAfterCheck), 'Default value was wrongfully overridden.');
}

public function env_does_not_override_on_wrong_config_keys(): array
/**
* @return array<array<string, array<string, string>>>
*/
public function envDoesNotOverrideOnWrongConfigKeysDataProvider(): array
{
$defaultPath = 'OPENMAGE_CONFIG__DEFAULT__GENERAL__ST';
$websitePath = 'OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__ST';
$storePath = 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__ST';

return [
[
'Case DEFAULT with ' . $defaultPath . ' will not override.' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace OpenMage\Tests\Unit\Mage\Core\Helper;

use Mage_Core_Helper_EnvironmentConfigLoader;

class EnvironmentConfigLoaderTestHelper extends Mage_Core_Helper_EnvironmentConfigLoader
{
public function exposedBuildPath(string $section, string $group, string $field): string
{
return $this->buildPath($section, $group, $field);
}

public function exposedBuildNodePath(string $scope, string $path): string
{
return $this->buildNodePath($scope, $path);
}
}
Loading

0 comments on commit 8198718

Please sign in to comment.