Skip to content

Commit

Permalink
Fix the processing of files (#320)
Browse files Browse the repository at this point in the history
When running the `process` command, the file path provided to the compactors was the absolute path whereas the `Box` class provides the relative path. This was causing issues with the PHP-Scoper patchers since one will likely expect the relative path and not account for the path to be absolute.
To avoid extra unintuitive work to the user, the relative path is provided as during a regular
compilation.

When running the `process` command with a `quiet` verbosity, only the processed file contents will
be outputed. This allows one to easily copy the processed file contents.
  • Loading branch information
theofidry authored Nov 7, 2018
1 parent ecb4b91 commit 7b908b9
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/Console/Command/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use KevinGH\Box\Compactor\Placeholder;
use KevinGH\Box\Compactors;
use KevinGH\Box\Configuration;
use function KevinGH\Box\FileSystem\make_path_relative;
use KevinGH\Box\PhpSettingsHandler;
use stdClass;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -107,16 +108,23 @@ protected function execute(InputInterface $input, OutputInterface $output): void
: $this->getConfig($input, $output, true)
;

$path = make_path_absolute($input->getArgument(self::FILE_ARGUMENT), getcwd());
$filePath = $input->getArgument(self::FILE_ARGUMENT);

$path = make_path_relative($filePath, $config->getBasePath());

$compactors = $this->retrieveCompactors($config);

$fileContents = file_contents($path);
$fileContents = file_contents(
$absoluteFilePath = make_path_absolute(
$filePath,
getcwd()
)
);

$io->writeln([
sprintf(
'⚡ Processing the contents of the file <info>%s</info>',
$path
$absoluteFilePath
),
'',
]);
Expand All @@ -126,13 +134,17 @@ protected function execute(InputInterface $input, OutputInterface $output): void

$fileProcessedContents = $compactors->compactContents($path, $fileContents);

$io->writeln([
'Processed contents:',
'',
'<comment>"""</comment>',
$fileProcessedContents,
'<comment>"""</comment>',
]);
if ($io->isQuiet()) {
$io->writeln($fileProcessedContents, OutputInterface::VERBOSITY_QUIET);
} else {
$io->writeln([
'Processed contents:',
'',
'<comment>"""</comment>',
$fileProcessedContents,
'<comment>"""</comment>',
]);
}
}

private function retrieveCompactors(Configuration $config): Compactors
Expand Down
147 changes: 145 additions & 2 deletions tests/Console/Command/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\touch;
use function preg_replace;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @covers \KevinGH\Box\Console\Command\Process
Expand All @@ -34,7 +35,7 @@ protected function getCommand(): Command
return new Process();
}

public function test_it_process_a_file_and_displays_the_processed_contents_with_no_config(): void
public function test_it_processes_a_file_and_displays_the_processed_contents_with_no_config(): void
{
dump_file('index.php', '');

Expand Down Expand Up @@ -71,7 +72,7 @@ public function test_it_process_a_file_and_displays_the_processed_contents_with_
$this->assertSame(0, $this->commandTester->getStatusCode());
}

public function test_it_process_a_file_and_displays_the_processed_contents_with_a_config(): void
public function test_it_processes_a_file_and_displays_the_processed_contents_with_a_config(): void
{
touch('index.php');
dump_file(
Expand Down Expand Up @@ -131,6 +132,148 @@ public function test_it_process_a_file_and_displays_the_processed_contents_with_
{"foo":"bar"}
"""
OUTPUT;

$this->assertSame($expected, $actual);
$this->assertSame(0, $this->commandTester->getStatusCode());
}

public function test_it_processes_the_file_relative_to_the_config_base_path(): void
{
dump_file(
'index.php',
<<<'PHP'
<?php
echo 'Hello world!';
PHP
);

dump_file(
'box.json',
<<<'JSON'
{
"replacements": {
"foo": "bar"
},
"compactors": [
"KevinGH\\Box\\Compactor\\PhpScoper"
]
}
JSON
);
dump_file(
'scoper.inc.php',
<<<'PHP'
<?php
return [
'prefix' => '_Prefix',
'patchers' => [
function (string $filePath, string $prefix, string $contents): string {
if ('index.php' !== $filePath) {
return $contents;
}
return str_replace('Hello world!', '!dlrow olleH', $contents);
},
],
];

PHP
);

$this->commandTester->execute(
[
'command' => 'process',
'file' => $this->tmp.'/index.php',
]
);
$actual = DisplayNormalizer::removeTrailingSpaces($this->commandTester->getDisplay(true));

$actual = preg_replace(
'/\s\/\/ Loading the configuration file([\s\S]*)box\.json[comment\<\>\n\s\/]*"\./',
' // Loading the configuration file "box.json".',
$actual
);

$expectedPath = $this->tmp.'/index.php';

$expected = <<<OUTPUT
// Loading the configuration file "box.json".
⚡ Processing the contents of the file $expectedPath
Registered replacement values:
+ @foo@: bar
Registered compactors:
+ KevinGH\Box\Compactor\PhpScoper
Processed contents:
"""
<?php
namespace _Prefix;
echo '!dlrow olleH';
"""
OUTPUT;

$this->assertSame($expected, $actual);
$this->assertSame(0, $this->commandTester->getStatusCode());
}



public function test_it_processes_a_file_and_displays_only_the_processed_contents_in_quiet_mode(): void
{
touch('index.php');
dump_file(
'acme.json',
<<<'JSON'
{
"foo": "@foo@"
}
JSON
);
dump_file(
'box.json',
<<<'JSON'
{
"replacements": {
"foo": "bar"
},
"compactors": [
"KevinGH\\Box\\Compactor\\Json"
]
}
JSON
);

$this->commandTester->execute(
[
'command' => 'process',
'file' => 'acme.json',
],
['verbosity' => OutputInterface::VERBOSITY_QUIET]
);
$actual = DisplayNormalizer::removeTrailingSpaces($this->commandTester->getDisplay(true));

$actual = preg_replace(
'/\s\/\/ Loading the configuration file([\s\S]*)box\.json[comment\<\>\n\s\/]*"\./',
' // Loading the configuration file "box.json".',
$actual
);

$expected = <<<'OUTPUT'
{"foo":"bar"}

OUTPUT;

$this->assertSame($expected, $actual);
Expand Down

0 comments on commit 7b908b9

Please sign in to comment.