Skip to content

Commit

Permalink
#14 cake3 make perm mode default (#15) (#17)
Browse files Browse the repository at this point in the history
* #7 Removes the CakePHP fixture init (#9) (#11)

Co-authored-by: Juan Pablo Ramirez <>

* #14 Makes the dirty table collector permanent per default

* #14 Test fix

Co-authored-by: Juan Pablo Ramirez <>
  • Loading branch information
pabloelcolombiano authored Feb 3, 2021
1 parent 5da9516 commit ba9ca18
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export DB_DRIVER=$DRIVER
#######################
#### Tests with non temporary sniffers
#######################
export SNIFFERS_IN_MAIN_MODE="true"
export SNIFFERS_IN_TEMP_MODE="true"
./vendor/bin/phpunit

#### DEPRECATED #####
Expand Down
59 changes: 36 additions & 23 deletions src/Sniffer/BaseTriggerBasedTableSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ abstract class BaseTriggerBasedTableSniffer extends BaseTableSniffer

const TRIGGER_PREFIX = 'dirty_table_spy_';

const MAIN_MODE = 'MAIN_MODE';
const MODE_KEY = 'dirtyTableCollectorMode';

const TEMP_MODE = 'TEMP_MODE';
/**
* The dirty table collector is a permanent table
*/
const PERM_MODE = 'PERM';

/**
* The dirty table collector is a temporary table
*/
const TEMP_MODE = 'TEMP';

/**
* @var string
Expand Down Expand Up @@ -65,26 +73,10 @@ abstract public function markAllTablesAsDirty();
*/
public function __construct(ConnectionInterface $connection)
{
$this->mode = self::TEMP_MODE;
$this->mode = $this->getDefaultMode($connection);
parent::__construct($connection);
}

/**
* @return ConnectionInterface
*/
public function getConnection(): ConnectionInterface
{
return $this->connection;
}

/**
* @param ConnectionInterface $connection
*/
public function setConnection(ConnectionInterface $connection)
{
$this->connection = $connection;
}

/**
* Find all tables where an insert happened
* This also includes empty tables, where a delete
Expand Down Expand Up @@ -134,7 +126,9 @@ public function dropDirtyTableCollector()
*/
public function cleanAllTables()
{
$this->markAllTablesAsDirty();
if ($this->isInTempMode()) {
$this->markAllTablesAsDirty();
}
$this->truncateDirtyTables();
}

Expand All @@ -144,7 +138,7 @@ public function cleanAllTables()
*/
public function activateMainMode()
{
$this->setMode(self::MAIN_MODE);
$this->setMode(self::PERM_MODE);
}

/**
Expand Down Expand Up @@ -172,7 +166,8 @@ public function setMode(string $mode)
/**
* Get the mode on which the sniffer is running
* This defines if the collector table is
* temporary or not
* temporary or not.
*
* @return string
*/
public function getMode(): string
Expand All @@ -183,6 +178,24 @@ public function getMode(): string
return $this->mode;
}

/**
* Defines the default mode for the dirty table collector.
*
* @param ConnectionInterface $connection
* @return string
* @throws \Exception
*/
public function getDefaultMode(ConnectionInterface $connection): string
{
$mode = $connection->config()[self::MODE_KEY] ?? self::PERM_MODE;
if (!in_array($mode, [self::TEMP_MODE, self::PERM_MODE])) {
$msg = self::MODE_KEY . ' can only be equal to ' . self::PERM_MODE . ' or ' . self::TEMP_MODE;
throw new \Exception($msg);
}

return $mode;
}

/**
* @return bool
*/
Expand All @@ -196,6 +209,6 @@ public function isInTempMode(): bool
*/
public function isInMainMode(): bool
{
return ($this->getMode() === self::MAIN_MODE);
return ($this->getMode() === self::PERM_MODE);
}
}
13 changes: 13 additions & 0 deletions tests/TestCase/Sniffer/SnifferRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Database\Driver\Sqlite;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use CakephpTestSuiteLight\Sniffer\BaseTriggerBasedTableSniffer;
use CakephpTestSuiteLight\Sniffer\MysqlTriggerBasedTableSniffer;
use CakephpTestSuiteLight\Sniffer\PostgresTriggerBasedTableSniffer;
use CakephpTestSuiteLight\Sniffer\SnifferRegistry;
Expand Down Expand Up @@ -64,4 +65,16 @@ public function testGetConnectionSnifferNameOnConnection()
$this->assertSame($sniffer, $act);
ConnectionManager::drop($connectionName);
}

public function testModeIsCorrect()
{
$tables = SnifferRegistry::get('test')->fetchAllTables();
$collectorIsVisible = in_array(BaseTriggerBasedTableSniffer::DIRTY_TABLE_COLLECTOR, $tables);
if (getenv('SNIFFERS_IN_TEMP_MODE') || !SnifferRegistry::get('test')->implementsTriggers()) {
$expected = false;
} else {
$expected = true;
}
$this->assertSame($expected, $collectorIsVisible);
}
}
21 changes: 8 additions & 13 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Log\Log;
use Cake\Utility\Inflector;
use Cake\Utility\Security;
use CakephpTestSuiteLight\Sniffer\BaseTriggerBasedTableSniffer;
use CakephpTestSuiteLight\Sniffer\SnifferRegistry;
use Migrations\Migrations;

Expand Down Expand Up @@ -149,6 +150,10 @@
$dbConnection['tableSniffer'] = getenv('TABLE_SNIFFER');
}

if (getenv('SNIFFERS_IN_TEMP_MODE')) {
$dbConnection[BaseTriggerBasedTableSniffer::MODE_KEY] = BaseTriggerBasedTableSniffer::TEMP_MODE;
}

ConnectionManager::setConfig('default', $dbConnection);
ConnectionManager::setConfig('test', $dbConnection);

Expand All @@ -158,6 +163,9 @@
$dummyConnection['skipInTestSuiteLight'] = true;
ConnectionManager::setConfig('test_dummy', $dummyConnection);

if (getenv('SNIFFERS_IN_TEMP_MODE')) {
ConnectionManager::get('test')->execute('DROP TABLE IF EXISTS ' . BaseTriggerBasedTableSniffer::DIRTY_TABLE_COLLECTOR);
}

Configure::write('Session', [
'defaults' => 'php',
Expand Down Expand Up @@ -191,16 +199,6 @@

Inflector::rules('singular', ['/(ss)$/i' => '\1']);


// Prepare the DB
SnifferRegistry::clear();

if (getenv('SNIFFERS_IN_MAIN_MODE') && SnifferRegistry::get('test')->implementsTriggers()) {
SnifferRegistry::get('test')->activateMainMode();
}

SnifferRegistry::get('test')->dropTriggers();

if (getenv('USE_NON_TRIGGERED_BASED_SNIFFERS') && !SnifferRegistry::get('test')->implementsTriggers()) {
SnifferRegistry::get('test')->dropTables(
SnifferRegistry::get('test')->getAllTables(true)
Expand All @@ -210,6 +208,3 @@
// Run migrations
$migrations = new Migrations();
$migrations->migrate();

// Clear the Sniffers, ready to start the tests
SnifferRegistry::clear();

0 comments on commit ba9ca18

Please sign in to comment.