Skip to content

Commit

Permalink
Merge pull request #326 from phel-lang/refactor/create-command-facade
Browse files Browse the repository at this point in the history
Create CommandFacade and use it as entry point instead of CommandFactory
  • Loading branch information
jenshaase committed Aug 12, 2021
2 parents e877f7c + 6c0ec96 commit 013b332
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 41 deletions.
22 changes: 11 additions & 11 deletions phel
@@ -1,26 +1,26 @@
#!/usr/bin/env php
<?php

$slash = DIRECTORY_SEPARATOR;
declare(strict_types=1);

use Phel\Command\CommandFactory;
use Phel\Command\CommandFacade;
use Symfony\Component\Console\Application;

$projectRootDir = getcwd() . $slash;
$autoloadPath = $projectRootDir . 'vendor' . $slash . 'autoload.php';
$projectRootDir = getcwd() . DIRECTORY_SEPARATOR;
$autoloadPath = $projectRootDir . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

if (!file_exists($autoloadPath)) {
exit("Cannot load composer's autoload file: " . $autoloadPath);
exit("Cannot load composer's autoload file: $autoloadPath\n");
}

require $autoloadPath;

$commandFactory = new CommandFactory();
$commandFacade = new CommandFacade();

$application = new Application();
$application->add($commandFactory->createExportCommand());
$application->add($commandFactory->createFormatCommand());
$application->add($commandFactory->createReplCommand());
$application->add($commandFactory->createRunCommand());
$application->add($commandFactory->createTestCommand());
$application->add($commandFacade->getExportCommand());
$application->add($commandFacade->getFormatCommand());
$application->add($commandFacade->getReplCommand());
$application->add($commandFacade->getRunCommand());
$application->add($commandFacade->getTestCommand());
$application->run();
43 changes: 43 additions & 0 deletions src/php/Command/CommandFacade.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Phel\Command;

use Gacela\Framework\AbstractFacade;
use Phel\Command\Export\ExportCommand;
use Phel\Command\Format\FormatCommand;
use Phel\Command\Repl\ReplCommand;
use Phel\Command\Run\RunCommand;
use Phel\Command\Test\TestCommand;

/**
* @method CommandFactory getFactory()
*/
final class CommandFacade extends AbstractFacade
{
public function getReplCommand(): ReplCommand
{
return $this->getFactory()->createReplCommand();
}

public function getRunCommand(): RunCommand
{
return $this->getFactory()->createRunCommand();
}

public function getTestCommand(): TestCommand
{
return $this->getFactory()->createTestCommand();
}

public function getFormatCommand(): FormatCommand
{
return $this->getFactory()->createFormatCommand();
}

public function getExportCommand(): ExportCommand
{
return $this->getFactory()->createExportCommand();
}
}
6 changes: 3 additions & 3 deletions tests/php/Integration/Command/AbstractCommandTest.php
Expand Up @@ -4,7 +4,7 @@

namespace PhelTest\Integration\Command;

use Phel\Command\CommandFactory;
use Phel\Command\CommandFacade;
use Phel\Runtime\RuntimeSingleton;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -16,9 +16,9 @@ protected function setUp(): void
RuntimeSingleton::reset();
}

protected function createCommandFactory(): CommandFactory
protected function createCommandFacade(): CommandFacade
{
return new CommandFactory();
return new CommandFacade();
}

protected function stubOutput(): OutputInterface
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Integration/Command/Export/ExportCommandTest.php
Expand Up @@ -19,8 +19,8 @@ public static function setUpBeforeClass(): void
public function test_export_command_multiple(): void
{
$command = $this
->createCommandFactory()
->createExportCommand()
->createCommandFacade()
->getExportCommand()
->addRuntimePath('test-cmd-export-multiple\\', [__DIR__ . '/src/test-cmd-export-multiple/']);

$this->expectOutputRegex('~Exported namespaces:~');
Expand Down
14 changes: 8 additions & 6 deletions tests/php/Integration/Command/Format/FormatCommandTest.php
Expand Up @@ -5,6 +5,7 @@
namespace PhelTest\Integration\Command\Format;

use Gacela\Framework\Config;
use Phel\Command\Format\FormatCommand;
use PhelTest\Integration\Command\AbstractCommandTest;
use Symfony\Component\Console\Input\InputInterface;

Expand All @@ -22,9 +23,7 @@ public function test_good_format(): void
$path = self::FIXTURES_DIR . 'good-format.phel';
$oldContent = file_get_contents($path);

$command = $this
->createCommandFactory()
->createFormatCommand();
$command = $this->getFormatCommand();

$this->expectOutputRegex('/No files were formatted+/s');

Expand All @@ -43,9 +42,7 @@ public function test_bad_format(): void
$path = self::FIXTURES_DIR . 'bad-format.phel';
$oldContent = file_get_contents($path);

$command = $this
->createCommandFactory()
->createFormatCommand();
$command = $this->getFormatCommand();

$this->expectOutputString(<<<TXT
Formatted files:
Expand All @@ -62,6 +59,11 @@ public function test_bad_format(): void
}
}

