Skip to content

Commit e6f7076

Browse files
committed
refactor: clean up shouldRun implementation
- Remove unnecessary $shouldRunSeeder variable - Use continue for early return when seeder is skipped - Skip adding skipped seeders to $called array (matches migration behavior) - Add assertions to verify $called array tracking
1 parent 40da170 commit e6f7076

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/Illuminate/Database/Seeder.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,33 @@ public function call($class, $silent = false, array $parameters = [])
4949

5050
$name = get_class($seeder);
5151

52-
$shouldRunSeeder = $seeder->shouldRun();
53-
54-
if (! $shouldRunSeeder) {
52+
if (! $seeder->shouldRun()) {
5553
if ($silent === false && isset($this->command)) {
5654
(new TwoColumnDetail($this->command->getOutput()))
5755
->render($name, '<fg=yellow;options=bold>SKIPPED</>');
5856

5957
$this->command->getOutput()->writeln('');
6058
}
61-
} else {
62-
if ($silent === false && isset($this->command)) {
63-
(new TwoColumnDetail($this->command->getOutput()))
64-
->render($name, '<fg=yellow;options=bold>RUNNING</>');
65-
}
6659

67-
$startTime = microtime(true);
60+
continue;
61+
}
6862

69-
$seeder->__invoke($parameters);
63+
if ($silent === false && isset($this->command)) {
64+
(new TwoColumnDetail($this->command->getOutput()))
65+
->render($name, '<fg=yellow;options=bold>RUNNING</>');
66+
}
7067

71-
if ($silent === false && isset($this->command)) {
72-
$runTime = number_format((microtime(true) - $startTime) * 1000);
68+
$startTime = microtime(true);
7369

74-
(new TwoColumnDetail($this->command->getOutput()))
75-
->render($name, "<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>");
70+
$seeder->__invoke($parameters);
7671

77-
$this->command->getOutput()->writeln('');
78-
}
72+
if ($silent === false && isset($this->command)) {
73+
$runTime = number_format((microtime(true) - $startTime) * 1000);
74+
75+
(new TwoColumnDetail($this->command->getOutput()))
76+
->render($name, "<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>");
77+
78+
$this->command->getOutput()->writeln('');
7979
}
8080

8181
static::$called[] = $class;

tests/Database/DatabaseSeederTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public function testCallSkipsSeederWhenShouldRunReturnsFalse()
119119
$child->shouldNotReceive('__invoke');
120120

121121
$seeder->call(TestSkippedSeeder::class);
122+
123+
$this->assertNotContains(TestSkippedSeeder::class, TestSeeder::$called);
122124
}
123125

124126
public function testCallRunsSeederWhenShouldRunReturnsTrue()
@@ -137,6 +139,8 @@ public function testCallRunsSeederWhenShouldRunReturnsTrue()
137139
$child->shouldReceive('__invoke')->once();
138140

139141
$seeder->call(TestSeeder::class);
142+
143+
$this->assertContains(TestSeeder::class, TestSeeder::$called);
140144
}
141145

142146
public function testCallSilentlySkipsSeederWhenShouldRunReturnsFalse()
@@ -150,5 +154,7 @@ public function testCallSilentlySkipsSeederWhenShouldRunReturnsFalse()
150154
$child->shouldNotReceive('__invoke');
151155

152156
$seeder->callSilent(TestSkippedSeeder::class);
157+
158+
$this->assertNotContains(TestSkippedSeeder::class, TestSeeder::$called);
153159
}
154160
}

0 commit comments

Comments
 (0)