Skip to content

Commit

Permalink
[TASK] Add tests for PatchUtils (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertsoft authored May 28, 2023
1 parent 2c11594 commit 674e13b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ jobs:
with:
coverage: xdebug
extensions: intl, zip
ini-values: memory_limit=-1, error_reporting=E_ALL, display_errors=On
ini-values: memory_limit=-1, error_reporting=E_ALL, display_errors=On, xdebug.mode="develop,coverage"
php-version: ${{ matrix.php-version }}
tools: composer

Expand Down
23 changes: 13 additions & 10 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace GsTYPO3\CorePatches;

use Composer\Config\ConfigSourceInterface;
use Composer\Config\JsonConfigSource;
use Composer\Factory;
use Composer\Json\JsonFile;
Expand Down Expand Up @@ -71,7 +72,7 @@ final class Config implements PersistenceInterface

private JsonFile $jsonFile;

private JsonConfigSource $jsonConfigSource;
private ConfigSourceInterface $configSource;

private Changes $changes;

Expand All @@ -83,10 +84,12 @@ final class Config implements PersistenceInterface

private Patches $patches;

public function __construct(?JsonFile $jsonFile = null, ?JsonConfigSource $jsonConfigSource = null)
{
public function __construct(
?JsonFile $jsonFile = null,
?ConfigSourceInterface $configSource = null
) {
$this->jsonFile = $jsonFile ?? new JsonFile(Factory::getComposerFile());
$this->jsonConfigSource = $jsonConfigSource ?? new JsonConfigSource($this->jsonFile);
$this->configSource = $configSource ?? new JsonConfigSource($this->jsonFile);

$this->changes = new Changes();
$this->preferredInstallChanged = new PreferredInstallChanged();
Expand Down Expand Up @@ -165,9 +168,9 @@ public function save(): self
);

if ($installMethod === '') {
$this->jsonConfigSource->removeConfigSetting($name);
$this->configSource->removeConfigSetting($name);
} else {
$this->jsonConfigSource->addConfigSetting($name, $installMethod);
$this->configSource->addConfigSetting($name, $installMethod);
}
}

Expand All @@ -179,9 +182,9 @@ public function save(): self
);

if ($this->patches->isEmpty()) {
$this->jsonConfigSource->removeProperty($name);
$this->configSource->removeProperty($name);
} else {
$this->jsonConfigSource->addProperty($name, $this->patches);
$this->configSource->addProperty($name, $this->patches);
}

// Save plugin configuration
Expand All @@ -192,9 +195,9 @@ public function save(): self
);

if ($this->isEmpty()) {
$this->jsonConfigSource->removeProperty($name);
$this->configSource->removeProperty($name);
} else {
$this->jsonConfigSource->addProperty($name, $this);
$this->configSource->addProperty($name, $this);
}

// Rewrite configuration to enforce correct formatting
Expand Down
24 changes: 18 additions & 6 deletions src/Utility/ComposerUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,29 @@ final class ComposerUtils

private PatchUtils $patchUtils;

public function __construct(Composer $composer, IOInterface $io)
{
public function __construct(
Composer $composer,
IOInterface $io,
?Config $config = null,
?Application $application = null,
?RestApi $restApi = null,
?PatchUtils $patchUtils = null
) {
$this->composer = $composer;
$this->io = $io;

$this->config = new Config(new JsonFile(Factory::getComposerFile(), null, $this->io));
$this->application = new Application();
$httpDownloader = Factory::createHttpDownloader($this->io, $this->composer->getConfig());

$this->config = $config ?? new Config(
new JsonFile(Factory::getComposerFile(), $httpDownloader, $this->io),
$this->composer->getConfig()->getConfigSource()
);

$this->application = $application ?? new Application();
$this->application->setAutoExit(false);

$this->gerritRestApi = new RestApi(Factory::createHttpDownloader($this->io, $this->composer->getConfig()));
$this->patchUtils = new PatchUtils($this->composer, $this->io);
$this->gerritRestApi = $restApi ?? new RestApi($httpDownloader);
$this->patchUtils = $patchUtils ?? new PatchUtils($this->composer, $this->io);
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/Utility/PatchUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function __construct(Composer $composer, IOInterface $io)
$this->io = $io;
}

public function getPatchFileName(string $destination, string $packageName, int $numericId): string
{
return sprintf(
'%s/%s-review-%s.patch',
$destination,
str_replace('/', '-', $packageName),
$numericId
);
}

public function patchIsPartOfChange(string $patchFileName, int $numericId): bool
{
return strpos($patchFileName, '-review-' . $numericId . '.patch') !== false;
Expand Down Expand Up @@ -221,12 +231,7 @@ public function save(

foreach ($patches as $packageName => $chunks) {
$content = implode('', $chunks);
$patchFileName = sprintf(
'%s/%s-review-%s.patch',
$destination,
str_replace('/', '-', $packageName),
$numericId
);
$patchFileName = $this->getPatchFileName($destination, $packageName, $numericId);
$this->io->write(sprintf(' - Creating patch <info>%s</info>', $patchFileName));
file_put_contents($patchFileName, $content);
$composerChanges[$packageName] = [$subject => $patchFileName];
Expand Down
89 changes: 89 additions & 0 deletions tests/Unit/Utility/PatchUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

/*
* This file is part of TYPO3 Core Patches.
*
* (c) Gilbertsoft LLC (gilbertsoft.org)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace GsTYPO3\CorePatches\Tests\Unit\Utility;

use Composer\Composer;
use Composer\Factory;
use Composer\IO\BufferIO;
use GsTYPO3\CorePatches\Tests\Unit\TestCase;
use GsTYPO3\CorePatches\Utility\PatchUtils;
use RuntimeException;

/**
* @covers \GsTYPO3\CorePatches\Utility\PatchUtils
*/
final class PatchUtilsTest extends TestCase
{
private string $previousWorkingDir;

private string $testWorkingDir;

private BufferIO $bufferIO;

private Composer $composer;

private PatchUtils $patchUtils;

protected function setUp(): void
{
parent::setUp();

if (($previousWorkingDir = getcwd()) === false) {
throw new RuntimeException('Unable to determine current directory.', 1_668_787_261);
}

$this->previousWorkingDir = $previousWorkingDir;
$this->testWorkingDir = self::getTestPath();
chdir($this->testWorkingDir);

self::createFiles($this->testWorkingDir, [
'composer.json' => 'FIX:composer.json',
]);

$this->bufferIO = new BufferIO();
$this->composer = Factory::create($this->bufferIO);

$this->patchUtils = new PatchUtils($this->composer, $this->bufferIO);
}

protected function tearDown(): void
{
chdir($this->previousWorkingDir);

parent::tearDown();
}

/**
* @medium
*/
public function testGetPatchFileName(): void
{
self::assertSame(
'destination/vendor-package-review-12345.patch',
$this->patchUtils->getPatchFileName('destination', 'vendor/package', 12345)
);
}

/**
* @medium
*/
public function testPatchIsPartOfChange(): void
{
self::assertTrue($this->patchUtils->patchIsPartOfChange(
$this->patchUtils->getPatchFileName('destination', 'vendor/package', 12345),
12345
));
self::assertFalse($this->patchUtils->patchIsPartOfChange('vendor-package.patch', 12345));
}
}

0 comments on commit 674e13b

Please sign in to comment.