Skip to content

Loading…

Add clone and unset capabilites to Maybe #891

Merged
merged 1 commit into from

2 participants

@brutuscat

No description provided.

@DavertMik
Codeception Testing Framework member

Great. Thanks

@DavertMik DavertMik merged commit 448a84f into Codeception:1.8

1 check failed

Details default The Travis CI build failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 27, 2014
  1. @brutuscat
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 2 deletions.
  1. +17 −0 src/Codeception/Maybe.php
  2. +29 −2 tests/unit/Codeception/MaybeTest.php
View
17 src/Codeception/Maybe.php
@@ -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)) {
View
31 tests/unit/Codeception/MaybeTest.php
@@ -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());
+
+ }
+}
Something went wrong with that request. Please try again.