700abe385f3feb8d37c953ecfae7a60d2e17a862
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / JsonMatches.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  * Asserts whether or not two JSON objects are equal.
13  *
14  * @since Class available since Release 3.7.0
15  */
16 class PHPUnit_Framework_Constraint_JsonMatches extends PHPUnit_Framework_Constraint
17 {
18     /**
19      * @var string
20      */
21     protected $value;
22
23     /**
24      * Creates a new constraint.
25      *
26      * @param string $value
27      */
28     public function __construct($value)
29     {
30         parent::__construct();
31         $this->value = $value;
32     }
33
34     /**
35      * Evaluates the constraint for parameter $other. Returns true if the
36      * constraint is met, false otherwise.
37      *
38      * This method can be overridden to implement the evaluation algorithm.
39      *
40      * @param mixed $other Value or object to evaluate.
41      *
42      * @return bool
43      */
44     protected function matches($other)
45     {
46         $decodedOther = json_decode($other);
47         if (json_last_error()) {
48             return false;
49         }
50
51         $decodedValue = json_decode($this->value);
52         if (json_last_error()) {
53             return false;
54         }
55
56         return $decodedOther == $decodedValue;
57     }
58
59     /**
60      * Returns a string representation of the object.
61      *
62      * @return string
63      */
64     public function toString()
65     {
66         return sprintf(
67             'matches JSON string "%s"',
68             $this->value
69         );
70     }
71 }