Skip to content

Commit

Permalink
Merge pull request #22 from FriendsOfCake/cake-4.x
Browse files Browse the repository at this point in the history
Cake 4.x
  • Loading branch information
ADmad authored Apr 12, 2023
2 parents af11866 + 6282a6d commit 186e1df
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 121 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ trim_trailing_whitespace = true
[*.yml]
indent_style = space
indent_size = 2

[*.neon]
indent_style = tab
indent_size = 4
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on: [push, pull_request]

jobs:
cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: phpstan:1.10, cs2pr

- name: Composer Install
run: composer install

- name: Run phpcs
run: vendor/bin/phpcs --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ | cs2pr

- name: Run phpstan
if: always()
run: phpstan analyse --error-format=github
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# Installation

For CakePHP 3.x compatible version:

```
composer require friendsofcake/fixturize
```
Expand Down Expand Up @@ -51,20 +49,4 @@ Re-run your tests and enjoy the speed!
</tbody>
</table>

Feel free to submit your own results above

# Bugs

If you happen to stumble upon a bug, please feel free to create a pull request with a fix
(optionally with a test), and a description of the bug and how it was resolved.

You can also create an issue with a description to raise awareness of the bug.

# Features

If you have a good idea for a Fixturize feature, please join us on IRC and let's discuss it. Pull
requests are always more than welcome.

# Support / Questions

You can join us on IRC in the #CakePHP channel for any support or questions.
Feel free to submit your own results above.
84 changes: 48 additions & 36 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
{
"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"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
},
"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"
}
}
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 8
paths:
- src
135 changes: 69 additions & 66 deletions src/TestSuite/Fixture/ChecksumTestFixture.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
<?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
* 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.
*
*/
class ChecksumTestFixture extends TestFixture
{
/**
* List of table hashes
*
* @var array<string, string>
*/
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 +42,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 +63,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 +101,32 @@ 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 . '`');

return $sth->fetchColumn(1);
}

$sth = $db->execute("CHECKSUM TABLE " . $this->table . ';');
$result = $sth->fetch('assoc');
$checksum = $result['Checksum'];
return $checksum;
// 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 186e1df

Please sign in to comment.