Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port PHPUnit and Behat setup tasks #2 #3

Merged
merged 5 commits into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions config/commands/drupal.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
command:

# Drupal-related command options.
drupal:

# Global options, this will be forwarded to all commands in 'drupal:' namespace.
options:
root: ${drupal.root}
base-url: ${drupal.base-url}

# Install Drupal site.
site-install:
options:
site-name: ${drupal.site.name}
Expand Down
10 changes: 10 additions & 0 deletions config/commands/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
command:
setup:
behat:
options:
source: "behat.yml.dist"
destination: "behat.yml"
phpunit:
options:
source: "phpunit.xml.dist"
destination: "phpunit.xml"
1 change: 1 addition & 0 deletions grumphp.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ imports:
parameters:
tasks.phpcs.ignore_patterns:
- vendor/
- loadTasks.php
tasks.phpmd.exclude:
- vendor/
5 changes: 5 additions & 0 deletions src/Commands/DrupalCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
class DrupalCommands extends BaseCommands
{
/**
* Install target site.
*
* This command will install the target site using configuration values
* provided in runner.yml.dist (overridable by runner.yml).
*
* @command drupal:site-install
*
* @option root Drupal root.
Expand Down
113 changes: 113 additions & 0 deletions src/Commands/SetupCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace EC\OpenEuropa\TaskRunner\Commands;

use EC\OpenEuropa\TaskRunner\Tasks\ReplaceConfigTokens as ReplaceConfigTokens;
use Symfony\Component\Console\Input\InputOption;

/**
* Class SetupCommands.
*
* @package EC\OpenEuropa\TaskRunner\Commands
*/
class SetupCommands extends BaseCommands
{
use ReplaceConfigTokens\loadTasks;

/**
* Setup Behat.
*
* This command will copy behat.yml.dist in behat.yml and replace
* configuration tokens with values provided in runner.yml.dist/runner.yml.
*
* For example, given the following configuration:
*
* > drupal:
* > root: build
*
* Then its token format would be: ${drupal.root}
*
* @command setup:behat
*
* @option source Source configuration file.
* @option destination Destination configuration file.
*
* @aliases setup:b,sb
*
* @param array $options
*
* @return \Robo\Contract\TaskInterface
*/
public function setupBehat(array $options = [
'source' => InputOption::VALUE_REQUIRED,
'destination' => InputOption::VALUE_REQUIRED,
])
{
return $this->setupReplace($options);
}

/**
* Setup PHPUnit.
*
* This command will copy phpunit.xml.dist in phpunit.xml and replace
* configuration tokens with values provided in runner.yml.dist/runner.yml.
*
* For example, given the following configuration:
*
* > drupal:
* > root: build
*
* Then its token format would be: ${drupal.root}
*
* @command setup:phpunit
*
* @option source Source configuration file.
* @option destination Destination configuration file.
*
* @aliases setup:p,sp
*
* @param array $options
*
* @return \Robo\Contract\TaskInterface
*/
public function setupPhpunit(array $options = [
'source' => InputOption::VALUE_REQUIRED,
'destination' => InputOption::VALUE_REQUIRED,
])
{
return $this->setupReplace($options);
}


/**
* Replace configuration tokens in a text file.
*
* This command will copy source file in destination file and replace
* configuration tokens with values provided in runner.yml.dist/runner.yml.
*
* For example, given the following configuration:
*
* > drupal:
* > root: build
*
* Then its token format would be: ${drupal.root}
*
* @command setup:replace
*
* @option source Source configuration file.
* @option destination Destination configuration file.
*
* @aliases setup:p,sp
*
* @param array $options
*
* @return \Robo\Contract\TaskInterface
*/
public function setupReplace(array $options = [
'source' => InputOption::VALUE_REQUIRED,
'destination' => InputOption::VALUE_REQUIRED,
])
{
return $this->taskReplaceConfigTokens($options['source'], $options['destination']);
}
}
1 change: 1 addition & 0 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private function createConfiguration(array $configPaths)
$configPaths = array_merge([
__DIR__.'/../config/runner.yml',
__DIR__.'/../config/commands/drupal.yml',
__DIR__.'/../config/commands/setup.yml',
], $configPaths);

return Robo::createConfiguration($configPaths);
Expand Down
92 changes: 92 additions & 0 deletions src/Tasks/ReplaceConfigTokens/ReplaceConfigTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace EC\OpenEuropa\TaskRunner\Tasks\ReplaceConfigTokens;

use Robo\Common\BuilderAwareTrait;
use Robo\Contract\BuilderAwareInterface;
use Robo\Task\BaseTask;
use Robo\Task\File\Replace;
use Robo\Task\Filesystem\FilesystemStack;

/**
* Class ReplaceConfigTokens
*
* @package EC\OpenEuropa\TaskRunner\Tasks\ReplaceConfigTokens
*/
class ReplaceConfigTokens extends BaseTask implements BuilderAwareInterface
{
use BuilderAwareTrait;

const TOKEN_REGEX = '/\$\{(([A-Za-z_\-]+\.?)+)\}/';

/**
* Source file.
*
* @var string
*/
protected $source;

/**
* Destination file.
*
* @var string
*/
protected $destination;

/**
* @var \Robo\Task\Filesystem\FilesystemStack
*/
protected $filesystem;

/**
* @var \Robo\Task\File\Replace
*/
protected $replace;

/**
* ReplaceConfigTokens constructor.
*
* @param string $source
* @param string $destination
*/
public function __construct($source, $destination)
{
$this->source = $source;
$this->destination = $destination;
$this->filesystem = new FilesystemStack();
$this->replace = new Replace($destination);
}

/**
* @return \Robo\Result
*/
public function run()
{
$content = file_get_contents($this->source);
$config = $this->getConfig();

$tokens = array_map(function ($key) use ($config) {
return $config->get($key);
}, $this->extractTokens($content));

return $this->collectionBuilder()->addTaskList([
$this->filesystem->copy($this->source, $this->destination, true),
$this->replace->from(array_keys($tokens))->to(array_values($tokens)),
])->run();
}

/**
* @param $text
*
* @return array
*/
protected function extractTokens($text)
{
preg_match_all(self::TOKEN_REGEX, $text, $matches);
if (isset($matches[0]) && !empty($matches[0]) && is_array($matches[0])) {
return array_combine($matches[0], $matches[1]);
}

return [];
}
}
22 changes: 22 additions & 0 deletions src/Tasks/ReplaceConfigTokens/loadTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EC\OpenEuropa\TaskRunner\Tasks\ReplaceConfigTokens;

/**
* Trait loadTasks
*
* @package EC\OpenEuropa\TaskRunner\Tasks\ReplaceConfigTokens
*/
trait loadTasks
{
/**
* @param $source
* @param $destination
*
* @return \EC\OpenEuropa\TaskRunner\Tasks\ReplaceConfigTokens\ReplaceConfigTokens
*/
public function taskReplaceConfigTokens($source, $destination)
{
return $this->task(ReplaceConfigTokens::class, $source, $destination);
}
}
17 changes: 11 additions & 6 deletions tests/AbstractTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@

namespace EC\OpenEuropa\TaskRunner\Tests;

use EC\OpenEuropa\TaskRunner\TaskRunner;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\BufferedOutput;
use Robo\TaskAccessor;
use Robo\Robo;
use Robo\Collection\CollectionBuilder;

/**
* Class AbstractTaskTest.
*
* @package EC\OpenEuropa\TaskRunner\Tests
*/
abstract class AbstractTaskTest extends TestCase implements ContainerAwareInterface
abstract class AbstractTaskTest extends AbstractTest implements ContainerAwareInterface
{
use TaskAccessor;
use ContainerAwareTrait;

/**
* @var \Symfony\Component\Console\Output\BufferedOutput
*/
protected $output;

/**
* Setup hook.
*/
public function setup()
{
$container = Robo::createDefaultContainer(null, new NullOutput());
$this->setContainer($container);
$this->output = new BufferedOutput();
$runner = new TaskRunner([], null, $this->output);
$this->setContainer($runner->getContainer());
}

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace EC\OpenEuropa\TaskRunner\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;

/**
* Class AbstractTest
*
* @package EC\OpenEuropa\TaskRunner\Tests
*/
abstract class AbstractTest extends TestCase
{

/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
protected function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs($object, $parameters);
}

/**
* @param $filepath
*
* @return mixed
*/
protected function getFixtureContent($filepath)
{
return Yaml::parse(file_get_contents(__DIR__."/fixtures/{$filepath}"));
}

/**
* @param $name
*
* @return string
*/
protected function getSandboxPath($name)
{
return __DIR__."/sandbox/{$name}";
}
}
Loading