Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrinko committed Jun 19, 2016
1 parent 6c5e4be commit c41356e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/unit/SerializerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


namespace Retrinko\Serializer\Test\Unit;


use Retrinko\Serializer\SerializerFactory;
use Retrinko\Serializer\Serializers\JsonSerializer;
use Retrinko\Serializer\Serializers\NullSerializer;
use Retrinko\Serializer\Serializers\PhpSerializer;

class SerializerFactoryTest extends \PHPUnit_Framework_TestCase
{
public function supportedContentTypesProvider()
{
return [
[PhpSerializer::SERIALIZED_CONTENT_TYPE,
'Retrinko\Serializer\Serializers\PhpSerializer'],
[JsonSerializer::SERIALIZED_CONTENT_TYPE,
'Retrinko\Serializer\Serializers\JsonSerializer'],
[NullSerializer::SERIALIZED_CONTENT_TYPE,
'Retrinko\Serializer\Serializers\NullSerializer']
];
}

/**
* @dataProvider supportedContentTypesProvider
*
* @param string $contentType
* @param string $expectedSerializerClass
*
* @throws \Retrinko\Serializer\Exceptions\Exception
*/
public function test_bySerializedContentType_withSupportedContentType_returnsProperSerializer($contentType, $expectedSerializerClass)
{
$this->assertTrue(SerializerFactory::bySerializedContentType($contentType) instanceof $expectedSerializerClass);
}

/**
* @expectedException \Retrinko\Serializer\Exceptions\Exception
*/
public function test_bySerializedContentType_withUnsupportedContentType_thowsException()
{
SerializerFactory::bySerializedContentType('unsupported content type');
}
}
30 changes: 30 additions & 0 deletions tests/unit/Serializers/JsonSerizalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace Retrinko\Serializer\Test\Unit\Serializers;


use Retrinko\Serializer\Serializers\JsonSerializer;

class JsonSerizalizerTest extends \PHPUnit_Framework_TestCase
{
public function test_getSerializedContentType_returnsProperValue()
{
$expected = 'application/json';
$this->assertEquals($expected, (new JsonSerializer())->getSerializedContentType());
}

public function test_serialize_retrunsProperValue()
{
$test = ['a', 'b', 'c'];
$expected = json_encode($test);
$this->assertEquals($expected, (new JsonSerializer())->serialize($test));
}

public function test_unserialize_retrunsProperValue()
{
$test = '["a","b","c"]';
$expected = json_decode($test, true);
$this->assertEquals($expected, (new JsonSerializer())->unserialize($test));
}
}
30 changes: 30 additions & 0 deletions tests/unit/Serializers/NullSerizalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace Retrinko\Serializer\Test\Unit\Serializers;


use Retrinko\Serializer\Serializers\NullSerializer;

class NullSerizalizerTest extends \PHPUnit_Framework_TestCase
{
public function test_getSerializedContentType_returnsProperValue()
{
$expected = 'text/plain';
$this->assertEquals($expected, (new NullSerializer())->getSerializedContentType());
}

public function test_serialize_retrunsProperValue()
{
$test = ['a', 'b', 'c'];
$expected = $test;
$this->assertEquals($expected, (new NullSerializer())->serialize($test));
}

public function test_unserialize_retrunsProperValue()
{
$test = '["a","b","c"]';
$expected = $test;
$this->assertEquals($expected, (new NullSerializer())->unserialize($test));
}
}
30 changes: 30 additions & 0 deletions tests/unit/Serializers/PhpSerizalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace Retrinko\Serializer\Test\Unit\Serializers;


use Retrinko\Serializer\Serializers\PhpSerializer;

class PhpSerizalizerTest extends \PHPUnit_Framework_TestCase
{
public function test_getSerializedContentType_returnsProperValue()
{
$expected = 'application/php/serialize/base64_encode';
$this->assertEquals($expected, (new PhpSerializer())->getSerializedContentType());
}

public function test_serialize_retrunsProperValue()
{
$test = ['a', 'b', 'c'];
$expected = base64_encode(serialize($test));
$this->assertEquals($expected, (new PhpSerializer())->serialize($test));
}

public function test_unserialize_retrunsProperValue()
{
$expected = ['a', 'b', 'c'];
$test = base64_encode(serialize($expected));
$this->assertEquals($expected, (new PhpSerializer())->unserialize($test));
}
}

0 comments on commit c41356e

Please sign in to comment.