File: CommonStringWrapperTestCase.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 (269 lines) | stat: -rw-r--r-- 10,619 bytes parent folder | download | duplicates (2)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php

declare(strict_types=1);

namespace LaminasTest\Stdlib\StringWrapper;

use Laminas\Stdlib\Exception\InvalidArgumentException;
use Laminas\Stdlib\StringWrapper\StringWrapperInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

use const STR_PAD_BOTH;
use const STR_PAD_LEFT;
use const STR_PAD_RIGHT;

// phpcs:ignore WebimpressCodingStandard.NamingConventions.AbstractClass.Prefix
abstract class CommonStringWrapperTestCase extends TestCase
{
    abstract protected function getWrapper(
        string|null $encoding = null,
        string|null $convertEncoding = null,
    ): StringWrapperInterface|false;

    /**
     * @psalm-return array<array-key, array{
     *     0: string,
     *     1: string,
     *     2: int
     * }>
     */
    public static function strlenProvider(): array
    {
        return [
            ['ascii', 'abcdefghijklmnopqrstuvwxyz', 26],
            ['utf-8', 'abcdefghijklmnopqrstuvwxyz', 26],
            ['utf-8', 'äöüß', 4],
        ];
    }

    #[DataProvider('strlenProvider')]
    public function testStrlen(string $encoding, string $str, int $expected): void
    {
        $wrapper = $this->getWrapper($encoding);
        if (! $wrapper) {
            self::markTestSkipped("Encoding {$encoding} not supported");
        }
        $result = $wrapper->strlen($str);
        self::assertSame($expected, $result);
    }

    /**
     * @psalm-return array<array-key, array{
     *     0: string,
     *     1: string,
     *     2: int,
     *     3: ?int,
     *     4: string
     * }>
     */
    public static function substrProvider(): array
    {
        return [
            ['ascii', 'abcdefghijkl', 1, 5, 'bcdef'],
            ['utf-8', 'abcdefghijkl', 1, null, 'bcdefghijkl'],
            ['utf-8', 'abcdefghijkl', 1, 5, 'bcdef'],
            ['utf-8', 'äöüß', 1, 2, 'öü'],
        ];
    }

    #[DataProvider('substrProvider')]
    public function testSubstr(string $encoding, string $str, int $offset, ?int $length, string $expected): void
    {
        $wrapper = $this->getWrapper($encoding);
        if (! $wrapper) {
            $this->markTestSkipped("Encoding {$encoding} not supported");
        }
        $result = $wrapper->substr($str, $offset, $length);
        self::assertSame($expected, $result);
    }

    /**
     * @psalm-return array<array-key, array{
     *     0: string,
     *     1: string,
     *     2: string,
     *     3: int,
     *     4: int
     * }>
     */
    public static function strposProvider(): array
    {
        return [
            ['ascii', 'abcdefghijkl', 'g', 3, 6],
            ['utf-8', 'abcdefghijkl', 'g', 3, 6],
            ['utf-8', 'äöüß', 'ü', 1, 2],
        ];
    }

    #[DataProvider('strposProvider')]
    public function testStrpos(string $encoding, string $haystack, string $needle, int $offset, int $expected): void
    {
        $wrapper = $this->getWrapper($encoding);
        if (! $wrapper) {
            $this->markTestSkipped("Encoding {$encoding} not supported");
        }
        $result = $wrapper->strpos($haystack, $needle, $offset);
        self::assertSame($expected, $result);
    }

    /**
     * @psalm-return array<array-key, array{
     *     0: string,
     *     1: string,
     *     2: string,
     *     3: string
     * }>
     */
    public static function convertProvider(): array
    {
        return [
            ['ascii', 'ascii', 'abc', 'abc'],
            ['ascii', 'utf-8', 'abc', 'abc'],
            ['utf-8', 'ascii', 'abc', 'abc'],
            ['utf-8', 'iso-8859-15', '€', "\xa4"],
            ['utf-8', 'iso-8859-16', '€', "\xa4"],
        ];
    }

