Skip to content

Commit

Permalink
Merge pull request #24 from open-sausages/pulls/1.0/this-is-not-what-…
Browse files Browse the repository at this point in the history
…I-ordered

BUG Fix parameter order
  • Loading branch information
Damian Mooyman authored Nov 21, 2017
2 parents 266b830 + 14b2641 commit 2b02905
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Transformer/YamlTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ function ($document) use ($flag) {
// and check their filename and maybe their document name, depending on the pattern.
// We don't want to do any pattern matching after the first hash as the document name
// is assumed to follow it.
$firstHash = strpos('#', $pattern);
$firstHash = strpos($pattern, '#');
$documentName = false;
if ($firstHash !== false) {
$documentName = substr($pattern, $firstHash + 1);
$pattern = substr($pattern, 0, $firstHash);
}

// Replace all `*` with `[^\.][a-zA-Z0-9\-_\/\.]+`, and quote other characters
$patternRegExp = '%^'.implode(
$patternRegExp = '%(^|[/\\\\])'.implode(
'[^\.][a-zA-Z0-9\-_\/\.]+',
array_map(
function ($part) {
Expand All @@ -445,6 +445,7 @@ function ($part) {
explode('*', $pattern)
)
).'%';

$matchedDocuments = [];
foreach ($documents as $document) {
// Ensure filename is relative
Expand Down
54 changes: 53 additions & 1 deletion tests/Transformer/YamlTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function testBeforeAfterStatementWithPath()
$content = <<<'YAML'
---
name: test2
before: 'test/*'
before: 'test/*#test'
---
test: 'should not overwrite'
YAML;
Expand All @@ -289,6 +289,7 @@ public function testBeforeAfterStatementWithPath()

$this->assertEquals('test', $collection->get('test'));

// this one is kind of moot because if the matching fails, it'll go after anyway...
$content = <<<'YAML'
---
name: test3
Expand All @@ -308,6 +309,57 @@ public function testBeforeAfterStatementWithPath()
$this->assertEquals('overwrite', $collection->get('test'));
}

public function testBeforeAfterStatementWithNestedPath()
{
$content = <<<'YAML'
---
name: test
---
test: 'test'
YAML;
mkdir($this->getConfigDirectory().'/test');
mkdir($this->getConfigDirectory().'/test/test1-1');
file_put_contents($this->getFilePath('test/test1-1/config.yml'), $content);

$content = <<<'YAML'
---
name: test2
before: 'test1-1/*#test'
---
test: 'should not overwrite'
YAML;
mkdir($this->getConfigDirectory().'/test2');
file_put_contents($this->getFilePath('test2/config.yml'), $content);

$collection = new MemoryConfigCollection;
$transformer = new YamlTransformer(
$this->getConfigDirectory(),
$this->getFinder()
);
$collection->transform([$transformer]);

$this->assertEquals('test', $collection->get('test'));

$content = <<<'YAML'
---
name: test3
after: 'test1-1/*#test'
---
test: 'overwrite'
YAML;
file_put_contents($this->getFilePath('test2/config.yml'), $content);

$collection = new MemoryConfigCollection;
$transformer = new YamlTransformer(
$this->getConfigDirectory(),
$this->getFinder()
);
$collection->transform([$transformer]);

$this->assertEquals('overwrite', $collection->get('test'));

}

/**
* Tests that an exception is correctly thrown when a circular dependency is present.
* This means when two YAML documents are stated as both becoming before (or after)
Expand Down

0 comments on commit 2b02905

Please sign in to comment.