Skip to content

Commit

Permalink
WithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jul 19, 2019
1 parent 1938281 commit fc0353f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/ContextAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ protected function _bindContext(ContextAware $to)
return $this;
}

/**
* Bind context to a context aware object, and return it
*
* @param ContextAware $to
*
* @return ContextAware
*/
protected function _withContext(ContextAware $to)
{
$to->setContext($this->getContext());
return $to;
}

/**
* @return Context
*/
Expand Down
9 changes: 8 additions & 1 deletion tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ public function testContextAware()

$obj2->setContext($ctx);
$obj2->bind($obj);
$this->assertTrue($obj->hasContext());
$this->assertTrue($obj2->hasContext());
$this->assertSame($ctx, $obj->getContext());

$obj3 = new TestContextAwareObject();
$obj4 = $obj2->with($obj3);
$this->assertTrue($obj3->hasContext());
$this->assertSame($ctx, $obj3->getContext());
$this->assertSame($obj3, $obj4);
$this->assertSame($ctx, $obj4->getContext());
}

public function testConfig()
Expand Down
5 changes: 5 additions & 0 deletions tests/Supporting/TestContextAwareObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public function bind(ContextAware $to)
{
return $this->_bindContext($to);
}

public function with(ContextAware $to)
{
return $this->_withContext($to);
}
}

0 comments on commit fc0353f

Please sign in to comment.