Skip to content

Commit 5f98f18

Browse files
committed
Adding database integration tests with sqlite3 db
1 parent e4c5614 commit 5f98f18

File tree

13 files changed

+250
-3
lines changed

13 files changed

+250
-3
lines changed

.zfproject.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<layoutsDirectory enabled="false"/>
2020
<modelsDirectory>
2121
<modelFile modelName="Comment"/>
22+
<dbTableDirectory>
23+
<dbTableFile dbTableName="Comment"/>
24+
</dbTableDirectory>
25+
<modelFile modelName="CommentMapper"/>
2226
</modelsDirectory>
2327
<modulesDirectory enabled="false"/>
2428
<viewsDirectory>

application/configs/application.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ autoloadernamespaces[] = "Zftest_"
1616
phpSettings.display_startup_errors = 1
1717
phpSettings.display_errors = 1
1818

19+
resources.db.adapter = "Pdo_Sqlite"
20+
resources.db.params.dbname = APPLICATION_PATH "/data/db/zftest-test.db"
21+
1922
[development : production]
2023
phpSettings.display_startup_errors = 1
2124
phpSettings.display_errors = 1

application/data/db/zftest-test.db

6 KB
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
class Application_Model_CommentMapper
4+
{
5+
protected $_dbTable;
6+
7+
public function setDbTable($dbTable)
8+
{
9+
if (is_string($dbTable)) {
10+
$dbTable = new $dbTable;
11+
}
12+
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
13+
throw new Zend_Exception('Invalid data gateway provided');
14+
}
15+
$this->_dbTable = $dbTable;
16+
return $this;
17+
}
18+
public function getDbTable()
19+
{
20+
if (null === $this->_dbTable) {
21+
$this->setDbTable('Application_Model_DbTable_Comment');
22+
}
23+
return $this->_dbTable;
24+
}
25+
public function find($id, Application_Model_Comment $comment)
26+
{
27+
$resultSet = $this->getDbTable()->find($id);
28+
if (!empty ($resultSet)) {
29+
$comment->populate($resultSet->current());
30+
}
31+
return $this;
32+
}
33+
public function fetchRow(Application_Model_Comment $comment, $where = null, $order = null)
34+
{
35+
$row = $this->getDbTable()->fetchRow($where, $order);
36+
if (!empty ($row)) {
37+
$comment->populate($row);
38+
}
39+
return $this;
40+
}
41+
public function fetchAll($where = null, $order = null, $count = null, $offset = null)
42+
{
43+
$entries = array ();
44+
$resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
45+
foreach ($resultSet as $row) {
46+
$entries[] = new Application_Model_Comment($row);
47+
}
48+
return $entries;
49+
}
50+
public function save(Application_Model_Comment $comment)
51+
{
52+
$data = $comment->toArray();
53+
unset ($data['id']);
54+
if (0 < $comment->getId()) {
55+
$this->getDbTable()->update($data, array ('id = ?' => $comment->getId()));
56+
} else {
57+
$this->getDbTable()->insert($data);
58+
}
59+
}
60+
public function delete(Application_Model_Comment $comment)
61+
{
62+
$this->getDbTable()->delete(array ('id = ?' => $comment->getId()));
63+
return $this;
64+
}
65+
}
66+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
class Application_Model_DbTable_Comment extends Zend_Db_Table_Abstract
4+
{
5+
6+
protected $_name = 'comment';
7+
8+
9+
}
10+

docs/schema.sqlite.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE `comment` (
2+
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
3+
`fullName` TEXT NOT NULL,
4+
`emailAddress` TEXT NOT NULL,
5+
`website` TEXT NULL,
6+
`comment` TEXT NULL
7+
);

library/Zftest/Validate/TextBox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Zftest_Validate_TextBox extends Zend_Validate_Abstract
77
public $max;
88

99
protected $_messageVariables = array (
10-
'max' => 'maximum',
10+
self::MSG_INVALID_SIZE => 'max',
1111
);
1212

1313
protected $_messageTemplates = array (
@@ -29,7 +29,7 @@ public function isValid($value)
2929
return false;
3030
}
3131

32-
$regex = '/([a-zA-Z0-9\s\-\_\'\"\.\[\]\{\}\#\!\?\:\;\*\/\n<?\/b|i>]+)/';
32+
$regex = '/([a-zA-Z0-9\s\-\_\'\"\.\[\]\{\}\#\!\?\:\;\,\*\/\n<?\/b|i>]+)/';
3333
$test = preg_match($regex, $value, $match);
3434
if (isset ($match[1]) && 0 === strcmp($value, $match[1])) {
3535
return true;

tests/_files/addDataSet.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<dataset>
3+
<comment
4+
fullName="B.A. Baracus"
5+
emailAddress="[email protected]"
6+
website="http://www.a-team.com"
7+
comment="I pitty the fool that doesn't test!"/>
8+
<comment
9+
fullName="Martin Fowler"
10+
emailAddress="[email protected]"
11+
website="http://martinfowler.com/"
12+
comment="Models are not right or wrong; they are more or less useful."/>
13+
<comment
14+
fullName="Michelangelo van Dam"
15+
emailAddress="[email protected]"
16+
website="http://www.dragonbe.com"
17+
comment="Unit Testing, It is so addictive!!!"/>
18+
</dataset>

tests/_files/deleteDataSet.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<dataset>
3+
<comment
4+
id="2"
5+
fullName="Martin Fowler"
6+
emailAddress="[email protected]"
7+
website="http://martinfowler.com/"
8+
comment="Models are not right or wrong; they are more or less useful."/>
9+
</dataset>

tests/_files/initialDataSet.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<dataset>
3+
<comment
4+
id="1"
5+
fullName="B.A. Baracus"
6+
emailAddress="[email protected]"
7+
website="http://www.a-team.com"
8+
comment="I pitty the fool that doesn't test!"/>
9+
<comment
10+
id="2"
11+
fullName="Martin Fowler"
12+
emailAddress="[email protected]"
13+
website="http://martinfowler.com/"
14+
comment="Models are not right or wrong; they are more or less useful."/>
15+
</dataset>

0 commit comments

Comments
 (0)