Skip to content
This repository has been archived by the owner on Aug 24, 2020. It is now read-only.

Commit

Permalink
Make sure that AnyValueSerializer only serializes AnyValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
JanZerebecki committed Sep 8, 2014
1 parent 3b923b9 commit dc3eec8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Serializers/Description/AnyValueSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Ask\Wikitext\Serializers\Description;

use Serializers\Serializer;
use Ask\Language\Description\AnyValue;
use InvalidArgumentException;

/**
* @licence GNU GPL v2+
Expand All @@ -15,7 +17,11 @@ class AnyValueSerializer implements Serializer {
* @see Serializer::serialize
*/
public function serialize( $object ) {
return '+';
if ( $object instanceof AnyValue ) {
return '+';
}

throw new InvalidArgumentException( 'Can only serialize instances of AnyValue' );
}

}
}
22 changes: 18 additions & 4 deletions tests/unit/Serializers/Description/AnyValueSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Ask\Language\Description\AnyValue;
use Ask\Wikitext\Serializers\Description\AnyValueSerializer;
use Ask\Language\Description\ValueDescription;
use DataValues\StringValue;
use Serializers\Serializer;

/**
* @covers Ask\Wikitext\Serializers\Description\AnyValueSerializer
Expand All @@ -14,12 +17,23 @@
*/
class AnyValueSerializerTest extends \PHPUnit_Framework_TestCase {

public function testFormatAnyValue() {
$anyValue = new AnyValue();
/**
* @var Serializer
*/
private $serializer;

$formatter = new AnyValueSerializer();
public function setUp() {
$this->serializer = new AnyValueSerializer();
}

$this->assertEquals( '+', $formatter->serialize( $anyValue ) );
public function testGivenNotAnyValue_serializeThrowsException() {
$this->setExpectedException( 'InvalidArgumentException' );
$this->serializer->serialize( new ValueDescription( new StringValue( 'foo' ) ) );
}

public function testFormatAnyValue() {
$anyValue = new AnyValue();
$this->assertEquals( '+', $this->serializer->serialize( $anyValue ) );
}

}

0 comments on commit dc3eec8

Please sign in to comment.