Skip to content

Commit

Permalink
Merge pull request #56 from madewithlove/use-api-client-v2
Browse files Browse the repository at this point in the history
Upgrade to htaccess-api-client v2
  • Loading branch information
WouterSioen committed Oct 8, 2021
2 parents fdfe344 + a6ae7a1 commit 713f63b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 45 deletions.
22 changes: 13 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: PHPStan
uses: docker://oskarstark/phpstan-ga
- uses: shivammathur/setup-php@v2
with:
args: analyse
php-version: '8.0'
tools: composer
- name: Install dependencies
run: composer update -n --prefer-dist
- name: PHPStan
run: vendor/bin/phpstan analyse

phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.3', '7.4', '8.0']
php-versions: ['8.0', '8.1']
prefer-lowest: ['', '--prefer-lowest']
steps:
- uses: actions/checkout@master
- uses: shivammathur/setup-php@v1
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: pcov
tools: composer:v2
tools: composer
- name: Install dependencies
if: matrix.php-versions != '8.0'
if: matrix.php-versions != '8.1'
run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }}
- name: Install dependencies
if: matrix.php-versions == '8.0'
if: matrix.php-versions == '8.1'
run: composer install -n --prefer-dist --ignore-platform-req=php
- name: Run PHPUnit unit tests
run: vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
- uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
if: matrix.php-versions == '7.4'
if: matrix.php-versions == '8.0'

run:
name: Run current version
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"description": "CLI interface for the best htaccess tester in the world.",
"type": "package",
"require": {
"php": "^7.3|^8.0",
"php": "^8.0",
"symfony/console": "^3.0 || ^4.0 || ^5.0",
"http-interop/http-factory-guzzle": "^1.0",
"php-http/guzzle7-adapter": "^1.0",
"madewithlove/htaccess-api-client": "^1.6",
"madewithlove/htaccess-api-client": "^2.0",
"symfony/yaml": "^3.0 || ^4.0 || ^5.0"
},
"bin": [
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ parameters:
paths:
- src
- bin
- tests
49 changes: 20 additions & 29 deletions src/HtaccessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Madewithlove;

use Madewithlove\Htaccess\TableRenderer;
use Madewithlove\HtaccessResult;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException as SymfonyRuntimeException;
Expand All @@ -16,24 +15,13 @@

final class HtaccessCommand extends Command
{
/**
* @var HtaccessClient
*/
private $htaccessClient;

/**
* @var TableRenderer
*/
private $tableRenderer;

protected static $defaultName = 'htaccess';

public function __construct(HtaccessClient $htaccessClient, TableRenderer $tableRenderer)
{
public function __construct(
private HtaccessClient $htaccessClient,
private TableRenderer $tableRenderer
) {
parent::__construct(self::$defaultName);

$this->htaccessClient = $htaccessClient;
$this->tableRenderer = $tableRenderer;
}

protected function configure(): void
Expand Down Expand Up @@ -125,13 +113,7 @@ private function testSingleUrl(string $url, string $htaccess, InputInterface $in

if ($input->getOption('share')) {
try {
/** @var ?string $referrer */
$referrer = $input->getOption('referrer');
/** @var ?string $serverName */
$serverName = $input->getOption('server-name');

$share = $this->htaccessClient->share($url, $htaccess, $referrer, $serverName);

$share = $this->htaccessClient->share($url, $htaccess, $this->getServerVariables($input));
$io->text('You can share this test run on ' . $share->getShareUrl());
} catch (HtaccessException $exception) {
// when sharing failed, just ignore it
Expand Down Expand Up @@ -186,17 +168,26 @@ private function validateInput(InputInterface $input): void

private function test(string $url, string $htaccess, InputInterface $input, ?SymfonyStyle $io = null): HtaccessResult
{
/** @var ?string $referrer */
$referrer = $input->getOption('referrer');
/** @var ?string $serverName */
$serverName = $input->getOption('server-name');

$result = $this->htaccessClient->test($url, $htaccess, $referrer, $serverName);
$result = $this->htaccessClient->test($url, $htaccess, $this->getServerVariables($input));

if ($io) {
$this->tableRenderer->renderHtaccessResult($result, $io);
}

return $result;
}

private function getServerVariables(InputInterface $input): ServerVariables
{
$serverVariables = ServerVariables::default();
if ($referrer = $input->getOption('referrer')) {
$serverVariables = $serverVariables->with(ServerVariable::HTTP_REFERER, $referrer);
}

if ($serverName = $input->getOption('server-name')) {
$serverVariables = $serverVariables->with(ServerVariable::SERVER_NAME, $serverName);
}

return $serverVariables;
}
}
46 changes: 41 additions & 5 deletions tests/HtaccessCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
use Http\Adapter\Guzzle7\Client;
use Http\Factory\Guzzle\ServerRequestFactory;
use Madewithlove\Htaccess\TableRenderer;
use Madewithlove\HtaccessClient;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;

final class HtaccessCommandTest extends TestCase
{
/**
* @var HtaccessCommand
*/
private $command;
private HtaccessCommand $command;

public function setUp(): void
{
Expand Down Expand Up @@ -184,4 +180,44 @@ public function it does mark an unsupported line as potentially invalid(
$commandTester->getDisplay()
);
}

/** @test */
public function it supports http referrer(): void
{
file_put_contents(
getcwd() . '/.htaccess',
"RewriteCond %{HTTP_REFERER} https://example.com\nRewriteRule .* /foo"
);

$commandTester = new CommandTester($this->command);
$commandTester->execute([
'url' => 'http://localhost',
'--referrer' => 'https://example.com'
]);

$this->assertStringContainsString(
'The output url is "http://localhost/foo"',
$commandTester->getDisplay()
);
}

/** @test */
public function it supports server name(): void
{
file_put_contents(
getcwd() . '/.htaccess',
"RewriteCond %{SERVER_NAME} example.com\nRewriteRule .* /foo"
);

$commandTester = new CommandTester($this->command);
$commandTester->execute([
'url' => 'http://localhost',
'--server-name' => 'example.com'
]);

$this->assertStringContainsString(
'The output url is "http://localhost/foo"',
$commandTester->getDisplay()
);
}
}

0 comments on commit 713f63b

Please sign in to comment.