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

Branch 0.3 #2

Merged
merged 52 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
5eb5d37
Added basis for unit and integration tests
Screenfeed Aug 13, 2020
c3754ed
Fixed a few small issues:
Screenfeed Aug 13, 2020
a625f3d
Added unit tests for the class DBUtilities
Screenfeed Aug 13, 2020
7b63510
Ignore the .vscode folder
Screenfeed Aug 13, 2020
1f5f17c
DBUtilities's unit tests: moved the log method
Screenfeed Aug 14, 2020
5cb382e
Fix DBUtilities::table_exists()
Screenfeed Aug 14, 2020
936f0ac
Added a filter to DBUtilities::can_log()
Screenfeed Aug 14, 2020
99f5c39
Added the constant required in DBUtilities::can_log()
Screenfeed Aug 14, 2020
1055d1f
Removed the "temporary table" logic from DBUtilities::table_exists(),…
Screenfeed Aug 18, 2020
54084e8
Adapted unit tests to latest changes in DBUtilities
Screenfeed Aug 18, 2020
8e05b06
Integration tests for DBUtilities
Screenfeed Aug 18, 2020
5188cb7
New method DBUtilities::get_last_error()
Screenfeed Aug 18, 2020
997486b
Added Table->get_last_error()
Screenfeed Aug 18, 2020
c0bf38c
Allow to use another class than DBUtilities
Screenfeed Aug 18, 2020
e6c609f
Added a @source tag to DBUtilitiesIntegration
Screenfeed Aug 18, 2020
23692ea
Small fixes in the Table class
Screenfeed Aug 19, 2020
9773814
Added missing DBUtilitiesUnit::get_last_error()
Screenfeed Aug 19, 2020
c2ca9bf
Unit tests for the Table class
Screenfeed Aug 19, 2020
fd61d98
Fix heading backslash removal
Screenfeed Aug 19, 2020
05902e9
Added a unit test for Table->get_table_worker()
Screenfeed Aug 19, 2020
91ad4c3
Integration tests for the Table class
Screenfeed Aug 20, 2020
391c919
New trait TemporaryTableTrait for integration tests
Screenfeed Aug 20, 2020
b546065
New trait LogsTrait for integration tests
Screenfeed Aug 20, 2020
8c9c2d5
Moved LogsTrait one level up
Screenfeed Aug 20, 2020
e42f843
Added 2 methods to StaticMethodMocker for consistency
Screenfeed Aug 20, 2020
f0015f1
AbstractTableDefinition: added constructor
Screenfeed Aug 21, 2020
4988bbc
Unit tests for AbstractTableDefinition
Screenfeed Aug 21, 2020
58fb76a
Moved AbstractTableDefinition’s unit tests into a TableDefinition sub…
Screenfeed Aug 21, 2020
ea6925f
Removed the TestCase class for AbstractTableDefinition, as it was use…
Screenfeed Aug 21, 2020
8dfdd4b
Integration tests for AbstractTableDefinition
Screenfeed Aug 21, 2020
62fb197
Fixes in TableUpgrader:
Screenfeed Aug 25, 2020
93da00e
Fixed TestCaseTrait->getPropertyValue()
Screenfeed Aug 25, 2020
5a6e125
In unit tests, unset $wpdb at tearDown
Screenfeed Aug 25, 2020
d0ad6f8
Removed unused $wpdb in Test_GetTableName
Screenfeed Aug 25, 2020
73e81b7
Unit tests for TableUpgrader
Screenfeed Aug 25, 2020
2dd6a18
Improves TestCaseTrait
Screenfeed Aug 28, 2020
f16263f
Use invokeMethod() in unit tests
Screenfeed Aug 28, 2020
ddd7aa0
Allow to set a custom version to the fixture class CustomTable
Screenfeed Aug 28, 2020
bec8c91
Integration tests for TableUpgrader
Screenfeed Aug 28, 2020
3eaf365
Harmonize TestCaseTrait
Screenfeed Aug 28, 2020
1eb3ed9
Add a $args param to TestCaseTrait->invokeMethod()
Screenfeed Sep 2, 2020
baf97d4
Improvements in AbstractCRUD
Screenfeed Sep 2, 2020
b569a43
Unit tests for AbstractCRUD
Screenfeed Sep 2, 2020
070c4db
AbstractCRUD: removed reference to unused $wpdb
Screenfeed Sep 3, 2020
ef88514
Integration tests for AbstractCRUD
Screenfeed Sep 3, 2020
5910f79
Copy/paste drama
Screenfeed Sep 4, 2020
f5c87d8
Unit tests for class Basic
Screenfeed Sep 7, 2020
3a9a67f
Disallow ARRAY_N in Basic->get()
Screenfeed Sep 10, 2020
a8c3b93
Integration tests for class Basic
Screenfeed Sep 10, 2020
6c873b5
Fix default value in comment
Screenfeed Sep 11, 2020
93cad94
From DBUtilities to DBWorker\Worker
Screenfeed Sep 15, 2020
30ed535
Improved the readme file by providing an example
Screenfeed Sep 16, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Directories
/.vscode
/report
/vendor

