/** * Test drop user account. */ public function testDropUser() { $this->connection->execute('CREATE USER ?@? IDENTIFIED BY ?', 'monty', '%', 'some_pass'); $this->assertTrue($this->connection->userExists('monty')); $this->connection->dropUser('monty'); $this->assertFalse($this->connection->userExists('monty')); }
/** * Test drop database. */ public function testDropDatabase() { $this->connection->execute('CREATE DATABASE activecollab_database_connection_test_create'); $this->assertTrue($this->connection->databaseExists('activecollab_database_connection_test_create')); $this->connection->dropDatabase('activecollab_database_connection_test_create'); $this->assertFalse($this->connection->databaseExists('activecollab_database_connection_test_create')); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->link = new \MySQLi('localhost', 'root', '', 'activecollab_promises_test'); if ($this->link->connect_error) { throw new \RuntimeException('Failed to connect to database. MySQL said: ' . $this->link->connect_error); } $this->connection = new MysqliConnection($this->link); $this->connection->execute('DROP TABLE IF EXISTS `' . PromisesInterface::PROMISES_TABLE_NAME . '`'); $this->now = new Carbon(); Carbon::setTestNow($this->now); }
/** * Check if attempts value for the given job has an expected value. * * @param int|null $expected * @param int $job_id */ protected function assertAttempts($expected, $job_id) { $result = $this->connection->executeFirstCell('SELECT `attempts` FROM `' . MySqlQueue::JOBS_TABLE_NAME . '` WHERE id = ?', $job_id); if ($expected === null) { $this->assertEmpty($result); } else { $this->assertSame($expected, (int) $result); } }
public function testExecuteFirstColumn() { $writer_names = $this->connection->selectFirstColumn('writers', 'name', null, 'name'); $this->assertInternalType('array', $writer_names); $this->assertEquals(['Alexander Pushkin', 'Fyodor Dostoyevsky', 'Leo Tolstoy'], $writer_names); $writer_birthdays = $this->connection->selectFirstColumn('writers', 'birthday', null, 'name'); $this->assertInternalType('array', $writer_birthdays); $this->assertEquals(['1799-06-06', '1821-11-11', '1828-09-09'], $writer_birthdays); }
/** * {@inheritdoc} */ public function escapeValue($unescaped) { // Date value if ($unescaped instanceof DateValue) { return "'" . $this->link->real_escape_string($unescaped->format('Y-m-d')) . "'"; // Date time value (including DateTimeValue) } elseif ($unescaped instanceof DateTime) { return "'" . $this->link->real_escape_string($unescaped->format('Y-m-d H:i:s')) . "'"; // Float } else { if (is_float($unescaped)) { return "'" . str_replace(',', '.', (double) $unescaped) . "'"; // replace , with . for locales where comma is used by the system (German for example) // Boolean (maps to TINYINT(1)) } else { if (is_bool($unescaped)) { return $unescaped ? "'1'" : "'0'"; // NULL } else { if ($unescaped === null) { return 'NULL'; // Escape first cell of each row } else { if ($unescaped instanceof ResultInterface) { if ($unescaped->count() < 1) { throw new InvalidArgumentException("Empty results can't be escaped"); } $escaped = []; foreach ($unescaped as $v) { $escaped[] = $this->escapeValue(array_shift($v)); } return '(' . implode(',', $escaped) . ')'; // Escape each array element } else { if (is_array($unescaped)) { if (empty($unescaped)) { throw new InvalidArgumentException("Empty arrays can't be escaped"); } $escaped = []; foreach ($unescaped as $v) { $escaped[] = $this->escapeValue($v); } return '(' . implode(',', $escaped) . ')'; // Regular string and integer escape } else { if (is_scalar($unescaped)) { return "'" . $this->link->real_escape_string($unescaped) . "'"; } else { throw new InvalidArgumentException('Value is expected to be scalar, array, or instance of: DateTime or Result'); } } } } } } } }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->addToContainer('link', function ($c) { $db_host = $this->getTestMySqlConnectionParam('host', 'localhost'); $db_port = $this->getTestMySqlConnectionParam('port', 3306); $db_user = $this->getTestMySqlConnectionParam('user', 'root'); $db_pass = $this->getTestMySqlConnectionParam('pass', ''); $db_name = $this->getTestMySqlConnectionParam('database', $this->getTestMySqlDatabaseName($c['app_name'])); $link = new \MySQLi("{$db_host}:{$db_port}", $db_user, $db_pass); if ($link->connect_error) { throw new RuntimeException('Failed to connect to database. MySQL said: ' . $link->connect_error); } if (!$link->select_db($db_name)) { throw new RuntimeException('Failed to select database'); } return $link; }); $this->addToContainer('connection', function ($c) { $connection = new MysqliConnection($c['link']); $connection->execute('SET foreign_key_checks = 0;'); foreach ($connection->getTableNames() as $table_name) { $connection->dropTable($table_name); } $connection->execute('SET foreign_key_checks = 1;'); return $connection; }); }
/** * Test drop table command. */ public function testDropTable() { $this->assertEquals(['writers1', 'writers2', 'writers3'], $this->connection->getTableNames()); $this->assertTrue($this->connection->tableExists('writers2')); $this->connection->dropTable('writers2'); $this->assertEquals(['writers1', 'writers3'], $this->connection->getTableNames()); $this->assertFalse($this->connection->tableExists('writers2')); }
/** * Tear down test environment. */ public function tearDown() { if ($triggers = $this->connection->execute('SHOW TRIGGERS')) { foreach ($triggers as $trigger) { $this->connection->execute('DROP TRIGGER ' . $this->connection->escapeFieldName($trigger['Trigger'])); } } $this->connection->execute('SET foreign_key_checks = 0;'); foreach ($this->connection->getTableNames() as $table_name) { $this->connection->dropTable($table_name); } $this->connection->execute('SET foreign_key_checks = 1;'); $this->connection = null; $this->link->close(); parent::tearDown(); }
/** * @expectedException \ActiveCollab\DatabaseConnection\Exception\QueryException */ public function testUnsafeDropNonExistingFk() { $this->assertCount(2, $this->connection->getForeignKeyNames('writers')); $this->connection->dropForeignKey('writers', 'FK that does not exist', false); $this->assertCount(2, $this->connection->getForeignKeyNames('writers')); }
/** * @expectedException \ActiveCollab\DatabaseConnection\Exception\QueryException * @expectedExceptionMessage Can't DROP 'field that does not exist'; check that column/key exists */ public function testUnsafeDropNonExistingField() { $this->assertCount(3, $this->connection->getFieldNames('writers')); $this->connection->dropField('writers', 'field that does not exist', false); $this->assertCount(3, $this->connection->getFieldNames('writers')); }
/** * @expectedException \InvalidArgumentException */ public function testExceptionDueToInvalidConditions() { $this->connection->update('writers', ['name' => 'Anton Chekhov', 'birthday' => new DateTime('1860-01-29')], 123); }
/** * @expectedException \ActiveCollab\DatabaseConnection\Exception\QueryException * @expectedExceptionMessage Can't DROP 'index that does not exist'; check that column/key exists */ public function testUnsafeDropNonExistingIndex() { $this->assertCount(2, $this->connection->getIndexNames('writers')); $this->connection->dropIndex('writers', 'index that does not exist', false); $this->assertCount(2, $this->connection->getIndexNames('writers')); }