Created
April 8, 2016 08:21
-
-
Save cedricziel/84c948568ebb602a415cbe06cecb4f8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\InfraBundle\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Output\BufferedOutput; | |
use Symfony\Component\HttpFoundation\Response; | |
/** | |
* Executes migrations when the CLI is not in reach. | |
* @Route("/_endpoint/migrations", service="vendor_infra.migration.migrationcontroller") | |
* | |
* @package Vendor\InfraBundle\Controller | |
*/ | |
class MigrationController extends Controller | |
{ | |
/** | |
* Shows the current status of migrations applied | |
* @Route("/") | |
* @Method("GET") | |
* | |
* @return Response | |
*/ | |
public function statusAction() | |
{ | |
$application = $this->getConsoleApplication(); | |
$input = new ArrayInput( | |
[ | |
'command' => 'doctrine:migrations:status', | |
] | |
); | |
// You can use NullOutput() if you don't need the output | |
$output = new BufferedOutput(); | |
$application->run($input, $output); | |
// return the output, don't use if you used NullOutput() | |
$content = $output->fetch(); | |
// return new Response(""), if you used NullOutput() | |
return new Response($content); | |
} | |
/** | |
* Migrates the database | |
* @Method("POST") | |
* @Route("/migrate") | |
* | |
* @return Response | |
* @throws \Exception | |
*/ | |
public function migrateAction() | |
{ | |
$application = $this->getConsoleApplication(); | |
$input = new ArrayInput( | |
[ | |
'command' => 'doctrine:migrations:migrate', | |
] | |
); | |
// You can use NullOutput() if you don't need the output | |
$output = new BufferedOutput(); | |
$application->run($input, $output); | |
// return the output, don't use if you used NullOutput() | |
$content = $output->fetch(); | |
// return new Response(""), if you used NullOutput() | |
return new Response($content); | |
} | |
/** | |
* @return Application | |
*/ | |
private function getConsoleApplication() | |
{ | |
$kernel = $this->get('kernel'); | |
$application = new Application($kernel); | |
$application->setAutoExit(false); | |
return $application; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\InfraBundle\Tests\Controller; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
/** | |
* @package Vendor\InfraBundle\Tests\Controller | |
*/ | |
class MigrationControllerTest extends WebTestCase | |
{ | |
/** | |
* Health indicator requests from the platform should | |
* result in a HTTP Status 200 | |
* | |
* @covers Vendor\InfraBundle\Controller\MigrationController::statusAction | |
*/ | |
public function testMigrationStatusResponse() | |
{ | |
$client = static::createClient(); | |
/** @noinspection PhpUnusedLocalVariableInspection */ | |
$crawler = $client->request('GET', '/_endpoint/migrations/'); | |
$this->assertContains('Database Driver', $client->getResponse()->getContent()); | |
$this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
} | |
/** | |
* The application should be able to migrate te database from a HTTP Request | |
* | |
* @covers Vendor\InfraBundle\Controller\MigrationController::migrateAction | |
*/ | |
public function testMigrationMigrateRequest() | |
{ | |
$client = static::createClient(); | |
/** @noinspection PhpUnusedLocalVariableInspection */ | |
$crawler = $client->request('POST', '/_endpoint/migrations/migrate'); | |
$this->assertContains( | |
'Error Migration can only be executed safely on \'mysql\'', | |
$client->getResponse()->getContent() | |
); | |
$this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment