|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RossBearman\Sqids\Tests\Feature\Console\Commands; |
| 6 | + |
| 7 | +use Illuminate\Support\Facades\Artisan; |
| 8 | +use Illuminate\Support\Facades\Config; |
| 9 | +use Orchestra\Testbench\Concerns\WithWorkbench; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use PHPUnit\Framework\Attributes\Test; |
| 12 | +use RossBearman\Sqids\Console\Commands\AlphabetCommand; |
| 13 | +use RossBearman\Sqids\Facades\Sqids; |
| 14 | +use RossBearman\Sqids\Tests\TestCase; |
| 15 | + |
| 16 | +final class AlphabetCommandTest extends TestCase |
| 17 | +{ |
| 18 | + use WithWorkbench; |
| 19 | + |
| 20 | + #[Test] |
| 21 | + public function it_can_generate_consistent_alphabets_for_models(): void |
| 22 | + { |
| 23 | + $status = Artisan::call(AlphabetCommand::class, [ |
| 24 | + 'models' => ['App\Models\Customer', 'App\Models\Order'], |
| 25 | + '--plain' => true, |
| 26 | + ]); |
| 27 | + $this->assertEquals(0, $status); |
| 28 | + |
| 29 | + $alphabets = collect(explode("\n", Artisan::output())) |
| 30 | + ->filter() |
| 31 | + ->map(function ($pair) { |
| 32 | + [$model, $alphabet] = explode(': ', $pair); |
| 33 | + |
| 34 | + return ['name' => $model, 'alphabet' => $alphabet]; |
| 35 | + }); |
| 36 | + |
| 37 | + $status = Artisan::call(AlphabetCommand::class, [ |
| 38 | + 'models' => ['App\Models\Customer', 'App\Models\Order'], |
| 39 | + '--plain' => true, |
| 40 | + ]); |
| 41 | + $this->assertEquals(0, $status); |
| 42 | + |
| 43 | + $output = Artisan::output(); |
| 44 | + |
| 45 | + foreach ($alphabets as $model) { |
| 46 | + $this->assertStringContainsString("{$model['name']}: {$model['alphabet']}", $output); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + #[Test] |
| 51 | + public function it_outputs_shuffled_alphabet_with_no_input(): void |
| 52 | + { |
| 53 | + $alphabet = Config::get('sqids.alphabet'); |
| 54 | + |
| 55 | + Artisan::call(AlphabetCommand::class); |
| 56 | + |
| 57 | + $output = rtrim(Artisan::output()); |
| 58 | + |
| 59 | + $this->assertNotSame($alphabet, $output); |
| 60 | + $this->assertSame(strlen($alphabet), strlen($output)); |
| 61 | + |
| 62 | + foreach (str_split($alphabet) as $element) { |
| 63 | + $this->assertStringContainsString($element, $output); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[Test] |
| 68 | + public function it_outputs_consistently_shuffled_alphabet_with_key(): void |
| 69 | + { |
| 70 | + Artisan::call(AlphabetCommand::class, ['--key' => 'test-key']); |
| 71 | + $outputOne = Artisan::output(); |
| 72 | + |
| 73 | + Artisan::call(AlphabetCommand::class, ['--key' => 'test-key']); |
| 74 | + $outputTwo = Artisan::output(); |
| 75 | + |
| 76 | + $this->assertSame($outputOne, $outputTwo); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + #[DataProvider('modelProvider')] |
| 81 | + public function it_prints_config_instructions_for_models(string $model, string $envKey): void |
| 82 | + { |
| 83 | + $alphabet = Sqids::fromClass($model)->alphabet->value; |
| 84 | + |
| 85 | + $status = Artisan::call(AlphabetCommand::class, [ |
| 86 | + 'models' => [$model], |
| 87 | + ]); |
| 88 | + $this->assertEquals(0, $status); |
| 89 | + |
| 90 | + $output = Artisan::output(); |
| 91 | + $this->assertStringContainsString("{$model}::class => env('{$envKey}')", $output); |
| 92 | + $this->assertStringContainsString("{$envKey}={$alphabet}", $output); |
| 93 | + } |
| 94 | + |
| 95 | + public static function modelProvider(): array |
| 96 | + { |
| 97 | + return [ |
| 98 | + ['Model', 'SQIDS_ALPHABET_MODEL'], |
| 99 | + ['\\Model', 'SQIDS_ALPHABET_MODEL'], |
| 100 | + ['App\\Model', 'SQIDS_ALPHABET_MODEL'], |
| 101 | + ['\\App\\Model', 'SQIDS_ALPHABET_MODEL'], |
| 102 | + ['App\\Models\\Model', 'SQIDS_ALPHABET_MODEL'], |
| 103 | + ['App\\Models\\Model\\Model', 'SQIDS_ALPHABET_MODEL'], |
| 104 | + ['ThreeWordModel', 'SQIDS_ALPHABET_THREE_WORD_MODEL'], |
| 105 | + ['App\\Models\\ThreeWordModel', 'SQIDS_ALPHABET_THREE_WORD_MODEL'], |
| 106 | + ]; |
| 107 | + } |
| 108 | +} |
0 commit comments