-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from uselagoon/feature/conditional_steps
Feature/conditional steps
- Loading branch information
Showing
10 changed files
with
284 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Migrator\Step; | ||
|
||
use function WyriHaximus\Twig\render; | ||
|
||
trait DynamicEnvironmentTrait { | ||
|
||
static $dynamicEnvironment = []; | ||
|
||
public static function fillDynamicEnvironmentFromEnv() { | ||
$envVars = getenv(); | ||
foreach ($envVars as $key => $val) { | ||
if($key == "JSON_PAYLOAD") { | ||
self::fillDynamicEnvironmentFromJSONPayload($val); | ||
} else { | ||
self::setVariable(sprintf("%s", $key), $val); | ||
} | ||
|
||
} | ||
} | ||
|
||
public static function fillDynamicEnvironmentFromJSONPayload($payload) { | ||
|
||
$decodedJson = base64_decode($payload); | ||
if(!$decodedJson) { | ||
throw new \Exception("Found JSON_PAYLOAD but could not decode it"); | ||
} | ||
|
||
$vars = json_decode($decodedJson, true); | ||
|
||
if(json_last_error() > 0) { | ||
throw new \Exception(sprintf("Could not decode JSONPAYLOAD: %s ", json_last_error_msg())); | ||
} | ||
|
||
foreach ($vars as $key => $val) { | ||
self::setVariable($key, $val); | ||
} | ||
} | ||
|
||
public static function setVariable($name, $value) { | ||
self::$dynamicEnvironment[$name] = $value; | ||
} | ||
|
||
public static function getVariable($name) { | ||
if(!key_exists($name, self::$dynamicEnvironment)) { | ||
throw new \Exception("Unable to find variable {$name} in dynamic environment - have you previously set it?"); | ||
} | ||
return self::$dynamicEnvironment[$name]; | ||
} | ||
|
||
public static function getAllVariables() { | ||
return self::$dynamicEnvironment; | ||
} | ||
|
||
public static function renderText($template, $extraVars = []) | ||
{ | ||
$subs = array_merge(self::getAllVariables(), $extraVars); | ||
return render($template, $subs); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
use Migrator\Runner; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RunnerTest extends TestCase | ||
{ | ||
|
||
protected function getStepsFromFile($filename) | ||
{ | ||
return \Symfony\Component\Yaml\Yaml::parse( | ||
file_get_contents($filename) | ||
); | ||
} | ||
|
||
/** | ||
* This test will simply check that the runner works generally | ||
* | ||
* @return void | ||
*/ | ||
function testBasicCase() | ||
{ | ||
//just testing the test case callback | ||
$testRan = false; | ||
$cb = function ($args) use (&$testRan) { | ||
$testRan = true; | ||
}; | ||
\Migrator\Step\Test::setCallback($cb); | ||
|
||
$steps = $this->getStepsFromFile( | ||
__DIR__ . "/assets/RunnerTestBasicCase.yaml" | ||
); | ||
$args = new \Migrator\RunnerArgs(); | ||
$args->steps = $steps['steps']; | ||
$runner = new \Migrator\Runner($args); | ||
$runner->run(); | ||
$this->assertTrue($testRan); | ||
\Migrator\Step\Test::clearCallbacks(); | ||
} | ||
|
||
/** | ||
* This drives/tests the conditional system - basic conditional | ||
* Further tests will work on textual substitutions etc. | ||
* | ||
* @return void | ||
*/ | ||
function testConditionalRun() | ||
{ | ||
//just testing the test case callback | ||
$conditionalStepRan = false; | ||
$cb = function ($args) use (&$conditionalStepRan) { | ||
if(!empty($args['testid']) && $args['testid'] == 1) | ||
{ | ||
$conditionalStepRan = true; | ||
} | ||
}; | ||
\Migrator\Step\Test::setCallback($cb); | ||
|
||
$steps = $this->getStepsFromFile( | ||
__DIR__ . "/assets/RunnerTestConditionalRun.yaml" | ||
); | ||
$args = new \Migrator\RunnerArgs(); | ||
$args->steps = $steps['steps']; | ||
$runner = new \Migrator\Runner($args); | ||
$runner->run(); | ||
$this->assertTrue($conditionalStepRan); | ||
\Migrator\Step\Test::clearCallbacks(); | ||
} | ||
|
||
/** | ||
* This drives/tests the conditional system - basic conditional | ||
* Further tests will work on textual substitutions etc. | ||
* | ||
* @return void | ||
*/ | ||
function testConditionalDidntRun() | ||
{ | ||
//just testing the test case callback | ||
$conditionalStepRan = false; | ||
$cb = function ($args) use (&$conditionalStepRan) { | ||
if(!empty($args['testid']) && $args['testid'] == 1) | ||
{ | ||
$conditionalStepRan = true; | ||
} | ||
}; | ||
\Migrator\Step\Test::setCallback($cb); | ||
|
||
$steps = $this->getStepsFromFile( | ||
__DIR__ . "/assets/RunnerTestConditionalDidntRun.yaml" | ||
); | ||
$args = new \Migrator\RunnerArgs(); | ||
$args->steps = $steps['steps']; | ||
$runner = new \Migrator\Runner($args); | ||
$runner->run(); | ||
$this->assertFalse($conditionalStepRan); | ||
\Migrator\Step\Test::clearCallbacks(); | ||
} | ||
|
||
/** | ||
* This drives/tests the conditional system - basic conditional | ||
* Further tests will work on textual substitutions etc. | ||
* | ||
* @return void | ||
*/ | ||
function testConditionalWithTwigConditions() | ||
{ | ||
//just testing the test case callback | ||
$conditionalStepShouldHaveRun = false; | ||
\Migrator\Step\DynamicEnvironmentTrait::setVariable('RUN_THIS', "true"); | ||
\Migrator\Step\DynamicEnvironmentTrait::setVariable('DONT_RUN_THAT', "true"); | ||
$conditionalStepShouldNotHaveRun = true; | ||
$cb = function ($args) use (&$conditionalStepShouldHaveRun, &$conditionalStepShouldNotHaveRun) { | ||
if(!empty($args['testid']) && $args['testid'] == 1) | ||
{ | ||
$conditionalStepShouldHaveRun = true; | ||
} | ||
if(!empty($args['testid']) && $args['testid'] == 2) | ||
{ | ||
$conditionalStepShouldNotHaveRun = false; | ||
} | ||
}; | ||
\Migrator\Step\Test::setCallback($cb); | ||
|
||
$steps = $this->getStepsFromFile( | ||
__DIR__ . "/assets/RunnerTestConditionalTwig.yaml" | ||
); | ||
$args = new \Migrator\RunnerArgs(); | ||
$args->steps = $steps['steps']; | ||
$runner = new \Migrator\Runner($args); | ||
$runner->run(); | ||
$this->assertTrue($conditionalStepShouldHaveRun); | ||
$this->assertTrue($conditionalStepShouldNotHaveRun); | ||
|
||
\Migrator\Step\Test::clearCallbacks(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
steps: | ||
- name: basic run test | ||
type: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
steps: | ||
- name: basic run test | ||
type: test | ||
- name: conditional will always run | ||
type: conditional | ||
condition: false | ||
steps: | ||
- name: conditional first level | ||
type: test | ||
testid: 1 |
Oops, something went wrong.