Skip to content

Commit

Permalink
Merge pull request #3 from YouweGit/feature/fix-pathing
Browse files Browse the repository at this point in the history
Update pathing that updates pathing that first included mediact will …
  • Loading branch information
leonhelmus committed Apr 12, 2022
2 parents 8a29b99 + 1bd6170 commit 2848874
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 50 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.11.0 - 2022-04-10
### Fixed
- Old pathing to Mediact pathing in files `phpcs.xml`, `phpmd.xml` & `grumphp.yml` will now be replaced by
Youwe pathing to prevent error of phpcs/phpmd/grumphp.

### Changed
- Versions of packages required are now not using `@stable` anymore.
- Versions of packages can now be updated.

## 2.10.0 - 2021-03-10
### Added
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"composer-plugin-api": "^1.1 || ^2.0",
"enlightn/security-checker": "^1.5",
"kint-php/kint": "@stable",
"youwe/coding-standard": "~3.3.0",
"youwe/coding-standard-phpstorm": "~2.3.0",
"youwe/composer-dependency-installer": "~1.2.0",
"youwe/composer-file-installer": "~1.2.0",
"youwe/coding-standard": "^3.3.0",
"youwe/coding-standard-phpstorm": "^2.3.0",
"youwe/composer-dependency-installer": "^1.3.0",
"youwe/composer-file-installer": "^1.2.0",
"phpro/grumphp": "@stable",
"phpstan/phpstan": "@stable",
"phpunit/phpunit": "@stable",
Expand Down
2 changes: 1 addition & 1 deletion src/Installer/ArchiveExcludeInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(
*
* @return void
*/
public function install()
public function install(): void
{
$definition = $this->file->read();
$excluded = $definition['archive']['exclude'] ?? [];
Expand Down
2 changes: 1 addition & 1 deletion src/Installer/ConfigInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
*
* @return void
*/
public function install()
public function install(): void
{
$definition = $this->file->read();
$config = $definition['config'] ?? [];
Expand Down
87 changes: 86 additions & 1 deletion src/Installer/FilesInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Composer\IO\IOInterface;
use Youwe\Composer\FileInstaller as ComposerFileInstaller;
use Youwe\FileMapping\FileMappingInterface;
use Youwe\TestingSuite\Composer\MappingResolver;

/**
Expand Down Expand Up @@ -49,10 +50,11 @@ public function __construct(
*
* @return void
*/
public function install()
public function install(): void
{
foreach ($this->mappingResolver->resolve() as $mapping) {
if (file_exists($mapping->getDestination())) {
$this->resolveYouwePathing($mapping);
continue;
}

Expand All @@ -66,4 +68,87 @@ public function install()
);
}
}

/**
* @param FileMappingInterface $unixFileMapping
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return void
*/
private function resolveYouwePathing(FileMappingInterface $unixFileMapping): void
{
$name = $unixFileMapping->getRelativeDestination();

if ($this->mappingResolver->getTypeResolver()->resolve() === 'magento2' && !in_array($name, ['grumphp.yml'])) {
if ($name === "phpcs.xml") {
$this->updatePath(
$unixFileMapping->getDestination(),
[
'./vendor/mediact/coding-standard-magento2/src/MediactMagento2',
'./vendor/mediact/coding-standard/src/MediaCT'
],
'./vendor/youwe/coding-standard-magento2/src/Magento2'
);
} elseif ($name === "phpmd.xml") {
$this->updatePath(
$unixFileMapping->getDestination(),
[
'./vendor/mediact/coding-standard-magento2/src/MediactMagento2/phpmd.xml',
'./vendor/mediact/coding-standard/src/MediaCT/phpmd.xml'
],
'./vendor/youwe/coding-standard-magento2/src/Magento2/phpmd.xml'
);
}
} elseif ($this->mappingResolver->getTypeResolver()->resolve() === 'magento') {
if ($name === "phpcs.xml") {
$this->updatePath(
$unixFileMapping->getDestination(),
['./vendor/mediact/coding-standard-magento1/src/MediactMagento1'],
'./vendor/youwe/coding-standard-magento1/src/Magento1'
);
}
} else {
if ($name === "phpcs.xml") {
$this->updatePath(
$unixFileMapping->getDestination(),
['./vendor/mediact/coding-standard/src/MediaCT'],
'./vendor/youwe/coding-standard/src/Global'
);
} elseif ($name === "phpmd.xml") {
$this->updatePath(
$unixFileMapping->getDestination(),
['./vendor/mediact/coding-standard/src/MediaCT/phpmd.xml'],
'./vendor/youwe/coding-standard-magento2/src/Magento2/phpmd.xml'
);
} elseif ($name === "grumphp.yml") {
$this->updatePath(
$unixFileMapping->getDestination(),
['vendor/mediact/testing-suite/config/default/grumphp.yml'],
'vendor/youwe/testing-suite/config/default/grumphp.yml'
);
}
}
}

/**
* @param string $destination
* @param array $oldPaths
* @param string $newPath
*
* @return void
*/
private function updatePath(
string $destination,
array $oldPaths,
string $newPath
): void {
$file = file_get_contents($destination);
$newFile = str_replace(
$oldPaths,
$newPath,
$file
);
file_put_contents($destination, $newFile);
}
}
2 changes: 1 addition & 1 deletion src/Installer/InstallerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface InstallerInterface
*
* @return void
*/
public function install();
public function install(): void;
}
26 changes: 18 additions & 8 deletions src/Installer/PackagesInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Composer\Composer;
use Composer\IO\IOInterface;
use Youwe\Composer\DependencyInstaller\DependencyInstaller;
use Youwe\TestingSuite\Composer\MappingResolver;
use Youwe\TestingSuite\Composer\ProjectTypeResolver;

