From 32fef306db3ebd6a3fc3c299fbe22e80ea312ce3 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 6 Sep 2024 15:07:22 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- src/Tasks/MigrateContentToElement.php | 27 +++++---- tests/Tasks/MigrateContentToElementTest.php | 63 ++++++++++++++------- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/Tasks/MigrateContentToElement.php b/src/Tasks/MigrateContentToElement.php index ab98ea23..91c003e0 100644 --- a/src/Tasks/MigrateContentToElement.php +++ b/src/Tasks/MigrateContentToElement.php @@ -12,7 +12,9 @@ use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\BuildTask; -use SilverStripe\Versioned\Versioned; +use SilverStripe\PolyExecution\PolyOutput; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; class MigrateContentToElement extends BuildTask { @@ -49,12 +51,14 @@ class MigrateContentToElement extends BuildTask */ private static $publish_changes = true; - protected $title = 'MigrateContentToElement'; + protected static string $commandName = 'elemental-migrate-content'; - protected $description = 'When installing Elemental this task converts content in the $Content ' + protected string $title = 'MigrateContentToElement'; + + protected static string $description = 'When installing Elemental this task converts content in the $Content ' . 'field to an ElementContent'; - public function run($request) + protected function execute(InputInterface $input, PolyOutput $output): int { $pageTypes = singleton(ElementalArea::class)->supportedPageTypes(); $count = 0; @@ -91,10 +95,12 @@ public function run($request) try { $page->write(); } catch (Exception $e) { - echo sprintf( - 'Could not clear content on page %s: %s', - $page->ID, - $e->getMessage() + $output->writeln( + '' . sprintf( + 'Could not clear content on page %s: %s', + $page->ID, + $e->getMessage() + ) . '' ); } @@ -125,9 +131,10 @@ public function run($request) $pageTypeCount++; } $count += $pageTypeCount; - echo 'Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content
'; + $output->writeln('Migrated ' . $pageTypeCount . ' ' . $pageType . ' pages\' content'); } - echo 'Finished migrating ' . $count . ' pages\' content
'; + $output->writeln('Finished migrating ' . $count . ' pages\' content'); + return Command::SUCCESS; } /** diff --git a/tests/Tasks/MigrateContentToElementTest.php b/tests/Tasks/MigrateContentToElementTest.php index 9615430a..387abd99 100644 --- a/tests/Tasks/MigrateContentToElementTest.php +++ b/tests/Tasks/MigrateContentToElementTest.php @@ -6,11 +6,13 @@ use DNADesign\Elemental\Tasks\MigrateContentToElement; use DNADesign\Elemental\Tests\Src\TestElement; use DNADesign\Elemental\Tests\Src\TestPage; -use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; +use SilverStripe\PolyExecution\PolyOutput; use SilverStripe\ORM\HasManyList; use SilverStripe\Versioned\Versioned; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\BufferedOutput; class MigrateContentToElementTest extends SapphireTest { @@ -42,9 +44,12 @@ public function testContentIsMigratedFromPagesToNewElements() $page = $this->objFromFixture(TestPage::class, 'page3'); $page->publishSingle(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 1 pages\' content', $output); @@ -72,9 +77,12 @@ public function testContentIsNotClearedWhenConfigured() $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $polyOutput = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $polyOutput); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 1 pages\' content', $output); @@ -85,9 +93,8 @@ public function testContentIsNotClearedWhenConfigured() $this->assertStringContainsString('This is page 3', $element->HTML, 'Content is still added to a new element'); // Run the task again and assert the page is not picked up again - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $task->run($input, $polyOutput); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 0 pages\' content', $output); $page = $this->objFromFixture(TestPage::class, 'page3'); @@ -101,9 +108,12 @@ public function testTargetElementConfigurationIsRespected() $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 1 pages\' content', $output); @@ -125,9 +135,12 @@ public function testPublishingConfigurationIsRespected() $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 1 pages\' content', $output); @@ -152,9 +165,12 @@ public function testPagesThatWereNotPublishedAreNotPublishedDuringThisTask() { $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 1 pages\' content', $output); @@ -187,9 +203,12 @@ public function testIgnoredClassesContentIsNotCleared() $page = $this->objFromFixture(TestPage::class, 'page3'); $page->publishSingle(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 0 pages\' content', $output);