From be38141eadfec8fa71b630e058c40f0b1d116522 Mon Sep 17 00:00:00 2001 From: Alexander Kartavenko Date: Fri, 26 Jul 2019 11:09:58 +0300 Subject: [PATCH] Refactoring --- README.md | 4 +-- src/Command.php | 8 ++--- tests/CommandTest.php | 70 +++++++++++++++++++------------------------ 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 4dc20dd..31e6607 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/Command.php b/src/Command.php index a633ebc..f702109 100644 --- a/src/Command.php +++ b/src/Command.php @@ -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 @@ -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); } /** @@ -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); } /** @@ -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 */ diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 9ae28f9..2ec636d 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -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(); @@ -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 @@ -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()); @@ -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()); @@ -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'); @@ -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(''); @@ -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()); @@ -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 @@ -167,9 +165,8 @@ public function testBuildEscapeArguments(): void $expected = <<getNewCommand(); $command->setQuoteCharacter(Command::QUOTE_SINGLE); - $command->setUrl('http://example.com'); $command->addOption('-d', $argument); $this->assertEquals($expected, $command->build()); @@ -179,9 +176,8 @@ public function testBuildEscapeArguments(): void $expected = <<getNewCommand(); $command->setQuoteCharacter(Command::QUOTE_SINGLE); - $command->setUrl('http://example.com'); $command->addOption('-d', $argument); $this->assertEquals($expected, $command->build()); @@ -191,9 +187,8 @@ public function testBuildEscapeArguments(): void $expected = <<getNewCommand(); $command->setQuoteCharacter(Command::QUOTE_SINGLE); - $command->setUrl('http://example.com'); $command->addOption('-d', $argument); $this->assertEquals($expected, $command->build()); @@ -203,9 +198,8 @@ public function testBuildEscapeArguments(): void $expected = <<getNewCommand(); $command->setQuoteCharacter(Command::QUOTE_DOUBLE); - $command->setUrl('http://example.com'); $command->addOption('-d', $argument); $this->assertEquals($expected, $command->build()); @@ -215,9 +209,8 @@ public function testBuildEscapeArguments(): void $expected = <<getNewCommand(); $command->setQuoteCharacter(Command::QUOTE_DOUBLE); - $command->setUrl('http://example.com'); $command->addOption('-d', $argument); $this->assertEquals($expected, $command->build()); @@ -227,17 +220,15 @@ public function testBuildEscapeArguments(): void $expected = <<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()); @@ -245,8 +236,7 @@ public function testBuildMultipleOptions(): void public function testBuildAddOptions(): void { - $command = new Command(); - $command->setUrl('http://example.com'); + $command = $this->getNewCommand(); $command->addOption('-v'); $command->addOptions([ '-L',