Skip to content

Commit

Permalink
Changing up trait to get rid of static references
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaize Kaye committed Jul 2, 2024
1 parent 36ae161 commit 44e7e58
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 35 deletions.
12 changes: 8 additions & 4 deletions RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public function run(
) {

// Bootstrap the environment
\Migrator\Step\StepParent::fillDynamicEnvironmentFromEnv();
// \Migrator\Step\StepParent::fillDynamicEnvironmentFromEnv();

$dynamicEnv = new \Migrator\Step\DynamicEnvironment();
$dynamicEnv->fillDynamicEnvironmentFromEnv();


\Migrator\LagoonUtilityBelt::setUpLagoon_yml();
$climate = new CLImate;
Expand Down Expand Up @@ -56,7 +60,7 @@ public function run(
if(!empty($migration['prerequisites'])) {
$climate->out("Found prerequisite steps - will run with no rollback\n\n");
$args->steps = $migration['prerequisites'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, $dynamicEnv);
$runner->run();
} else {
$climate->out("No prerequisites found - proceeding to run main steps\n\n");
Expand All @@ -68,7 +72,7 @@ public function run(

try {
$args->steps = $migration['steps'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, $dynamicEnv);
$runner->run();
} catch (\Exception $ex) {

Expand All @@ -80,7 +84,7 @@ public function run(
if(!empty($migration['rollback'])) {
$climate->out("Attempting to run rollback steps\n\n");
$args->steps = $migration['rollback'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, $dynamicEnv);
$runner->run();
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="false"
Expand Down
13 changes: 9 additions & 4 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Migrator;

use Migrator\Step\DynamicEnvironment;
use Migrator\Step\DynamicEnvironmentTrait;
use Migrator\Step\StepParent;

Expand All @@ -19,13 +20,17 @@ class Runner

protected $args;

public function __construct(RunnerArgs $args
/** @var DynamicEnvironment */
protected $dynamicEnvironment;

public function __construct(RunnerArgs $args, DynamicEnvironment $dynamicEnvironment
) {
$this->steps = $args->steps;
$this->cluster = $args->cluster;
$this->namespace = $args->namespace;
$this->token = $args->token;
$this->args = $args;
$this->dynamicEnvironment = $dynamicEnvironment;
}

public function __get($name)
Expand All @@ -51,12 +56,12 @@ public function run()
throw new \Exception(sprintf("Failed on step '%s' - no condition attached", $step['name']));
}

$condition = DynamicEnvironmentTrait::renderText($step['condition']);
$condition = $this->dynamicEnvironment->renderText($step['condition']);

if($condition == true) {
$args = $this->args;
$args->steps = $step['steps'];
$runner = new Runner($args);
$runner = new Runner($args, $this->dynamicEnvironment);
$runner->run();
}
} else if (!empty($step['assertTrue']) || !empty($step['assertFalse'])) {
Expand Down Expand Up @@ -133,7 +138,7 @@ protected function runStep($step)
// To make the steps more testable, we now inject the utility belt
$lub = new LagoonUtilityBelt($this->args->cluster, $this->args->namespace, $this->args->sshKey);

$stepObj = new $classname($lub, $this->args);
$stepObj = new $classname($lub, $this->args, $this->dynamicEnvironment);

$retryTimes = !empty($step['retry']) ? $step['retry'] : 0;
$retrySleep = !empty($step['retryDelaySeconds']) ? $step['retryDelaySeconds'] : 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

use function WyriHaximus\Twig\render;

trait DynamicEnvironmentTrait {
class DynamicEnvironment {

static $dynamicEnvironment = [];
public $dynamicEnvironment = [];

public static function fillDynamicEnvironmentFromEnv() {
public function fillDynamicEnvironmentFromEnv() {
$envVars = getenv();
foreach ($envVars as $key => $val) {
if($key == "JSON_PAYLOAD") {
self::fillDynamicEnvironmentFromJSONPayload($val);
$this->fillDynamicEnvironmentFromJSONPayload($val);
} else {
self::setVariable(sprintf("%s", $key), $val);
$this->setVariable(sprintf("%s", $key), $val);
}

}
}

public static function fillDynamicEnvironmentFromJSONPayload($payload) {
public function fillDynamicEnvironmentFromJSONPayload($payload) {

$decodedJson = base64_decode($payload);
if(!$decodedJson) {
Expand All @@ -34,28 +34,28 @@ public static function fillDynamicEnvironmentFromJSONPayload($payload) {
}

foreach ($vars as $key => $val) {
self::setVariable($key, $val);
$this->setVariable($key, $val);
}
}

public static function setVariable($name, $value) {
self::$dynamicEnvironment[$name] = $value;
public function setVariable($name, $value) {
$this->dynamicEnvironment[$name] = $value;
}

public static function getVariable($name) {
if(!key_exists($name, self::$dynamicEnvironment)) {
public function getVariable($name) {
if(!key_exists($name, $this->dynamicEnvironment)) {
throw new \Exception("Unable to find variable {$name} in dynamic environment - have you previously set it?");
}
return self::$dynamicEnvironment[$name];
return $this->dynamicEnvironment[$name];
}

public static function getAllVariables() {
return self::$dynamicEnvironment;
public function getAllVariables() {
return $this->dynamicEnvironment;
}

public static function renderText($template, $extraVars = [])
public function renderText($template, $extraVars = [])
{
$subs = array_merge(self::getAllVariables(), $extraVars);
$subs = array_merge($this->getAllVariables(), $extraVars);
return render($template, $subs);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Step/StepInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
interface StepInterface
{

public function __construct(LagoonUtilityBeltInterface $utilityBelt, RunnerArgs $args);
public function __construct(LagoonUtilityBeltInterface $utilityBelt, RunnerArgs $args, DynamicEnvironment $dynamicEnvironment);

public function run(array $args);

Expand Down
8 changes: 5 additions & 3 deletions src/Step/StepParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@

abstract class StepParent implements StepInterface
{
use LoggerTrait, DynamicEnvironmentTrait;
use LoggerTrait;
protected $cluster;
protected $namespace;
protected $utilityBelt;
protected $token;
protected $args;
protected $commandName;
protected $dynamicEnvironment;

public function __construct(LagoonUtilityBeltInterface $utilityBelt, RunnerArgs $args)
public function __construct(LagoonUtilityBeltInterface $utilityBelt, RunnerArgs $args, DynamicEnvironment $environment)
{
$this->cluster = $args->cluster;
$this->namespace = $args->namespace;
$this->token = $args->token;
$this->args = $args;
$this->utilityBelt = $utilityBelt;
$this->dynamicEnvironment = $environment;
}

/**
Expand Down Expand Up @@ -66,7 +68,7 @@ public function doTextSubstitutions($string)
'namespace' => $this->args->namespace,
];

return self::renderText($string, $extraSubs);
return $this->dynamicEnvironment->renderText($string, $extraSubs);
}

// We use dynamic dispatch to allow us to do some logging and
Expand Down
13 changes: 7 additions & 6 deletions tests/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function testBasicCase()
);
$args = new \Migrator\RunnerArgs();
$args->steps = $steps['steps'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, new \Migrator\Step\DynamicEnvironment());
$runner->run();
$this->assertTrue($testRan);
\Migrator\Step\Test::clearCallbacks();
Expand Down Expand Up @@ -61,7 +61,7 @@ function testConditionalRun()
);
$args = new \Migrator\RunnerArgs();
$args->steps = $steps['steps'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, new \Migrator\Step\DynamicEnvironment());
$runner->run();
$this->assertTrue($conditionalStepRan);
\Migrator\Step\Test::clearCallbacks();
Expand Down Expand Up @@ -90,7 +90,7 @@ function testConditionalDidntRun()
);
$args = new \Migrator\RunnerArgs();
$args->steps = $steps['steps'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, new \Migrator\Step\DynamicEnvironment());
$runner->run();
$this->assertFalse($conditionalStepRan);
\Migrator\Step\Test::clearCallbacks();
Expand All @@ -106,8 +106,9 @@ 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");
$dynamicEnvironmnet = new \Migrator\Step\DynamicEnvironment();
$dynamicEnvironmnet->setVariable('RUN_THIS', "true");
$dynamicEnvironmnet->setVariable('DONT_RUN_THAT', "true");
$conditionalStepShouldNotHaveRun = true;
$cb = function ($args) use (&$conditionalStepShouldHaveRun, &$conditionalStepShouldNotHaveRun) {
if(!empty($args['testid']) && $args['testid'] == 1)
Expand All @@ -126,7 +127,7 @@ function testConditionalWithTwigConditions()
);
$args = new \Migrator\RunnerArgs();
$args->steps = $steps['steps'];
$runner = new \Migrator\Runner($args);
$runner = new \Migrator\Runner($args, $dynamicEnvironmnet);
$runner->run();
$this->assertTrue($conditionalStepShouldHaveRun);
$this->assertTrue($conditionalStepShouldNotHaveRun);
Expand Down
35 changes: 35 additions & 0 deletions tests/Step/DynamicEnvironmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Step;

use Migrator\Step\DynamicEnvironment;
use PHPUnit\Framework\TestCase;
class DynamicEnvironmentTest extends TestCase
{

/**
* @return DynamicEnvironmentTraitTestClass
*/
public function getAndSetupDynamicEnv() {
$class = new DynamicEnvironment();
return $class;
}

public function testGetVariable()
{
$testClass = $this->getAndSetupDynamicEnv();
$testClass->setVariable("testval", 555);
$this->assertEquals(555, $testClass->getVariable("testval"));
}

public function testPassingByValue()
{
$innerfunc = function(DynamicEnvironment $de) {
$de->setVariable("setbyinner", true);
};

$dynamicEnv = new DynamicEnvironment();
$innerfunc($dynamicEnv);
$this->assertTrue($dynamicEnv->getVariable("setbyinner"));
}
}

0 comments on commit 44e7e58

Please sign in to comment.