Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): added command to execute a single upgrade
fixes #11889
- Loading branch information
Showing
7 changed files
with
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Elgg\Cli; | ||
|
||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Psr\Log\LogLevel; | ||
|
||
/** | ||
* elgg-cli upgrade:batch | ||
*/ | ||
class UpgradeBatchCommand extends Command { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() { | ||
$this->setName('upgrade:batch') | ||
->setDescription(elgg_echo('cli:upgrade:batch:description')) | ||
->addOption('force', 'f', InputOption::VALUE_NONE, | ||
elgg_echo('cli:upgrade:batch:option:force') | ||
) | ||
->addArgument('upgrades', InputArgument::REQUIRED | InputArgument::IS_ARRAY, | ||
elgg_echo('cli:upgrade:batch:argument:upgrades') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function command() { | ||
$upgrades = (array) $this->argument('upgrades'); | ||
$force = (bool) $this->option('force'); | ||
|
||
foreach ($upgrades as $upgrade_class) { | ||
$upgrade = _elgg_services()->upgradeLocator->getUpgradeByClass($upgrade_class); | ||
if (!$upgrade instanceof \ElggUpgrade) { | ||
$this->log(LogLevel::WARNING, elgg_echo('cli:upgrade:batch:notfound', [$upgrade_class])); | ||
continue; | ||
} | ||
|
||
if (!$force && $upgrade->isCompleted()) { | ||
continue; | ||
} | ||
|
||
_elgg_services()->upgrades->executeUpgrade($upgrade); | ||
} | ||
|
||
if (!$this->option('quiet')) { | ||
$this->write(elgg_echo('cli:upgrade:batch:finished')); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
engine/tests/phpunit/integration/Elgg/Cli/UpgradeBatchCommandTest.php
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,52 @@ | ||
<?php | ||
|
||
namespace Elgg\Cli; | ||
|
||
use Elgg\IntegrationTestCase; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* @group Cli | ||
*/ | ||
class UpgradeBatchCommandTest extends IntegrationTestCase { | ||
|
||
public function up() { | ||
self::createApplication([ | ||
'isolate'=> true, | ||
]); | ||
} | ||
|
||
public function down() { | ||
|
||
} | ||
|
||
public function testExecuteWithoutOptions() { | ||
$application = new Application(); | ||
$application->add(new UpgradeBatchCommand()); | ||
|
||
$command = $application->find('upgrade:batch'); | ||
$commandTester = new CommandTester($command); | ||
$commandTester->execute([ | ||
'command' => $command->getName(), | ||
'upgrades' => ['RandomNonExistingClass'], | ||
]); | ||
|
||
$this->assertStringContainsString(elgg_echo('cli:upgrade:batch:finished'), $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteWithQuietOutput() { | ||
$application = new Application(); | ||
$application->add(new UpgradeBatchCommand()); | ||
|
||
$command = $application->find('upgrade:batch'); | ||
$commandTester = new CommandTester($command); | ||
$commandTester->execute([ | ||
'command' => $command->getName(), | ||
'upgrades' => ['RandomNonExistingClass'], | ||
'--quiet' => true, | ||
]); | ||
|
||
$this->assertEmpty($commandTester->getDisplay()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
engine/tests/phpunit/integration/Elgg/Upgrade/LocatorIntegrationTest.php
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,77 @@ | ||
<?php | ||
|
||
namespace Elgg; | ||
|
||
use Elgg\Upgrade\AsynchronousUpgrade; | ||
use Elgg\Upgrade\Result; | ||
|
||
class LocatorIntegrationTest extends IntegrationTestCase { | ||
|
||
/** | ||
* @var \ElggUpgrade | ||
*/ | ||
protected $upgrade_entity; | ||
|
||
public function up() { | ||
|
||
$batch = new UpgradeLocatorTestBatch(); | ||
$version = $batch->getVersion(); | ||
|
||
$upgrade = new \ElggUpgrade(); | ||
$upgrade->setClass(UpgradeLocatorTestBatch::class); | ||
$upgrade->setId("test_plugin:$version"); | ||
$upgrade->title = "test_plugin:upgrade:$version:title"; | ||
$upgrade->description = "test_plugin:upgrade:$version:title"; | ||
$upgrade->access_id = ACCESS_PUBLIC; | ||
$upgrade->save(); | ||
|
||
$this->upgrade_entity = $upgrade; | ||
} | ||
|
||
public function down() { | ||
elgg_call(ELGG_IGNORE_ACCESS, function() { | ||
$this->upgrade_entity->delete(); | ||
}); | ||
} | ||
|
||
public function testCanGetExistingUpgradeFromId() { | ||
$found_entity = _elgg_services()->upgradeLocator->upgradeExists($this->upgrade_entity->id); | ||
$this->assertInstanceOf(\ElggUpgrade::class, $found_entity); | ||
$this->assertEquals($this->upgrade_entity->guid, $found_entity->guid); | ||
} | ||
|
||
public function testCanGetExistingUpgradeByClass() { | ||
$found_entity = _elgg_services()->upgradeLocator->getUpgradeByClass(UpgradeLocatorTestBatch::class); | ||
$this->assertInstanceOf(\ElggUpgrade::class, $found_entity); | ||
$this->assertEquals($this->upgrade_entity->guid, $found_entity->guid); | ||
} | ||
} | ||
|
||
class UpgradeLocatorTestBatch implements AsynchronousUpgrade { | ||
|
||
protected $_version; | ||
|
||
public function getVersion() { | ||
if (!isset($this->_version)) { | ||
$this->_version = date('Ymd') . rand(10, 99); | ||
} | ||
|
||
return $this->_version; | ||
} | ||
|
||
public function needsIncrementOffset() { | ||
return true; | ||
} | ||
|
||
public function shouldBeSkipped() { | ||
return false; | ||
} | ||
|
||
public function countItems() { | ||
return 100; | ||
} | ||
|
||
public function run(Result $result, $offset) { | ||
$result->addSuccesses(10); | ||
} | ||
} |
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