Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
Added missing serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
italolelis committed Jun 28, 2016
1 parent ec54421 commit 6e7ed9d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* reference, of if a Map is used with foreach by reference, an exception will
* be thrown.
*/
class Map implements MapInterface, \ArrayAccess
class Map implements MapInterface, \ArrayAccess, \JsonSerializable, \Serializable
{
use MapLikeTrait, SortTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* is used when adding a new element to a Set (ex. "$c[] =& ..."), or if a Set
* is used with foreach by reference, an exception will be thrown.
*/
class Set implements SetInterface, \ArrayAccess
class Set implements SetInterface, \ArrayAccess, \JsonSerializable, \Serializable
{
use SetLikeTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* passed by reference, of if a Vector is used with foreach by reference, an
* exception will be thrown.
*/
class Vector implements VectorInterface, \ArrayAccess
class Vector implements VectorInterface, \ArrayAccess, \JsonSerializable, \Serializable
{
use VectorLikeTrait, SortTrait;

Expand Down
56 changes: 56 additions & 0 deletions tests/CollectionSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Collections;

use Collections\Map;
use Collections\Queue;
use Collections\Set;
use Collections\Stack;
use Collections\Vector;

class CollectionSerializerTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @dataProvider collectionProvider
* @param $coll
* @param $expectedSerialization
*/
public function is_should_json_serialize_it($coll, $expectedSerialization)
{
$serializedColl = json_encode($coll);
$this->assertJsonStringEqualsJsonString($serializedColl, $expectedSerialization);
}

/**
* @test
* @dataProvider collectionProvider
* @param $coll
*/
public function is_should_php_serialize_it($coll)
{
$serializedColl = serialize($coll);
$this->assertInternalType("string", $serializedColl);
$collection = unserialize($serializedColl);
$this->assertInstanceOf(get_class($coll), $collection);
}

public function collectionProvider()
{
$data = [1, 2, 3, 4];

$queue = new Queue();
$queue->push(1);

$stack = new Stack();
$stack->push(1);

return [
[new Vector($data), '[1,2,3,4]'],
[new Set($data), '[1,2,3,4]'],
[new Map(['test' => 'test']), '{"test": "test"}'],
[$queue, '[1]'],
[$stack, '[1]'],
];
}
}

0 comments on commit 6e7ed9d

Please sign in to comment.