Skip to content

Commit

Permalink
Merge pull request #11 from hashbangcode/3_curl_output
Browse files Browse the repository at this point in the history
3: Added curl output renderer and tests.
  • Loading branch information
philipnorton42 authored Nov 19, 2023
2 parents 91cf27c + a8b8b72 commit c76c68a
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Output/CurlOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Hashbangcode\CurlConverter\Output;

use Hashbangcode\CurlConverter\CurlParametersInterface;

/**
* Class CurlOutput.
*
* @package Hashbangcode\CurlConverter\Output
*/
class CurlOutput implements OutputInterface
{

/**
* {@inheritdoc}
*/
public function render(CurlParametersInterface $curlParameters): string
{
$output = 'curl ';

if ($curlParameters->followRedirects()) {
$output .= '--location ';
}

if ($curlParameters->getHttpVerb() !== 'GET') {
$output .= '--request ' . $curlParameters->getHttpVerb() . ' ';
}

if ($curlParameters->isInsecure()) {
$output .= '--insecure ';
}

if ($curlParameters->areCredentialsSet()) {
$output .= '--user username:password ';
}

foreach ($curlParameters->getHeaders() as $header) {
$output .= '-H \'' . $header . '\' ';
}

if ($curlParameters->hasData()) {
$output .= '--data \'' . $curlParameters->getData() . '\' ';
}

$output .= $curlParameters->getUrl();

return $output;
}

}
18 changes: 17 additions & 1 deletion test/CurlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
namespace Hashbangcode\CurlConverter\Test;

use Hashbangcode\CurlConverter\Input\CurlInput;
use Hashbangcode\CurlConverter\Output\CurlOutput;
use Hashbangcode\CurlConverter\Output\PhpOutput;
use PHPUnit\Framework\TestCase;
use Hashbangcode\CurlConverter\CurlConverter;

class CurlConverterTest extends TestCase
{
public function testCurlCommandToPhpOutput()
public function testCurlCommandToPhp()
{
$command = 'curl https://www.example.com/';

$input = new CurlInput();
$output = new PhpOutput();

$converter = new CurlConverter($input, $output);
$converted = $converter->convert($command);

$this->assertIsString($converted);
$this->assertStringContainsString('<?php', $converted);
$this->assertStringContainsString('https://www.example.com', $converted);
}

public function testCurlCommandToCurlCommand()
{
$command = 'curl https://www.example.com/';

$input = new CurlInput();
$output = new CurlOutput();

$converter = new CurlConverter($input, $output);
$converted = $converter->convert($command);

$this->assertEquals($command, $converted);
}
}
97 changes: 97 additions & 0 deletions test/Output/CurlOutputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Hashbangcode\CurlConverter\Output\Test;

use Hashbangcode\CurlConverter\CurlParameters;
use Hashbangcode\CurlConverter\Output\CurlOutput;
use PHPUnit\Framework\TestCase;
use Hashbangcode\CurlConverter\Output\PhpOutput;

class CurlOutputTest extends TestCase
{
public function testCurlOutput()
{
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');

$phpOutput = new CurlOutput();
$output = $phpOutput->render($curlParameters);

$this->assertEquals('curl https://www.hashbangcode.com/', $output);
}

public function testCurlFollowRedirectsOutput()
{
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');
$curlParameters->setFollowRedirects(true);

$curlOutput = new CurlOutput();
$output = $curlOutput->render($curlParameters);

$this->assertEquals('curl --location https://www.hashbangcode.com/', $output);
}

public function testCurlHttpVerbOutput() {
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');
$curlParameters->setFollowRedirects(true);
$curlParameters->setHttpVerb('PUT');

$curlOutput = new CurlOutput();
$output = $curlOutput->render($curlParameters);

$this->assertEquals('curl --location --request PUT https://www.hashbangcode.com/', $output);
}

public function testCurlHeadersOutput() {
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');
$headers = [
'Cache-Control: max-age=0',
'Accept-Language: en-GB',
'Accept: text/html',
];
$curlParameters->setHeaders($headers);

$curlOutput = new CurlOutput();
$output = $curlOutput->render($curlParameters);

$this->assertEquals('curl -H \'Cache-Control: max-age=0\' -H \'Accept-Language: en-GB\' -H \'Accept: text/html\' https://www.hashbangcode.com/', $output);
}

public function testCurlInsecureOutput() {
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');
$curlParameters->setIsInsecure(true);

$curlOutput = new CurlOutput();
$output = $curlOutput->render($curlParameters);

$this->assertEquals('curl --insecure https://www.hashbangcode.com/', $output);
}

public function testCurlUsernameAndPasswordSetOutput() {
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');
$curlParameters->setUsername('username');
$curlParameters->setPassword('password');

$curlOutput = new CurlOutput();
$output = $curlOutput->render($curlParameters);

$this->assertEquals('curl --user username:password https://www.hashbangcode.com/', $output);
}

public function testCurlDataIsOutput() {
$curlParameters = new CurlParameters();
$curlParameters->setUrl('https://www.hashbangcode.com/');
$curlParameters->setData('something=123');
$curlParameters->setHttpVerb('POST');

$curlOutput = new CurlOutput();
$output = $curlOutput->render($curlParameters);

$this->assertEquals('curl --request POST --data \'something=123\' https://www.hashbangcode.com/', $output);
}
}

0 comments on commit c76c68a

Please sign in to comment.