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 23, 2022
1 parent 9a6ce1e commit 21916a5
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 @@ -548,7 +548,7 @@ public function rollback($environment, $target = null, $force = false, $targetMu
*/
public function seed($environment, $seed = null)
{
$seeds = $this->getSeeds();
$seeds = $this->getSeeds($environment);

if ($seed === null) {
// run all seeders
Expand Down Expand Up @@ -862,10 +862,11 @@ protected function orderSeedsByDependencies(array $seeds)
/**
* Gets an array of database seeders.
*
* @param string $environment Environment
* @throws \InvalidArgumentException
* @return \Phinx\Seed\AbstractSeed[]
*/
public function getSeeds()
public function getSeeds(string $environment)
{
if ($this->seeds === null) {
$phpFiles = $this->getSeedFiles();
Expand Down Expand Up @@ -902,6 +903,7 @@ public function getSeeds()
} 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 @@ -63,6 +68,24 @@ public function getDependencies()
return [];
}

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

return $this;
}

/**
* @inheritDoc
*/
public function getEnvironment()
{
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 @@ -35,6 +35,20 @@ interface SeedInterface
*/
public function run();

/**
* Sets the environment.
*
* @return \Phinx\Seed\SeedInterface
*/
public function setEnvironment(string $environment);

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

/**
* 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 @@ -5459,7 +5459,7 @@ public function testOrderSeeds()
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 @@ -5474,7 +5474,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 21916a5

Please sign in to comment.