Skip to content

Commit

Permalink
feat: yarn, npm and composer methods can use docker-image as run-cont…
Browse files Browse the repository at this point in the history
…exts now

This will also deprecate the compound properties like `yarnBuildCommand`, `composerRunContext` and move everything in their respective name-space, eg.

```
yarn:
  buildCommand: build
  context: docker-image
  image: node
```
etc. The old syntax can still be used, but will create a warning
  • Loading branch information
stmh committed Aug 14, 2022
1 parent 91b59b6 commit 894fcf6
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Method/ArtifactsBaseMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function buildArtifact(
$keys = array_unique($keys);

foreach ($keys as $key) {
if ($host_config->get($key, 'ignore')[0] == '.') {
if ($host_config->getProperty($key, 'ignore')[0] == '.') {
$dir = $install_dir . '/' . $host_config[$key];
} else {
$dir = $install_dir;
Expand Down
6 changes: 3 additions & 3 deletions src/Method/DrupalconsoleMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ private function getDrupalExec(string $root_folder, ShellProviderInterface $shel
private function getRootFolder(HostConfig $host_config)
{
$keys = [
'composerRootFolder',
'composer.rootFolder',
'gitRootFolder',
'rootFolder'
];
foreach ($keys as $key) {
if (!empty($host_config[$key])) {
return $host_config[$key];
if (!empty($host_config->getProperty($key))) {
return $host_config->getProperty($key);
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/Method/NpmMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ public function getName(): string
return 'npm';
}

public function getDeprecationMapping(): array
{
$mapping = parent::getDeprecationMapping();
$prefix = $this->getConfigPrefix();
return array_merge($mapping, [
"${prefix}BuildCommand" => "${prefix}.buildCommand",
]);
}

public function validateConfig(Node $config, ValidationErrorBagInterface $errors)
{
parent::validateConfig($config, $errors); // TODO: Change the autogenerated stub
parent::validateConfig($config, $errors);

$service = new ValidationService($config, $errors, 'NPM');
$service->hasKey('npmBuildCommand', 'build command to run with npm');
$service->hasKey('npm.buildCommand', 'build command to run with npm');
$service->deprecate([
"npmBuildCommand" => "please change to `npm.buildCommand`",
]);
}

protected function prepareCommand(HostConfig $host_config, TaskContextInterface $context, string $command): string
Expand Down
14 changes: 13 additions & 1 deletion src/Method/YarnMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ public function getName(): string
return 'yarn';
}

public function getDeprecationMapping(): array
{
$mapping = parent::getDeprecationMapping();
$prefix = $this->getConfigPrefix();
return array_merge($mapping, [
"${prefix}BuildCommand" => "${prefix}.buildCommand",
]);
}

public function validateConfig(Node $config, ValidationErrorBagInterface $errors)
{
parent::validateConfig($config, $errors); // TODO: Change the autogenerated stub
$service = new ValidationService($config, $errors, 'Yarn');
$service->hasKey('yarnBuildCommand', 'build command to run with yarn');
$service->deprecate([
"yarnBuildCommand" => "please change to `yarn.buildCommand`",
]);
$service->hasKey('yarn.buildCommand', 'build command to run with yarn');
}

protected function prepareCommand(HostConfig $host_config, TaskContextInterface $context, string $command): string
Expand Down
139 changes: 104 additions & 35 deletions tests/RunCommandBaseMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,138 @@

namespace Phabalicious\Tests;

use Phabalicious\Command\BaseCommand;
use Phabalicious\Configuration\ConfigurationService;
use Phabalicious\Configuration\Storage\Node;
use Phabalicious\Method\ComposerMethod;
use Phabalicious\Method\LaravelMethod;
use Phabalicious\Method\MethodFactory;
use Phabalicious\Method\MysqlMethod;
use Phabalicious\Method\NpmMethod;
use Phabalicious\Method\ScriptMethod;
use Phabalicious\Method\TaskContext;
use Phabalicious\Method\YarnMethod;
use Phabalicious\Validation\ValidationErrorBag;
use Psr\Log\AbstractLogger;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class LaravelMethodTest extends PhabTestCase
class RunCommandBaseMethodTest extends PhabTestCase
{
/** @var \Phabalicious\Method\DrushMethod */
private $method;

/** @var ConfigurationService */
private $configurationService;

/**
* @var \Phabalicious\Method\TaskContext
*/
protected $context;

/**
* @var mixed|\PHPUnit\Framework\MockObject\MockObject|\Psr\Log\AbstractLogger
*/
private $logger;

/**
* @var \Phabalicious\Method\MethodFactory
*/
private $methodFactory;

public function setup(): void
{
$logger = $this->getMockBuilder(AbstractLogger::class)->getMock();
$this->logger = $this->getMockBuilder(AbstractLogger::class)->getMock();
$app = $this->getMockBuilder(Application::class)->getMock();
$this->method = new LaravelMethod($logger);
$this->configurationService = new ConfigurationService($app, $logger);
$this->configurationService = new ConfigurationService($app, $this->logger);

$method_factory = new MethodFactory($this->configurationService, $this->logger);
$method_factory->addMethod(new ScriptMethod($this->logger));
$method_factory->addMethod(new YarnMethod($this->logger));
$method_factory->addMethod(new NpmMethod($this->logger));
$method_factory->addMethod(new ComposerMethod($this->logger));
$method_factory->addMethod(new LaravelMethod($this->logger));

$this->methodFactory = $method_factory;

$method_factory = new MethodFactory($this->configurationService, $logger);
$method_factory->addMethod(new ScriptMethod($logger));
$method_factory->addMethod(new MysqlMethod($logger));
$method_factory->addMethod($this->method);
$this->context = new TaskContext(
$this->getMockBuilder(BaseCommand::class)->disableOriginalConstructor()->getMock(),
$this->getMockBuilder(InputInterface::class)->getMock(),
$this->getMockBuilder(OutputInterface::class)->getMock()
);
$this->context->setConfigurationService($this->configurationService);

$this->configurationService->readConfiguration(__DIR__ . '/assets/laravel-tests/fabfile.yaml');

$this->configurationService->readConfiguration(__DIR__ . '/assets/run-command-tests/fabfile.yaml');
}

public function testGetDefaultConfig()

/**
* @dataProvider methodNameProvider
*/
public function testDeprecatedConfig($method_name, $has_build_command)
{
$host_config = [
'rootFolder' => '.',
'needs' => ['laravel'],
'artisanTasks' => [
'reset' => ['mycustomresettask']
],
"${method_name}BuildCommand" => 'build',
"${method_name}RunContext" => 'host',
"${method_name}RootFolder" => '/foo/bar',
];
$result = $this->method->getDefaultConfig($this->configurationService, new Node($host_config, 'code'));

$this->assertArrayHasKey('reset', $result['artisanTasks']);
$this->assertArrayHasKey('install', $result['artisanTasks']);
$this->assertEquals(['mycustomresettask'], $result['artisanTasks']['reset']);
$errors = new ValidationErrorBag();
$class_name = "Phabalicious\\Method\\" . ucwords($method_name) . "Method";
$method = new $class_name($this->logger);
$method->validateConfig(new Node($host_config, 'test'), $errors);
if ($has_build_command) {
$this->assertArrayHasKey("${method_name}BuildCommand", $errors->getWarnings());
}
$this->assertArrayHasKey("${method_name}RunContext", $errors->getWarnings());
$this->assertArrayHasKey("${method_name}RootFolder", $errors->getWarnings());
}

public function testCustomArtisanTasks()
/**
* @dataProvider methodNameProvider
*/
public function testDeprecationsStillAvailable($method_name, $has_build_command, $docker_image)
{
$result = $this->configurationService->getHostConfig('test-custom-artisan-tasks');
$this->assertArrayHasKey('reset', $result['artisanTasks']);
$this->assertArrayHasKey('install', $result['artisanTasks']);
$this->assertEquals(['mycustomresettask'], $result['artisanTasks']['reset']);
$this->assertEquals(['mycustominstalltask'], $result['artisanTasks']['install']);
$host_config = $this->configurationService->getHostConfig($method_name . '-deprecated');

if ($has_build_command) {
$this->assertEquals('build:prod', $host_config->getProperty("${method_name}.buildCommand"));
}
$this->assertEquals('docker-image', $host_config->getProperty("${method_name}.context"));
$this->assertEquals($docker_image, $host_config->getProperty("image"));
$this->assertEquals('/foo/bar', $host_config->getProperty("${method_name}.rootFolder"));
}

public function testDefaultCustomArtisanTasks()
/**
* @dataProvider hostConfigDataProvider
* @group docker
*/
public function testYarnRunCommand($config)
{
$result = $this->configurationService->getHostConfig('test-default-custom-artisan-tasks');
$this->assertArrayHasKey('reset', $result['artisanTasks']);
$this->assertArrayHasKey('install', $result['artisanTasks']);
$this->assertEquals(['mycustomdefaultresettask'], $result['artisanTasks']['reset']);
$this->assertEquals(['mycustominstalltask'], $result['artisanTasks']['install']);
$host_config = $this->configurationService->getHostConfig($config);

$this->context->set('command', 'info react');
$this->methodFactory->getMethod('yarn')->yarn($host_config, $this->context);
$result = $this->context->getCommandResult();

$this->assertEquals(0, $result->getExitCode());
}


public function hostConfigDataProvider(): array
{
return [
['on-host'],
['inside-docker-image'],
];
}

public function methodNameProvider()
{
return [
['yarn', true, 'node:16'],
['npm', true, 'node:16'],
['composer', false, 'composer'],
['laravel', false, 'php'],
];
}

}
60 changes: 40 additions & 20 deletions tests/assets/run-command-tests/fabfile.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
name: laravel-tests
name: run-command-tests

needs:
- laravel
- yarn

artisanTasks:
reset:
- mycustomdefaultresettask
install:
- mycustomdefaultinstalltask

hosts:
test:
rootFolder: /var/www
yarn-deprecated:
rootFolder: .
yarnRootFolder: /foo/bar
yarnBuildCommand: build:prod
yarnRunContext: docker-image
image: node:16
npm-deprecated:
needs:
- npm
rootFolder: .
npmRootFolder: /foo/bar
npmBuildCommand: build:prod
npmRunContext: docker-image
image: node:16
composer-deprecated:
needs:
- composer
rootFolder: .
composerRootFolder: /foo/bar
composerRunContext: docker-image
image: composer
laravel-deprecated:
needs:
- laravel
rootFolder: .
laravelRootFolder: /foo/bar
laravelRunContext: docker-image
image: php

test-custom-artisan-tasks:
on-host:
rootFolder: .
artisanTasks:
reset:
- mycustomresettask
install:
- mycustominstalltask
yarnBuildCommand: info react
yarnRunContext: host

test-default-custom-artisan-tasks:
rootFolder: '.'
artisanTasks:
install:
- mycustominstalltask
inside-docker-image:
inheritsFrom:
- on-host
yarn:
buildCommand: info react
context: docker-image
image: node:14

0 comments on commit 894fcf6

Please sign in to comment.