Skip to content

Commit 07ad3de

Browse files
committed
Configure tests for multiple server types
1 parent 1d05338 commit 07ad3de

File tree

15 files changed

+650
-61
lines changed

15 files changed

+650
-61
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ composer.lock
55
.php-cs-fixer.cache
66
.phpunit.cache
77
phpunit.xml
8-
8+
*.sqlite

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# PHPFUI\ORM [![Tests](https://github.com/phpfui/ORM/actions/workflows/tests.yml/badge.svg)](https://github.com/phpfui/ORM/actions?query=workflow%3Atests) [![Latest Packagist release](https://img.shields.io/packagist/v/phpfui/ORM.svg)](https://packagist.org/packages/phpfui/ORM) ![](https://img.shields.io/badge/PHPStan-level%206-brightgreen.svg?style=flat)
22

3-
### PHPFUI\ORM a minimal Object Relational Mapper (ORM) for MySQL, MariaDB and SQLite3
3+
### PHPFUI\ORM a minimal Object Relational Mapper (ORM) for MySQL, MariaDB, PostGre and SQLite3
44
Why another PHP ORM? In writing minimal and fast websites, it was determined that existing PHP ORM solutions were overly complex. **PHPFUI\ORM** is a little more than 6.7K lines of code in 50 files. It is designed to have a minimal memory footprint and excellent execution times for most database needs.
55

6-
Performance [comparison of PHPFUI\ORM to Eloquent](https://github.com/phpfui/php-orm-sql-benchmarks) for different SQL implementations.
6+
**PHPFUI\ORM** demonstrates superior performance for both speed and memory usage verses other ORMs. This is proven by a [comparison of PHPFUI\ORM to Other ORMs](https://github.com/phpfui/php-orm-sql-benchmarks) for different SQL implementations.
77

88
**PHPFUI\ORM** is not an attempt to write an abstraction around SQL as other ORMs do, rather it is a way to work with SQL that closely matches the semantics of SQL, with the power of PHP objects. It allows PHP to manipulate SQL queries without having to write SQL in plain text. This is very useful for queries generated via user interfaces where the user is given a lot of flexability in how a query is defined.
99

@@ -23,7 +23,7 @@ You will need to run the \PHPFUI\ORM\Tool\Generate\CRUD class against your datab
2323
- **Injection Safe** Uses PDO placeholders and field sanitation to prevent injection attacks.
2424
- **Raw SQL Query Support** Execute any valid SQL command.
2525
- **Multiple Database Support** Work with multiple databases simultaneously.
26-
- **Multi-Vendor Support** Built on PDO with support for MySQL, MariaDB and SQLite.
26+
- **Multi-Vendor Support** Built on PDO with support for MySQL, MariaDB, PostGre and SQLite.
2727

2828
## Usage
2929
### Setup

Tests/Fixtures/Record/Definition/Order.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public function initFieldDefinitions() : static
4848
if (! \count(static::$fields))
4949
{
5050
static::$fields = [
51-
'customer_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
52-
'employee_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
51+
'customer_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
52+
'employee_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
5353
'notes' => new \PHPFUI\ORM\FieldDefinition('longtext', 'string', 4294967295, true, null, ),
54-
'order_date' => new \PHPFUI\ORM\FieldDefinition('datetime', 'string', 20, false, null, ),
54+
'order_date' => new \PHPFUI\ORM\FieldDefinition('datetime', 'string', 20, false, 'CURRENT_TIMESTAMP', ),
5555
'order_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, false, ),
5656
'order_status_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, 0, ),
57-
'order_tax_status_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
57+
'order_tax_status_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
5858
'paid_date' => new \PHPFUI\ORM\FieldDefinition('datetime', 'string', 20, true, null, ),
5959
'payment_type' => new \PHPFUI\ORM\FieldDefinition('varchar(50)', 'string', 50, true, null, ),
6060
'ship_address' => new \PHPFUI\ORM\FieldDefinition('longtext', 'string', 4294967295, true, null, ),
@@ -64,10 +64,10 @@ public function initFieldDefinitions() : static
6464
'ship_state_province' => new \PHPFUI\ORM\FieldDefinition('varchar(50)', 'string', 50, true, null, ),
6565
'ship_zip_postal_code' => new \PHPFUI\ORM\FieldDefinition('varchar(50)', 'string', 50, true, null, ),
6666
'shipped_date' => new \PHPFUI\ORM\FieldDefinition('datetime', 'string', 20, true, null, ),
67-
'shipper_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
68-
'shipping_fee' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 19, true, 0.0000, ),
67+
'shipper_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
68+
'shipping_fee' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 20, true, 0.0000, ),
6969
'tax_rate' => new \PHPFUI\ORM\FieldDefinition('double', 'float', 0, true, 0, ),
70-
'taxes' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 19, true, 0.0000, ),
70+
'taxes' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 20, true, 0.0000, ),
7171
];
7272
}
7373

Tests/Fixtures/Record/Definition/OrderDetail.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public function initFieldDefinitions() : static
4040
static::$fields = [
4141
'date_allocated' => new \PHPFUI\ORM\FieldDefinition('datetime', 'string', 20, true, null, ),
4242
'discount' => new \PHPFUI\ORM\FieldDefinition('double', 'float', 0, false, 0, ),
43-
'inventory_transaction_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
43+
'inventory_transaction_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
4444
'order_detail_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, false, ),
45-
'order_detail_status_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
45+
'order_detail_status_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
4646
'order_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, false, ),
47-
'product_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
48-
'purchase_order_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
49-
'quantity' => new \PHPFUI\ORM\FieldDefinition('decimal(18,4)', 'float', 18, false, 0.0000, ),
50-
'unit_price' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 19, true, 0.0000, ),
47+
'product_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
48+
'purchase_order_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
49+
'quantity' => new \PHPFUI\ORM\FieldDefinition('decimal(18,4)', 'float', 19, false, 0.0000, ),
50+
'unit_price' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 20, true, 0.0000, ),
5151
];
5252
}
5353

Tests/Fixtures/Record/Definition/Product.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function initFieldDefinitions() : static
4040
'category' => new \PHPFUI\ORM\FieldDefinition('varchar(50)', 'string', 50, true, null, ),
4141
'description' => new \PHPFUI\ORM\FieldDefinition('longtext', 'string', 4294967295, true, null, ),
4242
'discontinued' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, false, 0, ),
43-
'list_price' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 19, false, 0.0000, ),
44-
'minimum_reorder_quantity' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
43+
'list_price' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 20, false, 0.0000, ),
44+
'minimum_reorder_quantity' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
4545
'product_code' => new \PHPFUI\ORM\FieldDefinition('varchar(25)', 'string', 25, true, null, ),
4646
'product_id' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, false, ),
4747
'product_name' => new \PHPFUI\ORM\FieldDefinition('varchar(50)', 'string', 50, true, null, ),
4848
'quantity_per_unit' => new \PHPFUI\ORM\FieldDefinition('varchar(50)', 'string', 50, true, null, ),
49-
'reorder_level' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
50-
'standard_cost' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 19, true, 0.0000, ),
51-
'target_level' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, null, ),
49+
'reorder_level' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
50+
'standard_cost' => new \PHPFUI\ORM\FieldDefinition('decimal(19,4)', 'float', 20, true, 0.0000, ),
51+
'target_level' => new \PHPFUI\ORM\FieldDefinition('integer', 'int', 0, true, NULL, ),
5252
];
5353
}
5454

Tests/Unit/DeleteTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public function testDeleteChildren() : void
1515
$order->delete();
1616
$this->assertCount(0, $orderDetailTable);
1717
$this->assertTrue($transaction->rollBack());
18-
$this->assertCount(3, $orderDetailTable);
18+
$order = new \Tests\Fixtures\Record\Order(31);
19+
$this->assertTrue($order->loaded(), 'Order 31 was not reloaded after rollback');
20+
$this->assertCount(3, $order->orderDetailChildren);
1921
}
2022

2123
public function testRecordDelete() : void

