Skip to content

Commit

Permalink
code & test
Browse files Browse the repository at this point in the history
  • Loading branch information
devajmeireles committed Jan 4, 2024
1 parent e64f6fb commit e972ac0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function compileInclude($value)
protected function compileServers($value)
{
$value = preg_replace_callback('/@servers\(\[(.*?)\]\)/s', function ($matches) {
return '@servers(['.preg_replace('/\s+/', '', $matches[1]).'])';
return '@servers(['.str(preg_replace('/\s+/', ' ', $matches[1]))->squish()->trim(' ')->value().'])';
}, $value);

$pattern = $this->createMatcher('servers');
Expand Down
41 changes: 37 additions & 4 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test_it_compiles_server_statement()
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>'bar']); ?>");
$this->assertSame($result, "<?php \$__container->servers(['foo' => 'bar']); ?>");

$str = <<<'EOL'
@servers([
Expand All @@ -57,7 +57,7 @@ public function test_it_compiles_server_statement()
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>['bar','baz','bah']]); ?>");
$this->assertSame($result, "<?php \$__container->servers(['foo' => [ 'bar', 'baz', 'bah' ]]); ?>");

$str = <<<'EOL'
@servers([
Expand All @@ -68,14 +68,47 @@ public function test_it_compiles_server_statement()
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>['bar'],'bar'=>['baz']]); ?>");
$this->assertSame($result, "<?php \$__container->servers(['foo' => ['bar'], 'bar' => ['baz']]); ?>");

$str = <<<'EOL'
@servers(['foo' => 'bar'])
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>'bar']); ?>");
$this->assertSame($result, "<?php \$__container->servers(['foo' => 'bar']); ?>");
}

public function test_it_compiles_server_statement_with_setup_raw_format()
{
$str = <<<'EOL'
@setup
$user = 'foo';
$server = 'bar';
$port = 22;
@endsetup
@servers([
'foo' => "{$user}@{$server} -p {$port}"
])
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$expected = <<<EOL
<?php \$port = isset(\$port) ? \$port : null; ?>
<?php \$server = isset(\$server) ? \$server : null; ?>
<?php \$user = isset(\$user) ? \$user : null; ?>
<?php
\$user = 'foo';
\$server = 'bar';
\$port = 22;
?>
<?php \$__container->servers(['foo' => "{\$user}@{\$server} -p {\$port}"]); ?>
EOL;


$this->assertSame($result, $expected);
}
}

0 comments on commit e972ac0

Please sign in to comment.