diff --git a/src/Tasks/MigrateContentToElement.php b/src/Tasks/MigrateContentToElement.php index ab98ea23..f5799f7d 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\HybridExecution\HybridOutput; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; class MigrateContentToElement extends BuildTask { @@ -49,12 +51,12 @@ class MigrateContentToElement extends BuildTask */ private static $publish_changes = true; - protected $title = 'MigrateContentToElement'; + protected string $title = 'MigrateContentToElement'; - protected $description = 'When installing Elemental this task converts content in the $Content ' + 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, HybridOutput $output): int { $pageTypes = singleton(ElementalArea::class)->supportedPageTypes(); $count = 0; @@ -91,10 +93,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 +129,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..50376804 100644 --- a/tests/Tasks/MigrateContentToElementTest.php +++ b/tests/Tasks/MigrateContentToElementTest.php @@ -9,8 +9,11 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; +use SilverStripe\HybridExecution\HybridOutput; 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 +45,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 HybridOutput(HybridOutput::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 +78,12 @@ public function testContentIsNotClearedWhenConfigured() $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $hybridOutput = new HybridOutput(HybridOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $hybridOutput); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 1 pages\' content', $output); @@ -85,9 +94,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, $hybridOutput); + $output = $buffer->fetch(); $this->assertStringContainsString('Finished migrating 0 pages\' content', $output); $page = $this->objFromFixture(TestPage::class, 'page3'); @@ -101,9 +109,12 @@ public function testTargetElementConfigurationIsRespected() $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new HybridOutput(HybridOutput::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 +136,12 @@ public function testPublishingConfigurationIsRespected() $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new HybridOutput(HybridOutput::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 +166,12 @@ public function testPagesThatWereNotPublishedAreNotPublishedDuringThisTask() { $task = new MigrateContentToElement(); - ob_start(); - $task->run(new HTTPRequest('GET', '')); - $output = ob_get_clean(); + $buffer = new BufferedOutput(); + $output = new HybridOutput(HybridOutput::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 +204,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 HybridOutput(HybridOutput::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);