xref: /webtrees/tests/app/Factories/EncodingFactoryTest.php (revision e873f434551745f888937263ff89e80db3b0f785)
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#[CoversClass(EncodingFactory::class)]
39class EncodingFactoryTest extends TestCase
40{
41    public function testDetectUsingByteOrderMark(): void
42    {
43        $factory = new EncodingFactory();
44
45        static::assertInstanceOf(
46            UTF8::class,
47            $factory->detect(UTF8::BYTE_ORDER_MARK)
48        );
49
50        static::assertInstanceOf(
51            UTF16BE::class,
52            $factory->detect(UTF16BE::BYTE_ORDER_MARK)
53        );
54
55        static::assertInstanceOf(
56            UTF16LE::class,
57            $factory->detect(UTF16LE::BYTE_ORDER_MARK)
58        );
59    }
60
61    public function testDetectUtf16UsingNullBytes(): void
62    {
63        $factory = new EncodingFactory();
64
65        static::assertInstanceOf(
66            UTF16BE::class,
67            $factory->detect("\x000")
68        );
69
70        static::assertInstanceOf(
71            UTF16LE::class,
72            $factory->detect("0\x00")
73        );
74    }
75
76    public function testDetectByCharAndVers(): void
77    {
78        $factory = new EncodingFactory();
79
80        static::assertInstanceOf(
81            MacRoman::class,
82            $factory->detect("0 HEAD\n1 CHAR MACINTOSH\n0 TRLR")
83        );
84    }
85
86    public function testMissingCharHeader(): void
87    {
88        $factory = new EncodingFactory();
89
90        static::assertInstanceOf(
91            UTF8::class,
92            $factory->detect("0 HEAD\n0 TRLR")
93        );
94    }
95
96    public function testMake(): void
97    {
98        $factory = new EncodingFactory();
99
100        static::assertInstanceOf(UTF8::class, $factory->make(UTF8::NAME));
101        static::assertInstanceOf(UTF16BE::class, $factory->make(UTF16BE::NAME));
102        static::assertInstanceOf(UTF16LE::class, $factory->make(UTF16LE::NAME));
103        static::assertInstanceOf(ANSEL::class, $factory->make(ANSEL::NAME));
104        static::assertInstanceOf(ASCII::class, $factory->make(ASCII::NAME));
105        static::assertInstanceOf(CP437::class, $factory->make(CP437::NAME));
106        static::assertInstanceOf(CP850::class, $factory->make(CP850::NAME));
107        static::assertInstanceOf(Windows1250::class, $factory->make(Windows1250::NAME));
108        static::assertInstanceOf(Windows1251::class, $factory->make(Windows1251::NAME));
109        static::assertInstanceOf(Windows1252::class, $factory->make(Windows1252::NAME));
110        static::assertInstanceOf(MacRoman::class, $factory->make(MacRoman::NAME));
111
112        $this->expectException(DomainException::class);
113        $factory->make('Not the name of a valid encoding');
114    }
115
116    public function testList(): void
117    {
118        $factory = new EncodingFactory();
119
120        $encodings = $factory->list();
121
122        static::assertCount(13, $encodings);
123
124        foreach ($encodings as $key => $value) {
125            static::assertInstanceOf(EncodingInterface::class, $factory->make($key));
126        }
127    }
128}
129