Expand Down
5 changes: 5 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ul-indent": { "indent": 4 },
"line-length": false,
"no-hard-tabs": false
}
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,35 @@ Requires **php 7.0** and **WordPress 4.9.6**. With php 7.1+, can be used with Wo

With composer:

```
```json
"require": {
"screenfeed/autowpdb": "dev-master"
},
```

## How to use

Create 2 classes and youre ready:
Create 1 or 2 classes and you're ready:

* One that "defines" your custom table (name, default values, value types, schema, etc) by extending *TableDefinition\AbstractTableDefinition*,
* One containing your CRUD methods (optional) by extending *CRUD\Basic*.
* Optionally, one containing your CRUD methods by extending *CRUD\Basic*.

Example:

```php
use Screenfeed\AutoWPDB\Table;
use Screenfeed\AutoWPDB\TableUpgrader;

add_action( 'plugins_loaded', 'my_plugin_init' );

function my_plugin_init() {
// Your class defining your custom DB table.
$table_def = new MyCustomTableDefinition();

// The upgrader: it will upgrade your DB table automatically if the schema changes.
$upgrader = new TableUpgrader( new Table( $table_def ) );
$upgrader->init();
}
```

Please take a look at [this plugin](https://github.com/Screenfeed/autowpdb-example-plugin) to see an example of use.
1 change: 1 addition & 0 deletions Tests/Fixtures/WordPress/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
27 changes: 27 additions & 0 deletions Tests/Fixtures/src/CRUD/CustomCRUD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Screenfeed\AutoWPDB\Tests\Fixtures\src\CRUD;

use Screenfeed\AutoWPDB\CRUD\AbstractCRUD;

class CustomCRUD extends AbstractCRUD {

public function insert( array $data ): int {
return 0;
}

public function replace( array $data ): int {
return 0;
}

public function get( array $select, array $where, string $output_type = OBJECT ) {
return null;
}

public function update( array $data, array $where ) {
return false;
}

public function delete( array $where ) {
return false;
}
}
22 changes: 22 additions & 0 deletions Tests/Fixtures/src/DBWorker/Worker/WorkerIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Screenfeed\AutoWPDB\Tests\Fixtures\src\DBWorker\Worker;

use Screenfeed\AutoWPDB\DBWorker\Worker;

/**
* Allows to perform some Integration tests for Worker->create_table() and Worker->delete_table().
* During the integration tests, WP only allows to create temporary tables, which won't be listed in 'SHOW TABLES'.
*
* @source https://wordpress.stackexchange.com/questions/220275/wordpress-unit-testing-cannot-create-tables
*/
class WorkerIntegration extends Worker {

public function table_exists( string $table_name ): bool {
global $wpdb;

$query = "SELECT * FROM `$table_name` LIMIT 1";
$wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

return "Table '{$wpdb->dbname}.$table_name' doesn't exist" !== $wpdb->last_error;
}
}
120 changes: 120 additions & 0 deletions Tests/Fixtures/src/Table/CustomTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
namespace Screenfeed\AutoWPDB\Tests\Fixtures\src\Table;

use Screenfeed\AutoWPDB\TableDefinition\AbstractTableDefinition;

/**
* Class that defines our custom table.
*
* @since 0.3
*/
class CustomTable extends AbstractTableDefinition {
protected $table_version;
protected $schema;
protected $short_name;
protected $table_is_global;

protected $default_table_version = 102;
protected $default_schema = "
file_id bigint(20) unsigned NOT NULL auto_increment,
file_date datetime NOT NULL default '0000-00-00 00:00:00',
path varchar(191) NOT NULL default '',
mime_type varchar(100) NOT NULL default '',
modified tinyint(1) unsigned NOT NULL default 0,
width smallint(2) unsigned NOT NULL default 0,
height smallint(2) unsigned NOT NULL default 0,
file_size int(4) unsigned NOT NULL default 0,
status varchar(20) default NULL,
error varchar(255) default NULL,
data longtext default NULL,
PRIMARY KEY (file_id),
UNIQUE KEY path (path),
KEY status (status),
KEY modified (modified)";
protected $default_short_name = 'foobar';
protected $default_table_is_global = true;

public function get_table_version(): int {
if ( ! isset( $this->table_version ) ) {
$this->table_version = $this->default_table_version;
}
return $this->table_version;
}

public function get_table_short_name(): string {
if ( ! isset( $this->short_name ) ) {
$this->short_name = $this->default_short_name;
}
return $this->short_name;
}

public function is_table_global(): bool {
if ( ! isset( $this->table_is_global ) ) {
$this->table_is_global = $this->default_table_is_global;
}
return $this->table_is_global;
}

public function get_primary_key(): string {
return 'file_id';
}

public function get_column_placeholders(): array {
return [
'file_id' => '%d',
'file_date' => '%s',
'path' => '%s',
'mime_type' => '%s',
'modified' => '%d',
'width' => '%d',
'height' => '%d',
'file_size' => '%d',
'status' => '%s',
'error' => '%s',
'data' => '%s',
];
}

public function get_column_defaults(): array {
return [
'file_id' => 0,
'file_date' => '0000-00-00 00:00:00',
'path' => '',
'mime_type' => '',
'modified' => 0,
'width' => 0,
'height' => 0,
'file_size' => 0,
'status' => null,
'error' => null,
'data' => [],
];
}

public function get_table_schema(): string {
if ( ! isset( $this->schema ) ) {
$this->schema = $this->default_schema;
}
return $this->schema;
}

/** ----------------------------------------------------------------------------------------- */
/** SETTERS ================================================================================= */
/** ----------------------------------------------------------------------------------------- */

public function set_table_version( $table_version ) {
$this->table_version = $table_version;
}

public function set_table_short_name( $short_name ) {
$this->short_name = $short_name;
}

public function set_table_schema( $schema ) {
$this->schema = $schema;
}

public function set_table_is_global( $table_is_global ) {
$this->table_is_global = $table_is_global;
}
}
89 changes: 89 additions & 0 deletions Tests/Integration/TemporaryTableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Screenfeed\AutoWPDB\Tests\Integration;

trait TemporaryTableTrait {
protected $table_name = 'foobar';
protected $target_table_name = 'targettable';
protected $drop_table = false;
protected $drop_target_table = false;

protected function init_temporary_tables() {
global $wpdb;

$this->table_name = $wpdb->prefix . $this->table_name;
$this->target_table_name = $wpdb->prefix . $this->target_table_name;
}

protected function maybe_drop_temporary_tables() {
global $wpdb;

if ( $this->drop_table ) {
$query = "DROP TEMPORARY TABLE IF EXISTS `{$this->table_name}`";
$result = $wpdb->query( $query );
}

if ( $this->drop_target_table ) {
$query = "DROP TEMPORARY TABLE IF EXISTS `{$this->target_table_name}`";
$result = $wpdb->query( $query );
}
}

protected function create_table( $table_name = '' ) {
global $wpdb;

if ( empty( $table_name ) ) {
$table_name = $this->table_name;
}

$charset_collate = $wpdb->get_charset_collate();
$schema = "
id bigint(20) unsigned NOT NULL auto_increment,
data longtext default NULL,
PRIMARY KEY (id)";

$wpdb->query( "CREATE TEMPORARY TABLE `$table_name` ($schema) $charset_collate" );
}

protected function add_row( $data, $table_name = '' ) {
global $wpdb;

if ( empty( $table_name ) ) {
$table_name = $this->table_name;
}

$wpdb->insert(
$table_name,
[ 'data' => $data ],
[ 'data' => '%s' ]
);

return (int) $wpdb->insert_id;
}

protected function get_rows( $table_name = '' ) {
global $wpdb;

if ( empty( $table_name ) ) {
$table_name = $this->table_name;
}

return $wpdb->get_results(
"SELECT * FROM $table_name ORDER BY `id` ASC",
ARRAY_A
);
}

protected function get_last_row( $table_name = '' ) {
global $wpdb;

if ( empty( $table_name ) ) {
$table_name = $this->table_name;
}

return $wpdb->get_row(
"SELECT * FROM {$table_name} ORDER BY `id` DESC LIMIT 1;",
ARRAY_A
);
}
}
35 changes: 35 additions & 0 deletions Tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Screenfeed\AutoWPDB\Tests\Integration;

use Brain\Monkey;
use Screenfeed\AutoWPDB\Tests\TestCaseTrait;
use WP_UnitTestCase;

abstract class TestCase extends WP_UnitTestCase {
use TestCaseTrait;

/**
* Prepares the test environment before each test.
*/
public function setUp() {
parent::setUp();
Monkey\setUp();
}

/**
* Cleans up the test environment after each test.
*/
public function tearDown() {
Monkey\tearDown();
parent::tearDown();
}

public function return_true() {
return true;
}

public function return_false() {
return false;
}
}
Loading