|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * League.Csv (https://csv.thephpleague.com) |
| 5 | + * |
| 6 | + * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace League\Csv; |
| 15 | + |
| 16 | +use JsonSerializable; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use TypeError; |
| 19 | +use UnitEnum; |
| 20 | + |
| 21 | +use function strtolower; |
| 22 | + |
| 23 | +final class EnumFormatterTest extends TestCase |
| 24 | +{ |
| 25 | + public function test_it_can_convert_backed_enum(): void |
| 26 | + { |
| 27 | + $arr = ['city' => City::Brussels, 'habitants' => 7_000_000]; |
| 28 | + $doc = Writer::fromString(); |
| 29 | + $doc->addFormatter(EnumFormatter::usingValue()); |
| 30 | + $doc->insertOne($arr); |
| 31 | + |
| 32 | + self::assertSame('Brussels,7000000'."\n", $doc->toString()); |
| 33 | + } |
| 34 | + |
| 35 | + public function test_it_can_convert_backed_enum_using_json_serializable(): void |
| 36 | + { |
| 37 | + $arr = ['city' => City::Brussels, 'habitants' => 7_000_000]; |
| 38 | + $doc = Writer::fromString(); |
| 39 | + $doc->addFormatter(EnumFormatter::usingJson()); |
| 40 | + $doc->insertOne($arr); |
| 41 | + |
| 42 | + self::assertSame('brussels,7000000'."\n", $doc->toString()); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_it_can_convert_backed_enum_using_callback(): void |
| 46 | + { |
| 47 | + $arr = ['city' => City::Brussels, 'habitants' => 7_000_000]; |
| 48 | + $doc = Writer::fromString(); |
| 49 | + $doc->addFormatter(EnumFormatter::usingCallback(fn (UnitEnum $value) => 'fourty-two')); |
| 50 | + $doc->insertOne($arr); |
| 51 | + |
| 52 | + self::assertSame('fourty-two,7000000'."\n", $doc->toString()); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_it_can_convert_backed_enum_using_name(): void |
| 56 | + { |
| 57 | + $arr = ['city' => City::KINSHASA, 'habitants' => 7_000_000]; |
| 58 | + $doc = Writer::fromString(); |
| 59 | + $doc->addFormatter(EnumFormatter::usingName()); |
| 60 | + $doc->insertOne($arr); |
| 61 | + |
| 62 | + self::assertSame('KINSHASA,7000000'."\n", $doc->toString()); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_it_fails_to_convert_an_unit_enum(): void |
| 66 | + { |
| 67 | + $arr = ['city' => Pure::Foo, 'habitants' => 7_000_000]; |
| 68 | + $doc = Writer::fromString(); |
| 69 | + $doc->addFormatter(EnumFormatter::usingValue()); |
| 70 | + |
| 71 | + $this->expectException(TypeError::class); |
| 72 | + $this->expectExceptionMessage('The Enum `'.Pure::class.'` cannot be encoded using the "value" strategy'); |
| 73 | + |
| 74 | + $doc->insertOne($arr); |
| 75 | + } |
| 76 | + |
| 77 | + public function test_it_fails_to_convert_to_json_without_json_serializable_interface(): void |
| 78 | + { |
| 79 | + $arr = ['city' => WithoutJson::Foo, 'habitants' => 7_000_000]; |
| 80 | + $doc = Writer::fromString(); |
| 81 | + $doc->addFormatter(EnumFormatter::usingJson()); |
| 82 | + |
| 83 | + $this->expectException(TypeError::class); |
| 84 | + $this->expectExceptionMessage('The Enum `'.WithoutJson::class.'` cannot be encoded using the "json" strategy'); |
| 85 | + |
| 86 | + $doc->insertOne($arr); |
| 87 | + } |
| 88 | + |
| 89 | + public function test_it_uses_json_serializable_representation_to_convert_an_unit_enum(): void |
| 90 | + { |
| 91 | + $arr = ['city' => WithoutJson::Foo, 'habitants' => 7_000_000]; |
| 92 | + $doc = Writer::fromString(); |
| 93 | + $doc->addFormatter(EnumFormatter::usingJson()); |
| 94 | + |
| 95 | + $this->expectException(TypeError::class); |
| 96 | + $this->expectExceptionMessage('The Enum `'.WithoutJson::class.'` cannot be encoded using the "json" strategy'); |
| 97 | + |
| 98 | + $doc->insertOne($arr); |
| 99 | + } |
| 100 | + |
| 101 | + public function test_it_uses_name_representation_to_convert_an_unit_enum(): void |
| 102 | + { |
| 103 | + $arr = ['city' => Pure::Foo, 'habitants' => 7_000_000]; |
| 104 | + $doc = Writer::fromString(); |
| 105 | + $doc->addFormatter(EnumFormatter::usingName()); |
| 106 | + $doc->insertOne($arr); |
| 107 | + |
| 108 | + self::assertSame('Foo,7000000'."\n", $doc->toString()); |
| 109 | + } |
| 110 | + |
| 111 | + public function test_it_uses_callback_representation_to_convert_an_unit_enum(): void |
| 112 | + { |
| 113 | + $arr = ['city' => Pure::Foo, 'habitants' => 7_000_000]; |
| 114 | + $doc = Writer::fromString(); |
| 115 | + $doc->addFormatter(EnumFormatter::usingCallback(fn (UnitEnum $value) => 'fourty-two')); |
| 116 | + $doc->insertOne($arr); |
| 117 | + |
| 118 | + self::assertSame('fourty-two,7000000'."\n", $doc->toString()); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +enum City: string implements JsonSerializable |
| 123 | +{ |
| 124 | + case Kigali = 'Kigali'; |
| 125 | + case KINSHASA = 'Kinshasa'; |
| 126 | + case Brussels = 'Brussels'; |
| 127 | + |
| 128 | + public function jsonSerialize(): string |
| 129 | + { |
| 130 | + return strtolower($this->value); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +enum Pure implements JsonSerializable |
| 135 | +{ |
| 136 | + case Foo; |
| 137 | + case Bar; |
| 138 | + |
| 139 | + public function jsonSerialize(): string |
| 140 | + { |
| 141 | + return strtolower($this->name); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +enum WithoutJson: string |
| 146 | +{ |
| 147 | + case Foo = 'FOO'; |
| 148 | + case Bar = 'BAR'; |
| 149 | +} |
0 commit comments