These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Serializers\Tests\Phpunit\Serializers; |
||
| 4 | |||
| 5 | use Serializers\DispatchingSerializer; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @covers Serializers\DispatchingSerializer |
||
| 9 | * |
||
| 10 | * @group Serialization |
||
| 11 | * |
||
| 12 | * @license GPL-2.0+ |
||
| 13 | * @author Jeroen De Dauw < [email protected] > |
||
| 14 | */ |
||
| 15 | class DispatchingSerializerTest extends \PHPUnit_Framework_TestCase { |
||
| 16 | |||
| 17 | public function testConstructWithNoSerializers() { |
||
| 18 | $serializer = new DispatchingSerializer( [] ); |
||
| 19 | |||
| 20 | $this->assertFalse( $serializer->isSerializerFor( 'foo' ) ); |
||
| 21 | $this->assertFalse( $serializer->isSerializerFor( null ) ); |
||
| 22 | |||
| 23 | $this->setExpectedException( 'Serializers\Exceptions\UnsupportedObjectException' ); |
||
| 24 | |||
| 25 | $serializer->serialize( 'foo' ); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testConstructWithInvalidArgumentsCausesException() { |
||
| 29 | $this->setExpectedException( 'InvalidArgumentException' ); |
||
| 30 | new DispatchingSerializer( [ new \stdClass() ] ); |
||
|
0 ignored issues
–
show
|
|||
| 31 | } |
||
| 32 | |||
| 33 | public function testCanSerialize() { |
||
| 34 | $subSerializer = $this->getMock( 'Serializers\DispatchableSerializer' ); |
||
| 35 | |||
| 36 | $subSerializer->expects( $this->exactly( 4 ) ) |
||
| 37 | ->method( 'isSerializerFor' ) |
||
| 38 | ->will( $this->returnCallback( function( $value ) { |
||
| 39 | return $value > 9000; |
||
| 40 | } ) ); |
||
| 41 | |||
| 42 | $serializer = new DispatchingSerializer( [ $subSerializer ] ); |
||
| 43 | |||
| 44 | $this->assertFalse( $serializer->isSerializerFor( 0 ) ); |
||
| 45 | $this->assertFalse( $serializer->isSerializerFor( 42 ) ); |
||
| 46 | $this->assertTrue( $serializer->isSerializerFor( 9001 ) ); |
||
| 47 | $this->assertTrue( $serializer->isSerializerFor( 31337 ) ); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testSerializeWithSerializableValues() { |
||
| 51 | $subSerializer = $this->getMock( 'Serializers\DispatchableSerializer' ); |
||
| 52 | |||
| 53 | $subSerializer->expects( $this->any() ) |
||
| 54 | ->method( 'isSerializerFor' ) |
||
| 55 | ->will( $this->returnValue( true ) ); |
||
| 56 | |||
| 57 | $subSerializer->expects( $this->any() ) |
||
| 58 | ->method( 'serialize' ) |
||
| 59 | ->will( $this->returnValue( 42 ) ); |
||
| 60 | |||
| 61 | $serializer = new DispatchingSerializer( [ $subSerializer ] ); |
||
| 62 | |||
| 63 | $this->assertEquals( 42, $serializer->serialize( 'foo' ) ); |
||
| 64 | $this->assertEquals( 42, $serializer->serialize( null ) ); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function testSerializeWithUnserializableValue() { |
||
| 68 | $subSerializer = $this->getMock( 'Serializers\DispatchableSerializer' ); |
||
| 69 | |||
| 70 | $subSerializer->expects( $this->once() ) |
||
| 71 | ->method( 'isSerializerFor' ) |
||
| 72 | ->will( $this->returnValue( false ) ); |
||
| 73 | |||
| 74 | $serializer = new DispatchingSerializer( [ $subSerializer ] ); |
||
| 75 | |||
| 76 | $this->setExpectedException( 'Serializers\Exceptions\UnsupportedObjectException' ); |
||
| 77 | $serializer->serialize( 0 ); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testSerializeWithMultipleSubSerializers() { |
||
| 81 | $subSerializer0 = $this->getMock( 'Serializers\DispatchableSerializer' ); |
||
| 82 | |||
| 83 | $subSerializer0->expects( $this->any() ) |
||
| 84 | ->method( 'isSerializerFor' ) |
||
| 85 | ->will( $this->returnValue( true ) ); |
||
| 86 | |||
| 87 | $subSerializer0->expects( $this->any() ) |
||
| 88 | ->method( 'serialize' ) |
||
| 89 | ->will( $this->returnValue( 42 ) ); |
||
| 90 | |||
| 91 | $subSerializer1 = $this->getMock( 'Serializers\DispatchableSerializer' ); |
||
| 92 | |||
| 93 | $subSerializer1->expects( $this->any() ) |
||
| 94 | ->method( 'isSerializerFor' ) |
||
| 95 | ->will( $this->returnValue( false ) ); |
||
| 96 | |||
| 97 | $subSerializer2 = clone $subSerializer1; |
||
| 98 | |||
| 99 | $serializer = new DispatchingSerializer( [ $subSerializer1, $subSerializer0, $subSerializer2 ] ); |
||
| 100 | |||
| 101 | $this->assertEquals( 42, $serializer->serialize( 'foo' ) ); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function testAddSerializer() { |
||
| 105 | $serializer = new DispatchingSerializer( [] ); |
||
| 106 | |||
| 107 | $subSerializer = $this->getMock( 'Serializers\DispatchableSerializer' ); |
||
| 108 | |||
| 109 | $subSerializer->expects( $this->any() ) |
||
| 110 | ->method( 'isSerializerFor' ) |
||
| 111 | ->will( $this->returnValue( true ) ); |
||
| 112 | |||
| 113 | $subSerializer->expects( $this->any() ) |
||
| 114 | ->method( 'serialize' ) |
||
| 115 | ->will( $this->returnValue( 42 ) ); |
||
| 116 | |||
| 117 | $serializer->addSerializer( $subSerializer ); |
||
| 118 | |||
| 119 | $this->assertEquals( |
||
| 120 | 42, |
||
| 121 | $serializer->serialize( null ) |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | } |
||
| 126 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: