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

Assert   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 39
ccs 13
cts 13
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeLineEndings() 0 5 1
A assertEqualStringsIgnoringLineEndings() 0 9 1
A assertStringContainsStringIgnoringLineEndings() 0 9 1
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