|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheetTests\Shared; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Exception as SSException; |
| 6 | +use PhpOffice\PhpSpreadsheet\Shared\Font; |
| 7 | +use PhpOffice\PhpSpreadsheet\Style\Font as StyleFont; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class FontFileNameTest extends TestCase |
| 11 | +{ |
| 12 | + private const DEFAULT_DIRECTORY = 'tests/data/Shared/FakeFonts/Default'; |
| 13 | + private const MAC_DIRECTORY = 'tests/data/Shared/FakeFonts/Mac'; |
| 14 | + |
| 15 | + /** @var string */ |
| 16 | + private $holdDirectory; |
| 17 | + |
| 18 | + /** @var array */ |
| 19 | + private $holdExtraFontArray; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->holdDirectory = Font::getTrueTypeFontPath(); |
| 24 | + $this->holdExtraFontArray = Font::getExtraFontArray(); |
| 25 | + Font::setExtraFontArray([ |
| 26 | + 'Extra Font' => [ |
| 27 | + 'x' => 'extrafont.ttf', |
| 28 | + 'xb' => 'extrafontbd.ttf', |
| 29 | + 'xi' => 'extrafonti.ttf', |
| 30 | + 'xbi' => 'extrafontbi.ttf', |
| 31 | + ], |
| 32 | + ]); |
| 33 | + } |
| 34 | + |
| 35 | + protected function tearDown(): void |
| 36 | + { |
| 37 | + Font::setTrueTypeFontPath($this->holdDirectory); |
| 38 | + Font::setExtraFontArray($this->holdExtraFontArray); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider providerDefault |
| 43 | + */ |
| 44 | + public function testDefaultFilenames(string $expected, array $fontArray): void |
| 45 | + { |
| 46 | + if ($expected === 'exception') { |
| 47 | + $this->expectException(SSException::class); |
| 48 | + $this->expectExceptionMessage('TrueType Font file not found'); |
| 49 | + } |
| 50 | + Font::setTrueTypeFontPath(self::DEFAULT_DIRECTORY); |
| 51 | + $font = (new StyleFont())->applyFromArray($fontArray); |
| 52 | + $result = Font::getTrueTypeFontFileFromFont($font); |
| 53 | + self::assertSame($expected, basename($result)); |
| 54 | + } |
| 55 | + |
| 56 | + public function providerDefault(): array |
| 57 | + { |
| 58 | + return [ |
| 59 | + ['arial.ttf', ['name' => 'Arial']], |
| 60 | + ['arialbd.ttf', ['name' => 'Arial', 'bold' => true]], |
| 61 | + ['ariali.ttf', ['name' => 'Arial', 'italic' => true]], |
| 62 | + ['arialbi.ttf', ['name' => 'Arial', 'bold' => true, 'italic' => true]], |
| 63 | + ['cour.ttf', ['name' => 'Courier New']], |
| 64 | + ['courbd.ttf', ['name' => 'Courier New', 'bold' => true]], |
| 65 | + ['couri.ttf', ['name' => 'Courier New', 'italic' => true]], |
| 66 | + ['courbi.ttf', ['name' => 'Courier New', 'bold' => true, 'italic' => true]], |
| 67 | + ['impact.ttf', ['name' => 'Impact']], |
| 68 | + 'no bold impact' => ['impact.ttf', ['name' => 'Impact', 'bold' => true]], |
| 69 | + 'no italic impact' => ['impact.ttf', ['name' => 'Impact', 'italic' => true]], |
| 70 | + 'no bold italic impact' => ['impact.ttf', ['name' => 'Impact', 'bold' => true, 'italic' => true]], |
| 71 | + ['tahoma.ttf', ['name' => 'Tahoma']], |
| 72 | + ['tahomabd.ttf', ['name' => 'Tahoma', 'bold' => true]], |
| 73 | + 'no italic tahoma' => ['tahoma.ttf', ['name' => 'Tahoma', 'italic' => true]], |
| 74 | + 'no bold italic tahoma' => ['tahomabd.ttf', ['name' => 'Tahoma', 'bold' => true, 'italic' => true]], |
| 75 | + 'Times New Roman not in directory for this test' => ['exception', ['name' => 'Times New Roman']], |
| 76 | + ['extrafont.ttf', ['name' => 'Extra Font']], |
| 77 | + ['extrafontbd.ttf', ['name' => 'Extra Font', 'bold' => true]], |
| 78 | + ['extrafonti.ttf', ['name' => 'Extra Font', 'italic' => true]], |
| 79 | + ['extrafontbi.ttf', ['name' => 'Extra Font', 'bold' => true, 'italic' => true]], |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @dataProvider providerMac |
| 85 | + */ |
| 86 | + public function testMacFilenames(string $expected, array $fontArray): void |
| 87 | + { |
| 88 | + if ($expected === 'exception') { |
| 89 | + $this->expectException(SSException::class); |
| 90 | + $this->expectExceptionMessage('TrueType Font file not found'); |
| 91 | + } |
| 92 | + Font::setTrueTypeFontPath(self::MAC_DIRECTORY); |
| 93 | + $font = (new StyleFont())->applyFromArray($fontArray); |
| 94 | + $result = Font::getTrueTypeFontFileFromFont($font); |
| 95 | + self::assertSame($expected, ucfirst(basename($result))); // allow for Windows case-insensitivity |
| 96 | + } |
| 97 | + |
| 98 | + public function providerMac(): array |
| 99 | + { |
| 100 | + return [ |
| 101 | + ['Arial.ttf', ['name' => 'Arial']], |
| 102 | + ['Arial Bold.ttf', ['name' => 'Arial', 'bold' => true]], |
| 103 | + ['Arial Italic.ttf', ['name' => 'Arial', 'italic' => true]], |
| 104 | + ['Arial Bold Italic.ttf', ['name' => 'Arial', 'bold' => true, 'italic' => true]], |
| 105 | + ['Courier New.ttf', ['name' => 'Courier New']], |
| 106 | + ['Courier New Bold.ttf', ['name' => 'Courier New', 'bold' => true]], |
| 107 | + ['Courier New Italic.ttf', ['name' => 'Courier New', 'italic' => true]], |
| 108 | + ['Courier New Bold Italic.ttf', ['name' => 'Courier New', 'bold' => true, 'italic' => true]], |
| 109 | + ['Impact.ttf', ['name' => 'Impact']], |
| 110 | + 'no bold impact' => ['Impact.ttf', ['name' => 'Impact', 'bold' => true]], |
| 111 | + 'no italic impact' => ['Impact.ttf', ['name' => 'Impact', 'italic' => true]], |
| 112 | + 'no bold italic impact' => ['Impact.ttf', ['name' => 'Impact', 'bold' => true, 'italic' => true]], |
| 113 | + ['Tahoma.ttf', ['name' => 'Tahoma']], |
| 114 | + ['Tahoma Bold.ttf', ['name' => 'Tahoma', 'bold' => true]], |
| 115 | + 'no italic tahoma' => ['Tahoma.ttf', ['name' => 'Tahoma', 'italic' => true]], |
| 116 | + 'no bold italic tahoma' => ['Tahoma Bold.ttf', ['name' => 'Tahoma', 'bold' => true, 'italic' => true]], |
| 117 | + 'Times New Roman not in directory for this test' => ['exception', ['name' => 'Times New Roman']], |
| 118 | + ['Extra Font.ttf', ['name' => 'Extra Font']], |
| 119 | + ['Extra Font Bold.ttf', ['name' => 'Extra Font', 'bold' => true]], |
| 120 | + ['Extra Font Italic.ttf', ['name' => 'Extra Font', 'italic' => true]], |
| 121 | + ['Extra Font Bold Italic.ttf', ['name' => 'Extra Font', 'bold' => true, 'italic' => true]], |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @dataProvider providerOverride |
| 127 | + */ |
| 128 | + public function testOverrideFilenames(string $expected, array $fontArray): void |
| 129 | + { |
| 130 | + Font::setTrueTypeFontPath(self::DEFAULT_DIRECTORY); |
| 131 | + Font::setExtraFontArray([ |
| 132 | + 'Arial' => [ |
| 133 | + 'x' => 'extrafont.ttf', |
| 134 | + 'xb' => 'extrafontbd.ttf', |
| 135 | + 'xi' => 'extrafonti.ttf', |
| 136 | + 'xbi' => 'extrafontbi.ttf', |
| 137 | + ], |
| 138 | + ]); |
| 139 | + $font = (new StyleFont())->applyFromArray($fontArray); |
| 140 | + $result = Font::getTrueTypeFontFileFromFont($font); |
| 141 | + self::assertSame($expected, basename($result)); |
| 142 | + } |
| 143 | + |
| 144 | + public function providerOverride(): array |
| 145 | + { |
| 146 | + return [ |
| 147 | + ['extrafont.ttf', ['name' => 'Arial']], |
| 148 | + ['extrafontbd.ttf', ['name' => 'Arial', 'bold' => true]], |
| 149 | + ['extrafonti.ttf', ['name' => 'Arial', 'italic' => true]], |
| 150 | + ['extrafontbi.ttf', ['name' => 'Arial', 'bold' => true, 'italic' => true]], |
| 151 | + ['cour.ttf', ['name' => 'Courier New']], |
| 152 | + ]; |
| 153 | + } |
| 154 | +} |
0 commit comments