xref: /webtrees/tests/app/Factories/EncodingFactoryTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Factories;
21
22use DomainException;
23use Fisharebest\Webtrees\Encodings\ANSEL;
24use Fisharebest\Webtrees\Encodings\ASCII;
25use Fisharebest\Webtrees\Encodings\CP437;
26use Fisharebest\Webtrees\Encodings\CP850;
27use Fisharebest\Webtrees\Encodings\EncodingInterface;
28use Fisharebest\Webtrees\Encodings\MacRoman;
29use Fisharebest\Webtrees\Encodings\UTF16BE;
30use Fisharebest\Webtrees\Encodings\UTF16LE;
31use Fisharebest\Webtrees\Encodings\UTF8;
32use Fisharebest\Webtrees\Encodings\Windows1250;
33use Fisharebest\Webtrees\Encodings\Windows1251;
34use Fisharebest\Webtrees\Encodings\Windows1252;
35use Fisharebest\Webtrees\TestCase;
36use PHPUnit\Framework\Attributes\CoversClass;
37
38
39#[CoversClass(EncodingFactory::class)]
40class EncodingFactoryTest extends TestCase
41{
42    public function testDetectUsingByteOrderMark(): void
43    {
44        $factory = new EncodingFactory();
45
46        static::assertInstanceOf(
47            UTF8::class,
48            $factory->detect(UTF8::BYTE_ORDER_MARK)
49        );
50
51        static::assertInstanceOf(
52            UTF16BE::class,
53            $factory->detect(UTF16BE::BYTE_ORDER_MARK)
54        );
55
56        static::assertInstanceOf(
57            UTF16LE::class,
58            $factory->detect(UTF16LE::BYTE_ORDER_MARK)
59        );
60    }
61
62    public function testDetectUtf16UsingNullBytes(): void
63    {
64        $factory = new EncodingFactory();
65
66        static::assertInstanceOf(
67            UTF16BE::class,
68            $factory->detect("\x000")
69        );
70
71        static::assertInstanceOf(
72            UTF16LE::class,
73            $factory->detect("0\x00")
74        );
75    }
76
77    public function testDetectByCharAndVers(): void
78    {
79        $factory = new EncodingFactory();
80
81        static::assertInstanceOf(
82            MacRoman::class,
83            $factory->detect("0 HEAD\n1 CHAR MACINTOSH\n0 TRLR")
84        );
85    }
86
87    public function testMissingCharHeader(): void
88    {
89        $factory = new EncodingFactory();
90
91        static::assertInstanceOf(
92            UTF8::class,
93            $factory->detect("0 HEAD\n0 TRLR")
94        );
95    }
96
97    public function testMake(): void
98    {
99        $factory = new EncodingFactory();
100
101        static::assertInstanceOf(UTF8::class, $factory->make(UTF8::NAME));
102        static::assertInstanceOf(UTF16BE::class, $factory->make(UTF16BE::NAME));
103        static::assertInstanceOf(UTF16LE::class, $factory->make(UTF16LE::NAME));
104        static::assertInstanceOf(ANSEL::class, $factory->make(ANSEL::NAME));
105        static::assertInstanceOf(ASCII::class, $factory->make(ASCII::NAME));
106        static::assertInstanceOf(CP437::class, $factory->make(CP437::NAME));
107        static::assertInstanceOf(CP850::class, $factory->make(CP850::NAME));
108        static::assertInstanceOf(Windows1250::class, $factory->make(Windows1250::NAME));
109        static::assertInstanceOf(Windows1251::class, $factory->make(Windows1251::NAME));
110        static::assertInstanceOf(Windows1252::class, $factory->make(Windows1252::NAME));
111        static::assertInstanceOf(MacRoman::class, $factory->make(MacRoman::NAME));
112
113        $this->expectException(DomainException::class);
114        $factory->make('Not the name of a valid encoding');
115    }
116
117    public function testList(): void
118    {
119        $factory = new EncodingFactory();
120
121        $encodings = $factory->list();
122
123        static::assertCount(13, $encodings);
124
125        foreach ($encodings as $key => $value) {
126            static::assertIsString($key);
127            static::assertIsString($value);
128            static::assertInstanceOf(EncodingInterface::class, $factory->make($key));
129        }
130    }
131}
132