Skip to content

Commit

Permalink
Merge pull request #38 from vierge-noire/next
Browse files Browse the repository at this point in the history
* #7 Removes the CakePHP fixture init (#8)

* #7 Removes the loadConfig (#12)

* #14 Makes the dirty table collector permanent per default (#18)

* #24 Fix connection alias issue (#27)

* #30 Dropping managed by Migrator (#34)

* #31 Use native dialects (#36)

* #32 Cleanup

* #35 Require migrator 2.2

Co-authored-by: Juan Pablo Ramirez <>
  • Loading branch information
pabloelcolombiano authored Apr 25, 2021
2 parents 3f72038 + 85712fb commit d8334bb
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 308 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ dummy_database
composer.lock
/.phpunit.result.cache
/tmp/
.env
tests/.env
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.2",
"cakephp/cakephp": "^4.0",
"ext-pdo": "*"
"ext-pdo": "*",
"vierge-noire/cakephp-test-migrator": "^2.2"
},
"require-dev": {
"cakephp/migrations": "^3.0",
Expand Down
7 changes: 5 additions & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export DB_DRIVER=$DRIVER

#######################
#### Tests with non temporary sniffers
#### Skip MySQL
#######################
export SNIFFERS_IN_TEMP_MODE="true"
./vendor/bin/phpunit
if [ $DRIVER != 'Mysql' ]; then
export SNIFFERS_IN_TEMP_MODE="true"
./vendor/bin/phpunit
fi

#### DEPRECATED #####
# Run the tests using
Expand Down
17 changes: 3 additions & 14 deletions src/FixtureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ public function skipConnection(string $connectionName, array $ignoredConnections
return true;
}

/**
* Get the appropriate sniffer and drop all tables
* @param string $connectionName
* @return void
*/
public function dropTables(string $connectionName): void
{
SnifferRegistry::get($connectionName)->dropTables(
SnifferRegistry::get($connectionName)->fetchAllTables()
);
}

/**
* Initialize all connections used by the manager
* @return array
Expand All @@ -127,6 +115,7 @@ public function fetchActiveConnections(): array
* Those are the connections that are neither ignored,
* nor irrelevant (debug_kit, non-test DBs etc...)
* @return array
* @throws \RuntimeException
*/
public function getActiveConnections(): array
{
Expand All @@ -148,7 +137,7 @@ public function getFixturesPerConnection(array $fixtures)
// For Cake ^4.0
return $this->_fixtureConnections($fixtures);
} else {
throw new Exception(
throw new \RuntimeException(
'Neither groupFixturesByConnection nor _fixtureConnections defined in ' . self::class
);
}
Expand Down Expand Up @@ -189,7 +178,7 @@ public function load(TestCase $test): void
get_class($test),
$e->getMessage()
);
throw new Exception($msg, 0, $e);
throw new \RuntimeException($msg, 0, $e);
}
}
});
Expand Down
27 changes: 16 additions & 11 deletions src/Sniffer/BaseTableSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,9 @@ abstract public function truncateDirtyTables(): void;
*/
abstract public function getDirtyTables(): array;

/**
* List all tables
* @return array
*/
abstract public function fetchAllTables(): array;

/**
* Drop tables passed as a parameter
* @deprecated table dropping is not handled by this package anymore.
* @param array $tables
* @return void
*/
Expand All @@ -61,7 +56,6 @@ abstract public function dropTables(array $tables): void;
public function __construct(ConnectionInterface $connection)
{
$this->setConnection($connection);
$this->start();
}

/**
Expand All @@ -87,13 +81,14 @@ public function setConnection(ConnectionInterface $connection): void
* Create the spying triggers
* @return void
*/
public function start(): void
public function init(): void
{
$this->getAllTables(true);
}

/**
* Stop spying
* @deprecated shutdowm will not be supported from 3.0 on.
* @return void
*/
public function shutdown(): void
Expand All @@ -103,12 +98,13 @@ public function shutdown(): void
* Stop spying and restart
* Useful if the schema or the
* dirty table collector changed
* @deprecated the schema changes are not handled anymore by this package.
* @return void
*/
public function restart(): void
{
$this->shutdown();
$this->start();
$this->init();
}

/**
Expand All @@ -119,6 +115,7 @@ public function restart(): void
* @param string $query
*
* @return array
* @deprecated queries should be fetched using the CakePHP native tools.
*/
public function fetchQuery(string $query): array
{
Expand All @@ -130,7 +127,6 @@ public function fetchQuery(string $query): array
} catch (\Exception $e) {
$name = $this->getConnection()->configName();
$db = $this->getConnection()->config()['database'];
var_dump($e->getMessage());
throw new Exception("Error in the connection '$name'. Is the database '$db' created and accessible?");
}

Expand Down Expand Up @@ -181,6 +177,15 @@ public function getAllTables(bool $forceFetch = false): array
return $this->allTables;
}

/**
* List all tables
* @return string[]
*/
public function fetchAllTables(): array
{
return $this->getConnection()->getSchemaCollection()->listTables();
}

/**
* Checks if the present class implements triggers
* @return bool
Expand All @@ -189,4 +194,4 @@ public function implementsTriggers(): bool
{
return $this instanceof BaseTriggerBasedTableSniffer;
}
}
}
88 changes: 70 additions & 18 deletions src/Sniffer/BaseTriggerBasedTableSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
namespace CakephpTestSuiteLight\Sniffer;

use Cake\Database\Exception;
use Cake\Datasource\ConnectionInterface;

abstract class BaseTriggerBasedTableSniffer extends BaseTableSniffer
Expand Down Expand Up @@ -44,12 +43,14 @@ abstract class BaseTriggerBasedTableSniffer extends BaseTableSniffer

/**
* Get triggers relative to the database dirty table collector
* @deprecated triggers are not dropped and do not need to be fetched.
* @return array
*/
abstract public function getTriggers(): array;

