Skip to content

Commit

Permalink
SDK-5690: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vol4onok committed Dec 14, 2023
1 parent 35bb22b commit 1a4f2ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/IntegratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function createIntegratorLockWriter(): IntegratorLockWriterInterface
*/
public function createIntegratorLockCleaner(): IntegratorLockCleanerInterface
{
return new IntegratorLockCleaner($this->getConfig(), new ProcessExecutor());
return new IntegratorLockCleaner($this->getConfig(), new ProcessExecutor(), $this->createFilesystem());
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/IntegratorLock/IntegratorLockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use SprykerSdk\Integrator\Executor\ProcessExecutor;
use SprykerSdk\Integrator\IntegratorConfig;
use Symfony\Component\Filesystem\Filesystem;

class IntegratorLockCleaner implements IntegratorLockCleanerInterface
{
Expand All @@ -29,14 +30,21 @@ class IntegratorLockCleaner implements IntegratorLockCleanerInterface
*/
protected ProcessExecutor $processExecutor;

/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected Filesystem $filesystem;

/**
* @param \SprykerSdk\Integrator\IntegratorConfig $config
* @param \SprykerSdk\Integrator\Executor\ProcessExecutor $processExecutor
* @param \Symfony\Component\Filesystem\Filesystem $filesystem
*/
public function __construct(IntegratorConfig $config, ProcessExecutor $processExecutor)
public function __construct(IntegratorConfig $config, ProcessExecutor $processExecutor, Filesystem $filesystem)
{
$this->config = $config;
$this->processExecutor = $processExecutor;
$this->filesystem = $filesystem;
}

/**
Expand All @@ -46,16 +54,16 @@ public function deleteLock(): void
{
$lockFilePath = $this->config->getIntegratorLockFilePath();

if ($this->isLockFileIgnoredByGit($lockFilePath) && !file_exists($lockFilePath)) {
if ($this->isLockFileIgnoredByGit($lockFilePath) && !$this->filesystem->exists($lockFilePath)) {
return;
}
if (file_exists($lockFilePath)) {
unlink($lockFilePath);
if ($this->filesystem->exists($lockFilePath)) {
$this->filesystem->remove($lockFilePath);
}
$gitignorePath = $this->config->getProjectRootDirectory() . static::GITIGNORE_FILE;

if (!file_exists($gitignorePath) || strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) {
file_put_contents($gitignorePath, $this->config::INTEGRATOR_LOCK . PHP_EOL, FILE_APPEND);
if (!$this->filesystem->exists($gitignorePath) || strpos((string)file_get_contents($gitignorePath), $this->config::INTEGRATOR_LOCK) === false) {
$this->filesystem->appendToFile($gitignorePath, PHP_EOL . $this->config::INTEGRATOR_LOCK . PHP_EOL);
}

$this->processExecutor->execute(['git', 'add', $lockFilePath, $gitignorePath]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function tearDownAfterClass(): void
}

/**
* @return \SprykerSdk\Integrator\Business\IntegratorFacade
* @return \SprykerSdk\Integrator\IntegratorFacade
*/
protected function createIntegratorFacade(): IntegratorFacade
{
Expand Down
2 changes: 0 additions & 2 deletions tests/SprykerSdkTest/Integrator/IntegratorFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,6 @@ public function testRunUpdateLock(): void
}

/**
* @group test1
*
* @return void
*/
public function testRunCleanLock(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SprykerSdk\Integrator\IntegratorLock\IntegratorLockReader;
use SprykerSdk\Integrator\IntegratorLock\IntegratorLockWriter;
use SprykerSdkTest\Integrator\BaseTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

class IntegratorLockTest extends BaseTestCase
Expand All @@ -35,8 +36,7 @@ public function testWriteFileLock(): void
],
],
];
$tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK;
touch($tmpIntegratorLockFilePath);
$tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath();

$integratorLockWriter = $this->createIntegratorLockWriter($tmpIntegratorLockFilePath);
$integratorLockWriter->storeLock($lockData);
Expand All @@ -54,8 +54,7 @@ public function testWriteFileLock(): void
*/
public function testReadFileLock(): void
{
$tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK;
touch($tmpIntegratorLockFilePath);
$tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath();
$compareFilePath = ROOT_TESTS . '/_data/composer/spryker_lock_test_write_lock.json';

file_put_contents($tmpIntegratorLockFilePath, file_get_contents($compareFilePath));
Expand All @@ -74,8 +73,7 @@ public function testReadFileLock(): void
*/
public function testDeleteFileLock(): void
{
$tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK;
touch($tmpIntegratorLockFilePath);
$tmpIntegratorLockFilePath = $this->createTmpIntegratorLockFilePath();

file_put_contents($tmpIntegratorLockFilePath, 'test');

Expand Down Expand Up @@ -118,7 +116,7 @@ private function createIntegratorLockCleaner(string $tmpIntegratorLockFilePath):
$processExecutorMock = $this->createMock(ProcessExecutor::class);
$processExecutorMock->method('execute')->willReturn($processMock);

return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), $processExecutorMock);
return new IntegratorLockCleaner($this->mockIntegratorConfig($tmpIntegratorLockFilePath), $processExecutorMock, new Filesystem());
}

/**
Expand All @@ -145,4 +143,15 @@ private function removeFile(string $path): void
{
$this->createFilesystem()->remove($path);
}

/**
* @return string
*/
protected function createTmpIntegratorLockFilePath(): string
{
$tmpIntegratorLockFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . IntegratorConfig::INTEGRATOR_LOCK;
touch($tmpIntegratorLockFilePath);

return $tmpIntegratorLockFilePath;
}
}

0 comments on commit 1a4f2ea

Please sign in to comment.