Skip to content

Commit

Permalink
add whitelist in phpunit
Browse files Browse the repository at this point in the history
add test case
fix few bugs
  • Loading branch information
songzijian committed Dec 2, 2016
1 parent 66ec342 commit 7d973d5
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 35 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
<file>./src/Parser/Parser.php</file>
</whitelist>
</filter>
</phpunit>
38 changes: 16 additions & 22 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,25 @@ protected function syncTable($sql)
$updateSqlList[] = "ADD PRIMARY KEY {$newCol}";
}
} elseif ($key == 'KEY') {
foreach ($newCol as $subKey => $subValue) {
if (!empty($oldCols['KEY'][$subKey])) {
if ($subValue != $oldCols['KEY'][$subKey]) {
$updateSqlList[] = "ADD INDEX `$subKey` $subValue";
foreach ($newCol as $keyName => $keyDefinition) {
if (!empty($oldCols['KEY'][$keyName])) {
if ($keyDefinition != $oldCols['KEY'][$keyName]) {
$updateSqlList[] = "DROP INDEX `$keyName`";
$updateSqlList[] = "ADD INDEX `$keyName` $keyDefinition";
}
} else {
$updateSqlList[] = "ADD INDEX `$subKey` $subValue";
$updateSqlList[] = "ADD INDEX `$keyName` $keyDefinition";
}
}
} elseif ($key == 'UNIQUE') {
foreach ($newCol as $subKey => $subValue) {
if (!empty($oldCols['UNIQUE'][$subKey])) {
if ($subValue != $oldCols['UNIQUE'][$subKey]) {
$updateSqlList[] = "DROP INDEX `$subKey`";
$updateSqlList[] = "ADD UNIQUE INDEX `$subKey` $subValue";
foreach ($newCol as $keyName => $keyDefinition) {
if (!empty($oldCols['UNIQUE'][$keyName])) {
if ($keyDefinition != $oldCols['UNIQUE'][$keyName]) {
$updateSqlList[] = "DROP INDEX `$keyName`";
$updateSqlList[] = "ADD UNIQUE INDEX `$keyName` $keyDefinition";
}
} else {
$updateSqlList[] = "ADD UNIQUE INDEX `$subKey` $subValue";
$updateSqlList[] = "ADD UNIQUE INDEX `$keyName` $keyDefinition";
}
}
}
Expand All @@ -164,14 +165,7 @@ protected function syncTable($sql)
foreach ($oldCols as $colName => $definition) {
if (in_array($colName, array('UNIQUE', 'KEY'))) { // drop index
if (empty($newCols[$colName])) {
foreach ($definition as $indexName => $indexColName) {
$deleteSqlList[] = "DROP INDEX `{$indexName}`";
}
}

if (!empty($newCols[$colName])) {
$diffArr = array_diff(array_keys($definition), array_keys($newCols[$colName]));
foreach ($diffArr as $indexName => $indexColName) {
foreach ($definition as $indexName => $indexDefinition) {
$deleteSqlList[] = "DROP INDEX `{$indexName}`";
}
}
Expand Down Expand Up @@ -417,7 +411,7 @@ protected function syncProcedure($sql)
$diff = $this->msg->highlightDiff($oldDef, $newDef);
$this->msg->verbose("\na different procedure definition was detected in `$newProcName`:");
$this->msg->vverbose("old def: " . $this->stripSpaces($oldDef), CommandMessage::MSG_STYLE_NONE);
$this->msg->verbose("new def: " . $diff, CommandMessage::MSG_STYLE_NONE);
$this->msg->verbose("new def: " . $this->stripSpaces($newDef), CommandMessage::MSG_STYLE_NONE);

$this->addExecSql("DROP PROCEDURE `{$newProcName}`;");
$this->addExecSql($newDef);
Expand Down Expand Up @@ -445,7 +439,7 @@ protected function getColDefListByTableDef($createSql)

preg_match("/\((.+)\)\s*(ENGINE|TYPE)\s*\=/is", $createSql, $matches);

$cols = explode("\n", $matches[1]);
$cols = empty($matches[1]) ? [] : explode("\n", $matches[1]);
$newCols = [];
foreach ($cols as $value) {
$value = trim($value);
Expand Down Expand Up @@ -477,7 +471,7 @@ protected function getColDefListByTableDef($createSql)

protected function stripSpaces($str)
{
return preg_replace("/\s*[\r?\n|\t]\s*/", ' ', $str);
return preg_replace("/\s*[\r?\n|\t]+\s*/", ' ', $str);
}

public function getMsgs()
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp()
->shouldDeferMissing();

// definer
$this->client->shouldReceive('getDefiner')->andReturn('DEFINER=`test`@`localhost`');
$this->client->shouldReceive('getDbDefiner')->andReturn('DEFINER=`test`@`localhost`');

// source sql
$this->loadSql();
Expand Down
110 changes: 110 additions & 0 deletions tests/Function/FunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

class FunctionTest extends BaseTest
{
function loadSql()
{}

protected function getSingleFunctionSql()
{
return <<<EOD
DROP FUNCTION IF EXISTS `SPLIT_STR`;
DELIMITER ;;
CREATE DEFINER=`test`@`localhost` FUNCTION `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(12),
pos INT
) RETURNS varchar(500) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END ;;
DELIMITER ;
EOD;
}

protected function getDiffSingleFunctionSql()
{
return <<<EOD
DROP FUNCTION IF EXISTS `SPLIT_STR`;
DELIMITER ;;
CREATE DEFINER=`test`@`localhost` FUNCTION `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(15),
pos INT
) RETURNS varchar(520) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END ;;
DELIMITER ;
EOD;
}

function testHandle_newFunction_shouldGenerateAlterScript()
{
$this->sourceSql = $this->getSingleFunctionSql();

$this->client->shouldReceive('dbHasFunction')->andReturnFalse();

$parser = new \Jezzis\MysqlSyncer\Parser\Parser($this->client, true);
$parser->parse($this->sourceSql);
$actualSql = $parser->getExecSqlList();

$sql = <<<EOD
CREATE DEFINER=`test`@`localhost` FUNCTION `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(12),
pos INT
) RETURNS varchar(500) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END
EOD;

$this->assertEquals([$sql], $actualSql);
}

function testHandle_diffFunction_shouldGenerateAlterScript()
{
$this->sourceSql = $this->getSingleFunctionSql();
$procedureDef = <<<EOD
CREATE DEFINER=`test`@`localhost` FUNCTION `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(15),
pos INT
) RETURNS varchar(520) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END
EOD;

$this->client->shouldReceive('dbHasFunction')->andReturnTrue();
$this->client->shouldReceive('getDefFromDB')->andReturn($procedureDef);

$parser = new \Jezzis\MysqlSyncer\Parser\Parser($this->client, true);
$parser->parse($this->sourceSql);
$actualSql = $parser->getExecSqlList();

$sqlList = [];
$sqlList[] = "DROP FUNCTION `SPLIT_STR`;";
$sqlList[] = "CREATE DEFINER=`test`@`localhost` FUNCTION `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(12),
pos INT
) RETURNS varchar(500) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END";

$this->assertEquals($sqlList, $actualSql);
}
}
110 changes: 110 additions & 0 deletions tests/Procedure/ProcedureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

class Procedure extends BaseTest
{
function loadSql()
{}

protected function getSingleProcedureSql()
{
return <<<EOD
DROP PROCEDURE IF EXISTS `SPLIT_STR`;
DELIMITER ;;
CREATE DEFINER=`test`@`localhost` PROCEDURE `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(12),
pos INT
) RETURNS varchar(500) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END ;;
DELIMITER ;
EOD;
}

protected function getDiffSingleProcedureSql()
{
return <<<EOD
DROP PROCEDURE IF EXISTS `SPLIT_STR`;
DELIMITER ;;
CREATE DEFINER=`test`@`localhost` PROCEDURE `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(15),
pos INT
) RETURNS varchar(520) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END ;;
DELIMITER ;
EOD;
}

function testHandle_newProcedure_shouldGenerateAlterScript()
{
$this->sourceSql = $this->getSingleProcedureSql();

$this->client->shouldReceive('dbHasProcedure')->andReturnFalse();

$parser = new \Jezzis\MysqlSyncer\Parser\Parser($this->client, true);
$parser->parse($this->sourceSql);
$actualSql = $parser->getExecSqlList();

$sql = <<<EOD
CREATE DEFINER=`test`@`localhost` PROCEDURE `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(12),
pos INT
) RETURNS varchar(500) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END
EOD;

$this->assertEquals([$sql], $actualSql);
}

function testHandle_diffProcedure_shouldGenerateAlterScript()
{
$this->sourceSql = $this->getSingleProcedureSql();
$procedureDef = <<<EOD
CREATE DEFINER=`test`@`localhost` PROCEDURE `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(15),
pos INT
) RETURNS varchar(520) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END
EOD;

$this->client->shouldReceive('dbHasProcedure')->andReturnTrue();
$this->client->shouldReceive('getDefFromDB')->andReturn($procedureDef);

$parser = new \Jezzis\MysqlSyncer\Parser\Parser($this->client, true);
$parser->parse($this->sourceSql);
$actualSql = $parser->getExecSqlList();

$sqlList = [];
$sqlList[] = "DROP PROCEDURE `SPLIT_STR`;";
$sqlList[] = "CREATE DEFINER=`test`@`localhost` PROCEDURE `SPLIT_STR`(
x VARCHAR(500),
delim VARCHAR(12),
pos INT
) RETURNS varchar(500) CHARSET utf8
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
END";

$this->assertEquals($sqlList, $actualSql);
}
}
2 changes: 0 additions & 2 deletions tests/Table/ColumnTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Mockery as m;

class ColumnTest extends BaseTest
{
public function loadSql()
Expand Down
Loading

0 comments on commit 7d973d5

Please sign in to comment.