Skip to content

Commit

Permalink
Add clone and unset capabilites to Maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
brutuscat committed Feb 27, 2014
1 parent 870ccf4 commit 63046ea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Codeception/Maybe.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 82,23 @@ function __call($method, $args)
return call_user_func_array(array($this->val, $method), $args);
}

function __clone()
{
if (is_object($this->val)) {
$this->val = clone $this->val;
}
}

public function __unset($key)
{
if (is_object($this->val)) {
if (isset($this->val->{$key}) || property_exists($this->val, $key)) {
unset($this->val->{$key});
return;
}
}
}

public function offsetExists($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
Expand Down
31 changes: 29 additions & 2 deletions tests/unit/Codeception/MaybeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 14,36 @@ class MaybeTest extends \Codeception\TestCase\Test
*/
protected $maybe;

public function testMaybe() {
public function testMaybe()
{
$this->maybe = new \Codeception\Maybe("Hello");
$this->assertEquals('Hello', $this->maybe);
}

}
public function testMaybeClone()
{
// Cloning with an object
$obj = new StdClass();
$maybe = new \Codeception\Maybe($obj);
$clone = clone $maybe;
$this->assertNotSame($obj, $clone->__value());

// Non object clone
$maybe = new \Codeception\Maybe("Hello");
$this->assertEquals('Hello', clone $maybe);

$maybe = new \Codeception\Maybe(3);
$clone = clone $maybe;
$this->assertEquals(3, $clone->__value());

$maybe = new \Codeception\Maybe(false);
$clone = clone $maybe;
$this->assertFalse($clone->__value());

// Null clone
$maybe = new \Codeception\Maybe(null);
$clone = clone $maybe;
$this->assertNull($clone->__value());

}
}

0 comments on commit 63046ea

Please sign in to comment.