|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +/* | 
|  | 4 | + * This file is part of the Symfony package. | 
|  | 5 | + * | 
|  | 6 | + * (c) Fabien Potencier <[email protected]> | 
|  | 7 | + * | 
|  | 8 | + * For the full copyright and license information, please view the LICENSE | 
|  | 9 | + * file that was distributed with this source code. | 
|  | 10 | + */ | 
|  | 11 | + | 
|  | 12 | +namespace Symfony\Polyfill\Tests\Intl\ListFormatter; | 
|  | 13 | + | 
|  | 14 | +use IntlListFormatter; | 
|  | 15 | +use PHPUnit\Framework\TestCase; | 
|  | 16 | + | 
|  | 17 | +/** | 
|  | 18 | + * @author Ayesh Karunaratne <[email protected]> | 
|  | 19 | + * | 
|  | 20 | + * @group class-polyfill | 
|  | 21 | + */ | 
|  | 22 | +class IntlListFormatterTest extends TestCase | 
|  | 23 | +{ | 
|  | 24 | +    public function testUnsupportedLocales() | 
|  | 25 | +    { | 
|  | 26 | +        new IntlListFormatter('en'); | 
|  | 27 | +        new IntlListFormatter('en-US'); | 
|  | 28 | +        new IntlListFormatter('en_US'); | 
|  | 29 | +        new IntlListFormatter('en-LK'); | 
|  | 30 | + | 
|  | 31 | +        if (PHP_VERSION_ID >= 80000) { | 
|  | 32 | +            $this->expectException(\ValueError::class); | 
|  | 33 | +        } | 
|  | 34 | +        else { | 
|  | 35 | +            $this->expectException(\InvalidArgumentException::class); | 
|  | 36 | +        } | 
|  | 37 | + | 
|  | 38 | +        new IntlListFormatter('ja'); | 
|  | 39 | +    } | 
|  | 40 | + | 
|  | 41 | +    public function testUnsupportedType() | 
|  | 42 | +    { | 
|  | 43 | +        if (PHP_VERSION_ID >= 80000) { | 
|  | 44 | +            $this->expectException(\ValueError::class); | 
|  | 45 | +        } | 
|  | 46 | +        else { | 
|  | 47 | +            $this->expectException(\InvalidArgumentException::class); | 
|  | 48 | +        } | 
|  | 49 | +        $this->expectExceptionMessage('must be one of IntlListFormatter::TYPE_AND, IntlListFormatter::TYPE_OR, or IntlListFormatter::TYPE_UNITS'); | 
|  | 50 | +        new IntlListFormatter('en', 42); | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    public function testUnsupportedWidth() | 
|  | 54 | +    { | 
|  | 55 | +        if (PHP_VERSION_ID >= 80000) { | 
|  | 56 | +            $this->expectException(\ValueError::class); | 
|  | 57 | +        } | 
|  | 58 | +        else { | 
|  | 59 | +            $this->expectException(\InvalidArgumentException::class); | 
|  | 60 | +        } | 
|  | 61 | +        $this->expectExceptionMessage('must be one of IntlListFormatter::WIDTH_WIDE, IntlListFormatter::WIDTH_SHORT, or IntlListFormatter::WIDTH_NARROW'); | 
|  | 62 | +        new IntlListFormatter('en', IntlListFormatter::TYPE_AND, 42); | 
|  | 63 | +    } | 
|  | 64 | + | 
|  | 65 | +    /** | 
|  | 66 | +     * @dataProvider formattingLists | 
|  | 67 | +     */ | 
|  | 68 | +    public function testFormatting(int $type, int $wide, array $strings, string $expected) | 
|  | 69 | +    { | 
|  | 70 | +        $formatter = new IntlListFormatter('en', $type, $wide); | 
|  | 71 | +        self::assertSame($expected, $formatter->format($strings)); | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    public function formattingLists() { | 
|  | 75 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, [], '']; | 
|  | 76 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, [1], '1']; | 
|  | 77 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, ['1'], '1']; | 
|  | 78 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, ['apple'], 'apple']; | 
|  | 79 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, ['apple'], 'apple']; | 
|  | 80 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry'], 'apple, banana, and strawberry']; | 
|  | 81 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, and orange']; | 
|  | 82 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, and 16']; | 
|  | 83 | + | 
|  | 84 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, [], '']; | 
|  | 85 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, [1], '1']; | 
|  | 86 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, ['1'], '1']; | 
|  | 87 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, ['apple'], 'apple']; | 
|  | 88 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, ['apple'], 'apple']; | 
|  | 89 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry'], 'apple, banana, & strawberry']; | 
|  | 90 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, & orange']; | 
|  | 91 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, & 16']; | 
|  | 92 | + | 
|  | 93 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, [], '']; | 
|  | 94 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, [1], '1']; | 
|  | 95 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, ['1'], '1']; | 
|  | 96 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, ['apple'], 'apple']; | 
|  | 97 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, ['apple'], 'apple']; | 
|  | 98 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry'], 'apple, banana, strawberry']; | 
|  | 99 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, orange']; | 
|  | 100 | +        yield [IntlListFormatter::TYPE_AND, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, 16']; | 
|  | 101 | + | 
|  | 102 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, [], '']; | 
|  | 103 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, [1], '1']; | 
|  | 104 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, ['1'], '1']; | 
|  | 105 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, ['apple'], 'apple']; | 
|  | 106 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, ['apple'], 'apple']; | 
|  | 107 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry'], 'apple, banana, or strawberry']; | 
|  | 108 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, or orange']; | 
|  | 109 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, or 16']; | 
|  | 110 | + | 
|  | 111 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, [], '']; | 
|  | 112 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, [1], '1']; | 
|  | 113 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, ['1'], '1']; | 
|  | 114 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, ['apple'], 'apple']; | 
|  | 115 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, ['apple'], 'apple']; | 
|  | 116 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry'], 'apple, banana, or strawberry']; | 
|  | 117 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, or orange']; | 
|  | 118 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, or 16']; | 
|  | 119 | + | 
|  | 120 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, [], '']; | 
|  | 121 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, [1], '1']; | 
|  | 122 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, ['1'], '1']; | 
|  | 123 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, ['apple'], 'apple']; | 
|  | 124 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, ['apple'], 'apple']; | 
|  | 125 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, or orange']; | 
|  | 126 | +        yield [IntlListFormatter::TYPE_OR, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, or 16']; | 
|  | 127 | + | 
|  | 128 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, [], '']; | 
|  | 129 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, [1], '1']; | 
|  | 130 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, ['1'], '1']; | 
|  | 131 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, ['apple'], 'apple']; | 
|  | 132 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, ['apple'], 'apple']; | 
|  | 133 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry'], 'apple, banana, strawberry']; | 
|  | 134 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, orange']; | 
|  | 135 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_WIDE, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, 16']; | 
|  | 136 | + | 
|  | 137 | + | 
|  | 138 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, [], '']; | 
|  | 139 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, [1], '1']; | 
|  | 140 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, ['1'], '1']; | 
|  | 141 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, ['apple'], 'apple']; | 
|  | 142 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, ['apple'], 'apple']; | 
|  | 143 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry'], 'apple, banana, strawberry']; | 
|  | 144 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry', 'orange'], 'apple, banana, strawberry, orange']; | 
|  | 145 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_SHORT, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple, banana, strawberry, orange, 16']; | 
|  | 146 | + | 
|  | 147 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, [], '']; | 
|  | 148 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, [1], '1']; | 
|  | 149 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, ['1'], '1']; | 
|  | 150 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, ['apple'], 'apple']; | 
|  | 151 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, ['apple'], 'apple']; | 
|  | 152 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry'], 'apple banana strawberry']; | 
|  | 153 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry', 'orange'], 'apple banana strawberry orange']; | 
|  | 154 | +        yield [IntlListFormatter::TYPE_UNITS, IntlListFormatter::WIDTH_NARROW, ['apple', 'banana', 'strawberry', 'orange', 16], 'apple banana strawberry orange 16']; | 
|  | 155 | +    } | 
|  | 156 | +} | 
0 commit comments