Skip to content

Commit

Permalink
Merge pull request #3373 from neoascetic/complex-params-replacing
Browse files Browse the repository at this point in the history
Dot notation for params replacement. Fixes #3339
  • Loading branch information
Naktibalda committed Jul 28, 2016
2 parents dda3ab2 + 7f88f6a commit 1f11b95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/Codeception/Util/Template.php
Expand Up @@ -63,10 +63,16 @@ public function produce()

foreach ($matches as $match) { // fill in placeholders
$placeholder = $match[1];
if (!isset($this->vars[$placeholder])) {
continue;
$value = $this->vars;
foreach (explode('.', $placeholder) as $segment) {
if (is_array($value) && array_key_exists($segment, $value)) {
$value = $value[$segment];
} else {
continue 2;
}
}
$result = str_replace($this->placehodlerStart . $placeholder . $this->placeholderEnd, $this->vars[$placeholder], $result);

$result = str_replace($this->placehodlerStart . $placeholder . $this->placeholderEnd, $value, $result);
}
return $result;
}
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/Codeception/Util/TemplateTest.php
Expand Up @@ -17,9 +17,16 @@ public function testTemplateCanHaveOtherPlaceholder()
$this->assertEquals('hello, davert', $template->produce());
}

public function testTemplateSupportsDotNotationForArrays()
{
$template = new Template("hello, {{user.data.name}}");
$template->place('user', ['data' => ['name' => 'davert']]);
$this->assertEquals('hello, davert', $template->produce());
}

public function testShouldSkipUnmatchedPlaceholder()
{
$template = new Template("hello, {{name}}");
$this->assertEquals('hello, {{name}}', $template->produce());
$template = new Template("hello, {{name}}");
$this->assertEquals('hello, {{name}}', $template->produce());
}
}
}

0 comments on commit 1f11b95

Please sign in to comment.