|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Generators; |
| 4 | + |
| 5 | +use Blueprint\Blueprint; |
| 6 | +use Blueprint\Generators\SeederGenerator; |
| 7 | +use Carbon\Carbon; |
| 8 | +use Illuminate\Support\Facades\App; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * @see SeederGenerator |
| 13 | + */ |
| 14 | +class SeederGeneratorTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Blueprint |
| 18 | + */ |
| 19 | + private $blueprint; |
| 20 | + |
| 21 | + /** @var SeederGenerator */ |
| 22 | + private $subject; |
| 23 | + |
| 24 | + |
| 25 | + private $files; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->files = \Mockery::mock(); |
| 32 | + |
| 33 | + $this->subject = new SeederGenerator($this->files); |
| 34 | + |
| 35 | + $this->blueprint = new Blueprint(); |
| 36 | + $this->blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer()); |
| 37 | + $this->blueprint->registerLexer(new \Blueprint\Lexers\SeederLexer()); |
| 38 | + $this->blueprint->registerGenerator($this->subject); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @test |
| 43 | + */ |
| 44 | + public function output_generates_nothing_for_empty_tree() |
| 45 | + { |
| 46 | + $this->files->shouldNotHaveReceived('put'); |
| 47 | + |
| 48 | + $this->assertEquals([], $this->subject->output(['seeders' => []])); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @test |
| 53 | + */ |
| 54 | + public function output_generates_seeders() |
| 55 | + { |
| 56 | + $this->files->expects('stub') |
| 57 | + ->with('seeder.stub') |
| 58 | + ->andReturn(file_get_contents('stubs/seeder.stub')); |
| 59 | + |
| 60 | + $this->files->expects('put') |
| 61 | + ->with('database/seeds/PostSeeder.php', $this->fixture('seeders/PostSeeder.php')); |
| 62 | + $this->files->expects('put') |
| 63 | + ->with('database/seeds/CommentSeeder.php', $this->fixture('seeders/CommentSeeder.php')); |
| 64 | + |
| 65 | + $tokens = $this->blueprint->parse($this->fixture('definitions/seeders.bp')); |
| 66 | + $tree = $this->blueprint->analyze($tokens); |
| 67 | + |
| 68 | + $this->assertEquals(['created' => ['database/seeds/PostSeeder.php', 'database/seeds/CommentSeeder.php']], $this->subject->output($tree)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @test |
| 73 | + */ |
| 74 | + public function output_generates_seeders_from_traced_models() |
| 75 | + { |
| 76 | + $this->files->expects('stub') |
| 77 | + ->with('seeder.stub') |
| 78 | + ->andReturn(file_get_contents('stubs/seeder.stub')); |
| 79 | + |
| 80 | + $this->files->expects('put') |
| 81 | + ->with('database/seeds/PostSeeder.php', $this->fixture('seeders/PostSeeder.php')); |
| 82 | + $this->files->expects('put') |
| 83 | + ->with('database/seeds/CommentSeeder.php', $this->fixture('seeders/CommentSeeder.php')); |
| 84 | + |
| 85 | + $tokens = $this->blueprint->parse($this->fixture('definitions/seeders.bp')); |
| 86 | + $tree = $this->blueprint->analyze($tokens); |
| 87 | + $tree['cache'] = $tree['models']; |
| 88 | + unset($tree['models']); |
| 89 | + |
| 90 | + $this->assertEquals(['created' => ['database/seeds/PostSeeder.php', 'database/seeds/CommentSeeder.php']], $this->subject->output($tree)); |
| 91 | + } |
| 92 | +} |
0 commit comments