Refactored unit test to ease fake extension creation
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / tests / db_general_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Xavier Perseguers <typo3@perseguers.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once('BaseTestCase.php');
27
28 /**
29 * Testcase for class ux_t3lib_db.
30 *
31 * $Id$
32 *
33 * @author Xavier Perseguers <typo3@perseguers.ch>
34 *
35 * @package TYPO3
36 * @subpackage dbal
37 */
38 class db_general_testcase extends BaseTestCase {
39
40 /**
41 * @var ux_t3lib_db (extended to make protected methods public)
42 */
43 protected $fixture;
44
45 /**
46 * @var array
47 */
48 protected $loadedExtensions;
49
50 /**
51 * @var array
52 */
53 protected $temporaryFiles;
54
55 /**
56 * Prepares the environment before running a test.
57 */
58 public function setUp() {
59 // Backup list of loaded extensions
60 $this->loadedExtensions = $GLOBALS['TYPO3_LOADED_EXT'];
61 $this->temporaryFiles = array();
62
63 $className = self::buildAccessibleProxy('ux_t3lib_db');
64 $this->fixture = new $className;
65 }
66
67 /**
68 * Cleans up the environment after running a test.
69 */
70 public function tearDown() {
71 // Clear DBAL-generated cache files
72 $this->fixture->clearCachedFieldInfo();
73 // Delete temporary files
74 foreach ($this->temporaryFiles as $filename) unlink($filename);
75 unset($this->fixture);
76 // Restore list of loaded extensions
77 $GLOBALS['TYPO3_LOADED_EXT'] = $this->loadedExtensions;
78 }
79
80 /**
81 * Cleans a SQL query.
82 *
83 * @param mixed $sql
84 * @return mixed (string or array)
85 */
86 private function cleanSql($sql) {
87 if (!is_string($sql)) {
88 return $sql;
89 }
90
91 $sql = str_replace("\n", ' ', $sql);
92 $sql = preg_replace('/\s+/', ' ', $sql);
93 return $sql;
94 }
95
96 /**
97 * Creates a fake extension with a given table definition.
98 *
99 * @param string $tableDefinition SQL script to create the extension's tables
100 * @return void
101 */
102 protected function createFakeExtension($tableDefinition) {
103 // Prepare a fake extension configuration
104 $ext_tables = t3lib_div::tempnam('ext_tables');
105 t3lib_div::writeFile($ext_tables, $tableDefinition);
106 $this->temporaryFiles[] = $ext_tables;
107
108 $GLOBALS['TYPO3_LOADED_EXT']['test_dbal'] = array(
109 'ext_tables.sql' => $ext_tables
110 );
111
112 // Append our test table to the list of existing tables
113 $this->fixture->clearCachedFieldInfo();
114 $this->fixture->_call('initInternalVariables');
115 }
116
117 /**
118 * @test
119 * @see http://bugs.typo3.org/view.php?id=12515
120 */
121 public function concatCanBeParsedAfterLikeOperator() {
122 $query = $this->cleanSql($this->fixture->SELECTquery(
123 '*',
124 'sys_refindex, tx_dam_file_tracking',
125 'sys_refindex.tablename = \'tx_dam_file_tracking\''
126 . ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path, tx_dam_file_tracking.file_name)'
127 ));
128 $expected = 'SELECT * FROM sys_refindex, tx_dam_file_tracking WHERE sys_refindex.tablename = \'tx_dam_file_tracking\'';
129 $expected .= ' AND sys_refindex.ref_string LIKE CONCAT(tx_dam_file_tracking.file_path,tx_dam_file_tracking.file_name)';
130 $this->assertEquals($expected, $query);
131 }
132
133 /**
134 * @test
135 * @see http://bugs.typo3.org/view.php?id=10965
136 */
137 public function floatNumberCanBeStoredInDatabase() {
138 $this->createFakeExtension('
139 CREATE TABLE tx_test_dbal (
140 foo double default \'0\',
141 foobar integer default \'0\'
142 );
143 ');
144 $data = array(
145 'foo' => 99.12,
146 'foobar' => -120,
147 );
148 $query = $this->cleanSql($this->fixture->INSERTquery('tx_test_dbal', $data));
149 $expected = 'INSERT INTO tx_test_dbal ( foo, foobar ) VALUES ( \'99.12\', \'-120\' )';
150 $this->assertEquals($expected, $query);
151 }
152 }
153 ?>