    #[DataProvider('convertProvider')]
    public function testConvert(string $encoding, string $convertEncoding, string $str, string $expected): void
    {
        $wrapper = $this->getWrapper($encoding, $convertEncoding);
        if (! $wrapper) {
            $this->markTestSkipped("Encoding {$encoding} or {$convertEncoding} not supported");
        }
        $result = $wrapper->convert($str);
        self::assertSame($expected, $result);
        // backword
        $result = $wrapper->convert($expected, true);
        self::assertSame($str, $result);
    }

    /**
     * @psalm-return array<string, array{
     *     0: string,
     *     1: string,
     *     2: int,
     *     3: string,
     *     4: bool,
     *     5: string
     * }>
     */
    public static function wordWrapProvider(): array
    {
        // @codingStandardsIgnoreStart
        return [
            // Standard cut tests
            'cut-single-line' => ['utf-8', 'äbüöcß', 2, ' ', true, 'äb üö cß'],
            'cut-multi-line' => ['utf-8', 'äbüöc ß äbüöcß', 2, ' ', true, 'äb üö c ß äb üö cß'],
            'cut-multi-line-short-words' => ['utf-8', 'Ä very long wöööööööööööörd.', 8, "\n", true, "Ä very\nlong\nwööööööö\nööööörd."],
            'cut-multi-line-with-previous-new-lines' => ['utf-8', "Ä very\nlong wöööööööööööörd.", 8, "\n", false, "Ä very\nlong\nwöööööööööööörd."],
            'long-break' => ['utf-8', "Ä very<br>long wöö<br>öööööööö<br>öörd.", 8, '<br>', false, "Ä very<br>long wöö<br>öööööööö<br>öörd."],
            // Alternative cut tests
            'cut-beginning-single-space' => ['utf-8', ' äüöäöü', 3, ' ', true, ' äüö äöü'],
            'cut-ending-single-space' => ['utf-8', 'äüöäöü ', 3, ' ', true, 'äüö äöü '],
            'cut-ending-single-space-with-non-space-divider' => ['utf-8', 'äöüäöü ', 3, '-', true, 'äöü-äöü-'],
            'cut-ending-two-spaces' => ['utf-8', 'äüöäöü  ', 3, ' ', true, 'äüö äöü  '],
            'no-cut-ending-single-space' => ['utf-8', '12345 ', 5, '-', false, '12345-'],
            'no-cut-ending-two-spaces' => ['utf-8', '12345  ', 5, '-', false, '12345- '],
            'cut-ending-three-spaces' => ['utf-8', 'äüöäöü  ', 3, ' ', true, 'äüö äöü  '],
            'cut-ending-two-breaks' => ['utf-8', 'äüöäöü--', 3, '-', true, 'äüö-äöü--'],
            'cut-tab' => ['utf-8', "äbü\töcß", 3, ' ', true, "äbü \töc ß"],
            'cut-new-line-with-space' => ['utf-8', "äbü\nößt", 3, ' ', true, "äbü \nöß t"],
            'cut-new-line-with-new-line' => ['utf-8', "äbü\nößte", 3, "\n", true, "äbü\nößt\ne"],
            // Break cut tests
            'cut-break-before' => ['ascii', 'foobar-foofoofoo', 8, '-', true, 'foobar-foofoofo-o'],
            'cut-break-with' => ['ascii', 'foobar-foobar', 6, '-', true, 'foobar-foobar'],
            'cut-break-within' => ['ascii', 'foobar-foobar', 7, '-', true, 'foobar-foobar'],
            'cut-break-within-end' => ['ascii', 'foobar-', 7, '-', true, 'foobar-'],
            'cut-break-after' => ['ascii', 'foobar-foobar', 5, '-', true, 'fooba-r-fooba-r'],
            // Standard no-cut tests
            'no-cut-single-line' => ['utf-8', 'äbüöcß', 2, ' ', false, 'äbüöcß'],
            'no-cut-multi-line' => ['utf-8', 'äbüöc ß äbüöcß', 2, "\n", false, "äbüöc\nß\näbüöcß"],
            'no-cut-multi-word' => ['utf-8', 'äöü äöü äöü', 5, "\n", false, "äöü\näöü\näöü"],
            // Break no-cut tests
            'no-cut-break-before' => ['ascii', 'foobar-foofoofoo', 8, '-', false, 'foobar-foofoofoo'],
            'no-cut-break-with' => ['ascii', 'foobar-foobar', 6, '-', false, 'foobar-foobar'],
            'no-cut-break-within' => ['ascii', 'foobar-foobar', 7, '-', false, 'foobar-foobar'],
            'no-cut-break-within-end' => ['ascii', 'foobar-', 7, '-', false, 'foobar-'],
            'no-cut-break-after' => ['ascii', 'foobar-foobar', 5, '-', false, 'foobar-foobar'],
        ];
        // @codingStandardsIgnoreEnd
    }

