|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests; |
| 6 | + |
| 7 | +use BladeUI\Icons\Generation\IconGenerator; |
| 8 | +use Illuminate\Filesystem\Filesystem; |
| 9 | +use SplFileInfo; |
| 10 | + |
| 11 | +class IconGeneratorTest extends TestCase |
| 12 | +{ |
| 13 | + const RESULT_DIR = __DIR__.'/fixtures/tmp'; |
| 14 | + |
| 15 | + private function clearResultsDirectory(): void |
| 16 | + { |
| 17 | + $fs = new Filesystem(); |
| 18 | + |
| 19 | + if ($fs->isDirectory(static::RESULT_DIR)) { |
| 20 | + $fs->deleteDirectory(static::RESULT_DIR); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /** @test */ |
| 25 | + public function it_can_generate_icon_sets() |
| 26 | + { |
| 27 | + IconGenerator::create([ |
| 28 | + [ |
| 29 | + 'source' => __DIR__.'/resources/svg', |
| 30 | + 'destination' => static::RESULT_DIR.'/primary', |
| 31 | + ], |
| 32 | + [ |
| 33 | + 'source' => __DIR__.'/resources/svg/solid', |
| 34 | + 'destination' => static::RESULT_DIR.'/solid', |
| 35 | + ], |
| 36 | + ])->generate(); |
| 37 | + |
| 38 | + $this->assertDirectoryExists(static::RESULT_DIR.'/primary'); |
| 39 | + $this->assertDirectoryExists(static::RESULT_DIR.'/solid'); |
| 40 | + $this->assertFileDoesNotExist(static::RESULT_DIR.'/primary/invalid-extension.txt'); |
| 41 | + $this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg'); |
| 42 | + $this->assertFileExists(static::RESULT_DIR.'/solid/camera.svg'); |
| 43 | + |
| 44 | + $this->clearResultsDirectory(); |
| 45 | + } |
| 46 | + |
| 47 | + /** @test */ |
| 48 | + public function it_will_clear_icons_before_generation_with_safe_mode_off() |
| 49 | + { |
| 50 | + // Run once to generate the "OLD" icons |
| 51 | + IconGenerator::create([ |
| 52 | + [ |
| 53 | + 'source' => __DIR__.'/resources/svg', |
| 54 | + 'destination' => static::RESULT_DIR.'/primary', |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'source' => __DIR__.'/resources/svg/solid', |
| 58 | + 'destination' => static::RESULT_DIR.'/solid', |
| 59 | + ], |
| 60 | + ])->generate(); |
| 61 | + |
| 62 | + $this->assertDirectoryExists(static::RESULT_DIR.'/primary'); |
| 63 | + $this->assertDirectoryExists(static::RESULT_DIR.'/solid'); |
| 64 | + $this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg'); |
| 65 | + $this->assertFileExists(static::RESULT_DIR.'/solid/camera.svg'); |
| 66 | + |
| 67 | + // Manually insert an "OLD ICON" that's been "removed". |
| 68 | + file_put_contents(static::RESULT_DIR.'/primary/cold-beans.svg', 'COOL BEANS!'); |
| 69 | + $this->assertFileExists(static::RESULT_DIR.'/primary/cold-beans.svg'); |
| 70 | + |
| 71 | + // Regenerate with Safe mode ON to verify "old" icons will be kept... |
| 72 | + IconGenerator::create([ |
| 73 | + [ |
| 74 | + 'source' => __DIR__.'/resources/svg', |
| 75 | + 'destination' => static::RESULT_DIR.'/primary', |
| 76 | + 'safe' => true, |
| 77 | + ], |
| 78 | + [ |
| 79 | + 'source' => __DIR__.'/resources/svg/solid', |
| 80 | + 'destination' => static::RESULT_DIR.'/solid', |
| 81 | + 'safe' => true, |
| 82 | + ], |
| 83 | + ])->generate(); |
| 84 | + |
| 85 | + $this->assertFileExists(static::RESULT_DIR.'/primary/cold-beans.svg'); |
| 86 | + |
| 87 | + // Regenerate the icons with safe feature enabled. |
| 88 | + IconGenerator::create([ |
| 89 | + [ |
| 90 | + 'source' => __DIR__.'/resources/svg', |
| 91 | + 'destination' => static::RESULT_DIR.'/primary', |
| 92 | + ], |
| 93 | + [ |
| 94 | + 'source' => __DIR__.'/resources/svg/solid', |
| 95 | + 'destination' => static::RESULT_DIR.'/solid', |
| 96 | + ], |
| 97 | + ])->generate(); |
| 98 | + |
| 99 | + $this->assertFileDoesNotExist(static::RESULT_DIR.'/primary/cold-beans.svg'); |
| 100 | + |
| 101 | + $this->clearResultsDirectory(); |
| 102 | + } |
| 103 | + |
| 104 | + /** @test */ |
| 105 | + public function it_can_use_generator_hooks() |
| 106 | + { |
| 107 | + $comment = '<!-- ICONS TEST --->'.PHP_EOL; |
| 108 | + |
| 109 | + IconGenerator::create([ |
| 110 | + [ |
| 111 | + 'source' => __DIR__.'/resources/svg', |
| 112 | + 'destination' => static::RESULT_DIR.'/primary', |
| 113 | + 'after' => static function ( |
| 114 | + string $tempFilepath, |
| 115 | + array $iconSet, |
| 116 | + SplFileInfo $file |
| 117 | + ) use ($comment) { |
| 118 | + $fileContents = file_get_contents($tempFilepath); |
| 119 | + file_put_contents($tempFilepath, $comment.$fileContents); |
| 120 | + }, |
| 121 | + ], |
| 122 | + ])->generate(); |
| 123 | + |
| 124 | + $this->assertDirectoryExists(static::RESULT_DIR.'/primary'); |
| 125 | + $this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg'); |
| 126 | + |
| 127 | + $iconContent = file_get_contents(static::RESULT_DIR.'/primary/camera.svg'); |
| 128 | + $this->assertStringStartsWith($comment, $iconContent); |
| 129 | + |
| 130 | + $this->clearResultsDirectory(); |
| 131 | + } |
| 132 | + |
| 133 | + /** @test */ |
| 134 | + public function it_will_remove_and_apply_prefixes() |
| 135 | + { |
| 136 | + IconGenerator::create([ |
| 137 | + [ |
| 138 | + 'source' => __DIR__.'/resources/svg', |
| 139 | + 'destination' => static::RESULT_DIR.'/primary', |
| 140 | + 'input-prefix' => 'zondicon-', |
| 141 | + 'output-prefix' => 'blade-', |
| 142 | + ], |
| 143 | + ])->generate(); |
| 144 | + |
| 145 | + $this->assertDirectoryExists(static::RESULT_DIR.'/primary'); |
| 146 | + $this->assertFileExists(static::RESULT_DIR.'/primary/blade-flag.svg'); |
| 147 | + $this->assertFileExists(static::RESULT_DIR.'/primary/blade-camera.svg'); |
| 148 | + $this->assertFileExists(static::RESULT_DIR.'/primary/blade-foo-camera.svg'); |
| 149 | + |
| 150 | + $this->clearResultsDirectory(); |
| 151 | + } |
| 152 | + |
| 153 | + /** @test */ |
| 154 | + public function it_will_remove_and_apply_suffixes() |
| 155 | + { |
| 156 | + IconGenerator::create([ |
| 157 | + [ |
| 158 | + 'source' => __DIR__.'/resources/svg', |
| 159 | + 'destination' => static::RESULT_DIR.'/primary', |
| 160 | + 'input-suffix' => '-camera', |
| 161 | + 'output-suffix' => '-wonky', |
| 162 | + ], |
| 163 | + ])->generate(); |
| 164 | + |
| 165 | + $this->assertDirectoryExists(static::RESULT_DIR.'/primary'); |
| 166 | + $this->assertFileExists(static::RESULT_DIR.'/primary/zondicon-flag-wonky.svg'); |
| 167 | + $this->assertFileExists(static::RESULT_DIR.'/primary/camera-wonky.svg'); |
| 168 | + $this->assertFileExists(static::RESULT_DIR.'/primary/foo-wonky.svg'); |
| 169 | + |
| 170 | + $this->clearResultsDirectory(); |
| 171 | + } |
| 172 | +} |
0 commit comments