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