Skip to content

Commit

Permalink
UHF-7324: Test revision commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Sep 16, 2023
1 parent d53f301 commit a1edf8b
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Commands/RevisionCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function delete(string $entityType, array $options = ['keep' => RevisionM
->fetchCol();

$totalEntities = $remainingEntities = count($entityIds);
$this->io()->writeln(new FormattableMarkup('Found @count @type entities', [
$this->io()->writeln((string) new FormattableMarkup('Found @count @type entities', [
'@count' => $totalEntities,
'@type' => $entityType,
]));
Expand All @@ -72,9 +72,9 @@ public function delete(string $entityType, array $options = ['keep' => RevisionM
$message = sprintf('Entity has less than %s revisions. Skipping', $options['keep']);

if ($revisionCount > 0) {
$message = new FormattableMarkup('Deleting @count revisions', ['@count' => $revisionCount]);
$message = (string) new FormattableMarkup('Deleting @count revisions', ['@count' => $revisionCount]);
}
$this->io()->writeln(new FormattableMarkup('[@current/@entities] @message ...', [
$this->io()->writeln((string) new FormattableMarkup('[@current/@entities] @message ...', [
'@current' => $remainingEntities--,
'@entities' => $totalEntities,
'@message' => $message,
Expand Down
164 changes: 164 additions & 0 deletions tests/src/Unit/Commands/RevisionCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\helfi_api_base\Unit\Commands;

use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Select;
use Drupal\Core\Database\StatementInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\helfi_api_base\Commands\RevisionCommands;
use Drupal\helfi_api_base\Entity\Revision\RevisionManager;
use Drupal\Tests\UnitTestCase;
use Drush\Commands\DrushCommands;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Tests revision commands.
*
* @group helfi_api_base
*/
class RevisionCommandsTest extends UnitTestCase {

use ProphecyTrait;

/**
* Gets the SUT.
*
* @param \Drupal\helfi_api_base\Entity\Revision\RevisionManager $revisionManager
* The revision manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface|null $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Database\Connection|null $connection
* The connection.
* @param \Prophecy\Prophecy\ObjectProphecy|null $io
* The IO prophecy.
*
* @return \Drupal\helfi_api_base\Commands\RevisionCommands
* The SUT.
*/
private function getSut(
RevisionManager $revisionManager,
EntityTypeManagerInterface $entityTypeManager = NULL,
Connection $connection = NULL,
ObjectProphecy $io = NULL,
) : RevisionCommands {
if (!$entityTypeManager) {
$definition = $this->prophesize(EntityTypeInterface::class);
$definition->getBaseTable()
->willReturn('base_table');
$definition->getKey('id')
->willReturn('id');
$entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
$entityTypeManager->getDefinition('node')
->willReturn($definition->reveal());
$entityTypeManager = $entityTypeManager->reveal();
}
if (!$connection) {
$connection = $this->prophesize(Connection::class)->reveal();
}
$sut = new RevisionCommands($revisionManager, $entityTypeManager, $connection);

if ($io) {
$output = $this->prophesize(OutputInterface::class);
$input = $this->prophesize(InputInterface::class);
$sut->restoreState($input->reveal(), $output->reveal(), $io->reveal());
}
return $sut;
}

/**
* Mocks the connection object.
*
* @param array $expected
* The expected return value.
*
* @return \Drupal\Core\Database\Connection
* The connection mock.
*/
private function getConnectionMock(array $expected) : Connection {
$statement = $this->prophesize(StatementInterface::class);

$statement->fetchCol(Argument::any())
->willReturn($expected);

$select = $this->prophesize(Select::class);
$select->fields('t', Argument::any())
->willReturn($select->reveal());

$select->execute()
->willReturn($statement);

$database = $this->prophesize(Connection::class);
$database->select(Argument::any(), 't')
->willReturn($select);

return $database->reveal();
}

/**
* Test command with invalid entity type.
*/
public function testInvalidEntityType() : void {
$io = $this->prophesize(SymfonyStyle::class);
$io->writeln(Argument::containingString('Given entity type is not supported.'))
->shouldBeCalled();

$revisionManager = $this->prophesize(RevisionManager::class);
$revisionManager->entityTypeIsSupported('node')->willReturn(FALSE);

$sut = $this->getSut($revisionManager->reveal(), io: $io);

$this->assertEquals(DrushCommands::EXIT_SUCCESS, $sut->delete('node'));
}

/**
* Test delete without any entities.
*/
public function testNoEntities() : void {
$io = $this->prophesize(SymfonyStyle::class);
$io->writeln(Argument::containingString('Found 0 node entities'))
->shouldBeCalled();

$revisionManager = $this->prophesize(RevisionManager::class);
$revisionManager->entityTypeIsSupported('node')->willReturn(TRUE);
$database = $this->getConnectionMock([]);

$sut = $this->getSut($revisionManager->reveal(), connection: $database, io: $io);

$this->assertEquals(DrushCommands::EXIT_SUCCESS, $sut->delete('node'));
}

/**
* Tests delete method with proper data.
*/
public function testDelete() : void {
$io = $this->prophesize(SymfonyStyle::class);
$io->writeln(Argument::containingString('Found 2 node entities'))
->shouldBeCalled();
$io->writeln(Argument::containingString('[2/2] Entity has less than 5 revisions. Skipping ...'))
->shouldBeCalled();
$io->writeln(Argument::containingString('[1/2] Deleting 6 revisions ...'))
->shouldBeCalled();

$revisionManager = $this->prophesize(RevisionManager::class);
$revisionManager->entityTypeIsSupported('node')->willReturn(TRUE);
$revisionManager->getRevisions('node', Argument::any(), Argument::any())
->willReturn([], [1, 2, 3, 4, 5, 6]);
$revisionManager->deleteRevisions('node', Argument::any())
->shouldBeCalledTimes(2);
$database = $this->getConnectionMock([1, 2]);

$sut = $this->getSut($revisionManager->reveal(), connection: $database, io: $io);

$this->assertEquals(DrushCommands::EXIT_SUCCESS, $sut->delete('node'));
}

}

0 comments on commit a1edf8b

Please sign in to comment.