public function execute($args) { array_shift($args); // Remove the phinx command $a = new PhinxApplication(); $a->run(new ArgvInput($args)); }
public function __call($name, $arguments) { $config_path = Registry::get('config.dir.root') . '/app/Tygh/UpgradeCenter/Migrations/config.migrations.php'; switch ($name) { case 'migrate': $_SERVER['argv'] = array('phinx', 'migrate', '-c' . $config_path, '-edevelopment'); break; case 'rollback': $_SERVER['argv'] = array('phinx', 'rollback', '-c' . $config_path, '-edevelopment'); break; default: return false; } $output = new Output(); $output->setConfig($this->config); $output->setVerbosity(Output::VERBOSITY_VERBOSE); $app = new PhinxApplication('0.4.3'); AdapterFactory::instance()->registerAdapter('mysqli', '\\Tygh\\UpgradeCenter\\Phinx\\MysqliAdapter'); $app->setAutoExit(false); $app->setCatchExceptions(false); try { $exit_code = $app->run(null, $output); } catch (\Exception $e) { // Convert PDOException to DatabaseException if (class_exists('\\PDOException') && $e instanceof \PDOException) { throw new DatabaseException($e->getMessage(), $e->getCode(), $e); } else { throw $e; } } return $exit_code === 0; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessageRegExp /The file "(.*)" already exists/ */ public function testThrowsExceptionWhenConfigFilePresent() { touch(sys_get_temp_dir() . '/phinx.yml'); $application = new PhinxApplication('testing'); $application->add(new Init()); $command = $application->find('init'); $commandTester = new CommandTester($command); $commandTester->execute(array('command' => $command->getName(), 'path' => sys_get_temp_dir()), array('decorated' => false)); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessageRegExp /The file "(.*)" already exists/ */ public function testThrowsExceptionWhenConfigFilePresent() { touch(sys_get_temp_dir() . '/phinx.yml'); $application = new PhinxApplication('testing'); $application->add(new Init()); // setup dependencies $output = new StreamOutput(fopen('php://memory', 'a', false)); $command = $application->find('init'); $commandTester = new CommandTester($command); $commandTester->execute(array('command' => $command->getName(), 'path' => sys_get_temp_dir()), array('decorated' => false)); }
/** * @dataProvider provider */ public function testRun($command, $result) { $app = new \Phinx\Console\PhinxApplication('testing'); $app->setAutoExit(false); // Set autoExit to false when testing $app->setCatchExceptions(false); $appTester = new ApplicationTester($app); $appTester->run(array('command' => $command)); $stream = $appTester->getOutput()->getStream(); rewind($stream); $this->assertRegExp($result, stream_get_contents($stream)); }
protected function migrate() { $input = new ArgvInput(['phinx', 'migrate', '--environment=memory']); $output = new NullOutput(); $phinx = new PhinxApplication(); $phinx->setAutoExit(false); $phinx->run($input, $output); /** @var Migrate $migrateCommand */ $migrateCommand = $phinx->get('migrate'); $adapter = $migrateCommand->getManager()->getEnvironment('memory')->getAdapter(); /** @var PDO $pdo */ $this->phinxPdo = $adapter->getConnection(); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The seed class name "badseedname" is invalid. Please use CamelCase format */ public function testExecuteWithInvalidClassName() { $application = new PhinxApplication('testing'); $application->add(new SeedCreate()); /** @var SeedCreate $command */ $command = $application->find('seed:create'); // mock the manager class /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ $managerStub = $this->getMock('\\Phinx\\Migration\\Manager', array(), array($this->config, $this->input, $this->output)); $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); $commandTester->execute(array('command' => $command->getName(), 'name' => 'badseedname'), array('decorated' => false)); }
public function testDatabaseNameSpecified() { $application = new PhinxApplication('testing'); $application->add(new Rollback()); /** @var Rollback $command */ $command = $application->find('rollback'); // mock the manager class /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ $managerStub = $this->getMock('\\Phinx\\Migration\\Manager', array(), array($this->config, $this->input, $this->output)); $managerStub->expects($this->once())->method('rollback'); $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); $commandTester->execute(array('command' => $command->getName()), array('decorated' => false)); $this->assertRegExp('/using database development/', $commandTester->getDisplay()); }
public function testFormatSpecified() { $application = new PhinxApplication('testing'); $application->add(new Status()); /** @var Status $command */ $command = $application->find('status'); // mock the manager class /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ $managerStub = $this->getMock('\\Phinx\\Migration\\Manager', array(), array($this->config, $this->input, $this->output)); $managerStub->expects($this->once())->method('printStatus')->will($this->returnValue(0)); $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); $return = $commandTester->execute(array('command' => $command->getName(), '--format' => 'json'), array('decorated' => false)); $this->assertEquals(0, $return); $this->assertRegExp('/using format json/', $commandTester->getDisplay()); }
public function __call($name, $arguments) { $config_path = Registry::get('config.dir.root') . '/app/Tygh/UpgradeCenter/Migrations/config.migrations.php'; switch ($name) { case 'migrate': $_SERVER['argv'] = array('phinx', 'migrate', '-c' . $config_path, '-edevelopment'); break; case 'rollback': $_SERVER['argv'] = array('phinx', 'rollback', '-c' . $config_path, '-edevelopment'); break; default: return false; } $output = new Output(); $output->setConfig($this->config); $app = new PhinxApplication('0.4.3'); AdapterFactory::instance()->registerAdapter('mysqli', '\\Tygh\\UpgradeCenter\\Phinx\\MysqliAdapter'); $app->setAutoExit(false); $app->run(null, $output); }
/** * Execute a command, capturing output and storing the exit code. * * @param array $command * @return string */ protected function executeRun(array $command) { // Output will be written to a temporary stream, so that it can be // collected after running the command. $stream = fopen('php://temp', 'w+'); // Execute the command, capturing the output in the temporary stream // and storing the exit code for debugging purposes. $this->exit_code = $this->app->doRun(new ArrayInput($command), new StreamOutput($stream)); // Get the output of the command and close the stream, which will // destroy the temporary file. $result = stream_get_contents($stream, -1, 0); fclose($stream); return $result; }
public function testExecuteMultipleSeeders() { $application = new PhinxApplication('testing'); $application->add(new SeedRun()); /** @var SeedRun $command */ $command = $application->find('seed:run'); // mock the manager class /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ $managerStub = $this->getMock('\\Phinx\\Migration\\Manager', array(), array($this->config, $this->input, $this->output)); $managerStub->expects($this->exactly(3))->method('seed')->withConsecutive(array($this->identicalTo('development'), $this->identicalTo('One')), array($this->identicalTo('development'), $this->identicalTo('Two')), array($this->identicalTo('development'), $this->identicalTo('Three'))); $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); $commandTester->execute(array('command' => $command->getName(), '--seed' => ['One', 'Two', 'Three']), array('decorated' => false)); $this->assertRegExp('/no environment specified/', $commandTester->getDisplay()); }
public static function migrateDatabase(Event $event = null, $testing = false) { if ($event) { // Use the event's IO $io = $event->getIO(); $arguments = $event->getArguments(); $testingArguments = array('testing', '--testing', '-t'); $testing = count(array_intersect($arguments, $testingArguments)) > 0; } else { // Create our own IO $input = new ArrayInput(array()); $output = new ConsoleOutput(); $helperSet = new HelperSet(array(new QuestionHelper())); $io = new ConsoleIO($input, $output, $helperSet); } try { $config = self::getDatabaseConfig($testing); } catch (\Exception $e) { $io->write("<bg=red>\n\n [WARNING] " . $e->getMessage() . ", the database won't be updated\n</>"); return; } // If the database doesn't exist, ask the user to create it and perform // the necessary migrations (unless the user didn't agree to // create the database) if (self::createDatabase($io, $config['host'], $config['username'], $config['password'], $config['database'])) { $io->write(''); // newline $arguments = array('migrate', '-e' => $testing ? 'test' : 'main'); $app = new PhinxApplication(); $app->doRun(new ArrayInput($arguments), new ConsoleOutput()); } }
/** * @param array $config * @param array $commandLine * @dataProvider provideSimpleTemplateGenerator */ public function testSimpleTemplateGeneratorsIsCorrectlyPopulated(array $config, array $commandLine) { $application = new PhinxApplication('testing'); $application->add(new Create()); /** @var Create $command $command */ $command = $application->find('create'); /** @var Manager $managerStub mock the manager class */ $managerStub = $this->getMock('\\Phinx\\Migration\\Manager', array(), array($this->config, $this->input, $this->output)); foreach ($config as $key => $value) { $this->config[$key] = $value; } $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); $commandLine = array_merge(array('command' => $command->getName()), $commandLine); $commandTester->execute($commandLine, array('decorated' => false)); // Get output. preg_match('`created (?P<MigrationFilename>.+(?P<Version>\\d{14}).*?)\\s`', $commandTester->getDisplay(), $match); // Was migration created? $this->assertFileExists($match['MigrationFilename'], 'Failed to create migration file from template generator'); // Get migration. $actualMigration = file_get_contents($match['MigrationFilename']); // Does the migration match our expectation? $expectedMigration = "useClassName Phinx\\Migration\\AbstractMigration / className {$commandLine['name']} / version {$match['Version']} / baseClassName AbstractMigration"; $this->assertSame($expectedMigration, $actualMigration, 'Failed to create migration file from template generator correctly.'); }
/** * @param array $config * @param array $commandLine * @dataProvider provideSimpleTemplateGenerator */ public function testSimpleTemplateGeneratorsIsCorrectlyPopulated(array $config, array $commandLine) { $application = new PhinxApplication('testing'); $application->add(new Create()); // setup dependencies $input = new ArrayInput([]); $output = new StreamOutput(fopen('php://memory', 'a', false)); /** @var Create $command $command */ $command = $application->find('create'); /** @var Manager $managerStub mock the manager class */ $managerStub = $this->getMock('\\Phinx\\Migration\\Manager', array(), array($this->config, $input, $output)); foreach ($config as $key => $value) { $this->config[$key] = $value; } $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); $commandLine = array_merge(array('command' => $command->getName()), $commandLine); $commandTester->execute($commandLine, array('decorated' => false)); // Get output. preg_match('`^using migration path (.*?)\\s+using migration base class (.*?)\\s+using template creation class (.*?)\\s+created (\\1.(\\d++)(.*?))\\s+`', $commandTester->getDisplay(), $match); // Was migration created? $this->assertFileExists($match[4], 'Failed to create migration file from template generator'); // Get migration. $actualMigration = file_get_contents($match[4]); // Does the migration match our expectation? $expectedMigration = "useClassName Phinx\\Migration\\AbstractMigration / className {$commandLine['name']} / version {$match[5]} / baseClassName AbstractMigration"; $this->assertSame($expectedMigration, $actualMigration, 'Failed to create migration file from template generator correctly.'); }