Skip to content

Commit

Permalink
Merge pull request #13 from phpsu/feature/add-conditional-arguments
Browse files Browse the repository at this point in the history
BC: add conditionals for builder and command
  • Loading branch information
ChrisB9 authored Sep 7, 2020
2 parents 954e7ee + 3e6a7fa commit 43be21d
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "MIT",
"authors": [
{
"name": "Chris Ben",
"name": "Christian Rodriguez Benthake",
"email": "[email protected]"
}
],
Expand Down
6 changes: 4 additions & 2 deletions src/ShellBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

final class ShellBuilder implements ShellInterface, \JsonSerializable
{
use ShellConditional;

/** @var array<ShellInterface|string> */
private $commandList = [];
/** @var int */
Expand Down Expand Up @@ -296,7 +298,7 @@ public function createCommandSubstition(): self

public function hasCommands(): bool
{
return empty($this->commandList) && empty($this->variables);
return empty($this->commandList) === false || empty($this->variables) === false;
}

/**
Expand Down Expand Up @@ -366,7 +368,7 @@ public function __toString(): string
}
if ($this->groupType === GroupType::SAMESHELL_GROUP) {
return sprintf(
'%s%s;%s',
'%s %s;%s',
ControlOperator::CURLY_BLOCK_DEFINITON_OPEN,
$result,
ControlOperator::CURLY_BLOCK_DEFINITON_CLOSE
Expand Down
2 changes: 2 additions & 0 deletions src/ShellCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
final class ShellCommand implements ShellInterface
{
use ShellConditional;

/**
* @var ShellWord
* @psalm-readonly
Expand Down
28 changes: 28 additions & 0 deletions src/ShellConditional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PHPSu\ShellCommandBuilder;

trait ShellConditional
{
public function if(bool $condition, callable $callback, callable $alternativeCallback = null): self
{
if ($condition) {
$result = $callback($this);
assert($result instanceof self);
return $result;
}
if ($alternativeCallback) {
$alternativeResult = $alternativeCallback($this);
assert($alternativeResult instanceof self);
return $alternativeResult;
}
return $this;
}

public function ifThis(callable $callOnThis, callable $callback, callable $alternativeCallback = null): self
{
return $this->if($callOnThis($this) === true, $callback, $alternativeCallback);
}
}
92 changes: 83 additions & 9 deletions tests/ShellBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class ShellBuilderTest extends TestCase
{
public function testBuilderConcept(): void
{
$result = 'a && b | c || d |& f && (g && h) || {i || j;}';
$result = 'a && b | c || d |& f && (g && h) || { i || j;}';
$builder = new ShellBuilder();
$a = $builder->createCommand('a');
$b = $builder->createCommand('b');
Expand Down Expand Up @@ -46,7 +46,7 @@ public function testBuilderConcept(): void

public function testBuilderConceptWithShortcut(): void
{
$result = 'a && b | c || d |& f && (g && h) || {i || j;}';
$result = 'a && b | c || d |& f && (g && h) || { i || j;}';
$builder = new ShellBuilder();
$builder
->add('a')
Expand Down Expand Up @@ -160,7 +160,7 @@ public function testShellBuilderGroupSameShell(): void
->add(
$builder->createCommand('echo')->addArgument('hello')
)->and('cat'));
$this->assertEquals("{echo 'hello' && cat;}", (string)$builder);
$this->assertEquals("{ echo 'hello' && cat;}", (string)$builder);
}

public function testSimpleSshCommand(): void
Expand Down Expand Up @@ -683,7 +683,7 @@ public function testCoprocessWithShellGroupAndRedirections(): void
->addToBuilder()
->redirectDescriptor('', true, null, 3);
$this->assertEquals(
'{coproc tee {tee logfile;} >&3 ;} 3>&1',
'{ coproc tee { tee logfile;} >&3 ;} 3>&1',
(string)ShellBuilder::new()->add($builder)->redirectDescriptor('', true, 3, 1)
);
}
Expand All @@ -700,11 +700,85 @@ public function testNamedCoprocessWithShellGroupAndRedirections(): void
->addToBuilder())
->redirectDescriptor('', true, null, 3);
$this->assertEquals(
'{coproc mycoproc {awk \'{print "foo" $0;fflush()}\';} >&3 ;} 3>&1',
'{ coproc mycoproc { awk \'{print "foo" $0;fflush()}\';} >&3 ;} 3>&1',
(string)ShellBuilder::new()->add($builder)->redirectDescriptor('', true, 3, 1)
);
}

public function testCondiditionalArguments(): void
{
// if false
$builder = ShellBuilder::new()
->if(false, static function (ShellBuilder $builder) {
return $builder->add('echo');
})
->ifThis(static function (ShellBuilder $builder) {
return $builder->hasCommands() === false;
}, static function (ShellBuilder $builder) {
return $builder->add('print');
});
static::assertEquals('print', (string)$builder);

// if true
$builder = ShellBuilder::new()
->if(true, static function (ShellBuilder $builder) {
return $builder->add('echo');
})
->ifThis(static function (ShellBuilder $builder) {
return $builder->hasCommands() === false;
}, static function (ShellBuilder $builder) {
return $builder->add('print');
});
static::assertEquals('echo', (string)$builder);
}

public function testComplexCondiditionalArguments(): void
{
$builder = ShellBuilder::new()
->if(
false,
static function (ShellBuilder $builder) {
return $builder->add('echo');
},
static function (ShellBuilder $builder) {
return $builder->add('awk');
}
)
->ifThis(static function (ShellBuilder $builder) {
return $builder->hasCommands() === false;
}, static function (ShellBuilder $builder) {
return $builder->add('print');
}, static function (ShellBuilder $builder) {
return $builder->and('print');
});
static::assertEquals('awk && print', (string)$builder);
}

public function testComplexCondiditionalArgumentsWithWrongArguments(): void
{
self::expectException(\ErrorException::class);
ShellBuilder::new()
->ifThis(static function (ShellBuilder $builder) {
return 'world';
}, static function (ShellBuilder $builder) {
return $builder->add('print');
}, static function (ShellBuilder $builder) {
return 'bla';
});
}

public function testCondiditionalArgumentsWithWrongArguments(): void
{
self::expectException(\ErrorException::class);
ShellBuilder::new()
->if(
true,
static function (ShellBuilder $builder) {
return 'hello world';
}
);
}

public function testSimpleAsyncShellBuilder(): void
{
$this->assertEquals(
Expand Down Expand Up @@ -789,18 +863,18 @@ public function testCommandVariableWithConditionalAndCommand(): void
public function testShellBuilderIsEmpty(): void
{
$builder = ShellBuilder::new();
$this->assertTrue($builder->hasCommands());
$this->assertFalse($builder->hasCommands());
}

public function testShellBuilderIsNotEmpty(): void
{
$builder = ShellBuilder::new();
$builder->addVariable('a', 'b');
$this->assertFalse($builder->hasCommands());
$builder->removeVariable('a');
$this->assertTrue($builder->hasCommands());
$builder->add('echo');
$builder->removeVariable('a');
$this->assertFalse($builder->hasCommands());
$builder->add('echo');
$this->assertTrue($builder->hasCommands());
}

public function testAddVariableWithoutSemicolon(): void
Expand Down
13 changes: 13 additions & 0 deletions tests/ShellCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPSu\ShellCommandBuilder\Definition\GroupType;
use PHPSu\ShellCommandBuilder\Exception\ShellBuilderException;
use PHPSu\ShellCommandBuilder\ShellBuilder;
use PHPSu\ShellCommandBuilder\ShellCommand;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -171,6 +172,18 @@ public function testShellCommandWithCommandSubstitutionToArray(): void
], $command['arguments']);
}

public function testConditionalArguments()
{
$command = ShellBuilder::command('test')
->if(1 + 1 === 3, static function (ShellCommand $command) {
return $command->addOption('f', '1 + 1 = 3');
})
->if(1 + 1 === 2, static function (ShellCommand $command) {
return $command->addOption('t', '1 + 1 = 2');
});
static::assertEquals((string)$command, 'test --t \'1 + 1 = 2\'');
}

public function testUnEscapedOption(): void
{
$command = (new ShellCommand('ls'))->addOption('color', 'true', false, true);
Expand Down

0 comments on commit 43be21d

Please sign in to comment.