Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / StringMatches.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 use SebastianBergmann\Diff\Differ;
12
13 /**
14  * ...
15  *
16  * @since Class available since Release 3.5.0
17  */
18 class PHPUnit_Framework_Constraint_StringMatches extends PHPUnit_Framework_Constraint_PCREMatch
19 {
20     /**
21      * @var string
22      */
23     protected $string;
24
25     /**
26      * @param string $string
27      */
28     public function __construct($string)
29     {
30         parent::__construct($string);
31
32         $this->pattern = $this->createPatternFromFormat(
33             preg_replace('/\r\n/', "\n", $string)
34         );
35
36         $this->string = $string;
37     }
38
39     protected function failureDescription($other)
40     {
41         return 'format description matches text';
42     }
43
44     protected function additionalFailureDescription($other)
45     {
46         $from = preg_split('(\r\n|\r|\n)', $this->string);
47         $to   = preg_split('(\r\n|\r|\n)', $other);
48
49         foreach ($from as $index => $line) {
50             if (isset($to[$index]) && $line !== $to[$index]) {
51                 $line = $this->createPatternFromFormat($line);
52
53                 if (preg_match($line, $to[$index]) > 0) {
54                     $from[$index] = $to[$index];
55                 }
56             }
57         }
58
59         $this->string = implode("\n", $from);
60         $other        = implode("\n", $to);
61
62         $differ = new Differ("--- Expected\n+++ Actual\n");
63
64         return $differ->diff($this->string, $other);
65     }
66
67     protected function createPatternFromFormat($string)
68     {
69         $string = str_replace(
70             array(
71             '%e',
72             '%s',
73             '%S',
74             '%a',
75             '%A',
76             '%w',
77             '%i',
78             '%d',
79             '%x',
80             '%f',
81             '%c'
82             ),
83             array(
84             '\\' . DIRECTORY_SEPARATOR,
85             '[^\r\n]+',
86             '[^\r\n]*',
87             '.+',
88             '.*',
89             '\s*',
90             '[+-]?\d+',
91             '\d+',
92             '[0-9a-fA-F]+',
93             '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
94             '.'
95             ),
96             preg_quote($string, '/')
97         );
98
99         return '/^' . $string . '$/s';
100     }
101 }