/**
* Drop triggers relative to the database dirty table collector
* @deprecated Triggers will not be dropped any more. The present package expects a clean schema.
* @return void
*/
abstract public function dropTriggers();
Expand All @@ -61,11 +62,17 @@ abstract public function dropTriggers();
abstract public function createTriggers(): void;

/**
* Mark all tables except phinxlogs as dirty
* Mark all tables except phinxlogs and dirty table collector as dirty.
* @return void
*/
abstract public function markAllTablesAsDirty(): void;

/**
* Create the procedure truncating the dirty tables.
* @return void
*/
abstract public function createTruncateDirtyTablesProcedure(): void;

/**
* BaseTableTruncator constructor.
* @param ConnectionInterface $connection
Expand All @@ -76,6 +83,45 @@ public function __construct(ConnectionInterface $connection)
parent::__construct($connection);
}

/**
* Check that the dirty table collector exists
*
* @return bool
*/
public function dirtyTableCollectorExists(): bool
{
return in_array(self::DIRTY_TABLE_COLLECTOR, $this->getAllTables(true));
}

/**
* @inheritDoc
*/
public function init(): void
{
if (!$this->dirtyTableCollectorExists()) {
$this->createDirtyTableCollector();
try {
$this->createTriggers();
} catch (\Throwable $e) {
$message = $e->getMessage();
$message .= ' ----- Please truncate your test schema manually and run the test suite again.';
throw new \RuntimeException($message);
}
$this->createTruncateDirtyTablesProcedure();
$this->markAllTablesAsDirty();
}
}

/**
* Get the name of the dirty table locator.
*
* @return string
*/
public function collectorName(): string
{
return BaseTriggerBasedTableSniffer::DIRTY_TABLE_COLLECTOR;
}

/**
* Find all tables where an insert happened
* This also includes empty tables, where a delete
Expand All @@ -85,13 +131,30 @@ public function __construct(ConnectionInterface $connection)
public function getDirtyTables(): array
{
try {
return $this->fetchQuery("SELECT table_name FROM " . self::DIRTY_TABLE_COLLECTOR);
} catch (\Exception $e) {
$this->restart();
return $this->getAllTablesExceptPhinxlogs(true);
return $this->fetchQuery("SELECT table_name FROM " . $this->collectorName());
} catch (\Throwable $e) {
$this->init();
return $this->getDirtyTables();
}
}

/**
* Fetch all tables, excluded from Phinx related and the dirty table collector.
*
* @param bool $forceFetch
* @return array
*/
public function getAllTablesExceptPhinxlogsAndCollector(bool $forceFetch = false): array
{
$allTables = $this->getAllTablesExceptPhinxlogs($forceFetch);

if (($key = array_search($this->collectorName(), $allTables)) !== false) {
unset($allTables[$key]);
}

return $allTables;
}

/**
* Create the table gathering the dirty tables
* @return void
Expand All @@ -110,6 +173,7 @@ public function createDirtyTableCollector(): void

/**
* Drop the table gathering the dirty tables
* @deprecated The dropping is handled by the schema manager.
* @return void
*/
public function dropDirtyTableCollector()
Expand All @@ -118,18 +182,6 @@ public function dropDirtyTableCollector()
$this->getConnection()->execute("DROP TABLE IF EXISTS {$dirtyTable}");
}

/**
* The dirty table collector being temporary,
* ensure that all tables are clean when starting the suite
* @return void
*/
public function cleanAllTables(): void
{
if ($this->isInTempMode()) {
$this->markAllTablesAsDirty();
}
}

/**
* The dirty table collector is not temporary
* @return void
Expand Down
19 changes: 6 additions & 13 deletions src/Sniffer/DriverTraits/MysqlSnifferTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
use Cake\Database\Connection;
use CakephpTestSuiteLight\Sniffer\BaseTriggerBasedTableSniffer;

/**
* Trait MysqlSnifferTrait
* @package CakephpTestSuiteLight\Sniffer\DriverTraits
* @deprecated Sniffers are not queried anymore.
*/
trait MysqlSnifferTrait
{
/**
Expand Down Expand Up @@ -49,18 +54,6 @@ public function dropTriggers(): void
$this->getConnection()->execute($stmts);
}

/**
* @inheritDoc
*/
public function fetchAllTables(): array
{
return $this->fetchQuery("
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE();
");
}

/**
* @inheritDoc
*/
Expand All @@ -79,4 +72,4 @@ public function dropTables(array $tables): void
});
});
}
}
}
19 changes: 6 additions & 13 deletions src/Sniffer/DriverTraits/PostgresSnifferTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
use Cake\Database\Connection;
use CakephpTestSuiteLight\Sniffer\BaseTriggerBasedTableSniffer;

/**
* Trait PostgresSnifferTrait
* @package CakephpTestSuiteLight\Sniffer\DriverTraits
* @deprecated Sniffers are not queried anymore.
*/
trait PostgresSnifferTrait
{
/**
Expand Down Expand Up @@ -55,18 +60,6 @@ public function dropTriggers(): void
}
}

/**
* @inheritDoc
*/
public function fetchAllTables(): array
{
return $this->fetchQuery("
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
");
}

/**
* @inheritDoc
*/
Expand All @@ -85,4 +78,4 @@ public function dropTables(array $tables): void
}
});
}
}
}
Loading

0 comments on commit d8334bb

Please sign in to comment.