Skip to content

Commit

Permalink
Merge pull request #50 from DavidMikeSimon/mysql_fixture_load
Browse files Browse the repository at this point in the history
Mysql fixture loading
  • Loading branch information
cmac1000 committed Nov 26, 2014
2 parents eccdb78 + 49f6ab3 commit e8a336c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#generally copied from https://github.com/nelmio/NelmioApiDocBundle/blob/master/.travis.yml
notifications:
webhooks: http://ac-dev-bishop.herokuapp.com/bishop/travisci/general
on_start: true

language: php

Expand Down
6 changes: 6 additions & 0 deletions Command/LoadFixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getApplication()->getKernel()->getContainer();

if ($container->get('kernel')->getEnvironment() == 'prod') {
throw new \RuntimeException(
"You may not load fixture data into a production environment"
);
}

$cls = $input->getArgument('fixtureClass');
if (is_null($cls)) {
$cls = $container->getParameter('ac_web_services.default_fixture_class');
Expand Down
50 changes: 48 additions & 2 deletions Fixture/CachedSqliteFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SqliteDriver;
use Doctrine\DBAL\Driver\PDOMySql\Driver as MySqlDriver;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\SqliteSchemaManager;
use Doctrine\DBAL\Migrations\Version;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

use \Mockery as m;
use \Functional as F;

abstract class CachedSqliteFixture extends CachedFixture
{
Expand All @@ -24,8 +27,18 @@ final protected function loadImpl($container)
{
$mainEM = $container->get('doctrine')->getManager();
$conn = $mainEM->getConnection();
if (!$conn->getDatabasePlatform() instanceof SqlitePlatform) {
throw new \RuntimeException("Cannot load sqlite fixture into non-sqlite database");
$platform = $conn->getDatabasePlatform();
if ($platform instanceof MySqlPlatform) {
print "\nAbout to overwrite MySQL database! If you're sure, type \"yes\":\n";
$line = fgets(STDIN);
if ($line == "yes\n") {
return $this->overwriteMysqlData($mainEM);
} else {
print "Fixture load cancelled.\n";
return;
}
} elseif (!($platform instanceof SqlitePlatform)) {
throw new \RuntimeException("Cannot load fixture into this SQL database type");
}

if (!is_dir(".tmp")) {
Expand All @@ -50,6 +63,39 @@ final protected function getFixtureObjectManager()
return $this->templateEM;
}

private function overwriteMysqlData($mainEM)
{
$fileMigs = F\map($this->migrationCodeFiles(), function($filename) {
if (preg_match("#Version(\d+).php$#", $filename, $matches)) {
return $matches[1];
} else {
throw new \RuntimeException("Invalid migration file $filename");
}
});

$conn = $mainEM->getConnection();
$dbMigs = F\pluck($conn->fetchAll("SELECT * FROM migration_versions"), 'version');
sort($dbMigs);

if ($fileMigs !== $dbMigs) {
throw new \RuntimeException("MySQL database needs to be migrated first");
}

$conn->query('SET FOREIGN_KEY_CHECKS=0');
$tables = F\map($conn->fetchAll("SHOW TABLES"), function ($row) {
return F\first($row);
});
foreach ($tables as $tableName) {
if ($tableName == 'migration_versions') { continue; }
$conn->executeUpdate($conn->getDatabasePlatform()->getTruncateTableSql($tableName));
}
$conn->query('SET FOREIGN_KEY_CHECKS=1');

$this->templateEM = $mainEM;
$this->execFixture();
$this->templateEM = null;
}

private function setupSchemaTemplate()
{
$schemaSource = "";
Expand Down
37 changes: 21 additions & 16 deletions TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,36 @@ protected function callApi($method, $uri, $options = [])
$client->request($method, $uri, $params, $files, $server, $content, $changeHist);
$response = $client->getResponse();

if (!isset($options['expectedCode'])) {
if (!array_key_exists('expectedCode', $options)) {
$options['expectedCode'] = 200;
}
if (!is_null($options['expectedCode'])) {
if ($response->getStatusCode() != $options['expectedCode']) {
$msg = "Expected status code " . $options['expectedCode'] .
", got " . $response->getStatusCode();
", got " . $response->getStatusCode() . ".\n";
if ($response->headers->get('Content-Type') == "application/json") {
$msg .= ". JSON content of invalid response:\n";
$content = json_decode($response->getContent(), true);

# Clean up the stack trace if there is one
if (isset($content['exception']) && isset($content['exception']['trace'])) {
$content['exception']['trace'] =
$this->cleanTrace($content['exception']['trace']);
}

$result = var_export($content, true);
if (strlen($result) > 20*1024) {
$result = substr($result, 0, 20*1024);
$result .= "\n.......\n.......";
if (is_null($content)) {
$msg .= "Response content (unparseable JSON):\n$result";
} else {
$msg .= "JSON content of invalid response:\n";
# Clean up the stack trace if there is one
if (isset($content['exception']) && isset($content['exception']['trace'])) {
$content['exception']['trace'] =
$this->cleanTrace($content['exception']['trace']);
}

$result = var_export($content, true);
if (strlen($result) > 20*1024) {
$result = substr($result, 0, 20*1024);
$result .= "\n.......\n.......";
}
$msg .= "$result\n";
}
$msg .= "$result\n";
} else {
$msg .= "Response content (first 16K):\n" . substr($result, 0, 1024*16);
}
$this->fail($msg);
$this->fail(trim($msg));
}
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"jms/serializer": "~0.15.0",
"willdurand/negotiation": "~1.2",
"mockery/mockery": "~0.9",
"fzaninotto/faker": "~1.3"
"fzaninotto/faker": "~1.3",
"lstrojny/functional-php": "*@dev"
},
"require-dev": {
"symfony/symfony": "~2.2",
Expand Down

0 comments on commit e8a336c

Please sign in to comment.