Skip to content

Commit

Permalink
Add Extends (#4)
Browse files Browse the repository at this point in the history
* Add Extends

* Hydrate using protected and privates
  • Loading branch information
MrEssex authored Apr 13, 2022
1 parent aa92c0d commit fa94c2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public static function create(string $projectRoot = null, string $environment =
return $ctx;
}

public static function extends(Context $ctx)
{
$c = new static();
$c->_projectRoot = $ctx->_projectRoot;
$c->_env = $ctx->_env;
$c->_cfg = $ctx->_cfg;
$c->_meta = $ctx->_meta;
$c->_routeData = $ctx->_routeData;

$c->_id = $ctx->_id;
$c->_events = $ctx->_events;
$c->_request = $ctx->_request;
$c->_cookieJar = $ctx->_cookieJar;
return $c;
}

protected function _generateId()
{
return uniqid('ctx-', true);
Expand Down
30 changes: 30 additions & 0 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,34 @@ public function testCannotSetContext()
$this->expectExceptionMessage("You cannot set context on context");
$ctx->setContext($ctx2);
}

public function testExtendingContext()
{
$ctx = new Context();
$ctx->setEnvironment(Context::ENV_PROD);
$ctx->setProjectRoot('/abc');
$ctx2 = Context::extends($ctx);

$this->assertEquals($ctx->getEnvironment(), $ctx2->getEnvironment());
$this->assertEquals($ctx->getProjectRoot(), $ctx2->getProjectRoot());
$this->assertEquals($ctx->id(), $ctx2->id());

$ctx = Context::create('/abc', Context::ENV_QA);
$ctx->meta()->set('abc', 'def');
$ctx2 = Context::extends($ctx);

$this->assertEquals($ctx->getProjectRoot(), $ctx2->getProjectRoot());
$this->assertEquals('def', $ctx2->meta()->get('abc'));

$cnf = new ConfigProvider();
$ctx->setConfig($cnf);
$ctx->routeData()->set('123', '456');

$ctx2 = Context::extends($ctx);

$this->assertSame($cnf, $ctx2->getConfig());
$this->assertEquals('def', $ctx2->meta()->get('abc'));
$this->assertEquals('456', $ctx2->routeData()->get('123'));

}
}

0 comments on commit fa94c2d

Please sign in to comment.