/**
Expand All @@ -33,23 +34,26 @@ class PackagesInstaller implements InstallerInterface

/** @var array */
private $mapping = [
'default' => [],
MappingResolver::DEFAULT_MAPPING_TYPE => [],
'magento1' => [
[
'name' => 'youwe/coding-standard-magento1',
'version' => '@stable'
'version' => '^1.3.0',
'dev' => true
]
],
'magento2' => [
[
'name' => 'youwe/coding-standard-magento2',
'version' => '@stable'
'version' => '^1.7.0',
'dev' => true
]
],
'laravel' => [
[
'name' => 'elgentos/laravel-coding-standard',
'version' => '@stable'
'version' => '^1.0.0',
'dev' => true
]
]
];
Expand Down Expand Up @@ -82,15 +86,15 @@ public function __construct(
*
* @return void
*/
public function install()
public function install(): void
{
$type = $this->typeResolver->resolve();
if (!isset($this->mapping[$type])) {
return;
}

foreach ($this->mapping[$type] as $package) {
if (!$this->isPackageRequired($package['name'])) {
if (!$this->isPackageRequired($package['name'], $package['version'])) {
$this->io->write(
sprintf('Requiring package %s', $package['name'])
);
Expand All @@ -110,10 +114,16 @@ public function install()
*
* @return bool
*/
private function isPackageRequired(string $packageName): bool
private function isPackageRequired(string $packageName, string $version): bool
{
foreach ($this->composer->getPackage()->getRequires() as $require) {
if ($require->getTarget() === $packageName) {
if ($require->getTarget() === $packageName && $require->getPrettyConstraint() === $version) {
return true;
}
}

foreach ($this->composer->getPackage()->getDevRequires() as $devRequire) {
if ($devRequire->getTarget() === $packageName && $devRequire->getPrettyConstraint() === $version) {
return true;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/MappingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class MappingResolver
/** @var ProjectTypeResolver */
private $typeResolver;

public const DEFAULT_MAPPING_TYPE = 'default';

/**
* Constructor.
*
Expand Down Expand Up @@ -48,4 +50,9 @@ public function resolve(): FileMappingReaderInterface
...$files
);
}

public function getTypeResolver(): ProjectTypeResolver
{
return $this->typeResolver;
}
}
19 changes: 13 additions & 6 deletions src/ProjectTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class ProjectTypeResolver
/**
* The key from the composer configuration which contains other configuration.
*/
public const COMPOSER_CONFIG_KEY = 'youwe-testing-suite';
public const COMPOSER_CONFIG_KEYS = [
'youwe-testing-suite',
'mediact-testing-suite'
];

/**
* The key in the configuration, which determines the overwrite for the type.
Expand All @@ -39,6 +42,8 @@ class ProjectTypeResolver
'laravel-project' => 'laravel',
];

public const DEFAULT_PROJECT_TYPE = 'default';

/**
* Constructor.
*
Expand All @@ -60,17 +65,19 @@ public function resolve(): string
{
$config = $this->composer->getConfig();

if ($config->has(static::COMPOSER_CONFIG_KEY)) {
$configNode = $config->get(static::COMPOSER_CONFIG_KEY);
if (isset($configNode[static::COMPOSER_CONFIG_TYPE_KEY])) {
return $configNode[static::COMPOSER_CONFIG_TYPE_KEY];
foreach (static::COMPOSER_CONFIG_KEYS as $key) {
if ($config->has($key)) {
$configNode = $config->get($key);
if (isset($configNode[static::COMPOSER_CONFIG_TYPE_KEY])) {
return $configNode[static::COMPOSER_CONFIG_TYPE_KEY];
}
}
}

$packageType = $this->composer->getPackage()->getType();

return array_key_exists($packageType, $this->mapping)
? $this->mapping[$packageType]
: 'default';
: self::DEFAULT_PROJECT_TYPE;
}
}
5 changes: 0 additions & 5 deletions templates/files/default/phpunit_dist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResult="false">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
Expand Down
2 changes: 1 addition & 1 deletion tests/Installer/FilesInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Youwe\TestingSuite\Composer\MappingResolver;

/**
* @coversDefaultClass \Youwe\TestingSuite\Composer\Installer\FilesInstaller
* @coversDefaultClass FilesInstaller
* @SuppressWarnings(PHPMD)
*/
class FilesInstallerTest extends TestCase
Expand Down
13 changes: 1 addition & 12 deletions tests/Installer/PackagesInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,10 @@ public function testInstall(
array $expected = null
) {
$composer = $this->createMock(Composer::class);
$package = $this->createMock(Package::class);
$typeResolver = $this->createMock(ProjectTypeResolver::class);
$depInstaller = $this->createMock(DependencyInstaller::class);
$io = $this->createMock(IOInterface::class);

$composer
->expects(self::any())
->method('getPackage')
->willReturn($package);

$package
->expects(self::any())
->method('getRequires')
->willReturn($requires);

$typeResolver
->expects(self::any())
->method('resolve')
Expand Down Expand Up @@ -99,7 +88,7 @@ public function dataProvider(): array
$this->createLinkMocks(
['foo/bar', 'youwe/coding-standard-magento1']
),
null
[['youwe/coding-standard-magento1']]
],
[
'magento2',
Expand Down
Loading

0 comments on commit 2848874

Please sign in to comment.