TYPO3 CMS  TYPO3_8-7
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 
19 
23 class PreparedStatementTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
28  protected $databaseStub;
29 
34  protected function setUp()
35  {
36  $this->databaseStub = $this->setUpAndReturnDatabaseStub();
37  }
38 
40  // Utility functions
42 
47  private function setUpAndReturnDatabaseStub()
48  {
49  $GLOBALS['TYPO3_DB'] = $this->getAccessibleMock(
50  DatabaseConnection::class,
51  ['prepare_PREPAREDquery'],
52  [],
53  '',
54  false,
55  false
56  );
57 
58  return $GLOBALS['TYPO3_DB'];
59  }
60 
67  private function createPreparedStatement($query)
68  {
69  return new PreparedStatement($query, 'pages');
70  }
71 
73  // Tests for the utility functions
75 
80  {
81  $this->assertTrue($this->setUpAndReturnDatabaseStub() instanceof DatabaseConnection);
82  }
83 
88  {
89  $this->assertTrue($this->createPreparedStatement('dummy') instanceof PreparedStatement);
90  }
91 
93  // Tests for \TYPO3\CMS\Core\Database\PreparedStatement
95 
103  {
104  return [
105  'one named integer parameter' => [
106  'SELECT * FROM pages WHERE pid=:pid',
107  [':pid' => 1],
108  'SELECT * FROM pages WHERE pid=?'
109  ],
110  'one unnamed integer parameter' => [
111  'SELECT * FROM pages WHERE pid=?',
112  [1],
113  'SELECT * FROM pages WHERE pid=?'
114  ],
115  'one named integer parameter is replaced multiple times' => [
116  'SELECT * FROM pages WHERE pid=:pid OR uid=:pid',
117  [':pid' => 1],
118  'SELECT * FROM pages WHERE pid=? OR uid=?'
119  ],
120  'two named integer parameters are replaced' => [
121  'SELECT * FROM pages WHERE pid=:pid OR uid=:uid',
122  [':pid' => 1, ':uid' => 10],
123  'SELECT * FROM pages WHERE pid=? OR uid=?'
124  ],
125  'two unnamed integer parameters are replaced' => [
126  'SELECT * FROM pages WHERE pid=? OR uid=?',
127  [1, 1],
128  'SELECT * FROM pages WHERE pid=? OR uid=?'
129  ],
130  ];
131  }
132 
143  public function parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
144  {
145  $statement = $this->createPreparedStatement($query);
146  $this->databaseStub->expects($this->any())
147  ->method('prepare_PREPAREDquery')
148  ->with($this->equalTo($expectedResult));
149  $statement->execute($parameters);
150  }
151 
162  public function parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
163  {
164  $statement = $this->createPreparedStatement($query);
165  $this->databaseStub->expects($this->any())
166  ->method('prepare_PREPAREDquery')
167  ->with($this->equalTo($expectedResult));
168  $statement->bindValues($parameters);
169  $statement->execute();
170  }
171 
179  {
180  return [
181  'integer passed with param type NULL' => [
182  1,
184  1282489834
185  ],
186  'string passed with param type NULL' => [
187  '1',
189  1282489834
190  ],
191  'bool passed with param type NULL' => [
192  true,
194  1282489834
195  ],
196  'NULL passed with param type INT' => [
197  null,
199  1281868686
200  ],
201  'string passed with param type INT' => [
202  '1',
204  1281868686
205  ],
206  'bool passed with param type INT' => [
207  true,
209  1281868686
210  ],
211  'NULL passed with param type BOOL' => [
212  null,
214  1281868687
215  ],
216  'string passed with param type BOOL' => [
217  '1',
219  1281868687
220  ],
221  'integer passed with param type BOOL' => [
222  1,
224  1281868687
225  ]
226  ];
227  }
228 
239  public function invalidParameterTypesPassedToBindValueThrowsException($parameter, $type, $exceptionCode)
240  {
241  $this->expectException(\InvalidArgumentException::class);
242  $this->expectExceptionCode($exceptionCode);
243 
244  $statement = $this->createPreparedStatement('');
245  $statement->bindValue(1, $parameter, $type);
246  }
247 
255  {
256  return [
257  'using other prefix than colon' => [
259  'SELECT * FROM pages WHERE pid=#pid',
260  ['#pid' => 1]
261  ],
262  'using non alphanumerical character' => [
264  'SELECT * FROM pages WHERE title=:stra≠e',
265  [':stra≠e' => 1]
266  ],
267  'no colon used' => [
269  'SELECT * FROM pages WHERE pid=pid',
270  ['pid' => 1]
271  ],
272  'colon at the end' => [
274  'SELECT * FROM pages WHERE pid=pid:',
275  ['pid:' => 1]
276  ],
277  'colon without alphanumerical character' => [
279  'SELECT * FROM pages WHERE pid=:',
280  [':' => 1]
281  ]
282  ];
283  }
284 
293  public function passingInvalidMarkersThrowsException($query, $parameters)
294  {
295  $this->expectException(\InvalidArgumentException::class);
296  $this->expectExceptionCode(1395055513);
297 
298  $statement = $this->createPreparedStatement($query);
299  $statement->bindValues($parameters);
300  }
301 }
parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
invalidParameterTypesPassedToBindValueThrowsException($parameter, $type, $exceptionCode)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']