View source
<?php
namespace Drupal\KernelTests\Core\Database;
class UpdateComplexTest extends DatabaseTestBase {
public function testOrConditionUpdate() {
$update = $this->connection
->update('test')
->fields([
'job' => 'Musician',
])
->condition($this->connection
->condition('OR')
->condition('name', 'John')
->condition('name', 'Paul'));
$num_updated = $update
->execute();
$this
->assertSame(2, $num_updated, 'Updated 2 records.');
$num_matches = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
':job' => 'Musician',
])
->fetchField();
$this
->assertSame('2', $num_matches, 'Updated fields successfully.');
}
public function testInConditionUpdate() {
$num_updated = $this->connection
->update('test')
->fields([
'job' => 'Musician',
])
->condition('name', [
'John',
'Paul',
], 'IN')
->execute();
$this
->assertSame(2, $num_updated, 'Updated 2 records.');
$num_matches = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
':job' => 'Musician',
])
->fetchField();
$this
->assertSame('2', $num_matches, 'Updated fields successfully.');
}
public function testNotInConditionUpdate() {
$num_updated = $this->connection
->update('test')
->fields([
'job' => 'Musician',
])
->condition('name', [
'John',
'Paul',
'George',
], 'NoT IN')
->execute();
$this
->assertSame(1, $num_updated, 'Updated 1 record.');
$num_matches = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
':job' => 'Musician',
])
->fetchField();
$this
->assertSame('1', $num_matches, 'Updated fields successfully.');
}
public function testBetweenConditionUpdate() {
$num_updated = $this->connection
->update('test')
->fields([
'job' => 'Musician',
])
->condition('age', [
25,
26,
], 'BETWEEN')
->execute();
$this
->assertSame(2, $num_updated, 'Updated 2 records.');
$num_matches = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
':job' => 'Musician',
])
->fetchField();
$this
->assertSame('2', $num_matches, 'Updated fields successfully.');
}
public function testLikeConditionUpdate() {
$num_updated = $this->connection
->update('test')
->fields([
'job' => 'Musician',
])
->condition('name', '%ge%', 'LIKE')
->execute();
$this
->assertSame(1, $num_updated, 'Updated 1 record.');
$num_matches = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
':job' => 'Musician',
])
->fetchField();
$this
->assertSame('1', $num_matches, 'Updated fields successfully.');
}
public function testUpdateExpression() {
$before_age = $this->connection
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetchField();
$num_updated = $this->connection
->update('test')
->condition('name', 'Ringo')
->fields([
'job' => 'Musician',
])
->expression('age', '[age] + :age', [
':age' => 4,
])
->execute();
$this
->assertSame(1, $num_updated, 'Updated 1 record.');
$num_matches = $this->connection
->query('SELECT COUNT(*) FROM {test} WHERE [job] = :job', [
':job' => 'Musician',
])
->fetchField();
$this
->assertSame('1', $num_matches, 'Updated fields successfully.');
$person = $this->connection
->query('SELECT * FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetch();
$this
->assertEquals('Ringo', $person->name, 'Name set correctly.');
$this
->assertEquals($before_age + 4, $person->age, 'Age set correctly.');
$this
->assertEquals('Musician', $person->job, 'Job set correctly.');
}
public function testUpdateOnlyExpression() {
$before_age = $this->connection
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetchField();
$num_updated = $this->connection
->update('test')
->condition('name', 'Ringo')
->expression('age', '[age] + :age', [
':age' => 4,
])
->execute();
$this
->assertSame(1, $num_updated, 'Updated 1 record.');
$after_age = $this->connection
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetchField();
$this
->assertEquals($before_age + 4, $after_age, 'Age updated correctly');
}
public function testSubSelectUpdate() {
$subselect = $this->connection
->select('test_task', 't');
$subselect
->addExpression('MAX([priority]) + :increment', 'max_priority', [
':increment' => 30,
]);
$select = clone $subselect;
$query = $this->connection
->update('test')
->expression('age', $subselect)
->condition('name', 'Ringo');
$num_updated = $query
->execute();
$after_age = $this->connection
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetchField();
$expected_age = $select
->execute()
->fetchField();
$this
->assertEquals($expected_age, $after_age);
$this
->assertEquals(1, $num_updated);
}
}