Skip to content

Commit

Permalink
Pass environment to seed from manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb committed Aug 24, 2022
1 parent 18bf93e commit fb5d54d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Phinx/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public function rollback(string $environment, $target = null, bool $force = fals
*/
public function seed(string $environment, ?string $seed = null): void
{
$seeds = $this->getSeeds();
$seeds = $this->getSeeds($environment);

if ($seed === null) {
// run all seeders
Expand Down Expand Up @@ -918,10 +918,11 @@ protected function orderSeedsByDependencies(array $seeds): array
/**
* Gets an array of database seeders.
*
* @param string $environment Environment
* @throws \InvalidArgumentException
* @return \Phinx\Seed\SeedInterface[]
*/
public function getSeeds(): array
public function getSeeds(string $environment): array
{
if ($this->seeds === null) {
$phpFiles = $this->getSeedFiles();
Expand Down Expand Up @@ -958,6 +959,7 @@ public function getSeeds(): array
} else {
$seed = new $class();
}
$seed->setEnvironment($environment);
$input = $this->getInput();
if ($input !== null) {
$seed->setInput($input);
Expand Down
23 changes: 23 additions & 0 deletions src/Phinx/Seed/AbstractSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
abstract class AbstractSeed implements SeedInterface
{
/**
* @var string
*/
protected $environment;

/**
* @var \Phinx\Db\Adapter\AdapterInterface
*/
Expand Down Expand Up @@ -61,6 +66,24 @@ public function getDependencies(): array
return [];
}

/**
* @inheritDoc
*/
public function setEnvironment(string $environment)
{
$this->environment = $environment;

return $this;
}

/**
* @inheritDoc
*/
public function getEnvironment(): string
{
return $this->environment;
}

/**
* @inheritDoc
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Phinx/Seed/SeedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ public function run(): void;
*/
public function getDependencies(): array;

/**
* Sets the environment.
*
* @return $this
*/
public function setEnvironment(string $environment);

/**
* Gets the environment.
*
* @return string
*/
public function getEnvironment(): string;

/**
* Sets the database adapter.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Phinx/Migration/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5450,7 +5450,7 @@ public function testExecuteANonExistentSeedWorksAsExpectedWithMixedNamespace()

public function testOrderSeeds()
{
$seeds = array_values($this->manager->getSeeds());
$seeds = array_values($this->manager->getSeeds('mockenv'));
$this->assertInstanceOf('UserSeeder', $seeds[0]);
$this->assertInstanceOf('GSeeder', $seeds[1]);
$this->assertInstanceOf('PostSeeder', $seeds[2]);
Expand All @@ -5472,7 +5472,7 @@ public function testSeedWillNotBeExecuted()
public function testGettingInputObject()
{
$migrations = $this->manager->getMigrations('mockenv');
$seeds = $this->manager->getSeeds();
$seeds = $this->manager->getSeeds('mockenv');
$inputObject = $this->manager->getInput();
$this->assertInstanceOf('\Symfony\Component\Console\Input\InputInterface', $inputObject);

Expand All @@ -5487,7 +5487,7 @@ public function testGettingInputObject()
public function testGettingOutputObject()
{
$migrations = $this->manager->getMigrations('mockenv');
$seeds = $this->manager->getSeeds();
$seeds = $this->manager->getSeeds('mockenv');
$outputObject = $this->manager->getOutput();
$this->assertInstanceOf('\Symfony\Component\Console\Output\OutputInterface', $outputObject);

Expand Down

0 comments on commit fb5d54d

Please sign in to comment.