TYPO3 CMS  TYPO3_7-6
PreparedStatementTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
25  protected $databaseStub;
26 
33  protected function setUp()
34  {
35  $this->databaseStub = $this->setUpAndReturnDatabaseStub();
36  }
37 
39  // Utility functions
41 
46  private function setUpAndReturnDatabaseStub()
47  {
48  $GLOBALS['TYPO3_DB'] = $this->getAccessibleMock(
49  \TYPO3\CMS\Core\Database\DatabaseConnection::class,
50  ['prepare_PREPAREDquery'],
51  [],
52  '',
53  false,
54  false
55  );
56 
57  return $GLOBALS['TYPO3_DB'];
58  }
59 
66  private function createPreparedStatement($query)
67  {
68  return new \TYPO3\CMS\Core\Database\PreparedStatement($query, 'pages');
69  }
70 
72  // Tests for the utility functions
74 
80  {
81  $this->assertTrue($this->setUpAndReturnDatabaseStub() instanceof \TYPO3\CMS\Core\Database\DatabaseConnection);
82  }
83 
89  {
90  $this->assertTrue($this->createPreparedStatement('dummy') instanceof \TYPO3\CMS\Core\Database\PreparedStatement);
91  }
92 
94  // Tests for \TYPO3\CMS\Core\Database\PreparedStatement
96 
104  {
105  return [
106  'one named integer parameter' => ['SELECT * FROM pages WHERE pid=:pid', [':pid' => 1], 'SELECT * FROM pages WHERE pid=?'],
107  'one unnamed integer parameter' => ['SELECT * FROM pages WHERE pid=?', [1], 'SELECT * FROM pages WHERE pid=?'],
108  'one named integer parameter is replaced multiple times' => ['SELECT * FROM pages WHERE pid=:pid OR uid=:pid', [':pid' => 1], 'SELECT * FROM pages WHERE pid=? OR uid=?'],
109  'two named integer parameters are replaced' => ['SELECT * FROM pages WHERE pid=:pid OR uid=:uid', [':pid' => 1, ':uid' => 10], 'SELECT * FROM pages WHERE pid=? OR uid=?'],
110  'two unnamed integer parameters are replaced' => ['SELECT * FROM pages WHERE pid=? OR uid=?', [1, 1], 'SELECT * FROM pages WHERE pid=? OR uid=?'],
111  ];
112  }
113 
125  public function parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
126  {
127  $statement = $this->createPreparedStatement($query);
128  $this->databaseStub->expects($this->any())->method('prepare_PREPAREDquery')->with($this->equalTo($expectedResult));
129  $statement->execute($parameters);
130  }
131 
143  public function parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
144  {
145  $statement = $this->createPreparedStatement($query);
146  $this->databaseStub->expects($this->any())->method('prepare_PREPAREDquery')->with($this->equalTo($expectedResult));
147  $statement->bindValues($parameters);
148  $statement->execute();
149  }
150 
158  {
159  return [
160  'integer passed with param type NULL' => [1, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_NULL],
161  'string passed with param type NULL' => ['1', \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_NULL],
162  'bool passed with param type NULL' => [true, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_NULL],
163  'NULL passed with param type INT' => [null, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_INT],
164  'string passed with param type INT' => ['1', \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_INT],
165  'bool passed with param type INT' => [true, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_INT],
166  'NULL passed with param type BOOL' => [null, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_BOOL],
167  'string passed with param type BOOL' => ['1', \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_BOOL],
168  'integer passed with param type BOOL' => [1, \TYPO3\CMS\Core\Database\PreparedStatement::PARAM_BOOL]
169  ];
170  }
171 
183  public function invalidParameterTypesPassedToBindValueThrowsException($parameter, $type)
184  {
185  $statement = $this->createPreparedStatement('');
186  $statement->bindValue(1, $parameter, $type);
187  }
188 
196  {
197  return [
198  'using other prefix than colon' => ['SELECT * FROM pages WHERE pid=#pid', ['#pid' => 1]],
199  'using non alphanumerical character' => ['SELECT * FROM pages WHERE title=:stra≠e', [':stra≠e' => 1]],
200  'no colon used' => ['SELECT * FROM pages WHERE pid=pid', ['pid' => 1]],
201  'colon at the end' => ['SELECT * FROM pages WHERE pid=pid:', ['pid:' => 1]],
202  'colon without alphanumerical character' => ['SELECT * FROM pages WHERE pid=:', [':' => 1]]
203  ];
204  }
205 
216  public function passingInvalidMarkersThrowsException($query, $parameters)
217  {
218  $statement = $this->createPreparedStatement($query);
219  $statement->bindValues($parameters);
220  }
221 }
parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']