Tests/Unit/MigrationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class MigrationTest extends \PHPUnit\Framework\TestCase
66
{
77
public function testMigrations() : void
88
{
9-
$transaction = new \PHPFUI\ORM\Transaction();
109
$tables = \PHPFUI\ORM::getTables();
1110
$this->assertContains('migration', $tables);
1211
$migrator = new \PHPFUI\ORM\Migrator();

Tests/bootstrap.php

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,56 @@
22

33
\error_reporting(E_ALL);
44

5-
// allow the autoloader to be included from any script that needs it.
5+
// setup autoloader including vendors
66
function autoload(string $className) : void
77
{
88
$path = \str_replace('\\', DIRECTORY_SEPARATOR, __DIR__ . "/../{$className}.php");
99

1010
@include_once $path;
1111
}
12-
1312
\spl_autoload_register('autoload');
14-
1513
include __DIR__ . '//..//vendor//autoload.php';
1614

15+
16+
function getSqlFile(string $baseName, string $driver) : string
17+
{
18+
$schema = $baseName . '.sql';
19+
$file = __DIR__ . '/../northwind/' . $driver . '/' . $schema;
20+
21+
if (\file_exists($file))
22+
{
23+
$schema = $file;
24+
}
25+
else
26+
{
27+
$schema = __DIR__ . '/../northwind/' . $schema;
28+
}
29+
30+
return $schema;
31+
}
32+
33+
function loadFile(string $file) : void
34+
{
35+
$sql = '';
36+
37+
foreach (\file($file) as $line)
38+
{
39+
// Ignoring comments from the SQL script
40+
if (\str_starts_with((string)$line, '--') || '' == $line || \str_starts_with((string)$line, '#'))
41+
{
42+
continue;
43+
}
44+
45+
$sql .= $line;
46+
47+
if (\str_ends_with(\trim((string)$line), ';'))
48+
{
49+
\PHPFUI\ORM::execute($sql);
50+
$sql = '';
51+
}
52+
} // end foreach
53+
}
54+
1755
// generate models and validators
1856

1957
$files = new RecursiveIteratorIterator(
@@ -29,7 +67,25 @@ function autoload(string $className) : void
2967
}
3068
}
3169

32-
$pdo = new \PHPFUI\ORM\PDOInstance('sqlite:' . __DIR__ . '/../northwind/northwind.db');
70+
71+
$config = include __DIR__ . '/../testConfig.php';
72+
$dsn = $driver = $config['dsn'];
73+
$driver = \substr($dsn, 0, \strpos($dsn, ':'));
74+
75+
$schemaFile = \getSqlFile('schema', $driver);
76+
$dataFile = \getSqlFile('data', $driver);
77+
78+
if ('sqlite' == $driver && ! \str_contains($dsn, ':memory:'))
79+
{
80+
$sqliteFile = \substr($dsn, \strpos($dsn, ':') + 1);
81+
\fclose(\fopen($sqliteFile, 'w'));
82+
}
83+
84+
$pdo = new \PHPFUI\ORM\PDOInstance($dsn, $config['name'], $config['key']);
85+
if ('mysql' == $driver)
86+
{
87+
$pdo->execute('set autocommit=0');
88+
}
3389

3490
\PHPFUI\ORM::addConnection($pdo);
3591
\PHPFUI\ORM::$namespaceRoot = __DIR__ . '/..';
@@ -42,6 +98,11 @@ function autoload(string $className) : void
4298
\PHPFUI\Translation\Translator::setTranslationDirectory(__DIR__ . '/../translations');
4399
\PHPFUI\Translation\Translator::setLocale('en_US');
44100

101+
// load schema
102+
\loadFile($schemaFile);
103+
// load data
104+
\loadFile($dataFile);
105+
45106
$tables = \PHPFUI\ORM::getTables();
46107

47108
if (! \count($tables))

northwind/northwind-data.sql renamed to northwind/data.sql

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
#
2-
# Converted from MS Access 2010 Northwind database (northwind.accdb) using
3-
# Bullzip MS Access to MySQL Version 5.1.242. http://www.bullzip.com
4-
#
5-
# CHANGES MADE AFTER INITIAL CONVERSION
6-
# * column and row names in CamelCase converted to lower_case_with_underscore
7-
# * space and slash ("/") in table and column names replaced with _underscore_
8-
# * tables converted to singular
9-
# * id column names converted to "table_name_id"
10-
# * foreign key column names converted to xxx_id
11-
# * variables of type TIMESTAMP converted to DATETIME to avoid TIMESTAMP
12-
# range limitation (1997 - 2038 UTC), and other limitations.
13-
# * unique and foreign key checks disabled while loading data
14-
#
15-
#------------------------------------------------------------------
16-
#
17-
18-
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
19-
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
20-
21-
USE `northwind`;
1+
-- SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
2+
-- SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
223

234
# insert all the tables with no related data dependancies first
245

@@ -712,6 +693,6 @@ INSERT INTO `invoice` (`invoice_id`, `order_id`, `invoice_date`, `due_date`, `ta
712693
INSERT INTO `invoice` (`invoice_id`, `order_id`, `invoice_date`, `due_date`, `tax`, `shipping`, `amount_due`) VALUES (39, 58, '2006-04-04 11:43:08', NULL, 0, 0, 0);
713694
# 35 records
714695

715-
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
716-
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
696+
--SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
697+
--SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
717698

0 commit comments

Comments
 (0)