Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
Added testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Branca committed Jun 29, 2016
1 parent 347f260 commit ffcddf7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/StackFormation/ValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ function ($matches) use ($sourceBlueprint, $exceptionMsgAppendix) {
if (is_array($value)) {
$value = $this->resolveConditionalValue($value, $sourceBlueprint);
}
if ($value == $matches[0]) {
throw new \Exception('Direct circular reference detected');
}
return $value;
},
$string
Expand Down
64 changes: 59 additions & 5 deletions tests/ConditionalValueTest.php → tests/ValueResolverTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class ConditionalValueTest extends PHPUnit_Framework_TestCase
class ValueResolverTest extends PHPUnit_Framework_TestCase
{

/**
Expand All @@ -15,7 +15,16 @@ public function setUp() {

public function getMockedPlaceholderResolver() {
$config = $this->getMock('\StackFormation\Config', [], [], '', false);
$config->method('getGlobalVars')->willReturn(['GlobalFoo' => 'GlobalBar']);
$config->method('getGlobalVars')->willReturn([
'GlobalFoo' => 'GlobalBar',
'GlobalFoo2' => 'GlobalBar2',
'GlobalBar' => 'GlobalFoo3',
'rescursiveA' => '{var:rescursiveB}',
'rescursiveB' => 'Hello',
'circularA' => '{var:circularB}',
'circularB' => '{var:circularA}',
'directCircular' => '{var:directCircular}',
]);

$stackFactoryMock = $this->getMock('\StackFormation\StackFactory', [], [], '', false);
$stackFactoryMock->method('getStackOutput')->willReturn('dummyOutput');
Expand All @@ -25,7 +34,7 @@ public function getMockedPlaceholderResolver() {
$profileManagerMock = $this->getMock('\StackFormation\Profile\Manager', [], [], '', false);

$placeholderResolver = new \StackFormation\ValueResolver(
new \StackFormation\DependencyTracker(),
null,
$profileManagerMock,
$config
);
Expand Down Expand Up @@ -156,7 +165,52 @@ public function resolveDataProvider() {
public function missingEnv()
{
$this->setExpectedException('Exception', "Environment variable 'DDD' not found");
$actualValue = $this->valueResolver->resolveConditionalValue(['{env:DDD}' => 13]);
$this->valueResolver->resolveConditionalValue(['{env:DDD}' => 13]);
}

}
/**
* @test
*/
public function missingVar()
{
$this->setExpectedException('Exception', "Variable 'DDD' not found (Type:conditional_value, Key:{var:DDD})");
$this->valueResolver->resolveConditionalValue(['{var:DDD}' => 13]);
}

/**
* @test
*/
public function nestedVars()
{
$result = $this->valueResolver->resolvePlaceholders('{var:{var:GlobalFoo}}');
$this->assertEquals('GlobalFoo3', $result);
}

/**
* @test
*/
public function recursiveReferences()
{
$result = $this->valueResolver->resolvePlaceholders('{var:rescursiveA}');
$this->assertEquals('Hello', $result);
}

/**
* @test
*/
public function directCircularReferences()
{
$this->setExpectedException('Exception', 'Direct circular reference detected');
$result = $this->valueResolver->resolvePlaceholders('{var:directCircular}');
}

/**
* @test
*/
public function circularReferences()
{
$this->setExpectedException('Exception', 'Max nesting level reached. Looks like a circular dependency.');
$this->valueResolver->resolvePlaceholders('{var:circularA}');
}

}

0 comments on commit ffcddf7

Please sign in to comment.