Skip to content

Commit

Permalink
rector tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Dec 16, 2024
1 parent bd91142 commit f91279b
Show file tree
Hide file tree
Showing 130 changed files with 1,788 additions and 1,651 deletions.
12 changes: 9 additions & 3 deletions .rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
#__DIR__ . '/tests',
__DIR__ . '/tests',
])
->withSkip([
MakeInheritedMethodVisibilitySameAsParentRector::class => [
Expand All @@ -18,6 +20,12 @@
RemoveAlwaysTrueIfConditionRector::class => [
__DIR__ . '/src/N98/Magento/Initializer.php',
],
RemoveUnusedPrivateMethodRector::class => [
__DIR__ . '/tests/N98/Magento/Command/System/Setup/IncrementalCommandTest.php',
],
PrivatizeFinalClassMethodRector::class => [
__DIR__ . '/tests/N98/Magento/Command/System/Setup/IncrementalCommandTest.php',
],
])
->withPreparedSets(
true,
Expand All @@ -34,8 +42,6 @@
true,
false,
true,
true,
true,
true
)
->withTypeCoverageLevel(0);
10 changes: 5 additions & 5 deletions src/N98/Magento/Command/System/Setup/ChangeVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

$moduleVersion = $input->getArgument('version');
$moduleName = $this->getModule($input);
$setupName = $input->getArgument('setup');
$moduleSetups = $this->getModuleSetupResources($moduleName);
$moduleVersion = $input->getArgument('version');
$moduleName = $this->getModule($input);
$setupName = $input->getArgument('setup');
$moduleSetups = $this->getModuleSetupResources($moduleName);

if ($moduleSetups === []) {
$output->writeln(sprintf('No setup resources found for module: "%s"', $moduleName));
Expand All @@ -63,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
public function updateSetupResource(string $moduleName, string $setupResource, string $version, OutputInterface $output): void
{
/** @var Mage_Core_Model_Resource_Resource $mageCoreModelAbstract */
$mageCoreModelAbstract = Mage::getModel('core/resource');
$mageCoreModelAbstract = Mage::getResourceSingleton('core/resource');

$mageCoreModelAbstract->setDbVersion($setupResource, $version);
$mageCoreModelAbstract->setDataVersion($setupResource, $version);
Expand Down
40 changes: 14 additions & 26 deletions tests/N98/Magento/Application/ConfigFileTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* this file is part of magerun
*
Expand All @@ -18,62 +20,48 @@
* @covers N98\Magento\Application\ConfigFile
* @package N98\Magento\Application
*/
class ConfigFileTest extends TestCase
final class ConfigFileTest extends TestCase
{
/**
* @test
*/
public function creation()
public function testCreation()
{
$configFile = new ConfigFile();
self::assertInstanceOf(ConfigFile::class, $configFile);
$this->assertInstanceOf(ConfigFile::class, $configFile);

$configFile = ConfigFile::createFromFile(__FILE__);
self::assertInstanceOf(ConfigFile::class, $configFile);
$this->assertInstanceOf(ConfigFile::class, $configFile);
}

/**
* @test
*/
public function applyVariables()
public function testApplyVariables()
{
$configFile = new ConfigFile();
$configFile->loadFile('data://,- %root%');
$configFile->applyVariables('root-folder');

self::assertSame(['root-folder'], $configFile->toArray());
$this->assertSame(['root-folder'], $configFile->toArray());
}

/**
* @test
*/
public function mergeArray()
public function testMergeArray()
{
$configFile = new ConfigFile();
$configFile->loadFile('data://,- bar');

$result = $configFile->mergeArray(['foo']);

self::assertSame(['foo', 'bar'], $result);
$this->assertSame(['foo', 'bar'], $result);
}

/**
* @test
*/
public function parseEmptyFile()
public function testParseEmptyFile()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to parse config-file \'data://,\'');
$this->expectExceptionMessage("Failed to parse config-file 'data://,'");
$configFile = new ConfigFile();
$configFile->loadFile('data://,');
$this->addToAssertionCount(1);
$configFile->toArray();
self::fail('An expected exception has not been thrown.');
}

/**
* @test
*/
public function invalidFileThrowsException()
public function testInvalidFileThrowsException()
{
$this->expectException(InvalidArgumentException::class);
@ConfigFile::createFromFile(':');
Expand Down
105 changes: 44 additions & 61 deletions tests/N98/Magento/Application/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* @author Tom Klingenberg <https://github.com/ktomk>
*/
Expand All @@ -25,96 +27,83 @@
* @covers N98\Magento\Application\Config
* @package N98\Magento\Application
*/
class ConfigTest extends TestCase
final class ConfigTest extends TestCase
{
/**
* @test
*/
public function creation()
public function testCreation()
{
$config = new Config();
self::assertInstanceOf(__NAMESPACE__ . '\\Config', $config);
$this->assertInstanceOf(__NAMESPACE__ . '\\Config', $config);
}

/**
* @test
*/
public function loader()
public function testLoader()
{
$config = new Config();

try {
$config->load();
self::fail('An expected exception was not thrown');
} catch (ErrorException $e) {
self::assertEquals('Configuration not yet fully loaded', $e->getMessage());
} catch (ErrorException $errorException) {
$this->assertSame('Configuration not yet fully loaded', $errorException->getMessage());
}

self::assertEquals([], $config->getConfig());
$this->assertSame([], $config->getConfig());

$loader = $config->getLoader();
self::assertInstanceOf(__NAMESPACE__ . '\\ConfigurationLoader', $loader);
self::assertSame($loader, $config->getLoader());
$configurationLoader = $config->getLoader();
$this->assertInstanceOf(__NAMESPACE__ . '\\ConfigurationLoader', $configurationLoader);
$this->assertSame($configurationLoader, $config->getLoader());

$loader->loadStageTwo('');
$configurationLoader->loadStageTwo('');
$config->load();

self::assertIsArray($config->getConfig());
self::assertGreaterThan(4, count($config->getConfig()));
$this->assertIsArray($config->getConfig());
$this->assertGreaterThan(4, count($config->getConfig()));

$config->setLoader($loader);
$config->setLoader($configurationLoader);
}

/**
* config array setter is used in some tests on @see \N98\Magento\Application::setConfig()
*
* @test
*/
public function setConfig()
public function testSetConfig()
{
$config = new Config();
$config->setConfig([0, 1, 2]);

$actual = $config->getConfig();
self::assertSame($actual[1], 1);
$this->assertSame(1, $actual[1]);
}

/**
* @test
*/
public function configCommandAlias()
public function testConfigCommandAlias()
{
$config = new Config();
$input = new ArgvInput();
$actual = $config->checkConfigCommandAlias($input);
self::assertInstanceOf(InputInterface::class, $actual);
$this->assertInstanceOf(InputInterface::class, $actual);

$saved = $_SERVER['argv'];
{
$config->setConfig(['commands' => ['aliases' => [['list-help' => 'list --help']]]]);
$definition = new InputDefinition();
$definition->addArgument(new InputArgument('command'));
$inputDefinition = new InputDefinition();
$inputDefinition->addArgument(new InputArgument('command'));

$argv = ['/path/to/command', 'list-help'];
$_SERVER['argv'] = $argv;
$input = new ArgvInput($argv, $definition);
self::assertSame('list-help', (string) $input);
$input = new ArgvInput($argv, $inputDefinition);
$this->assertSame('list-help', (string) $input);
$actual = $config->checkConfigCommandAlias($input);
self::assertSame('list-help', $actual->getFirstArgument());
self::assertSame('list-help --help', (string) $actual);
$this->assertSame('list-help', $actual->getFirstArgument());
$this->assertSame('list-help --help', (string) $actual);
}
$_SERVER['argv'] = $saved;

$command = new Command('list');

$config->registerConfigCommandAlias($command);

self::assertSame(['list-help'], $command->getAliases());
$this->assertSame(['list-help'], $command->getAliases());
}

/**
* @test
*/
public function customCommands()
public function testCustomCommands()
{
$configArray = [
'commands' => [
Expand All @@ -125,10 +114,10 @@ public function customCommands()
],
];

$output = new BufferedOutput();
$output->setVerbosity($output::VERBOSITY_DEBUG);
$bufferedOutput = new BufferedOutput();
$bufferedOutput->setVerbosity($bufferedOutput::VERBOSITY_DEBUG);

$config = new Config([], false, $output);
$config = new Config([], false, $bufferedOutput);
$config->setConfig($configArray);

/** @var Application|MockObject $application */
Expand All @@ -138,41 +127,35 @@ public function customCommands()
$config->registerCustomCommands($application);
}

/**
* @test
*/
public function registerCustomAutoloaders()
public function testRegisterCustomAutoloaders()
{
$array = ['autoloaders' => ['$prefix' => '$path'], 'autoloaders_psr4' => ['$prefix\\' => '$path']];

$expected =
'<debug>Registered PSR-0 autoloader </debug> $prefix -> $path' . "\n" .
'<debug>Registered PSR-4 autoloader </debug> $prefix\\ -> $path' . "\n";

$output = new BufferedOutput();
$bufferedOutput = new BufferedOutput();

$config = new Config([], false, $output);
$config = new Config([], false, $bufferedOutput);
$config->setConfig($array);

$autloader = new ClassLoader();
$config->registerCustomAutoloaders($autloader);
$classLoader = new ClassLoader();
$config->registerCustomAutoloaders($classLoader);

$output->setVerbosity($output::VERBOSITY_DEBUG);
$config->registerCustomAutoloaders($autloader);
$bufferedOutput->setVerbosity($bufferedOutput::VERBOSITY_DEBUG);
$config->registerCustomAutoloaders($classLoader);

self::assertSame($expected, $output->fetch());
$this->assertSame($expected, $bufferedOutput->fetch());
}

/**
* @test
*/
public function loadPartialConfig()
public function testLoadPartialConfig()
{
$config = new Config();
self::assertEquals([], $config->getDetectSubFolders());
$this->assertSame([], $config->getDetectSubFolders());
$config->loadPartialConfig(false);
$actual = $config->getDetectSubFolders();
self::assertIsArray($actual);
self::assertNotEquals([], $actual);
$this->assertIsArray($actual);
$this->assertNotSame([], $actual);
}
}
13 changes: 6 additions & 7 deletions tests/N98/Magento/Application/ConfigurationLoaderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* this file is part of magerun
*
Expand All @@ -11,14 +13,11 @@
use N98\Magento\Command\TestCase;
use Symfony\Component\Console\Output\NullOutput;

class ConfigurationLoaderTest extends TestCase
final class ConfigurationLoaderTest extends TestCase
{
/**
* @test
*/
public function creation()
public function testCreation()
{
$loader = new ConfigurationLoader([], false, new NullOutput());
self::assertInstanceOf(__NAMESPACE__ . '\\ConfigurationLoader', $loader);
$configurationLoader = new ConfigurationLoader([], false, new NullOutput());
$this->assertInstanceOf(__NAMESPACE__ . '\\ConfigurationLoader', $configurationLoader);
}
}
Loading

0 comments on commit f91279b

Please sign in to comment.