mirror of
https://github.com/flarum/core.git
synced 2025-07-31 13:40:20 +02:00
Add console extender (#2057)
* Made the console command system extender-friendly * Added console extender * Added ConsoleTestCase to integration tests * Added integration tests for console extender * Marked event-based console extension system as deprecated * Moved trimming command output of whitespace into superclass * Renamed 'add' to 'command' * Added special processing for laravel commands * Code style fixes * More style fixes * Fixed $this->container
This commit is contained in:
committed by
GitHub
parent
ca32879e1e
commit
c84dd4d5bf
44
framework/core/tests/integration/ConsoleTestCase.php
Normal file
44
framework/core/tests/integration/ConsoleTestCase.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
use Symfony\Component\Console\Application as ConsoleApplication;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
|
||||
abstract class ConsoleTestCase extends TestCase
|
||||
{
|
||||
protected $console;
|
||||
|
||||
protected function console()
|
||||
{
|
||||
if (is_null($this->console)) {
|
||||
$this->console = new ConsoleApplication('Flarum', Application::VERSION);
|
||||
$this->console->setAutoExit(false);
|
||||
|
||||
foreach ($this->app()->getConsoleCommands() as $command) {
|
||||
$this->console->add($command);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->console;
|
||||
}
|
||||
|
||||
protected function runCommand(array $inputArray)
|
||||
{
|
||||
$input = new ArrayInput($inputArray);
|
||||
$output = new BufferedOutput();
|
||||
|
||||
$this->console()->run($input, $output);
|
||||
|
||||
return trim($output->fetch());
|
||||
}
|
||||
}
|
65
framework/core/tests/integration/extenders/ConsoleTest.php
Normal file
65
framework/core/tests/integration/extenders/ConsoleTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Console\AbstractCommand;
|
||||
use Flarum\Extend;
|
||||
use Flarum\Tests\integration\ConsoleTestCase;
|
||||
|
||||
class ConsoleTest extends ConsoleTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_command_doesnt_exist_by_default()
|
||||
{
|
||||
$input = [
|
||||
'command' => 'customTestCommand'
|
||||
];
|
||||
|
||||
$this->assertEquals('Command "customTestCommand" is not defined.', $this->runCommand($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_command_exists_when_added()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Console())
|
||||
->command(CustomCommand::class)
|
||||
);
|
||||
|
||||
$input = [
|
||||
'command' => 'customTestCommand'
|
||||
];
|
||||
|
||||
$this->assertEquals('Custom Command.', $this->runCommand($input));
|
||||
}
|
||||
}
|
||||
|
||||
class CustomCommand extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('customTestCommand');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function fire()
|
||||
{
|
||||
$this->info('Custom Command.');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user