Skip to content

Commit

Permalink
Merge pull request #17 from uselagoon/chore/refactortrait
Browse files Browse the repository at this point in the history
Changing up trait to get rid of static references
  • Loading branch information
bomoko authored Jul 3, 2024
2 parents 36ae161 + 799b89c commit 3266dd7
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 48 deletions.
11 changes: 7 additions & 4 deletions RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function run(
) {

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

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


\Migrator\LagoonUtilityBelt::setUpLagoon_yml();
$climate = new CLImate;
Expand Down Expand Up @@ -56,7 +59,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 +71,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 +83,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
2 changes: 1 addition & 1 deletion src/Step/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function runImplementation(array $args)
);
}
if($registerBuildIdAs !== false) {
self::setVariable($registerBuildIdAs, $buildId);
$this->dynamicEnvironment->setVariable($registerBuildIdAs, $buildId);
}
}

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
9 changes: 5 additions & 4 deletions src/Step/StepParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Migrator\Step;

use _PHPStan_7dd5f1b1b\Nette\Neon\Exception;
use Migrator\LagoonUtilityBelt;
use Migrator\LagoonUtilityBeltInterface;
use Migrator\LoggerTrait;
Expand All @@ -12,21 +11,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 +67,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
2 changes: 1 addition & 1 deletion src/Step/Waitfordeploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function runImplementation(array $args)
"passIfTextExistsInLogs",
$args
) ? $args["passIfTextExistsInLogs"] : null;
$buildId = !empty($args['buildId']) ? self::getVariable($args['buildId']) : null;
$buildId = !empty($args['buildId']) ? $this->dynamicEnvironment->getVariable($args['buildId']) : null;
if (empty($buildId)) {
throw new \Exception(
"Cannot run step waitfordeploy without setting a 'buildId'"
Expand Down
10 changes: 6 additions & 4 deletions tests/DeployTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testBasicRun() {
$lub = $this->createStub(LagoonUtilityBelt::class);
$lub->method("deployEnvironment")->willReturn("testBuildId");
$runnerArgs = new RunnerArgs();
$deploy = new Deploy($lub, $runnerArgs);
$deploy = new Deploy($lub, $runnerArgs, new DynamicEnvironment(), new DynamicEnvironment());
$this->expectNotToPerformAssertions();
$deploy->run([]);
}
Expand All @@ -37,7 +37,7 @@ public function testNoSubstitutionsInArgs() {
$runnerArgs->project = $args['project'];
$runnerArgs->environment = $args['environment'];

$deploy = new Deploy($lub, $runnerArgs);
$deploy = new Deploy($lub, $runnerArgs, new DynamicEnvironment());

$deploy->run($args);

Expand Down Expand Up @@ -68,10 +68,12 @@ public function testStandardTextualSubstitutionsInArgs() {
$runnerArgs->project = $args['project'];
$runnerArgs->environment = $args['environment'];

$deploy = new Deploy($lub, $runnerArgs);
$dynamicEnv = new DynamicEnvironment();

$deploy = new Deploy($lub, $runnerArgs, $dynamicEnv);

// Let's set up the basic text subs we want to do
Deploy::setVariable('LAGOON_BACKUPS_DISABLED', "false");
$dynamicEnv->setVariable('LAGOON_BACKUPS_DISABLED', "false");

$deploy->run($args);

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"));
}
}
Loading

0 comments on commit 3266dd7

Please sign in to comment.