Skip to content

Commit d2a9f57

Browse files
authored
More coverage tweaks (#96)
1 parent 5f95ce4 commit d2a9f57

File tree

3 files changed

+23
-38
lines changed

3 files changed

+23
-38
lines changed

lib/Connection.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
namespace ActiveRecord;
88

9-
use ActiveRecord\Adapter\SqliteAdapter;
10-
use ActiveRecord\Exception\ConnectionException;
119
use ActiveRecord\Exception\DatabaseException;
1210
use Psr\Log\LoggerInterface;
1311

@@ -363,10 +361,7 @@ public function query(string $sql, array &$values = [])
363361
throw new DatabaseException();
364362
}
365363
} catch (\PDOException $e) {
366-
if ($this instanceof SqliteAdapter && 'HY000' === $e->getCode()) {
367-
throw new DatabaseException($e);
368-
}
369-
throw new ConnectionException($e);
364+
throw new DatabaseException($e);
370365
}
371366

372367
$sth->setFetchMode(\PDO::FETCH_ASSOC);
@@ -452,19 +447,15 @@ public function transaction(): void
452447
*/
453448
public function commit(): void
454449
{
455-
if (!$this->connection->commit()) {
456-
throw new DatabaseException();
457-
}
450+
assert($this->connection->commit(), new DatabaseException('Failed to commit'));
458451
}
459452

460453
/**
461454
* Rollback a transaction.
462455
*/
463456
public function rollback(): void
464457
{
465-
if (!$this->connection->rollback()) {
466-
throw new DatabaseException();
467-
}
458+
assert($this->connection->rollback(), new DatabaseException('Failed to roll back'));
468459
}
469460

470461
/**

lib/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function get_fully_qualified_table_name(): string
367367
{
368368
$table = $this->conn->quote_name($this->table);
369369

370-
if (isset($this->db_name)) {
370+
if (!empty($this->db_name)) {
371371
$table = $this->conn->quote_name($this->db_name) . ".$table";
372372
}
373373

test/ActiveRecordWriteTest.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use ActiveRecord\Exception\DatabaseException;
77
use ActiveRecord\Exception\ReadOnlyException;
88
use ActiveRecord\Exception\UndefinedPropertyException;
9+
use ActiveRecord\Table;
910
use test\models\Author;
1011
use test\models\Book;
12+
use test\models\Course;
1113
use test\models\Venue;
1214

1315
class DirtyAuthor extends ActiveRecord\Model
@@ -90,10 +92,22 @@ public function testSaveAutoIncrementId()
9092
$this->assertTrue($venue->id > 0);
9193
}
9294

95+
public function testFullyQualifiedNameWithExplicitDbName()
96+
{
97+
Course::$db = 'test';
98+
$name = Table::load(Course::class)
99+
->get_fully_qualified_table_name();
100+
$this->assert_sql_includes('`test`.`courses`', $name);
101+
Course::$db = '';
102+
}
103+
93104
public function testSequenceWasSet()
94105
{
95106
if (ConnectionManager::get_connection()->supports_sequences()) {
96-
$this->assertEquals(ConnectionManager::get_connection()->get_sequence_name('authors', 'author_id'), Author::table()->sequence);
107+
$this->assertEquals(
108+
ConnectionManager::get_connection()->get_sequence_name('authors', 'author_id'),
109+
Author::table()->sequence
110+
);
97111
} else {
98112
$this->assertNull(Author::table()->sequence);
99113
}
@@ -363,36 +377,16 @@ public function testSetDateFlagsDirtyWithPhpDatetime()
363377
$this->assertArrayHasKey('some_date', $author->dirty_attributes());
364378
}
365379

366-
public function testWhereDeleteAll()
380+
public function testDeleteAll()
367381
{
368-
$num_affected = Author::where('parent_author_id = ?', 2)->delete_all();
382+
$num_affected = Author::where('parent_author_id = 2')->delete_all();
369383
$this->assertEquals(2, $num_affected);
370384
}
371385

372-
public function testDistinctDeleteAllNotSupported()
386+
public function testDeleteAllWithDistinct()
373387
{
374388
$this->expectException(ActiveRecordException::class);
375-
Author::distinct()->delete_all();
376-
}
377-
378-
public function testDeleteAll()
379-
{
380-
$num_affected = Author::delete_all();
381-
$this->assertEquals(5, $num_affected);
382-
}
383-
384-
public function testDeleteAllWithLimitAndOrder()
385-
{
386-
if (!ConnectionManager::get_connection()->accepts_limit_and_order_for_update_and_delete()) {
387-
$this->markTestSkipped('Only MySQL & Sqlite accept limit/order with UPDATE clause');
388-
}
389-
390-
$num_affected = Author::limit(1)
391-
->order('name asc')
392-
->where(['parent_author_id = ?', 2])
393-
->delete_all();
394-
$this->assertEquals(1, $num_affected);
395-
$this->assertTrue(false !== strpos(Author::table()->last_sql, 'ORDER BY name asc LIMIT 1'));
389+
Author::distinct()->where('parent_author_id = 2')->delete_all();
396390
}
397391

398392
public function testUpdateAllWithSetAsString()

0 commit comments

Comments
 (0)