Skip to content

Commit

Permalink
PHP Magic & TDD Workshop #2
Browse files Browse the repository at this point in the history
  • Loading branch information
storyn26383 committed Apr 29, 2017
1 parent bbc49fb commit 030fb3d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace App;

use Exception;
use ArrayAccess;
use ArrayIterator;
use JsonSerializable;
use IteratorAggregate;
use BadMethodCallException;

class Magic
class Magic implements ArrayAccess, IteratorAggregate, JsonSerializable
{
public $cloner = false;

protected $attributes;

public function __construct($attributes = [])
Expand Down Expand Up @@ -58,4 +65,46 @@ public static function __callStatic($method, $args)
{
throw new BadMethodCallException('Hello Magic!');
}

public function __sleep()
{
$className = static::class;

throw new Exception("Serialization of '{$className}' is not allowed");
}

public function __clone()
{
$this->cloner = true;
}

public function offsetGet($key)
{
return $this->__get($key);
}

public function offsetSet($key, $value)
{
$this->__set($key, $value);
}

public function offsetUnset($key)
{
$this->__unset($key);
}

public function offsetExists($key)
{
return $this->__isset($key);
}

public function getIterator()
{
return new ArrayIterator($this->getAttributes());
}

public function jsonSerialize()
{
return $this->getAttributes();
}
}
74 changes: 74 additions & 0 deletions tests/MagicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use App\Magic;
use Exception;
use BadMethodCallException;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -102,4 +103,77 @@ public function testCallStatic()

Magic::undefinedMethod();
}

// __sleep
// __wakeup
public function testSleep()
{
$magic = new Magic;

$this->expectException(Exception::class);
$this->expectExceptionMessage("Serialization of 'App\\Magic' is not allowed");

serialize($magic);
}

// __clone
public function testClone()
{
$magic = new Magic;
$cloner = clone $magic;

$this->assertTrue($cloner->cloner);
}

// ArrayAccess
public function testOffsetGet()
{
$magic = new Magic(['foo' => 'bar']);

$this->assertEquals('bar', $magic['foo']);
}

public function testOffsetSet()
{
$magic = new Magic;

$magic['foo'] = 'bar';

$this->assertEquals('bar', $magic['foo']);
}

public function testOffsetUnset()
{
$magic = new Magic(['foo' => 'bar']);

unset($magic['foo']);

$this->assertNull($magic['foo']);
}

public function testOffsetExists()
{
$magic = new Magic(['foo' => 'bar']);

$this->assertTrue(isset($magic['foo']));
$this->assertFalse(empty($magic['foo']));
}

// IteratorAggregate
public function testIterate()
{
$magic = new Magic($attrs = ['foo' => 'bar']);

foreach ($magic as $key => $value) {
$this->assertEquals($attrs[$key], $value);
}
}

// JsonSerializable
public function testJsonSerialize()
{
$magic = new Magic($attrs = ['foo' => 'bar']);

$this->assertEquals(json_encode($attrs), json_encode($magic));
}
}

0 comments on commit 030fb3d

Please sign in to comment.