forked from Big-Shark/forked-php-orm-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbstractTestSuite.php
More file actions
133 lines (105 loc) · 3.51 KB
/
AbstractTestSuite.php
File metadata and controls
133 lines (105 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
require_once __DIR__ . '/sfTimer.php';
abstract class AbstractTestSuite
{
/** @var PDO $con */
protected $con;
protected $products = array();
const NB_TEST = 500;
abstract function initialize();
abstract function clearCache();
abstract function beginTransaction();
abstract function commit();
abstract function insert($i);
abstract function test_insert();
abstract function update($i);
abstract function test_update();
abstract function find($i);
abstract function test_find();
abstract function complexQuery($i);
abstract function test_complexQuery();
abstract function relations($i);
abstract function test_relations();
public function initTables()
{
try {
$this->con->exec('DROP TABLE [products]');
$this->con->exec('DROP TABLE [products_tags]');
$this->con->exec('DROP TABLE [tags]');
$this->con->exec('DROP TABLE [categories]');
$this->con->exec('DROP TABLE [images]');
} catch (PDOException $e) {
// do nothing - the tables probably don't exist yet
}
$this->con->exec('CREATE TABLE [products]
(
[id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[name] VARCHAR(255) NOT NULL,
[sku] VARCHAR(24) NOT NULL,
[price] FLOAT,
[category_id] INTEGER,
FOREIGN KEY (category_id) REFERENCES categories(id)
)');
$this->con->exec('CREATE TABLE [categories]
(
[id] INTEGER PRIMARY KEY AUTOINCREMENT,
[name] VARCHAR(128) NOT NULL
)');
$this->con->exec('CREATE TABLE [images]
(
[id] INTEGER PRIMARY KEY AUTOINCREMENT,
[imageable_id] INTEGER,
[imageable_type] VARCHAR(128),
[path] VARCHAR(128) NOT NULL
)');
$this->con->exec('CREATE TABLE [tags]
(
[id] INTEGER PRIMARY KEY AUTOINCREMENT,
[name] VARCHAR(128) NOT NULL
)');
$this->con->exec('CREATE TABLE [products_tags]
(
[id] INTEGER PRIMARY KEY AUTOINCREMENT,
[product_id] INTEGER,
[tag_id] INTEGER,
[position] INTEGER
)');
}
public function run()
{
$t1 = $this->runMethod('insert');
$t2 = $this->runMethod('update');
$t3 = $this->runMethod('find');
$t4 = $this->runMethod('complexQuery');
$t5 = $this->runMethod('relations');
echo sprintf("| %32s | %6d | %6d | %6d | %6d | %7d |", str_replace('TestSuite', '', get_class($this)), $t1, $t2, $t3, $t4, $t5);
}
public function runMethod($methodName, $nbTest = self::NB_TEST)
{
// prepare method are used to isolate some operations outside the tests
// for the getters test we isolate the retrieval of the object from the db
$prepareMethod = 'prepare_' . $methodName;
if (method_exists($this, $prepareMethod)) {
$this->$prepareMethod();
}
$testMethod = 'test_' . $methodName;
$this->$testMethod();
$timer = new sfTimer();
$this->clearCache();
for ($i = 0; $i < $nbTest; $i++) {
$this->$methodName($i);
}
$t = $timer->getElapsedTime();
return $t * 1000;
}
public function assertEquals($expected, $actual, $message = null) {
if ($expected != $actual) {
throw new Exception($message ?? sprintf('%s is not the same %s', $expected, $actual));
}
}
public function assertNotNull($actual, $message = null) {
if (null == $actual) {
throw new Exception($message ?? sprintf('%s is null or zero', $actual));
}
}
}