Skip to content

Commit

Permalink
Merge pull request #19 from swiffer/cake-4.x
Browse files Browse the repository at this point in the history
CakePHP 4.x compatibility
  • Loading branch information
dakota authored Feb 17, 2020
2 parents af11866 + e86f103 commit 0985c03
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 102 deletions.
79 changes: 43 additions & 36 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
{
"name": "friendsofcake/fixturize",
"description": "CakePHP Fixture classes to help increase productivity or performance",
"type": "cakephp-plugin",
"keywords": [
"cakephp",
"fixture",
"fixtures",
"unittest",
"phpunit",
"performance"
],
"homepage": "https://github.com/FriendsOfCake/fixturize",
"license": "MIT",
"authors": [
{
"name": "Christian Winther",
"role": "Author",
"homepage": "http://cakephp.nu/"
"name": "friendsofcake/fixturize",
"description": "CakePHP Fixture classes to help increase productivity or performance",
"type": "cakephp-plugin",
"keywords": [
"cakephp",
"fixture",
"fixtures",
"unittest",
"phpunit",
"performance"
],
"homepage": "https://github.com/FriendsOfCake/fixturize",
"license": "MIT",
"authors": [
{
"name": "Christian Winther",
"role": "Author",
"homepage": "http://cakephp.nu/"
},
{
"name": "José Lorenzo Rodríguez",
"role": "Contributor",
"homepage": "https://github.com/lorenzo"
}
],
"autoload": {
"psr-4": {
"FriendsOfCake\\Fixturize\\": "src"
}
},
{
"name": "José Lorenzo Rodríguez",
"role": "Contributor",
"homepage": "https://github.com/lorenzo"
}
],
"autoload": {
"psr-4": {
"FriendsOfCake\\Fixturize\\": "src"
"scripts": {
"cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/",
"cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/"
},
"require": {
"cakephp/cakephp": "^4.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.0"
},
"support": {
"source": "https://github.com/FriendsOfCake/fixturize",
"issues": "https://github.com/FriendsOfCake/fixturize/issues",
"irc": "irc://irc.freenode.org/cakephp"
}
},
"require": {
"cakephp/cakephp": "^3.2"
},
"support": {
"source": "https://github.com/FriendsOfCake/fixturize",
"issues": "https://github.com/FriendsOfCake/fixturize/issues",
"irc": "irc://irc.freenode.org/cakephp"
}
}
135 changes: 69 additions & 66 deletions src/TestSuite/Fixture/ChecksumTestFixture.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types=1);

namespace FriendsOfCake\Fixturize\TestSuite\Fixture;

use Cake\TestSuite\Fixture\TestFixture;
use Cake\Database\Driver\Mysql;
use Cake\Datasource\ConnectionInterface;
use Cake\TestSuite\Fixture\TestFixture;

/**
* This class will inspect the database table hash and detect any change to the underlying
Expand All @@ -15,23 +17,24 @@
*/
class ChecksumTestFixture extends TestFixture
{
/**
* List of table hashes
*
* @var array
*/
protected static $_tableHashes = [];

/**
* List of table hashes
*
* @var array
*/
public static $_tableHashes = [];

/**
* Inserts records in the database
*
* This will only happen if the underlying table is modified in any way or
* does not exist with a hash yet.
*
* @param ConnectionInterface $db
* @return boolean
*/
/**
* Inserts records in the database
*
* This will only happen if the underlying table is modified in any way or
* does not exist with a hash yet.
*
* @param \Cake\Datasource\ConnectionInterface $db An instance of the connection
* into which the records will be inserted.
* @return \Cake\Database\StatementInterface|bool on success or if there are no records to insert,
* or false on failure.
*/
public function insert(ConnectionInterface $db)
{
if ($this->_tableUnmodified($db)) {
Expand All @@ -40,18 +43,19 @@ public function insert(ConnectionInterface $db)

$result = parent::insert($db);
static::$_tableHashes[$this->_getTableKey()] = $this->_hash($db);

return $result;
}

/**
* Deletes all table information.
*
* This will only happen if the underlying table is modified in any way
*
* @param ConnectionInterface $db
* @return void
*/
public function truncate(ConnectionInterface $db)
/**
* Deletes all table information.
*
* This will only happen if the underlying table is modified in any way
*
* @param \Cake\Datasource\ConnectionInterface $db A reference to a db instance
* @return bool
*/
public function truncate(ConnectionInterface $db): bool
{
if ($this->_tableUnmodified($db)) {
return true;
Expand All @@ -60,30 +64,31 @@ public function truncate(ConnectionInterface $db)
return parent::truncate($db);
}

/**
* Drops the table from the test datasource
*
* @param ConnectionInterface $db
* @return void
*/
public function drop(ConnectionInterface $db)
/**
* Drops the table from the test datasource
*
* @param \Cake\Datasource\ConnectionInterface $db An instance of the connection the fixture should be removed from.
* @return bool True on success, false on failure.
*/
public function drop(ConnectionInterface $db): bool
{
unset(static::$_tableHashes[$this->_getTableKey()]);

return parent::drop($db);
}

/**
* Test if a table is modified or not
*
* If there is no known hash, treat it as being modified
*
* In all other cases where the initial and current hash differs, assume
* the table has changed
*
* @param DboSource $db
* @return boolean
*/
protected function _tableUnmodified($db)
/**
* Test if a table is modified or not
*
* If there is no known hash, treat it as being modified
*
* In all other cases where the initial and current hash differs, assume
* the table has changed
*
* @param \Cake\Datasource\ConnectionInterface $db A reference to a db instance
* @return bool
*/
protected function _tableUnmodified(ConnectionInterface $db): bool
{
$tableKey = $this->_getTableKey();
if (!array_key_exists($tableKey, static::$_tableHashes)) {
Expand All @@ -97,33 +102,31 @@ protected function _tableUnmodified($db)
return false;
}

/**
* Get the table hash from MySQL for a specific table
*
* @param ConnectionInterface $db
* @return string
*/
protected function _hash(ConnectionInterface $db)
/**
* Get the table hash from MySQL for a specific table
*
* @param \Cake\Datasource\ConnectionInterface $db A reference to a db instance
* @return string
*/
protected function _hash(ConnectionInterface $db): string
{
$driver = $db->getDriver();

if (!$driver instanceof Mysql) {
// Have no better idea right now to make it always regenerate the tables
return microtime();
}
if ($driver instanceof Mysql) {
$sth = $db->execute("CHECKSUM TABLE " . $this->table);

$sth = $db->execute("CHECKSUM TABLE " . $this->table . ';');
$result = $sth->fetch('assoc');
$checksum = $result['Checksum'];
return $checksum;
return $sth->fetchColumn(1);
}
// Have no better idea right now to make it always regenerate the tables
return microtime();
}

/**
* Get the key for table hashes
*
* @return string key for specify connection and table
*/
protected function _getTableKey ()
/**
* Get the key for table hashes
*
* @return string key for specify connection and table
*/
protected function _getTableKey(): string
{
return $this->connection() . '-' . $this->table;
}
Expand Down

0 comments on commit 0985c03

Please sign in to comment.