xref: /webtrees/tests/app/Factories/EncodingFactoryTest.php (revision 73f6d2b56f07f42ca02190f0dfc40653a1e91834)
11c6adce8SGreg Roach<?php
21c6adce8SGreg Roach
31c6adce8SGreg Roach/**
41c6adce8SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
61c6adce8SGreg Roach * This program is free software: you can redistribute it and/or modify
71c6adce8SGreg Roach * it under the terms of the GNU General Public License as published by
81c6adce8SGreg Roach * the Free Software Foundation, either version 3 of the License, or
91c6adce8SGreg Roach * (at your option) any later version.
101c6adce8SGreg Roach * This program is distributed in the hope that it will be useful,
111c6adce8SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121c6adce8SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131c6adce8SGreg Roach * GNU General Public License for more details.
141c6adce8SGreg Roach * You should have received a copy of the GNU General Public License
151c6adce8SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
161c6adce8SGreg Roach */
171c6adce8SGreg Roach
181c6adce8SGreg Roachdeclare(strict_types=1);
191c6adce8SGreg Roach
201c6adce8SGreg Roachnamespace Fisharebest\Webtrees\Factories;
211c6adce8SGreg Roach
221c6adce8SGreg Roachuse DomainException;
231c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\ANSEL;
241c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\ASCII;
251c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\CP437;
261c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\CP850;
271c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\EncodingInterface;
281c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\MacRoman;
291c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\UTF16BE;
301c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\UTF16LE;
311c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\UTF8;
321c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\Windows1250;
331c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\Windows1251;
341c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\Windows1252;
351c6adce8SGreg Roachuse Fisharebest\Webtrees\TestCase;
36*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
371c6adce8SGreg Roach
38*202c018bSGreg Roach#[CoversClass(EncodingFactory::class)]
391c6adce8SGreg Roachclass EncodingFactoryTest extends TestCase
401c6adce8SGreg Roach{
411c6adce8SGreg Roach    public function testDetectUsingByteOrderMark(): void
421c6adce8SGreg Roach    {
431c6adce8SGreg Roach        $factory = new EncodingFactory();
441c6adce8SGreg Roach
45f01ab4acSGreg Roach        static::assertInstanceOf(
461c6adce8SGreg Roach            UTF8::class,
471c6adce8SGreg Roach            $factory->detect(UTF8::BYTE_ORDER_MARK)
481c6adce8SGreg Roach        );
491c6adce8SGreg Roach
50f01ab4acSGreg Roach        static::assertInstanceOf(
511c6adce8SGreg Roach            UTF16BE::class,
521c6adce8SGreg Roach            $factory->detect(UTF16BE::BYTE_ORDER_MARK)
531c6adce8SGreg Roach        );
541c6adce8SGreg Roach
55f01ab4acSGreg Roach        static::assertInstanceOf(
561c6adce8SGreg Roach            UTF16LE::class,
571c6adce8SGreg Roach            $factory->detect(UTF16LE::BYTE_ORDER_MARK)
581c6adce8SGreg Roach        );
591c6adce8SGreg Roach    }
601c6adce8SGreg Roach
611c6adce8SGreg Roach    public function testDetectUtf16UsingNullBytes(): void
621c6adce8SGreg Roach    {
631c6adce8SGreg Roach        $factory = new EncodingFactory();
641c6adce8SGreg Roach
65f01ab4acSGreg Roach        static::assertInstanceOf(
661c6adce8SGreg Roach            UTF16BE::class,
671c6adce8SGreg Roach            $factory->detect("\x000")
681c6adce8SGreg Roach        );
691c6adce8SGreg Roach
70f01ab4acSGreg Roach        static::assertInstanceOf(
711c6adce8SGreg Roach            UTF16LE::class,
721c6adce8SGreg Roach            $factory->detect("0\x00")
731c6adce8SGreg Roach        );
741c6adce8SGreg Roach    }
751c6adce8SGreg Roach
761c6adce8SGreg Roach    public function testDetectByCharAndVers(): void
771c6adce8SGreg Roach    {
781c6adce8SGreg Roach        $factory = new EncodingFactory();
791c6adce8SGreg Roach
80f01ab4acSGreg Roach        static::assertInstanceOf(
811c6adce8SGreg Roach            MacRoman::class,
821c6adce8SGreg Roach            $factory->detect("0 HEAD\n1 CHAR MACINTOSH\n0 TRLR")
831c6adce8SGreg Roach        );
841c6adce8SGreg Roach    }
851c6adce8SGreg Roach
86aa377663SGreg Roach    public function testMissingCharHeader(): void
87aa377663SGreg Roach    {
88aa377663SGreg Roach        $factory = new EncodingFactory();
89aa377663SGreg Roach
90aa377663SGreg Roach        static::assertInstanceOf(
915dd4472aSGreg Roach            UTF8::class,
92aa377663SGreg Roach            $factory->detect("0 HEAD\n0 TRLR")
93aa377663SGreg Roach        );
94aa377663SGreg Roach    }
95aa377663SGreg Roach
961c6adce8SGreg Roach    public function testMake(): void
971c6adce8SGreg Roach    {
981c6adce8SGreg Roach        $factory = new EncodingFactory();
991c6adce8SGreg Roach
100f01ab4acSGreg Roach        static::assertInstanceOf(UTF8::class, $factory->make(UTF8::NAME));
101f01ab4acSGreg Roach        static::assertInstanceOf(UTF16BE::class, $factory->make(UTF16BE::NAME));
102f01ab4acSGreg Roach        static::assertInstanceOf(UTF16LE::class, $factory->make(UTF16LE::NAME));
103f01ab4acSGreg Roach        static::assertInstanceOf(ANSEL::class, $factory->make(ANSEL::NAME));
104f01ab4acSGreg Roach        static::assertInstanceOf(ASCII::class, $factory->make(ASCII::NAME));
105f01ab4acSGreg Roach        static::assertInstanceOf(CP437::class, $factory->make(CP437::NAME));
106f01ab4acSGreg Roach        static::assertInstanceOf(CP850::class, $factory->make(CP850::NAME));
107f01ab4acSGreg Roach        static::assertInstanceOf(Windows1250::class, $factory->make(Windows1250::NAME));
108f01ab4acSGreg Roach        static::assertInstanceOf(Windows1251::class, $factory->make(Windows1251::NAME));
109f01ab4acSGreg Roach        static::assertInstanceOf(Windows1252::class, $factory->make(Windows1252::NAME));
110f01ab4acSGreg Roach        static::assertInstanceOf(MacRoman::class, $factory->make(MacRoman::NAME));
1111c6adce8SGreg Roach
1121c6adce8SGreg Roach        $this->expectException(DomainException::class);
1131c6adce8SGreg Roach        $factory->make('Not the name of a valid encoding');
1141c6adce8SGreg Roach    }
1151c6adce8SGreg Roach
1161c6adce8SGreg Roach    public function testList(): void
1171c6adce8SGreg Roach    {
1181c6adce8SGreg Roach        $factory = new EncodingFactory();
1191c6adce8SGreg Roach
1201c6adce8SGreg Roach        $encodings = $factory->list();
1211c6adce8SGreg Roach
122f01ab4acSGreg Roach        static::assertCount(13, $encodings);
1231c6adce8SGreg Roach
1241c6adce8SGreg Roach        foreach ($encodings as $key => $value) {
125f01ab4acSGreg Roach            static::assertInstanceOf(EncodingInterface::class, $factory->make($key));
1261c6adce8SGreg Roach        }
1271c6adce8SGreg Roach    }
1281c6adce8SGreg Roach}
129