~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Framework/Constraint/IsJson.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
11
/**
 
12
 * Constraint that asserts that a string is valid JSON.
 
13
 *
 
14
 * @package    PHPUnit
 
15
 * @subpackage Framework_Constraint
 
16
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
17
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
18
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
19
 * @link       http://www.phpunit.de/
 
20
 * @since      Class available since Release 3.7.20
 
21
 */
 
22
class PHPUnit_Framework_Constraint_IsJson extends PHPUnit_Framework_Constraint
 
23
{
 
24
    /**
 
25
     * Evaluates the constraint for parameter $other. Returns true if the
 
26
     * constraint is met, false otherwise.
 
27
     *
 
28
     * @param  mixed $other Value or object to evaluate.
 
29
     * @return bool
 
30
     */
 
31
    protected function matches($other)
 
32
    {
 
33
        json_decode($other);
 
34
        if (json_last_error()) {
 
35
            return false;
 
36
        }
 
37
 
 
38
        return true;
 
39
    }
 
40
 
 
41
    /**
 
42
     * Returns the description of the failure
 
43
     *
 
44
     * The beginning of failure messages is "Failed asserting that" in most
 
45
     * cases. This method should return the second part of that sentence.
 
46
     *
 
47
     * @param  mixed  $other Evaluated value or object.
 
48
     * @return string
 
49
     */
 
50
    protected function failureDescription($other)
 
51
    {
 
52
        json_decode($other);
 
53
        $error = PHPUnit_Framework_Constraint_JsonMatches_ErrorMessageProvider::determineJsonError(
 
54
            json_last_error()
 
55
        );
 
56
 
 
57
        return sprintf(
 
58
            '%s is valid JSON (%s)',
 
59
            $this->exporter->shortenedExport($other),
 
60
            $error
 
61
        );
 
62
    }
 
63
 
 
64
    /**
 
65
     * Returns a string representation of the constraint.
 
66
     *
 
67
     * @return string
 
68
     */
 
69
    public function toString()
 
70
    {
 
71
        return 'is valid JSON';
 
72
    }
 
73
}