Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Feb 27, 2024
1 parent 3487d92 commit 4bca544
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"minimum-stability": "dev",
"require": {
"drupal/helfi_api_base": "*",
"drush/drush": "^11 || ^12"
"drush/drush": "^11 || ^12 || ^13"
},
"require-dev": {
"composer/installers": "^2",
"drupal/core-recommended": "^10.2",
"drupal/core-dev": "^10.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"drupal/coder": "^8.3",
"phpspec/prophecy-phpunit": "^2",
Expand All @@ -30,6 +31,10 @@
"psr-4": {
"DrupalToolsTest\\": "tests/fixtures"
},
"classmap": [
"sut/core",
"sut/modules/contrib"
],
"files": [ "tests/fixtures/migration_fixture.php" ]
},
"repositories": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@
use Drush\Attributes\Argument;
use Drush\Attributes\Command;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* A drush command to check whether given Helfi packages are up-to-date.
*/
final class VersionDrushCommands extends DrushCommands {
final class CheckPackageVersionsCommands extends DrushCommands {

/**
* Constructs a new instance.
*
* @param \Drupal\helfi_api_base\Package\VersionChecker $versionChecker
* The version checker service.
* @param \Symfony\Component\Console\Style\StyleInterface|null $io
* The IO.
*/
public function __construct(
private readonly VersionChecker $versionChecker,
?StyleInterface $io = NULL,
) {
$this->io = $io ?: new SymfonyStyle($this->input(), $this->output());
parent::__construct();
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
return new self(
$container->get('helfi_api_base.package_version_checker'),
);
}
Expand Down Expand Up @@ -70,7 +77,7 @@ public function checkVersions(string $file) : int {
return DrushCommands::EXIT_SUCCESS;
}

$this->io()->table(
$this->io->table(
['Name', 'Version', 'Latest version'],
$versions,
);
Expand Down
Empty file added tests/src/Functional/.keep
Empty file.
87 changes: 87 additions & 0 deletions tests/src/Kernel/CheckPackageVersionsCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_drupal_tools\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait;
use DrupalTools\Drush\Commands\CheckPackageVersionsCommands;
use Drush\Commands\DrushCommands;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Console\Style\StyleInterface;

/**
* Tests Package version checker commands.
*
* @group helfi_drupal_tools
*/
final class CheckPackageVersionsCommandsTest extends KernelTestBase {

use ApiTestTrait;
use ProphecyTrait;

/**
* {@inheritdoc}
*/
protected static $modules = ['helfi_api_base'];

/**
* Tests version check with invalid composer.json file.
*/
public function testInvalidComposerFile() : void {
$this->expectException(\RuntimeException::class);
CheckPackageVersionsCommands::create($this->container)->checkVersions('nonexistent.lock');
}

/**
* Tests version check.
*/
public function testVersionCheck() : void {
$this->setupMockHttpClient([
new Response(body: json_encode([
'packages' => [
'drupal/helfi_api_base' => [
[
'name' => 'drupal/helfi_api_base',
'version' => '1.1.0',
],
],
],
])),
new Response(body: json_encode([
'packages' => [
'drupal/helfi_api_base' => [
[
'name' => 'drupal/helfi_api_base',
'version' => '1.0.18',
],
],
],
])),
]);
$io = $this->prophesize(StyleInterface::class);
$io->table(Argument::any(), [
[
'name' => 'drupal/helfi_api_base',
'currentVersion' => '1.0.18',
'latestVersion' => '1.1.0',
],
])->shouldBeCalledTimes(1);

$sut = new CheckPackageVersionsCommands(
$this->container->get('helfi_api_base.package_version_checker'),
$io->reveal(),
);
// Test with old version and make sure we exit with failure.
$return = $sut->checkVersions(__DIR__ . '/../../fixtures/composer.lock');
$this->assertEquals(DrushCommands::EXIT_FAILURE_WITH_CLARITY, $return);

// Test with up-to-date version and make sure we exit with success.
$return = $sut->checkVersions(__DIR__ . '/../../fixtures/composer.lock');
$this->assertEquals(DrushCommands::EXIT_SUCCESS, $return);
}

}

0 comments on commit 4bca544

Please sign in to comment.