seedRepository = Mockery::mock(SeedRepository::class); $this->runner = new SeedRunner($this->seedRepository); }); it('skips seeder if already run', function () { $seeder = Mockery::mock(Seeder::class); $seeder->shouldReceive('getName') ->once() ->andReturn('TestSeeder'); $this->seedRepository->shouldReceive('hasRun') ->once() ->with('TestSeeder') ->andReturn(true); $this->seedRepository->shouldNotReceive('markAsRun'); $this->runner->run($seeder); }); it('runs seeder if not run yet', function () { $seeder = Mockery::mock(Seeder::class); $seeder->shouldReceive('getName') ->once() ->andReturn('TestSeeder'); $seeder->shouldReceive('getDescription') ->once() ->andReturn('Test description'); $seeder->shouldReceive('seed') ->once(); $this->seedRepository->shouldReceive('hasRun') ->once() ->with('TestSeeder') ->andReturn(false); $this->seedRepository->shouldReceive('markAsRun') ->once() ->with('TestSeeder', 'Test description'); $this->runner->run($seeder); }); it('throws exception if seeder fails', function () { $seeder = Mockery::mock(Seeder::class); $seeder->shouldReceive('getName') ->once() ->andReturn('TestSeeder'); $seeder->shouldReceive('seed') ->once() ->andThrow(new \RuntimeException('Seeder failed')); $this->seedRepository->shouldReceive('hasRun') ->once() ->with('TestSeeder') ->andReturn(false); $this->seedRepository->shouldNotReceive('markAsRun'); expect(fn () => $this->runner->run($seeder)) ->toThrow(\RuntimeException::class, 'Seeder failed'); }); it('runs multiple seeders', function () { $seeder1 = Mockery::mock(Seeder::class); $seeder1->shouldReceive('getName')->andReturn('Seeder1'); $seeder1->shouldReceive('getDescription')->andReturn('Description 1'); $seeder1->shouldReceive('seed'); $seeder2 = Mockery::mock(Seeder::class); $seeder2->shouldReceive('getName')->andReturn('Seeder2'); $seeder2->shouldReceive('getDescription')->andReturn('Description 2'); $seeder2->shouldReceive('seed'); $this->seedRepository->shouldReceive('hasRun') ->with('Seeder1') ->andReturn(false); $this->seedRepository->shouldReceive('hasRun') ->with('Seeder2') ->andReturn(false); $this->seedRepository->shouldReceive('markAsRun') ->with('Seeder1', 'Description 1'); $this->seedRepository->shouldReceive('markAsRun') ->with('Seeder2', 'Description 2'); $this->runner->runAll([$seeder1, $seeder2]); }); });