Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CakePHP 4.x compatibility #19

Merged
merged 8 commits into from
Feb 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
130 changes: 67 additions & 63 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
@@ -15,43 +17,43 @@
*/
class ChecksumTestFixture extends TestFixture
{
/**
* List of table hashes
*
* @var array
*/
public 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
*/
public function insert(ConnectionInterface $db)
/**
* 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 A reference to a db instance
* @return bool
*/
public function insert(ConnectionInterface $db): bool
{
if ($this->_tableUnmodified($db)) {
return true;
}

$result = parent::insert($db);
static::$_tableHashes[$this->_getTableKey()] = $this->_hash($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;
@@ -60,50 +62,51 @@ 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 A reference to a db instance
* @return bool
*/
public function drop(ConnectionInterface $db): bool
{
unset(static::$_tableHashes[$this->_getTableKey()]);
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)) {
if (!array_key_exists($tableKey, static::$tableHashes)) {
return false;
}

if (static::$_tableHashes[$tableKey] === $this->_hash($db)) {
if (static::$tableHashes[$tableKey] === $this->_hash($db)) {
return true;
}

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();

@@ -115,15 +118,16 @@ protected function _hash(ConnectionInterface $db)
$sth = $db->execute("CHECKSUM TABLE " . $this->table . ';');
$result = $sth->fetch('assoc');
$checksum = $result['Checksum'];

return $checksum;
}

/**
* 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;
}