Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reworked console application to use the generic Mason application int…

…ernally.
  • Loading branch information...
commit 0a06ab48b42a6b3a5221b8fca68f43b36b8ecfac 1 parent c6197e1
@jmalloc jmalloc authored
View
20 examples/mason.yml
@@ -27,13 +27,13 @@ test:
# with:
# - uglify $source $target # non dotted names are treated as shell commands
-foo:
- require:
- - test
- run:
- - ls -la
- - echo OH YEAH!
-
-foo:
- require:
- - test
+# foo:
+# require:
+# - test
+# run:
+# - ls -la
+# - echo OH YEAH!
+#
+# foo:
+# require:
+# - test
View
6 src/Application/Application.php
@@ -15,7 +15,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
-class Application
+class Application implements ApplicationInterface
{
use LoggerAwareTrait;
@@ -59,7 +59,7 @@ public function loadExtensions()
foreach ($extensions as $extension) {
$this
->extensionManager()
- ->load($logger, $this, $extension);
+ ->load($extension, $this);
}
}
@@ -81,6 +81,8 @@ public function loadConfiguration($configurationPath)
$this
->configurationResolver()
->resolve($logger, $configuration);
+
+ return $configuration;
}
/**
View
95 src/Console/Application.php
@@ -6,9 +6,7 @@
use Mason\Application\Application as MasonApplication;
use Mason\Configuration\Exception\InvalidConfigurationException;
use Mason\PackageInfo;
-use Psr\Log\NullLogger;
use Symfony\Component\Console\Application as SymfonyApplication;
-use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@@ -18,9 +16,11 @@ class Application extends SymfonyApplication
{
use IsolatorTrait;
- public function __construct($packageRoot)
- {
- $this->application = new MasonApplication($packageRoot);
+ public function __construct(
+ $packageRoot,
+ MasonApplication $application = null
+ ) {
+ $this->application = $application ?: new MasonApplication($packageRoot);
$this->configurationOption = new InputOption(
'config',
'c',
@@ -30,10 +30,6 @@ public function __construct($packageRoot)
);
parent::__construct(PackageInfo::NAME, PackageInfo::VERSION);
-
- $this->add(
- new Command\ValidateCommand
- );
}
/**
@@ -60,6 +56,8 @@ public function doRun(InputInterface $input, OutputInterface $output)
{
$this->application->setLogger(new Logger($output));
+ $this->application->loadExtensions();
+
$this->configurationPath = $this->parseConfigurationPath($input);
if ($this->configurationPath) {
@@ -70,23 +68,6 @@ public function doRun(InputInterface $input, OutputInterface $output)
}
/**
- * Runs the current command.
- *
- * If an event dispatcher has been attached to the application,
- * events are also dispatched during the life-cycle of the command.
- *
- * @param SymfonyCommand $command A Command instance
- * @param InputInterface $input An Input instance
- * @param OutputInterface $output An Output instance
- *
- * @return integer 0 if everything went fine, or an error code
- */
- protected function doRunCommand(SymfonyCommand $command, InputInterface $input, OutputInterface $output)
- {
- return parent::doRunCommand($command, $input, $output);
- }
-
- /**
* Gets the name of the command based on input.
*
* @param InputInterface $input The input interface
@@ -98,8 +79,8 @@ protected function getCommandName(InputInterface $input)
$name = $input->getFirstArgument();
// If the first argument is the config file, then no command name was
- // provided (Symfony *requires* that the first argument is the command
- // name) ...
+ // provided (Symfony Console *requires* that the first argument is the
+ // command name) ...
if ($name === $this->configurationPath) {
return null;
}
@@ -152,44 +133,28 @@ public function parseConfigurationPath(InputInterface $input)
return null;
}
- /**
- * Read the configuration in order to get a command list.
- *
- * An invalid configuration is ignored.
- *
- * @param InputInterface &$input
- */
- protected function loadConfiguration(InputInterface &$input)
+ private function loadConfiguration(InputInterface &$input)
{
- // try {
- // $configuration = $this
- // ->application
- // ->configurationReader()
- // ->readFile(
- // $this->configurationPath,
- // new NullLogger
- // );
- // } catch (InvalidConfigurationException $e) {
- // return;
- // }
-
- // $this->application->setConfiguration($configuration);
-
- // // Add CLI commands for each of the tasks ...
- // foreach ($configuration->tasks() as $task) {
- // $this->add(
- // new Command\RunTaskCommand($task)
- // );
- // }
-
- // // No command was specified, run the default command ...
- // if (null === $this->getCommandName($input)) {
- // if ($task = $configuration->defaultTask()) {
- // $input = new ArrayInput(
- // array('command' => $task->name())
- // );
- // }
- // }
+ try {
+ $configuration = $this->application->loadConfiguration($this->configurationPath);
+ } catch (InvalidConfigurationException $e) {
+ return;
+ }
+
+ foreach ($configuration->tasks() as $task) {
+ $this->add(
+ new Command\RunTaskCommand($task)
+ );
+ }
+
+ // No command was specified, run the default command ...
+ if (null === $this->getCommandName($input)) {
+ if ($task = $configuration->defaultTask()) {
+ $input = new ArrayInput(
+ array('command' => $task->name())
+ );
+ }
+ }
}
private $application;
View
59 src/Console/Command/ValidateCommand.php
@@ -1,59 +0,0 @@
-<?php
-namespace Mason\Console\Command;
-
-use Mason\Application\ApplicationInterface;
-use Mason\Configuration\Exception\InvalidConfigurationException;
-use Symfony\Component\Console\Command\Command;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-
-class ValidateCommand extends Command
-{
- public function __construct(ApplicationInterface $application)
- {
- $this->application = $application;
-
- parent::__construct();
- }
-
- protected function configure()
- {
- $this->setName('config:validate');
- $this->setDescription('Validate a Mason configuration file.');
- }
-
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $logger = $this->application->logger();
- $extensionLoader = $this->application->extensionLoader();
- $extensionManager = $this->application->extensionManager();
-
- $extensions = $extensionLoader->loadExtensions($logger);
- foreach ($extensions as $extension) {
- $extensionManager->load($extension);
- }
-
- try {
- $configuration = $this->application->configurationReader()->readFile(
- $logger,
- $input->getOption('config')
- );
- $this->application->configurationResolver()->resolve(
- $logger,
- $configuration
- );
- } catch (InvalidConfigurationException $e) {
- return 1;
- }
-
- $output->writeln('<info>Configuration is valid.</info>');
-
- return 0;
- }
-
- private $application;
-}
View
2  src/Extension/ExtensionLoader.php
@@ -18,6 +18,8 @@ public function __construct($indexPath)
}
/**
+ * Fetch the path to the extension index file generated by the Mason Composer plugin.
+ *
* @return string The path to the extension index file generated by the Mason Composer plugin.
*/
public function indexPath()
View
50 test/suite/Application/ApplicationTest.php
@@ -11,6 +11,56 @@ public function setUp()
$this->application = new Application('/path/to/root');
}
+ public function testLoadExtensions()
+ {
+ $extension1 = Phake::mock('Mason\Extension\ExtensionInterface');
+ $extension2 = Phake::mock('Mason\Extension\ExtensionInterface');
+ $extensionLoader = Phake::mock('Mason\Extension\ExtensionLoaderInterface');
+ $extensionManager = Phake::mock('Mason\Extension\ExtensionManagerInterface');
+
+ Phake::when($extensionLoader)
+ ->loadExtensions(Phake::anyParameters())
+ ->thenReturn([$extension1, $extension2]);
+
+ $this->application->setExtensionLoader($extensionLoader);
+ $this->application->setExtensionManager($extensionManager);
+
+ $this->application->loadExtensions();
+
+ Phake::verify($extensionLoader)->loadExtensions($this->identicalTo($this->application->logger()));
+ Phake::verify($extensionManager)->load($this->identicalTo($extension1), $this->application);
+ Phake::verify($extensionManager)->load($this->identicalTo($extension2), $this->application);
+ }
+
+ public function testLoadConfiguration()
+ {
+ $configuration = Phake::mock('Mason\Configuration\Configuration');
+ $configurationReader = Phake::mock('Mason\Configuration\ConfigurationReaderInterface');
+ $configurationResolver = Phake::mock('Mason\Configuration\ConfigurationResolverInterface');
+
+ Phake::when($configurationReader)
+ ->readFile(Phake::anyParameters())
+ ->thenReturn($configuration);
+
+ $this->application->setConfigurationReader($configurationReader);
+ $this->application->setConfigurationResolver($configurationResolver);
+
+ $result = $this->application->loadConfiguration('/path/to/config.yml');
+
+ $this->assertSame($configuration, $result);
+ $this->assertSame($configuration, $this->application->configuration());
+
+ Phake::verify($configurationReader)->readFile(
+ $this->identicalTo($this->application->logger()),
+ '/path/to/config.yml'
+ );
+
+ Phake::verify($configurationResolver)->resolve(
+ $this->identicalTo($this->application->logger()),
+ $this->identicalTo($configuration)
+ );
+ }
+
public function testExtensionManager()
{
$extensionManager = $this->application->extensionManager();
View
81 test/suite/Console/ApplicationTest.php
@@ -10,27 +10,18 @@
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\OutputInterface;
-class ApplicationTest //extends PHPUnit_Framework_TestCase
+class ApplicationTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
- $this->markTestIncomplete();
-
- // Setup service locator ...
- $this->serviceLocatorFactory = Phake::mock('Mason\Service\ServiceLocatorFactory');
- $this->serviceLocator = Phake::partialMock('Mason\Service\ServiceLocator');
-
- Phake::when($this->serviceLocatorFactory)
- ->create()
- ->thenReturn($this->serviceLocator);
-
// Setup the application ...
$this->isolator = Phake::mock(Isolator::className());
+ $this->masonApplication = Phake::mock('Mason\Application\Application');
$this->output = Phake::mock('Symfony\Component\Console\Output\OutputInterface');
$this->application = Phake::partialMock(
'Mason\Console\Application',
'/path/to/project',
- $this->serviceLocatorFactory
+ $this->masonApplication
);
$this->application->setIsolator($this->isolator);
$this->application->setAutoExit(false);
@@ -40,10 +31,9 @@ public function setUp()
$this->task = new Task('foo');
$this->configuration->addTask($this->task);
$this->configurationReader = Phake::partialMock('Mason\Configuration\ConfigurationReader');
- $this->serviceLocator->setConfigurationReader($this->configurationReader);
- Phake::when($this->configurationReader)
- ->readFile(Phake::anyParameters())
+ Phake::when($this->masonApplication)
+ ->loadConfiguration(Phake::anyParameters())
->thenReturn($this->configuration);
}
@@ -51,10 +41,6 @@ public function testConstructor()
{
$this->assertSame('Mason', $this->application->getName());
$this->assertSame('0.0.0', $this->application->getVersion());
-
- Phake::verify($this->application)->add(
- $this->isInstanceOf('Mason\Console\Command\ValidateCommand')
- );
}
public function testGetDefaultInputDefinition()
@@ -64,6 +50,19 @@ public function testGetDefaultInputDefinition()
$this->assertTrue($definition->hasOption('config'));
}
+ public function testParseConfigurationPath()
+ {
+ Phake::when($this->isolator)
+ ->is_file('foo/bar.yml')
+ ->thenReturn(true);
+
+ $input = new ArgvInput(['mason', '--config', 'foo/bar.yml']);
+ $this->assertSame('foo/bar.yml', $this->application->parseConfigurationPath($input));
+
+ $input = new ArgvInput(['mason', '-c', 'foo/bar.yml']);
+ $this->assertSame('foo/bar.yml', $this->application->parseConfigurationPath($input));
+ }
+
public function testParseConfigurationPathDefault()
{
$input = new ArgvInput(['mason']);
@@ -82,19 +81,6 @@ public function testParseConfigurationPathDefaultFileExists()
$this->assertSame('./mason.yml', $this->application->parseConfigurationPath($input));
}
- public function testParseConfigurationPath()
- {
- Phake::when($this->isolator)
- ->is_file('foo/bar.yml')
- ->thenReturn(true);
-
- $input = new ArgvInput(['mason', '--config', 'foo/bar.yml']);
- $this->assertSame('foo/bar.yml', $this->application->parseConfigurationPath($input));
-
- $input = new ArgvInput(['mason', '-c', 'foo/bar.yml']);
- $this->assertSame('foo/bar.yml', $this->application->parseConfigurationPath($input));
- }
-
public function testParseConfigurationPathWithUnknownFile()
{
$input = new ArgvInput(['mason', '--config', 'foo/bar.yml']);
@@ -146,11 +132,8 @@ public function testDoRunWithConfiguration()
$this->application->run($input, $this->output)
);
- Phake::verify($this->configurationReader)->readFile(
- './mason.yml',
- $this->isInstanceOf(
- 'Psr\Log\NullLogger'
- )
+ Phake::verify($this->masonApplication)->loadConfiguration(
+ './mason.yml'
);
Phake::verify($this->application)->add(
@@ -166,8 +149,8 @@ public function testDoRunWithInvalidConfiguration()
->is_file('./mason.yml')
->thenReturn(true);
- Phake::when($this->configurationReader)
- ->readFile(Phake::anyParameters())
+ Phake::when($this->masonApplication)
+ ->loadConfiguration(Phake::anyParameters())
->thenThrow(new InvalidConfigurationException('Invalid configuration.'));
$this->assertSame(
@@ -215,24 +198,4 @@ public function testDoRunExecutesListWhenConfigurationHasNoDefaultTask()
// Ensure that the app tried to find the default task command ...
Phake::verify($this->application)->find('list');
}
-
- public function testDoRunSetsServiceLocator()
- {
- $command = Phake::partialMock('Mason\Console\Command\ValidateCommand');
-
- Phake::when($command)
- ->execute(Phake::anyParameters())
- ->thenReturn(null);
-
- $this->application->add($command);
-
- $input = new ArgvInput(['mason', 'config:validate']);
-
- $this->assertSame(
- 0,
- $this->application->run($input, $this->output)
- );
-
- Phake::verify($command)->setServiceLocator($this->serviceLocator);
- }
}
View
35 test/suite/Console/Command/ValidateCommandTest.php
@@ -1,35 +0,0 @@
-<?php
-namespace Mason\Console\Command;
-
-use Phake;
-use PHPUnit_Framework_TestCase;
-use Symfony\Component\Console\Input\ArgvInput;
-
-class ValidateCommandTest extends PHPUnit_Framework_TestCase
-{
- public function setUp()
- {
- $this->application = Phake::mock('Mason\Application\ApplicationInterface');
- $this->output = Phake::mock('Symfony\Component\Console\Output\OutputInterface');
- $this->command = new ValidateCommand($this->application);
- }
-
- public function testConfigure()
- {
- $this->assertSame('config:validate', $this->command->getName());
- $this->assertSame('Validate a Mason configuration file.', $this->command->getDescription());
- }
-
- public function testExecute()
- {
- $this->markTestIncomplete();
-
- $input = new ArgvInput(
- array('mason', 'validate')
- );
-
- $result = $this->command->run($input, $this->output);
-
- $this->assertSame(0, $result);
- }
-}
View
5 test/suite/Extension/ExtensionLoaderTest.php
@@ -17,6 +17,11 @@ public function setUp()
$this->loader->setIsolator($this->isolator);
}
+ public function testIndexPath()
+ {
+ $this->assertSame($this->path, $this->loader->indexPath());
+ }
+
public function testLoadExtensions()
{
Phake::when($this->isolator)
Please sign in to comment.
Something went wrong with that request. Please try again.