Skip to content

Commit abd9add

Browse files
committed
Fix tests
1 parent 0d33410 commit abd9add

File tree

4 files changed

+74
-18
lines changed

4 files changed

+74
-18
lines changed

src/wcmf/lib/core/impl/DefaultSession.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,25 @@ class DefaultSession implements Session {
3434
* @param $configuration
3535
*/
3636
public function __construct(Configuration $configuration) {
37-
// session configuration
38-
ini_set('session.cookie_lifetime', 0);
39-
ini_set('session.use_cookies', 1);
40-
ini_set('session.use_only_cookies', 1);
41-
ini_set('session.use_strict_mode', 1);
42-
ini_set('session.cookie_httponly', 1);
43-
ini_set('session.cookie_secure', (URIUtil::isHttps() ? 1 : 0));
44-
ini_set('session.use_trans_sid', 0);
45-
ini_set('session.cache_limiter', 'nocache');
46-
ini_set('session.hash_function', 1);
47-
if (in_array('sha256', hash_algos())) {
48-
ini_set('session.hash_function', 'sha256');
49-
}
50-
$this->cookiePrefix = strtolower(StringUtil::slug($configuration->getValue('title', 'application')));
37+
// NOTE: prevent "headers already sent" errors in phpunit tests
38+
if (!headers_sent()) {
39+
// session configuration
40+
ini_set('session.cookie_lifetime', 0);
41+
ini_set('session.use_cookies', 1);
42+
ini_set('session.use_only_cookies', 1);
43+
ini_set('session.use_strict_mode', 1);
44+
ini_set('session.cookie_httponly', 1);
45+
ini_set('session.cookie_secure', (URIUtil::isHttps() ? 1 : 0));
46+
ini_set('session.use_trans_sid', 0);
47+
ini_set('session.cache_limiter', 'nocache');
48+
ini_set('session.hash_function', 1);
49+
if (in_array('sha256', hash_algos())) {
50+
ini_set('session.hash_function', 'sha256');
51+
}
52+
$this->cookiePrefix = strtolower(StringUtil::slug($configuration->getValue('title', 'application')));
5153

52-
session_name($this->getCookieName());
54+
session_name($this->getCookieName());
55+
}
5356
}
5457

5558
public function __destruct() {

src/wcmf/lib/model/mapper/SelectStatement.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function getParameters($stripColons=false) {
157157
public function getRowCount() {
158158
$mapper = ObjectFactory::getInstance('persistenceFacade')->getMapper($this->type);
159159
if ($this->table) {
160-
$table = !is_array($this->table) ? $this-> table : key($this->table);
160+
$table = !is_array($this->table) ? $this->table : key($this->table);
161161
// use pk columns for counting
162162
$countColumns = array_map(function($valueName) use ($mapper, $table) {
163163
return $mapper->quoteIdentifier($table).'.'.$mapper->quoteIdentifier($mapper->getAttribute($valueName)->getColumn());
@@ -191,6 +191,8 @@ public function getRowCount() {
191191
try {
192192
$result = $countStatement->query();
193193
$row = $result->fetch();
194+
// update cache with count query
195+
$this->cachedSql = array_merge($this->cachedSql, $countStatement->cachedSql);
194196
return $row['nRows'];
195197
}
196198
catch (\Exception $ex) {
@@ -272,6 +274,10 @@ public function getAliasNames($table) {
272274
* @return String
273275
*/
274276
public function getSql() {
277+
if ($this->id == self::NO_CACHE) {
278+
return trim((new Sql($this->getAdapter()))->buildSqlString($this));
279+
}
280+
275281
$cacheKey = self::getCacheId($this->id);
276282
if (!isset($this->cachedSql[$cacheKey])) {
277283
$sql = trim((new Sql($this->getAdapter()))->buildSqlString($this));
@@ -328,7 +334,7 @@ protected static function getCacheId($id) {
328334
*/
329335

330336
public function __sleep() {
331-
return ['id', 'type', 'meta', 'cachedSql', 'specifications'];
337+
return ['id', 'type', 'table', 'meta', 'cachedSql', 'specifications'];
332338
}
333339

334340
public function __wakeup() {

src/wcmf/test/configuration.xml

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<file>tests/persistence/PersistentObjectTest.php</file>
5252
<file>tests/persistence/RDBMapperTest.php</file>
5353
<file>tests/persistence/RelationDescriptionTest.php</file>
54+
<file>tests/persistence/SelectTest.php</file>
5455
<file>tests/persistence/SortTest.php</file>
5556
<file>tests/persistence/StringQueryTest.php</file>
5657
<file>tests/persistence/TransactionTest.php</file>

src/wcmf/test/tests/persistence/SelectTest.php

+47-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function getDataSet() {
6464
]);
6565
}
6666

67-
public function testSimple() {
67+
public function testWithCache() {
6868
$selectId = __CLASS__.__METHOD__;
6969
$time = time();
7070

@@ -76,12 +76,15 @@ public function testSimple() {
7676
$select->columns(['id']);
7777
$select->where('id = :id');
7878
$select->setParameters([':id' => 203]);
79+
$this->assertEquals(1, $select->getRowCount());
80+
7981
$result = $select->query();
8082
$rows = $result->fetchAll();
8183

8284
$this->assertEquals(1, sizeof($rows));
8385
$this->assertEquals(203, $rows[0][0]);
8486
$this->assertEquals(203, $rows[0]['id']);
87+
$this->assertEquals(1, $select->getRowCount());
8588

8689
// test cache
8790
$select->save();
@@ -90,6 +93,49 @@ public function testSimple() {
9093
$select2 = SelectStatement::get($mapper, $selectId);
9194
$this->assertEquals($selectId, $select2->getId());
9295
$this->assertEquals($time, $select2->getMeta('created'));
96+
97+
$select2->where('id = :id');
98+
$select2->setParameters([':id' => 203]);
99+
$this->assertEquals(1, $select2->getRowCount());
100+
101+
$result2 = $select2->query();
102+
$rows2 = $result2->fetchAll();
103+
104+
$this->assertEquals(1, sizeof($rows2));
105+
$this->assertEquals(203, $rows2[0][0]);
106+
$this->assertEquals(203, $rows2[0]['id']);
107+
$this->assertEquals(1, $select2->getRowCount());
108+
}
109+
110+
public function testWithoutCache() {
111+
$selectId = __CLASS__.__METHOD__;
112+
$time = time();
113+
114+
$persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
115+
$mapper = $persistenceFacade->getMapper('Chapter');
116+
$select = SelectStatement::get($mapper);
117+
$select->from('Chapter');
118+
$this->assertEquals(3, $select->getRowCount());
119+
120+
$result = $select->query();
121+
$rows = $result->fetchAll();
122+
123+
$this->assertEquals(3, sizeof($rows));
124+
$this->assertEquals(300, $rows[0][0]);
125+
$this->assertEquals(300, $rows[0]['id']);
126+
$this->assertEquals(302, $rows[1][0]);
127+
$this->assertEquals(302, $rows[1]['id']);
128+
$this->assertEquals(303, $rows[2][0]);
129+
$this->assertEquals(303, $rows[2]['id']);
130+
$this->assertEquals(3, $select->getRowCount());
131+
132+
// test cache
133+
$select->save();
134+
$this->assertFalse($select->isCached());
135+
136+
$select2 = SelectStatement::get($mapper);
137+
$select2->from('Chapter');
138+
$this->assertEquals(3, $select2->getRowCount());
93139
}
94140

95141
public function testMapper() {

0 commit comments

Comments
 (0)