Last active
December 15, 2015 05:49
-
-
Save themouette/5212341 to your computer and use it in GitHub Desktop.
A base test cases for symfony2 projects with a sqlite dump replacement. Used on a project with huge fixtures, sqlite file copy is the fastest that could be done. sh generate sqlite dump from mysql. WARNING: table fields order is important for conversion.
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\ApplicationBundle\Tests\Utils; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseTestCase; | |
class WebTestCase extends BaseTestCase | |
{ | |
protected $databaseResetedOnce = false; | |
protected function ignoreDeprecated() | |
{ | |
$level = ini_get('error_reporting'); | |
error_reporting($level ^ E_USER_DEPRECATED); | |
} | |
protected function resetErrorReporting() | |
{ | |
$level = ini_get('error_reporting'); | |
error_reporting($level | E_USER_DEPRECATED); | |
} | |
protected function tearDown() | |
{ | |
parent::tearDown(); | |
$this->resetErrorReporting(); | |
} | |
/** | |
* returns an authenticated client | |
*/ | |
protected function getAuthenticatedClient($username = 'admin', $password = 'admin') | |
{ | |
$client = static::createClient(); | |
$crawler = $client->request('GET', '/login'); | |
$form = $crawler->selectButton('Connexion')->form(); | |
$form['_username'] = 'admin'; | |
$form['_password'] = 'admin'; | |
$client->submit($form); | |
// ensure it is a redirection | |
$response = $client->getResponse(); | |
// go to homepage | |
$crawler = $client->followRedirect(); | |
// if there is an error | |
$this->assertTrue( | |
$response->isRedirect('/') || $response->isRedirect('http://localhost/'), | |
$client->getResponse()->getContent()); | |
return $client; | |
} | |
protected function getBootedKernel() | |
{ | |
if (!self::$kernel) { | |
self::$kernel = self::createKernel(); | |
} | |
self::$kernel->boot(); | |
return self::$kernel; | |
} | |
protected function getService( $name, $kernel = null ) | |
{ | |
return $this->getBootedKernel()->getContainer()->get( $name ); | |
} | |
protected function hasService( $name, $kernel = null ) | |
{ | |
return $this->getBootedKernel()->getContainer()->has( $name ); | |
} | |
protected function getEm() | |
{ | |
return $this->getService( 'doctrine.orm.entity_manager' ); | |
} | |
public function getSqliteDumpFile() | |
{ | |
return __DIR__ . '/../../../../../dump.db'; | |
} | |
public function resetDatabase() | |
{ | |
$dumpfile = $this->getSqliteDumpFile(); | |
$dest = '/tmp/database.db'; | |
if (!file_exists($dumpfile)) { | |
$this->markTestSkipped(sprintf('original dump file "%s" is required', $dumpfile)); | |
} | |
$this->databaseResetedOnce = true; | |
if (!file_exists($dest) || filemtime($dest) !== filemtime($dumpfile)) { | |
copy($dumpfile, $dest); | |
touch($dest, filemtime($dumpfile)); | |
} | |
return $this; | |
} | |
public function ensureDatabase() | |
{ | |
if (!$this->databaseResetedOnce) { | |
$this->resetDatabase(); | |
} | |
return $this; | |
} | |
} |
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
#!/usr/bin/env bash | |
if [ -x /opt/local/bin/greadlink ] | |
then | |
ROOT_PATH=$(dirname $(dirname $(greadlink -f $0))) | |
else | |
ROOT_PATH=$(dirname $(dirname $(readlink -f $0))) | |
fi | |
DBNAME='my-db' | |
DBUSER='my-user' | |
DBPASS='p4$$w0rd' | |
OLD_PATH=`pwd` | |
cd $ROOT_PATH | |
if [ "x$1" == "x" ]; then | |
echo "Usage: $0 <dumpname>" | |
exit | |
fi | |
if [ ! -f "$1" ]; then | |
echo "generate dump"; | |
mysqldump -u $DBUSER -h localhost -p$DBPASS --compatible=ansi --skip-opt $DBNAME > $1 | |
fi | |
if [ ! -f "$1.db" ]; then | |
echo "create datastructure"; | |
./app/console doctrine:schema:create --env=test --dump-sql | sqlite3 $1.db | |
fi | |
echo "Convert dump"; | |
cat $1 | | |
grep -v ' KEY "' | | |
grep -v ' UNIQUE KEY "' | | |
grep -v ' PRIMARY KEY ' | | |
sed -e '/^SET/d' \ | |
-e 's/ unsigned / /g' \ | |
-e 's/ auto_increment/ primary key autoincrement/g' \ | |
-e 's/ smallint([0-9]*) / integer /g' \ | |
-e 's/ tinyint([0-9]*) / integer /g' \ | |
-e 's/ int([0-9]*) / integer /g' \ | |
-e 's/ character set [^ ]* / /g' \ | |
-e 's/ enum([^)]*) / varchar(255) /g' \ | |
-e 's/ on update [^,]*//g' \ | |
-e 's/\\r\\n/\\n/g' \ | |
-e 's/\\"/"/g' | | |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' | | |
perl -pe ' | |
if (/^(INSERT.+?)\(/) { | |
$a=$1; | |
s/\\'\''/'\'\''/g; | |
s/\\n/\n/g; | |
s/\),\(/\);\n$a\(/g; | |
} | |
' > $1.sql | |
echo "Insert into sqlite"; | |
cat $1.sql | sqlite3 $1.db 2> $1.err | |
ERRORS=`cat $1.err | wc -l` | |
if [ $ERRORS == 0 ]; then | |
echo "Conversion completed without error. Output file: $1.db" | |
rm $1.sql | |
rm $1.err | |
else | |
echo "There were errors during conversion. Please review $1.err and $1.sql for details." | |
fi | |
cd $OLD_PATH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment