TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Unit/Database/PreparedStatementTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Database;
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 
24 {
28  protected $databaseStub;
29 
36  protected function setUp()
37  {
38  $this->databaseStub = $this->setUpAndReturnDatabaseStub();
39  }
40 
42  // Utility functions
44 
49  private function setUpAndReturnDatabaseStub()
50  {
51  $GLOBALS['TYPO3_DB'] = $this->getAccessibleMock(
52  DatabaseConnection::class,
53  ['prepare_PREPAREDquery'],
54  [],
55  '',
56  false,
57  false
58  );
59 
60  return $GLOBALS['TYPO3_DB'];
61  }
62 
69  private function createPreparedStatement($query)
70  {
71  return new PreparedStatement($query, 'pages');
72  }
73 
75  // Tests for the utility functions
77 
83  {
84  $this->assertTrue($this->setUpAndReturnDatabaseStub() instanceof DatabaseConnection);
85  }
86 
92  {
93  $this->assertTrue($this->createPreparedStatement('dummy') instanceof PreparedStatement);
94  }
95 
97  // Tests for \TYPO3\CMS\Core\Database\PreparedStatement
99 
107  {
108  return [
109  'one named integer parameter' => [
110  'SELECT * FROM pages WHERE pid=:pid',
111  [':pid' => 1],
112  'SELECT * FROM pages WHERE pid=?'
113  ],
114  'one unnamed integer parameter' => [
115  'SELECT * FROM pages WHERE pid=?',
116  [1],
117  'SELECT * FROM pages WHERE pid=?'
118  ],
119  'one named integer parameter is replaced multiple times' => [
120  'SELECT * FROM pages WHERE pid=:pid OR uid=:pid',
121  [':pid' => 1],
122  'SELECT * FROM pages WHERE pid=? OR uid=?'
123  ],
124  'two named integer parameters are replaced' => [
125  'SELECT * FROM pages WHERE pid=:pid OR uid=:uid',
126  [':pid' => 1, ':uid' => 10],
127  'SELECT * FROM pages WHERE pid=? OR uid=?'
128  ],
129  'two unnamed integer parameters are replaced' => [
130  'SELECT * FROM pages WHERE pid=? OR uid=?',
131  [1, 1],
132  'SELECT * FROM pages WHERE pid=? OR uid=?'
133  ],
134  ];
135  }
136 
148  public function parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
149  {
150  $statement = $this->createPreparedStatement($query);
151  $this->databaseStub->expects($this->any())
152  ->method('prepare_PREPAREDquery')
153  ->with($this->equalTo($expectedResult));
154  $statement->execute($parameters);
155  }
156 
168  public function parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
169  {
170  $statement = $this->createPreparedStatement($query);
171  $this->databaseStub->expects($this->any())
172  ->method('prepare_PREPAREDquery')
173  ->with($this->equalTo($expectedResult));
174  $statement->bindValues($parameters);
175  $statement->execute();
176  }
177 
185  {
186  return [
187  'integer passed with param type NULL' => [
188  1,
190  1282489834
191  ],
192  'string passed with param type NULL' => [
193  '1',
195  1282489834
196  ],
197  'bool passed with param type NULL' => [
198  true,
200  1282489834
201  ],
202  'NULL passed with param type INT' => [
203  null,
205  1281868686
206  ],
207  'string passed with param type INT' => [
208  '1',
210  1281868686
211  ],
212  'bool passed with param type INT' => [
213  true,
215  1281868686
216  ],
217  'NULL passed with param type BOOL' => [
218  null,
220  1281868687
221  ],
222  'string passed with param type BOOL' => [
223  '1',
225  1281868687
226  ],
227  'integer passed with param type BOOL' => [
228  1,
230  1281868687
231  ]
232  ];
233  }
234 
246  public function invalidParameterTypesPassedToBindValueThrowsException($parameter, $type, $exceptionCode)
247  {
248  $this->expectException(\InvalidArgumentException::class);
249  $this->expectExceptionCode($exceptionCode);
250 
251  $statement = $this->createPreparedStatement('');
252  $statement->bindValue(1, $parameter, $type);
253  }
254 
262  {
263  return [
264  'using other prefix than colon' => [
266  'SELECT * FROM pages WHERE pid=#pid',
267  ['#pid' => 1]
268  ],
269  'using non alphanumerical character' => [
271  'SELECT * FROM pages WHERE title=:stra≠e',
272  [':stra≠e' => 1]
273  ],
274  'no colon used' => [
276  'SELECT * FROM pages WHERE pid=pid',
277  ['pid' => 1]
278  ],
279  'colon at the end' => [
281  'SELECT * FROM pages WHERE pid=pid:',
282  ['pid:' => 1]
283  ],
284  'colon without alphanumerical character' => [
286  'SELECT * FROM pages WHERE pid=:',
287  [':' => 1]
288  ]
289  ];
290  }
291 
301  public function passingInvalidMarkersThrowsException($query, $parameters)
302  {
303  $this->expectException(\InvalidArgumentException::class);
304  $this->expectExceptionCode(1395055513);
305 
306  $statement = $this->createPreparedStatement($query);
307  $statement->bindValues($parameters);
308  }
309 }
parametersAreReplacedInQueryWhenBoundWithBindValues($query, $parameters, $expectedResult)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
parametersAreReplacedByQuestionMarkInQueryByCallingExecute($query, $parameters, $expectedResult)
getAccessibleMock($originalClassName, $methods=[], array $arguments=[], $mockClassName= '', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)