Completed
Pull Request — dev (#11)
by
unknown
05:12 queued 01:17
created

JsonEncoderTest::testNonSerializableEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Vectorface\SnappyRouterTests\Encoder;
4
5
use Vectorface\SnappyRouter\Encoder\JsonEncoder;
6
use Vectorface\SnappyRouter\Response\Response;
7
8
/**
9
 * Tests the JsonEncoder class.
10
 * @copyright Copyright (c) 2014, VectorFace, Inc.
11
 * @author Dan Bruce <[email protected]>
12
 */
13
class JsonEncoderTest extends AbstractEncoderTest
14
{
15
    /**
16
     * Returns the encoder to be tested.
17
     * @return \Vectorface\SnappyRouter\Encoder\AbstractEncoder Returns an instance of an encoder.
18
     */
19
    public function getEncoder()
20
    {
21
        return new JsonEncoder();
22
    }
23
24
    /**
25
     * A data provider for the testEncode method.
26
     */
27
    public function encodeProvider()
28
    {
29
        $testObject = new \stdClass();
30
        $testObject->id = 1234;
31
        return array(
32
            array(
33
                '"test1234"',
34
                'test1234'
35
            ),
36
            array(
37
                '{"id":1234}',
38
                array('id' => 1234)
39
            ),
40
            array(
41
                '{"id":1234}',
42
                $testObject,
43
            ),
44
            array(
45
                'null',
46
                null
47
            ),
48
            array(
49
                '"testSerialize"',
50
                $this
51
            )
52
        );
53
    }
54
55
    /**
56
     * Tests that we get an exception if we attempt to encode something that
57
     * is not serializable as JSON.
58
     * @expectedException Vectorface\SnappyRouter\Exception\EncoderException
59
     * @expectedExceptionMessage Unable to encode response as JSON.
60
     */
61
    public function testNonSerializableEncode()
62
    {
63
        $encoder = new JsonEncoder();
64
        $resource = fopen(__FILE__, 'r'); // resources can't be serialized
65
        $encoder->encode(new Response($resource));
66
    }
67
68
    public function jsonSerialize()
69
    {
70
        return 'testSerialize';
71
    }
72
}
73