3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 use SebastianBergmann\Diff\Differ;
16 * @since Class available since Release 3.5.0
18 class PHPUnit_Framework_Constraint_StringMatches extends PHPUnit_Framework_Constraint_PCREMatch
26 * @param string $string
28 public function __construct($string)
30 parent::__construct($string);
32 $this->pattern = $this->createPatternFromFormat(
33 preg_replace('/\r\n/', "\n", $string)
36 $this->string = $string;
39 protected function failureDescription($other)
41 return 'format description matches text';
44 protected function additionalFailureDescription($other)
46 $from = preg_split('(\r\n|\r|\n)', $this->string);
47 $to = preg_split('(\r\n|\r|\n)', $other);
49 foreach ($from as $index => $line) {
50 if (isset($to[$index]) && $line !== $to[$index]) {
51 $line = $this->createPatternFromFormat($line);
53 if (preg_match($line, $to[$index]) > 0) {
54 $from[$index] = $to[$index];
59 $this->string = implode("\n", $from);
60 $other = implode("\n", $to);
62 $differ = new Differ("--- Expected\n+++ Actual\n");
64 return $differ->diff($this->string, $other);
67 protected function createPatternFromFormat($string)
69 $string = str_replace(
84 '\\' . DIRECTORY_SEPARATOR,
93 '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
96 preg_quote($string, '/')
99 return '/^' . $string . '$/s';