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