-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UHF-10713: Add getOutdated() to VersionChecker service
- Loading branch information
Showing
7 changed files
with
208 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Exception; | ||
|
||
/** | ||
* Indicated failure to check versions. | ||
*/ | ||
class VersionCheckException extends \RuntimeException { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Package; | ||
|
||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Process for `composer outdated` command. | ||
*/ | ||
class ComposerOutdatedProcess { | ||
|
||
/** | ||
* Runs `composer outdated`. | ||
* | ||
* @return array | ||
* Decoded JSON from `composer outdated`. | ||
* | ||
* @throws \Symfony\Component\Process\Exception\ProcessFailedException | ||
*/ | ||
public function run($workingDir): array { | ||
$process = new Process([ | ||
'composer', 'outdated', '--direct', '--format=json', '--working-dir=' . $workingDir, | ||
]); | ||
$process->mustRun(); | ||
return json_decode($process->getOutput(), TRUE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\Unit\Package; | ||
|
||
use Drupal\helfi_api_base\Exception\VersionCheckException; | ||
use Drupal\helfi_api_base\Package\ComposerOutdatedProcess; | ||
use Drupal\helfi_api_base\Package\Version; | ||
use Drupal\helfi_api_base\Package\VersionChecker; | ||
use Drupal\Tests\UnitTestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
/** | ||
* Tests Version checker. | ||
* | ||
* @group helfi_api_base | ||
*/ | ||
class VersionCheckerTest extends UnitTestCase { | ||
|
||
use ProphecyTrait; | ||
|
||
/** | ||
* Tests missing composer file. | ||
*/ | ||
public function testMissingComposerFile(): void { | ||
$process = $this->prophesize(ComposerOutdatedProcess::class); | ||
$process->run(Argument::any())->willReturn([]); | ||
$sut = new VersionChecker('nonexistent.lock', $process->reveal()); | ||
|
||
$this->expectException(VersionCheckException::class); | ||
$sut->getOutdated(); | ||
} | ||
|
||
/** | ||
* Tests composer command failure. | ||
*/ | ||
public function testProcessFailure(): void { | ||
$process = $this->prophesize(ComposerOutdatedProcess::class); | ||
$process | ||
->run(Argument::any()) | ||
->shouldBeCalled() | ||
->willThrow($this->prophesize(ProcessFailedException::class)->reveal()); | ||
|
||
$sut = new VersionChecker(__DIR__ . '/../../../fixtures/composer.lock', $process->reveal()); | ||
|
||
$this->expectException(VersionCheckException::class); | ||
$sut->getOutdated(); | ||
} | ||
|
||
/** | ||
* Tests getOutdated(). | ||
*/ | ||
public function testGetOutdated(): void { | ||
$process = $this->prophesize(ComposerOutdatedProcess::class); | ||
$process | ||
->run(Argument::any()) | ||
->shouldBeCalled() | ||
->willReturn([ | ||
'installed' => [ | ||
[ | ||
'name' => 'drupal/helfi_api_base', | ||
'version' => '1.0.18', | ||
'latest' => '1.1.0', | ||
], | ||
], | ||
]); | ||
|
||
$sut = new VersionChecker(__DIR__ . '/../../../fixtures/composer.lock', $process->reveal()); | ||
|
||
$outdated = $sut->getOutdated(); | ||
|
||
$this->assertNotEmpty($outdated); | ||
$outdated = reset($outdated); | ||
$this->assertInstanceOf(Version::class, $outdated); | ||
$this->assertEquals('drupal/helfi_api_base', $outdated->name); | ||
$this->assertEquals('1.0.18', $outdated->version); | ||
$this->assertEquals('1.1.0', $outdated->latestVersion); | ||
$this->assertFalse($outdated->isLatest); | ||
|
||
} | ||
|
||
} |