Passed
Push — master ( 141848...a80b50 )
by Philippe
06:19
created

DirectoryTest::testToTextsMatchingRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use PhpSpellcheck\Source\Directory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Directory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use PhpSpellcheck\Text;
7
use PhpSpellcheck\Utils\TextEncoding;
8
use PHPUnit\Framework\TestCase;
9
10
class DirectoryTest extends TestCase
11
{
12
    private const TEXT_FIXTURES_PATH = __DIR__ . '/../Fixtures/Text/Directory';
13
14
    public function testToTexts(): void
15
    {
16
        $textsFromDirectory = (new Directory(self::TEXT_FIXTURES_PATH))->toTexts(['ctx' => 'in tests']);
17
        $expectedValues = [
18
            new Text(
19
                "mispélling3\n",
20
                TextEncoding::UTF8,
21
                ['ctx' => 'in tests', 'filePath' => realpath(self::TEXT_FIXTURES_PATH . '/mispelling3.txt')]
22
            ),
23
            new Text(
24
                "mispelling2\n",
25
                TextEncoding::ASCII,
26
                ['ctx' => 'in tests', 'filePath' => realpath(self::TEXT_FIXTURES_PATH . '/mispelling2.txt')]
27
            ),
28
            new Text(
29
                "mispelling4\n",
30
                TextEncoding::ASCII,
31
                [
32
                'ctx' => 'in tests',
33
                    'filePath' => realpath(self::TEXT_FIXTURES_PATH . '/SubDirectory/mispelling4.txt'),
34
                ]
35
            ),
36
        ];
37
        $realValues = iterator_to_array($textsFromDirectory);
38
39
        foreach ($expectedValues as $value) {
40
            $this->assertContains($value, $realValues, '', false, false);
41
        }
42
    }
43
44
    public function testToTextsMatchingRegex(): void
45
    {
46
        $textsFromDirectory = (new Directory(self::TEXT_FIXTURES_PATH, '/^((?!mispelling3\.txt).)*$/'))
47
            ->toTexts(['ctx' => 'in tests']);
48
49
        $expectedValues = [
50
            new Text(
51
                "mispelling2\n",
52
                TextEncoding::ASCII,
53
                ['ctx' => 'in tests', 'filePath' => realpath(self::TEXT_FIXTURES_PATH . '/mispelling2.txt')]
54
            ),
55
            new Text("mispelling4\n", TextEncoding::ASCII, [
56
                'ctx' => 'in tests',
57
                'filePath' => realpath(self::TEXT_FIXTURES_PATH . '/SubDirectory/mispelling4.txt'),
58
            ]),
59
        ];
60
        $realValues = iterator_to_array($textsFromDirectory);
61
62
        foreach ($expectedValues as $value) {
63
            $this->assertContains($value, $realValues, '', false, false);
64
        }
65
    }
66
}
67