1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11class Framework_Constraint_JsonMatches_ErrorMessageProviderTest extends PHPUnit_Framework_TestCase
12{
13    /**
14     * @dataProvider translateTypeToPrefixDataprovider
15     */
16    public function testTranslateTypeToPrefix($expected, $type)
17    {
18        $this->assertEquals(
19            $expected,
20            PHPUnit_Framework_Constraint_JsonMatches_ErrorMessageProvider::translateTypeToPrefix($type)
21        );
22    }
23
24    /**
25     * @dataProvider determineJsonErrorDataprovider
26     */
27    public function testDetermineJsonError($expected, $error, $prefix)
28    {
29        $this->assertEquals(
30            $expected,
31            PHPUnit_Framework_Constraint_JsonMatches_ErrorMessageProvider::determineJsonError(
32                $error,
33                $prefix
34            )
35        );
36    }
37
38    public static function determineJsonErrorDataprovider()
39    {
40        return [
41            'JSON_ERROR_NONE'  => [
42                null, 'json_error_none', ''
43            ],
44            'JSON_ERROR_DEPTH' => [
45                'Maximum stack depth exceeded', JSON_ERROR_DEPTH, ''
46            ],
47            'prefixed JSON_ERROR_DEPTH' => [
48                'TUX: Maximum stack depth exceeded', JSON_ERROR_DEPTH, 'TUX: '
49            ],
50            'JSON_ERROR_STATE_MISMatch' => [
51                'Underflow or the modes mismatch', JSON_ERROR_STATE_MISMATCH, ''
52            ],
53            'JSON_ERROR_CTRL_CHAR' => [
54                'Unexpected control character found', JSON_ERROR_CTRL_CHAR, ''
55            ],
56            'JSON_ERROR_SYNTAX' => [
57                'Syntax error, malformed JSON', JSON_ERROR_SYNTAX, ''
58            ],
59            'JSON_ERROR_UTF8`' => [
60                'Malformed UTF-8 characters, possibly incorrectly encoded',
61                JSON_ERROR_UTF8,
62                ''
63            ],
64            'Invalid error indicator' => [
65                'Unknown error', 55, ''
66            ],
67        ];
68    }
69
70    public static function translateTypeToPrefixDataprovider()
71    {
72        return [
73            'expected' => ['Expected value JSON decode error - ', 'expected'],
74            'actual'   => ['Actual value JSON decode error - ', 'actual'],
75            'default'  => ['', ''],
76        ];
77    }
78}
79