Passed
Pull Request — master (#27)
by Sergei
02:36
created

assertStringContainsStringIgnoringLineEndings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Test\Support;
6
7
use PHPUnit\Framework\Assert as PhpUnitAssert;
8
use PHPUnit\Framework\ExpectationFailedException;
9
10
final class Assert
11
{
12
    /**
13
     * Asserts that two strings equality ignoring line endings.
14
     *
15
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
16
     * @throws ExpectationFailedException
17
     */
18 12
    public static function assertEqualStringsIgnoringLineEndings(
19
        string $expected,
20
        string $actual,
21
        string $message = ''
22
    ): void {
23 12
        $expected = self::normalizeLineEndings($expected);
24 12
        $actual = self::normalizeLineEndings($actual);
25
26 12
        PhpUnitAssert::assertEquals($expected, $actual, $message);
27 9
    }
28
29
    /**
30
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
31
     * @throws ExpectationFailedException
32
     */
33 3
    public static function assertStringContainsStringIgnoringLineEndings(
34
        string $needle,
35
        string $haystack,
36
        string $message = ''
37
    ): void {
38 3
        $needle = self::normalizeLineEndings($needle);
39 3
        $haystack = self::normalizeLineEndings($haystack);
40
41 3
        PhpUnitAssert::assertStringContainsString($needle, $haystack, $message);
42 2
    }
43
44 15
    private static function normalizeLineEndings(string $value): string
45
    {
46 15
        return strtr($value, [
47 15
            "\r\n" => "\n",
48
            "\r" => "\n",
49
        ]);
50
    }
51
}
52