    #[DataProvider('wordWrapProvider')]
    public function testWordWrap(
        string $encoding,
        string $string,
        int $width,
        string $break,
        bool $cut,
        mixed $expected,
    ): void {
        $wrapper = $this->getWrapper($encoding);
        if (! $wrapper) {
            $this->markTestSkipped("Encoding {$encoding} not supported");
        }
        $result = $wrapper->wordWrap($string, $width, $break, $cut);
        self::assertSame($expected, $result);
    }

    public function testWordWrapInvalidArgument(): void
    {
        $wrapper = $this->getWrapper();
        if (! $wrapper) {
            $this->fail("Can't instantiate wrapper");
        }
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage("Cannot force cut when width is zero");
        $wrapper->wordWrap('a', 0, "\n", true);
    }

    /**
     * @psalm-return array<string, array{
     *     0: string,
     *     1: string,
     *     2: int,
     *     3: string,
     *     4: int,
     *     5: string
     * }>
     */
    public static function strPadProvider(): array
    {
        return [
            // single-byte
            'left-padding_single-byte'   => ['ascii', 'aaa', 5, 'o', STR_PAD_LEFT, 'ooaaa'],
            'center-padding_single-byte' => ['ascii', 'aaa', 6, 'o', STR_PAD_BOTH, 'oaaaoo'],
            'right-padding_single-byte'  => ['ascii', 'aaa', 5, 'o', STR_PAD_RIGHT, 'aaaoo'],
            // multi-byte
            'left-padding_multi-byte'   => ['utf-8', 'äää', 5, 'ö', STR_PAD_LEFT, 'ööäää'],
            'center-padding_multi-byte' => ['utf-8', 'äää', 6, 'ö', STR_PAD_BOTH, 'öäääöö'],
            'right-padding_multi-byte'  => ['utf-8', 'äää', 5, 'ö', STR_PAD_RIGHT, 'äääöö'],
            // Laminas-12186
            'input-longer-than-pad-length' => ['utf-8', 'äääöö', 2, 'ö', STR_PAD_RIGHT, 'äääöö'],
            'input-same-as-pad-length'     => ['utf-8', 'äääöö', 5, 'ö', STR_PAD_RIGHT, 'äääöö'],
            'negative-pad-length'          => ['utf-8', 'äääöö', -2, 'ö', STR_PAD_RIGHT, 'äääöö'],
        ];
    }

    #[Group('Laminas-12186')]
    #[DataProvider('strPadProvider')]
    public function testStrPad(
        string $encoding,
        string $input,
        int $padLength,
        string $padString,
        int $padType,
        mixed $expected,
    ): void {
        $wrapper = $this->getWrapper($encoding);
        if (! $wrapper) {
            $this->markTestSkipped("Encoding {$encoding} not supported");
        }
        $result = $wrapper->strPad($input, $padLength, $padString, $padType);
        self::assertSame($expected, $result);
    }
}