Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
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
namespace PHPUnit\Framework\Constraint;
11
 
12
use const DIRECTORY_SEPARATOR;
13
use function explode;
14
use function implode;
15
use function preg_match;
16
use function preg_quote;
17
use function preg_replace;
18
use function strtr;
19
use SebastianBergmann\Diff\Differ;
20
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
21
 
22
/**
23
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
24
 */
25
final class StringMatchesFormatDescription extends RegularExpression
26
{
27
    /**
28
     * @var string
29
     */
30
    private $string;
31
 
32
    public function __construct(string $string)
33
    {
34
        parent::__construct(
35
            $this->createPatternFromFormat(
36
                $this->convertNewlines($string)
37
            )
38
        );
39
 
40
        $this->string = $string;
41
    }
42
 
43
    /**
44
     * Evaluates the constraint for parameter $other. Returns true if the
45
     * constraint is met, false otherwise.
46
     *
47
     * @param mixed $other value or object to evaluate
48
     */
49
    protected function matches($other): bool
50
    {
51
        return parent::matches(
52
            $this->convertNewlines($other)
53
        );
54
    }
55
 
56
    protected function failureDescription($other): string
57
    {
58
        return 'string matches format description';
59
    }
60
 
61
    protected function additionalFailureDescription($other): string
62
    {
63
        $from = explode("\n", $this->string);
64
        $to   = explode("\n", $this->convertNewlines($other));
65
 
66
        foreach ($from as $index => $line) {
67
            if (isset($to[$index]) && $line !== $to[$index]) {
68
                $line = $this->createPatternFromFormat($line);
69
 
70
                if (preg_match($line, $to[$index]) > 0) {
71
                    $from[$index] = $to[$index];
72
                }
73
            }
74
        }
75
 
76
        $this->string = implode("\n", $from);
77
        $other        = implode("\n", $to);
78
 
79
        return (new Differ(new UnifiedDiffOutputBuilder("--- Expected\n+++ Actual\n")))->diff($this->string, $other);
80
    }
81
 
82
    private function createPatternFromFormat(string $string): string
83
    {
84
        $string = strtr(
85
            preg_quote($string, '/'),
86
            [
87
                '%%' => '%',
88
                '%e' => '\\' . DIRECTORY_SEPARATOR,
89
                '%s' => '[^\r\n]+',
90
                '%S' => '[^\r\n]*',
91
                '%a' => '.+',
92
                '%A' => '.*',
93
                '%w' => '\s*',
94
                '%i' => '[+-]?\d+',
95
                '%d' => '\d+',
96
                '%x' => '[0-9a-fA-F]+',
97
                '%f' => '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
98
                '%c' => '.',
99
            ]
100
        );
101
 
102
        return '/^' . $string . '$/s';
103
    }
104
 
105
    private function convertNewlines(string $text): string
106
    {
107
        return preg_replace('/\r\n/', "\n", $text);
108
    }
109
}