private function getFormatCommand(): FormatCommand
{
return $this->createCommandFacade()->getFormatCommand();
}

private function stubInput(array $paths): InputInterface
{
$input = $this->createStub(InputInterface::class);
Expand Down
21 changes: 11 additions & 10 deletions tests/php/Integration/Command/Run/RunCommandTest.php
Expand Up @@ -5,6 +5,7 @@
namespace PhelTest\Integration\Command\Run;

use Gacela\Framework\Config;
use Phel\Command\Run\RunCommand;
use PhelTest\Integration\Command\AbstractCommandTest;
use Symfony\Component\Console\Input\InputInterface;

Expand All @@ -19,8 +20,7 @@ public function test_run_by_namespace(): void
{
$this->expectOutputRegex('~hello world~');

$this->createCommandFactory()
->createRunCommand()
$this->getRunCommand()
->addRuntimePath('test\\', [__DIR__ . '/Fixtures'])
->run(
$this->stubInput('test\\test-script'),
Expand All @@ -32,8 +32,7 @@ public function test_run_by_filename(): void
{
$this->expectOutputRegex('~hello world~');

$this->createCommandFactory()
->createRunCommand()
$this->getRunCommand()
->addRuntimePath('test\\', [__DIR__ . '/Fixtures'])
->run(
$this->stubInput(__DIR__ . '/Fixtures/test-script.phel'),
Expand All @@ -46,8 +45,7 @@ public function test_cannot_parse_file(): void
$filename = __DIR__ . '/Fixtures/test-script-not-parsable.phel';
$this->expectOutputRegex(sprintf('~Cannot parse file: %s~', $filename));

$this->createCommandFactory()
->createRunCommand()
$this->getRunCommand()
->run($this->stubInput($filename), $this->stubOutput());
}

Expand All @@ -56,8 +54,7 @@ public function test_cannot_read_file(): void
$filename = __DIR__ . '/Fixtures/this-file-does-not-exist.phel';
$this->expectOutputRegex(sprintf('~Cannot load namespace: %s~', $filename));

$this->createCommandFactory()
->createRunCommand()
$this->getRunCommand()
->run($this->stubInput($filename), $this->stubOutput());
}

Expand All @@ -66,11 +63,15 @@ public function test_file_without_ns(): void
$filename = __DIR__ . '/Fixtures/test-script-without-ns.phel';
$this->expectOutputRegex(sprintf('~Cannot extract namespace from file: %s~', $filename));

$this->createCommandFactory()
->createRunCommand()
$this->getRunCommand()
->run($this->stubInput($filename), $this->stubOutput());
}

private function getRunCommand(): RunCommand
{
return $this->createCommandFacade()->getRunCommand();
}

private function stubInput(string $path): InputInterface
{
$input = $this->createStub(InputInterface::class);
Expand Down
18 changes: 9 additions & 9 deletions tests/php/Integration/Command/Test/TestCommandTest.php
Expand Up @@ -5,6 +5,7 @@
namespace PhelTest\Integration\Command\Test;

use Gacela\Framework\Config;
use Phel\Command\Test\TestCommand;
use PhelTest\Integration\Command\AbstractCommandTest;
use Symfony\Component\Console\Input\InputInterface;

Expand All @@ -19,9 +20,7 @@ public function test_all_in_project(): void
{
$currentDir = __DIR__ . '/Fixtures/test-cmd-project-success/';

$command = $this
->createCommandFactory()
->createTestCommand()
$command = $this->getTestCommand()
->addRuntimePath('test-cmd-project-success\\', [$currentDir]);

$this->expectOutputRegex('/\.\..*/');
Expand All @@ -37,9 +36,7 @@ public function test_one_file_in_project(): void
{
$currentDir = __DIR__ . '/Fixtures/test-cmd-project-success/';

$command = $this
->createCommandFactory()
->createTestCommand()
$command = $this->getTestCommand()
->addRuntimePath('test-cmd-project-success\\', [$currentDir]);

$this->expectOutputRegex('/\..*/');
Expand All @@ -58,9 +55,7 @@ public function test_all_in_failed_project(): void
{
$currentDir = __DIR__ . '/Fixtures/test-cmd-project-failure/';

$command = $this
->createCommandFactory()
->createTestCommand()
$command = $this->getTestCommand()
->addRuntimePath('test-cmd-project-failure\\', [$currentDir]);

$this->expectOutputRegex('/E.*/');
Expand All @@ -72,6 +67,11 @@ public function test_all_in_failed_project(): void
$command->run($this->stubInput([]), $this->stubOutput());
}

private function getTestCommand(): TestCommand
{
return $this->createCommandFacade()->getTestCommand();
}

private function stubInput(array $paths): InputInterface
{
$input = $this->createStub(InputInterface::class);
Expand Down

0 comments on commit 013b332

Please sign in to comment.