Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkart committed Jul 26, 2019
1 parent 00de0f3 commit be38141
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ $curl = $command->build();
// curl -v -L http://example.com

// change order
$command->setTemplate(Command::TEMPLATE_NAME . Command::TEMPLATE_URL . Command::TEMPLATE_OPTIONS);
$command->setTemplate(Command::TEMPLATE_COMMAND_NAME . Command::TEMPLATE_URL . Command::TEMPLATE_OPTIONS);
$curl = $command->build();
// curl http://example.com -v -L

// remove options
$command->setTemplate(Command::TEMPLATE_NAME . Command::TEMPLATE_URL);
$command->setTemplate(Command::TEMPLATE_COMMAND_NAME . Command::TEMPLATE_URL);
$curl = $command->build();
// curl http://example.com
```
Expand Down
8 changes: 4 additions & 4 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Command
/**
* Template part which represents command name
*/
public const TEMPLATE_NAME = '{name}';
public const TEMPLATE_COMMAND_NAME = '{name}';

/**
* Template part which represents command line options
Expand Down Expand Up @@ -200,7 +200,7 @@ public function setCommand(string $command): void
*/
private function initTemplate(): void
{
$this->setTemplate(static::TEMPLATE_NAME . static::TEMPLATE_OPTIONS . static::TEMPLATE_URL);
$this->setTemplate(static::TEMPLATE_COMMAND_NAME . static::TEMPLATE_OPTIONS . static::TEMPLATE_URL);
}

/**
Expand All @@ -216,7 +216,7 @@ private function initQuoteCharacter(): void
*/
private function buildName(): void
{
$this->buildTemplatePart(static::TEMPLATE_NAME, static::COMMAND_NAME);
$this->buildTemplatePart(static::TEMPLATE_COMMAND_NAME, static::COMMAND_NAME);
}

/**
Expand Down Expand Up @@ -320,7 +320,7 @@ private function quote(string $argument): string
}

/**
* Escapes double quotes in the argument
* Escapes quotes in the argument
* @param string $argument
* @return string
*/
Expand Down
70 changes: 30 additions & 40 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

class CommandTest extends TestCase
{
public function getNewCommand(): Command
{
$command = new Command();
$command->setUrl('http://example.com');

return $command;
}

public function testBuildMinimal(): void
{
$command = new Command();
Expand All @@ -17,8 +25,8 @@ public function testBuildMinimal(): void
public function testBuildWithUrl(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$this->assertEquals('curl http://example.com', $command->build());
$command->setUrl('http://example.com/test');
$this->assertEquals('curl http://example.com/test', $command->build());
}

public function testBuildWithOption(): void
Expand All @@ -38,16 +46,14 @@ public function testBuildWithOptions(): void

public function testBuildWithUrlAndOption(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-v');
$this->assertEquals('curl -v http://example.com', $command->build());
}

public function testBuildWithUrlAndOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-v');
$command->addOption('-L');
$this->assertEquals('curl -v -L http://example.com', $command->build());
Expand All @@ -57,8 +63,7 @@ public function testBuildWithUrlAndOptions(): void

public function testBuildSetOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();

$command->setOptions([]);
$this->assertEquals('curl http://example.com', $command->build());
Expand All @@ -85,8 +90,7 @@ public function testBuildSetOptions(): void

public function testBuildDuplicatedOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-v');
$command->addOption('-L');
$command->addOption('-L');
Expand All @@ -95,14 +99,13 @@ public function testBuildDuplicatedOptions(): void

public function testBuildSetTemplate(): void
{
$command = new Command();
$command->setTemplate(Command::TEMPLATE_NAME . Command::TEMPLATE_URL . Command::TEMPLATE_OPTIONS);
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->setTemplate(Command::TEMPLATE_COMMAND_NAME . Command::TEMPLATE_URL . Command::TEMPLATE_OPTIONS);
$command->addOption('-v');
$command->addOption('-L');
$this->assertEquals('curl http://example.com -v -L', $command->build());

$command->setTemplate(Command::TEMPLATE_NAME . Command::TEMPLATE_URL);
$command->setTemplate(Command::TEMPLATE_COMMAND_NAME . Command::TEMPLATE_URL);
$this->assertEquals('curl http://example.com', $command->build());

$command->setTemplate('');
Expand All @@ -111,8 +114,7 @@ public function testBuildSetTemplate(): void

public function testBuildWithLongOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('--verbose');
$this->assertEquals('curl --verbose http://example.com', $command->build());

Expand All @@ -122,28 +124,24 @@ public function testBuildWithLongOptions(): void

public function testBuildWithArgumentsToOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-d', 'arbitrary');
$this->assertEquals("curl -d 'arbitrary' http://example.com", $command->build());

// data from file
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-d', '@json.txt');
$this->assertEquals("curl -d '@json.txt' http://example.com", $command->build());

// argument with spaces
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-d', 'I am your father');
$this->assertEquals("curl -d 'I am your father' http://example.com", $command->build());
}

public function testBuildSetQuoteCharacter(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-d', 'arbitrary');

// default is singe
Expand All @@ -167,9 +165,8 @@ public function testBuildEscapeArguments(): void
$expected = <<<EXP
curl -d $'x=test\'1' http://example.com
EXP;
$command = new Command();
$command = $this->getNewCommand();
$command->setQuoteCharacter(Command::QUOTE_SINGLE);
$command->setUrl('http://example.com');
$command->addOption('-d', $argument);
$this->assertEquals($expected, $command->build());

Expand All @@ -179,9 +176,8 @@ public function testBuildEscapeArguments(): void
$expected = <<<EXP
curl -d 'x=test"2' http://example.com
EXP;
$command = new Command();
$command = $this->getNewCommand();
$command->setQuoteCharacter(Command::QUOTE_SINGLE);
$command->setUrl('http://example.com');
$command->addOption('-d', $argument);
$this->assertEquals($expected, $command->build());

Expand All @@ -191,9 +187,8 @@ public function testBuildEscapeArguments(): void
$expected = <<<EXP
curl -d $'x=test\'1"2' http://example.com
EXP;
$command = new Command();
$command = $this->getNewCommand();
$command->setQuoteCharacter(Command::QUOTE_SINGLE);
$command->setUrl('http://example.com');
$command->addOption('-d', $argument);
$this->assertEquals($expected, $command->build());

Expand All @@ -203,9 +198,8 @@ public function testBuildEscapeArguments(): void
$expected = <<<EXP
curl -d "x=test'1" http://example.com
EXP;
$command = new Command();
$command = $this->getNewCommand();
$command->setQuoteCharacter(Command::QUOTE_DOUBLE);
$command->setUrl('http://example.com');
$command->addOption('-d', $argument);
$this->assertEquals($expected, $command->build());

Expand All @@ -215,9 +209,8 @@ public function testBuildEscapeArguments(): void
$expected = <<<EXP
curl -d "x=test\"2" http://example.com
EXP;
$command = new Command();
$command = $this->getNewCommand();
$command->setQuoteCharacter(Command::QUOTE_DOUBLE);
$command->setUrl('http://example.com');
$command->addOption('-d', $argument);
$this->assertEquals($expected, $command->build());

Expand All @@ -227,26 +220,23 @@ public function testBuildEscapeArguments(): void
$expected = <<<EXP
curl -d "x=test'1\"2" http://example.com
EXP;
$command = new Command();
$command = $this->getNewCommand();
$command->setQuoteCharacter(Command::QUOTE_DOUBLE);
$command->setUrl('http://example.com');
$command->addOption('-d', $argument);
$this->assertEquals($expected, $command->build());
}

public function testBuildMultipleOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-H', 'Connection: keep-alive');
$command->addOption('-H', 'Cache-Control: max-age=0');
$this->assertEquals("curl -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' http://example.com", $command->build());
}

public function testBuildAddOptions(): void
{
$command = new Command();
$command->setUrl('http://example.com');
$command = $this->getNewCommand();
$command->addOption('-v');
$command->addOptions([
'-L',
Expand Down

0 comments on commit be38141

Please sign in to comment.