xref: /webtrees/tests/app/Factories/EncodingFactoryTest.php (revision f01ab4ac305e1fac9efbeef65f5be51ced21e7a7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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::make
100     */
101    public function testMake(): void
102    {
103        $factory = new EncodingFactory();
104
105        static::assertInstanceOf(UTF8::class, $factory->make(UTF8::NAME));
106        static::assertInstanceOf(UTF16BE::class, $factory->make(UTF16BE::NAME));
107        static::assertInstanceOf(UTF16LE::class, $factory->make(UTF16LE::NAME));
108        static::assertInstanceOf(ANSEL::class, $factory->make(ANSEL::NAME));
109        static::assertInstanceOf(ASCII::class, $factory->make(ASCII::NAME));
110        static::assertInstanceOf(CP437::class, $factory->make(CP437::NAME));
111        static::assertInstanceOf(CP850::class, $factory->make(CP850::NAME));
112        static::assertInstanceOf(Windows1250::class, $factory->make(Windows1250::NAME));
113        static::assertInstanceOf(Windows1251::class, $factory->make(Windows1251::NAME));
114        static::assertInstanceOf(Windows1252::class, $factory->make(Windows1252::NAME));
115        static::assertInstanceOf(MacRoman::class, $factory->make(MacRoman::NAME));
116
117        $this->expectException(DomainException::class);
118        $factory->make('Not the name of a valid encoding');
119    }
120
121    /**
122     * @covers \Fisharebest\Webtrees\Factories\EncodingFactory::list
123     */
124    public function testList(): void
125    {
126        $factory = new EncodingFactory();
127
128        $encodings = $factory->list();
129
130        static::assertCount(13, $encodings);
131
132        foreach ($encodings as $key => $value) {
133            static::assertIsString($key);
134            static::assertIsString($value);
135            static::assertInstanceOf(EncodingInterface::class, $factory->make($key));
136        }
137    }
138}
139