|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. |
| 4 | + * @license GNU General Public License version 2 or later; see LICENSE |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Joomla\Database\Tests\Mysqli; |
| 8 | + |
| 9 | +use Joomla\Database\DatabaseDriver; |
| 10 | +use Joomla\Database\Exception\ExecutionFailureException; |
| 11 | +use Joomla\Database\Mysqli\MysqliStatement; |
| 12 | +use Joomla\Test\DatabaseTestCase; |
| 13 | + |
| 14 | +class MysqliPreparedStatementTest extends DatabaseTestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * This method is called before the first test of this test class is run. |
| 18 | + * |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public static function setUpBeforeClass(): void |
| 22 | + { |
| 23 | + parent::setUpBeforeClass(); |
| 24 | + |
| 25 | + if (!static::$connection || static::$connection->getName() !== 'mysqli') { |
| 26 | + self::markTestSkipped('MySQL database not configured.'); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Sets up the fixture. |
| 32 | + * |
| 33 | + * This method is called before a test is executed. |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + try { |
| 42 | + foreach (DatabaseDriver::splitSql(file_get_contents(dirname(__DIR__) . '/Stubs/Schema/mysql.sql')) as $query) { |
| 43 | + static::$connection->setQuery($query) |
| 44 | + ->execute(); |
| 45 | + } |
| 46 | + } catch (ExecutionFailureException $exception) { |
| 47 | + $this->markTestSkipped( |
| 48 | + \sprintf( |
| 49 | + 'Could not load MySQL database: %s', |
| 50 | + $exception->getMessage() |
| 51 | + ) |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Tears down the fixture. |
| 58 | + * |
| 59 | + * This method is called after a test is executed. |
| 60 | + */ |
| 61 | + protected function tearDown(): void |
| 62 | + { |
| 63 | + foreach (static::$connection->getTableList() as $table) { |
| 64 | + static::$connection->dropTable($table); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * Make sure the mysqli driver correctly maps named query parameters appearing more than once. |
| 71 | + */ |
| 72 | + public function testPrepareParameterKeyMappingWithDuplicateKey() |
| 73 | + { |
| 74 | + $statement = 'SELECT * FROM dbtest WHERE `title` LIKE :search OR `description` LIKE :search'; |
| 75 | + $mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement); |
| 76 | + $rawQuery = $mysqliStatementObject->prepareParameterKeyMapping($statement); |
| 77 | + |
| 78 | + $this->assertEquals( |
| 79 | + "SELECT * FROM dbtest WHERE `title` LIKE ? OR `description` LIKE ?", |
| 80 | + $rawQuery |
| 81 | + ); |
| 82 | + |
| 83 | + $refObject = new \ReflectionObject($mysqliStatementObject); |
| 84 | + $refMapping = $refObject->getProperty('parameterKeyMapping'); |
| 85 | + /** @noinspection PhpExpressionResultUnusedInspection */ |
| 86 | + $refMapping->setAccessible(true); |
| 87 | + $parameterKeyMapping = $refMapping->getValue($mysqliStatementObject); |
| 88 | + |
| 89 | + $this->assertEquals( |
| 90 | + [ |
| 91 | + ':search' => [0, 1], |
| 92 | + ], |
| 93 | + $parameterKeyMapping |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Regression test to ensure mapping query parameters appearing once didn't break. |
| 99 | + */ |
| 100 | + public function testPrepareParameterKeyMappingWithSingleKey() |
| 101 | + { |
| 102 | + $statement = 'SELECT * FROM dbtest WHERE `title` LIKE :search OR `description` LIKE :search2'; |
| 103 | + $mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement); |
| 104 | + $rawQuery = $mysqliStatementObject->prepareParameterKeyMapping($statement); |
| 105 | + |
| 106 | + $this->assertEquals( |
| 107 | + "SELECT * FROM dbtest WHERE `title` LIKE ? OR `description` LIKE ?", |
| 108 | + $rawQuery |
| 109 | + ); |
| 110 | + |
| 111 | + $refObject = new \ReflectionObject($mysqliStatementObject); |
| 112 | + $refMapping = $refObject->getProperty('parameterKeyMapping'); |
| 113 | + /** @noinspection PhpExpressionResultUnusedInspection */ |
| 114 | + $refMapping->setAccessible(true); |
| 115 | + $parameterKeyMapping = $refMapping->getValue($mysqliStatementObject); |
| 116 | + |
| 117 | + $this->assertEquals( |
| 118 | + [ |
| 119 | + ':search' => 0, |
| 120 | + ':search2' => 1, |
| 121 | + ], |
| 122 | + $parameterKeyMapping |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Make sure the mysqli driver correctly runs queries with named parameters appearing more than once. |
| 128 | + * |
| 129 | + * @doesNotPerformAssertions |
| 130 | + */ |
| 131 | + public function testPreparedStatementWithDuplicateKey() |
| 132 | + { |
| 133 | + $statement = 'SELECT * FROM dbtest WHERE `title` LIKE :search OR `description` LIKE :search'; |
| 134 | + $mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement); |
| 135 | + $dummyValue = 'test'; |
| 136 | + $mysqliStatementObject->bindParam(':search', $dummyValue); |
| 137 | + |
| 138 | + $mysqliStatementObject->execute(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Regression test to ensure running queries with named parameters appearing once didn't break. |
| 143 | + * |
| 144 | + * @doesNotPerformAssertions |
| 145 | + */ |
| 146 | + public function testPreparedStatementWithSingleKey() |
| 147 | + { |
| 148 | + $statement = 'SELECT * FROM dbtest WHERE `title` LIKE :search OR `description` LIKE :search2'; |
| 149 | + $mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement); |
| 150 | + $dummyValue = 'test'; |
| 151 | + $dummyValue2 = 'test'; |
| 152 | + $mysqliStatementObject->bindParam(':search', $dummyValue); |
| 153 | + $mysqliStatementObject->bindParam(':search2', $dummyValue); |
| 154 | + |
| 155 | + $mysqliStatementObject->execute(); |
| 156 | + } |
| 157 | +} |
0 commit comments