TYPO3 CMS  TYPO3_6-2
PreparedStatementTest.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $databaseStub;
28 
35  protected function setUp() {
36  $this->databaseStub = $this->setUpAndReturnDatabaseStub();
37  }
38 
40  // Utility functions
42 
47  private function setUpAndReturnDatabaseStub() {
48  $GLOBALS['TYPO3_DB'] = $this->getAccessibleMock(
49  'TYPO3\\CMS\\Core\\Database\\DatabaseConnection',
50  array('prepare_PREPAREDquery'),
51  array(),
52  '',
53  FALSE,
54  FALSE
55  );
56 
57  return $GLOBALS['TYPO3_DB'];
58  }
59 
66  private function createPreparedStatement($query) {
67  return new \TYPO3\CMS\Core\Database\PreparedStatement($query, 'pages');
68  }
69 
71  // Tests for the utility functions
73 
79  $this->assertTrue($this->setUpAndReturnDatabaseStub() instanceof \TYPO3\CMS\Core\Database\DatabaseConnection);
80  }
81 
87  $this->assertTrue($this->createPreparedStatement('dummy') instanceof \TYPO3\CMS\Core\Database\PreparedStatement);
88  }
89 
91  // Tests for \TYPO3\CMS\Core\Database\PreparedStatement
93 
101  return array(
102  'one named integer parameter' => array('SELECT * FROM pages WHERE pid=:pid', array(':pid' => 1), 'SELECT * FROM pages WHERE pid=?'),
103  'one unnamed integer parameter' => array('SELECT * FROM pages WHERE pid=?', array(1), 'SELECT * FROM pages WHERE pid=?'),
104  'one named integer parameter is replaced multiple times' => array('SELECT * FROM pages WHERE pid=:pid OR uid=:pid', array(':pid' => 1), 'SELECT * FROM pages WHERE pid=? OR uid=?'),
105  'two named integer parameters are replaced' => array('SELECT * FROM pages WHERE pid=:pid OR uid=:uid', array(':pid' => 1, ':uid' => 10), 'SELECT * FROM pages WHERE pid=? OR uid=?'),
106  'two unnamed integer parameters are replaced' => array('SELECT * FROM pages WHERE pid=? OR uid=?', array(1, 1), 'SELECT * FROM pages WHERE pid=? OR uid=?'),
107  );
108  }
109 
122  $statement = $this->createPreparedStatement($query);
123  $this->databaseStub->expects($this->any())->method('prepare_PREPAREDquery')->with($this->equalTo($expectedResult));
124  $statement->execute($parameters);
125  }
126 
138  public function parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult) {
139  $statement = $this->createPreparedStatement($query);
140  $this->databaseStub->expects($this->any())->method('prepare_PREPAREDquery')->with($this->equalTo($expectedResult));
141  $statement->bindValues($parameters);
142  $statement->execute();
143  }
144 
152  return array(
153  'integer passed with param type NULL' => array(1, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_NULL),
154  'string passed with param type NULL' => array('1', \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_NULL),
155  'bool passed with param type NULL' => array(TRUE, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_NULL),
156  'NULL passed with param type INT' => array(NULL, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_INT),
157  'string passed with param type INT' => array('1', \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_INT),
158  'bool passed with param type INT' => array(TRUE, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_INT),
159  'NULL passed with param type BOOL' => array(NULL, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_BOOL),
160  'string passed with param type BOOL' => array('1', \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_BOOL),
161  'integer passed with param type BOOL' => array(1, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_BOOL)
162  );
163  }
164 
176  public function invalidParameterTypesPassedToBindValueThrowsException($parameter, $type) {
177  $statement = $this->createPreparedStatement('');
178  $statement->bindValue(1, $parameter, $type);
179  }
180 
188  return array(
189  'using other prefix than colon' => array('SELECT * FROM pages WHERE pid=#pid', array('#pid' => 1)),
190  'using non alphanumerical character' => array('SELECT * FROM pages WHERE title=:stra≠e', array(':stra≠e' => 1)),
191  'no colon used' => array('SELECT * FROM pages WHERE pid=pid', array('pid' => 1)),
192  'colon at the end' => array('SELECT * FROM pages WHERE pid=pid:', array('pid:' => 1)),
193  'colon without alphanumerical character' => array('SELECT * FROM pages WHERE pid=:', array(':' => 1))
194  );
195  }
196 
208  $statement = $this->createPreparedStatement($query);
209  $statement->bindValues($parameters);
210  }
211 
212 }
parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
$parameters
Definition: FileDumpEID.php:15
parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]