Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / IsJson.php
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  * @since Class available since Release 3.7.20
15  */
16 class PHPUnit_Framework_Constraint_IsJson extends PHPUnit_Framework_Constraint
17 {
18     /**
19      * Evaluates the constraint for parameter $other. Returns true if the
20      * constraint is met, false otherwise.
21      *
22      * @param mixed $other Value or object to evaluate.
23      *
24      * @return bool
25      */
26     protected function matches($other)
27     {
28         json_decode($other);
29         if (json_last_error()) {
30             return false;
31         }
32
33         return true;
34     }
35
36     /**
37      * Returns the description of the failure
38      *
39      * The beginning of failure messages is "Failed asserting that" in most
40      * cases. This method should return the second part of that sentence.
41      *
42      * @param mixed $other Evaluated value or object.
43      *
44      * @return string
45      */
46     protected function failureDescription($other)
47     {
48         json_decode($other);
49         $error = PHPUnit_Framework_Constraint_JsonMatches_ErrorMessageProvider::determineJsonError(
50             json_last_error()
51         );
52
53         return sprintf(
54             '%s is valid JSON (%s)',
55             $this->exporter->shortenedExport($other),
56             $error
57         );
58     }
59
60     /**
61      * Returns a string representation of the constraint.
62      *
63      * @return string
64      */
65     public function toString()
66     {
67         return 'is valid JSON';
68     }
69 }