diff --git a/src/Context.php b/src/Context.php index 85b19d4..8fb2a9b 100644 --- a/src/Context.php +++ b/src/Context.php @@ -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); diff --git a/tests/ContextTest.php b/tests/ContextTest.php index 0d859ba..c1ac318 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -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')); + + } }