File: GlobTest.php

package info (click to toggle)
php-zend-stdlib 3.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: php: 5,639; xml: 473; makefile: 14
file content (148 lines) | stat: -rw-r--r-- 3,698 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

declare(strict_types=1);

namespace LaminasTest\Stdlib;

use Laminas\Stdlib\Exception\RuntimeException;
use Laminas\Stdlib\Glob;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function count;
use function defined;
use function glob;
use function realpath;
use function str_repeat;

use const GLOB_BRACE;

final class GlobTest extends TestCase
{
    public function testFallback(): void
    {
        if (! defined('GLOB_BRACE')) {
            $this->markTestSkipped('GLOB_BRACE not available');
        }

        $expected = glob(__DIR__ . '/_files/{alph,bet}a', GLOB_BRACE);
        $actual   = Glob::glob(
            __DIR__ . '/_files/{alph,bet}a',
            Glob::GLOB_BRACE,
            true
        );

        self::assertEquals($actual, $expected);

        $notExpectedPath = realpath(__DIR__ . '/_files/{alph,bet}a');

        self::assertNotContains(
            $notExpectedPath,
            $actual
        );
    }

    public function testNonMatchingGlobReturnsArray(): void
    {
        $result = Glob::glob(
            '/some/path/{,*.}{this,orthis}.php',
            Glob::GLOB_BRACE
        );
        self::assertIsArray($result);
    }

    public function testThrowExceptionOnError(): void
    {
        $this->expectException(RuntimeException::class);

        // run into a max path length error
        $path = '/' . str_repeat('a', 10000);
        Glob::glob($path);
    }

    /** @param list<non-empty-string> $expectedSequence */
    #[DataProvider('patternsProvider')]
    public function testPatterns(string $pattern, array $expectedSequence): void
    {
        $result = Glob::glob(__DIR__ . '/_files/' . $pattern, Glob::GLOB_BRACE);

        self::assertCount(count($expectedSequence), $result);

        foreach ($expectedSequence as $i => $expectedFileName) {
            self::assertStringEndsWith($expectedFileName, $result[$i]);
        }
    }

    /**
     * @psalm-return array<array-key, array{
     *     0: string,
     *     1: list<non-empty-string>
     * }>
     */
    public static function patternsProvider(): array
    {
        return [
            [
                "{{,*.}alph,{,*.}bet}a",
                [
                    'alpha',
                    'eta.alpha',
                    'zeta.alpha',
                    'beta',
                    'eta.beta',
                    'zeta.beta',
                ],
            ],
        ];
    }

    public function testGlobWithoutGlobBraceFlag(): void
    {
        $expected = [
            realpath(__DIR__ . '/_files/{alph,bet}a'),
        ];

        self::assertEquals(
            glob(__DIR__ . '/_files/{alph,bet}a', 0),
            $expected
        );
    }

    /**
     * @psalm-return array<array-key, array{
     *     int,
     *     int,
     *     bool
     * }>
     */
    public static function flagsIsEqualsToMethodDataProvider(): array
    {
        return [
            [
                Glob::GLOB_BRACE,
                Glob::GLOB_BRACE,
                true,
            ],
            [
                Glob::GLOB_BRACE,
                Glob::GLOB_NOSORT,
                false,
            ],
        ];
    }

    #[DataProvider('flagsIsEqualsToMethodDataProvider')]
    public function testFlagsIsEqualsToMethod(
        int $flags,
        int $otherFlags,
        bool $expected
    ): void {
        /**
         * @psalm-suppress InternalMethod this test is specifically testing the behavior of this method,
         *                                to prevent regressions
         */
        $actual = Glob::flagsIsEqualTo($flags, $otherFlags);

        $this->assertEquals($expected, $actual);
    }
}