Skip to content

Commit

Permalink
Merge pull request #26 from FriendsOfCake/cake5
Browse files Browse the repository at this point in the history
Initial Upgrade to CakePHP 5.x (#23)
  • Loading branch information
ADmad authored Oct 13, 2023
2 parents f8dd662 + 627b11b commit 32594c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
extensions: mbstring, intl
coverage: none
tools: phpstan:1.10, cs2pr
Expand Down
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,25 @@
}
},
"scripts": {
"cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/",
"cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/"
"cs-check": "phpcs --colors --parallel=16 -p src/",
"cs-fix": "phpcbf --colors --parallel=16 -p src/",
"phpstan": "tools/phpstan analyse",
"psalm": "tools/psalm --show-info=false",
"stan": [
"@phpstan",
"@psalm"
],
"stan-baseline": "tools/phpstan --generate-baseline",
"psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml",
"tools-setup": "phive install",
"test": "phpunit"
},
"require": {
"cakephp/cakephp": "^4.0"
"php": ">=8.1",
"cakephp/cakephp": "^5.0.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.0"
"cakephp/cakephp-codesniffer": "^5.0"
},
"support": {
"source": "https://github.com/FriendsOfCake/fixturize",
Expand Down
62 changes: 26 additions & 36 deletions src/TestSuite/Fixture/ChecksumTestFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

namespace FriendsOfCake\Fixturize\TestSuite\Fixture;

use Cake\Database\Connection;
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
* data set and automatically re-create the table and data
* This class will inspect the database table hash and detect any change to
* the underlying data set and automatically re-create the table and data
*
* If no data has changed, the usual truncate/insert flow is bypassed, increasing the speed
* of the test suite with heavy fixture usage up significantly.
* If no data has changed, the usual truncate/insert flow is bypassed,
* increasing the speed of the test suite with heavy fixture usage up
* significantly.
*/
class ChecksumTestFixture extends TestFixture
{
Expand All @@ -21,27 +23,27 @@ class ChecksumTestFixture extends TestFixture
*
* @var array<string, string>
*/
protected static $_tableHashes = [];
protected static array $_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 \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,
* @param \Cake\Datasource\ConnectionInterface $connection An instance
* of the connection into which the records will be inserted.
* @return bool on success or if there are no records to insert,
* or false on failure.
*/
public function insert(ConnectionInterface $db)
public function insert(ConnectionInterface $connection): bool
{
if ($this->_tableUnmodified($db)) {
if ($this->_tableUnmodified($connection)) {
return true;
}

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

return $result;
}
Expand All @@ -51,29 +53,16 @@ public function insert(ConnectionInterface $db)
*
* This will only happen if the underlying table is modified in any way
*
* @param \Cake\Datasource\ConnectionInterface $db A reference to a db instance
* @param \Cake\Datasource\ConnectionInterface $connection A reference to a db instance
* @return bool
*/
public function truncate(ConnectionInterface $db): bool
public function truncate(ConnectionInterface $connection): bool
{
if ($this->_tableUnmodified($db)) {
if ($this->_tableUnmodified($connection)) {
return true;
}

return parent::truncate($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);
return parent::truncate($connection);
}

/**
Expand All @@ -84,17 +73,17 @@ public function drop(ConnectionInterface $db): bool
* 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
* @param \Cake\Datasource\ConnectionInterface $connection A reference to a db instance
* @return bool
*/
protected function _tableUnmodified(ConnectionInterface $db): bool
protected function _tableUnmodified(ConnectionInterface $connection): bool
{
$tableKey = $this->_getTableKey();
if (!array_key_exists($tableKey, static::$_tableHashes)) {
return false;
}

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

Expand All @@ -104,15 +93,16 @@ protected function _tableUnmodified(ConnectionInterface $db): bool
/**
* Get the table hash from MySQL for a specific table
*
* @param \Cake\Datasource\ConnectionInterface $db A reference to a db instance
* @param \Cake\Datasource\ConnectionInterface $connection A reference to a db instance
* @return string
*/
protected function _hash(ConnectionInterface $db): string
protected function _hash(ConnectionInterface $connection): string
{
$driver = $db->getDriver();
assert($connection instanceof Connection);
$driver = $connection->getDriver();

if ($driver instanceof Mysql) {
$sth = $db->execute('CHECKSUM TABLE `' . $this->table . '`');
$sth = $connection->execute('CHECKSUM TABLE `' . $this->table . '`');

return (string)$sth->fetchColumn(1);
}
Expand Down

0 comments on commit 32594c8

Please sign in to comment.