Skip to content

Commit

Permalink
add initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Jul 4, 2016
0 parents commit 159a3ba
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = false

[*]
indent_style = space
indent_size = 4
charset = "utf-8"
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_style = space
indent_size = 2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.pyc
docs/_build
phpunit.xml
vendor/
composer.lock
tmp
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Christian "Jippi" Winther

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[![Latest Stable Version](https://img.shields.io/packagist/v/FriendsOfCake/fixturize.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/fixturize)

# Installation

For CakePHP 3.x compatible version:

```
composer require friendsofcake/fixturize
```

# Introduction

The fixturize plugin will help improve performance of your fixture based tests.

This plugin currently only work with MySQL databases.

# Usage

Instead of ``use Cake\TestSuite\Fixture\TestFixture;`` simply use ``use FriendsOfCake\Fixturize\TestSuite\Fixture\ChecksumTestFixture as TestFixture;``.

Re-run your tests and enjoy the speed!

# 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 Crud 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 #FriendsOfCake channel for any support or questions.
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"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\\Fixture\\": "src"
}
},
"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"
}
}
111 changes: 111 additions & 0 deletions src/TestSuite/Fixture/ChecksumTestFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
namespace FriendsOfCake\Fixturize\TestSuite\Fixture;

use Cake\TestSuite\Fixture\TestFixture;
use Cake\Datasource\ConnectionInterface;

/**
* 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
*/
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)
{
if ($this->_tableUnmodified($db)) {
return true;
}

$result = parent::insert($db);
static::$_tableHashes[$this->table] = $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)
{
if ($this->_tableUnmodified($db)) {
return true;
}

return parent::truncate($db);
}

/**
* Drops the table from the test datasource
*
* @param ConnectionInterface $db
* @return void
*/
public function drop(ConnectionInterface $db)
{
unset(static::$_tableHashes[$this->table]);
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)
{
if (empty(static::$_tableHashes[$this->table])) {
return false;
}

if (static::$_tableHashes[$this->table] === $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)
{
$sth = $db->execute("CHECKSUM TABLE " . $this->table);
$result = $sth->fetch('assoc');
$checksum = $result['Checksum'];
return $checksum;
}
}

0 comments on commit 159a3ba

Please sign in to comment.