Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ineersa committed Jul 5, 2024
1 parent 0e9ca74 commit f147c87
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/Database/LibSQLConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public function connect(array $config): LibSQLDatabase
protected function checkConfig(array $config): void
{
if (empty($config['driver']) || $config['driver'] !== 'libsql') {
throw new \InvalidArgumentException("Got driver - " . $config['driver'] . ", please check your URL and driver config");
throw new \InvalidArgumentException('Got driver - '.$config['driver'].', please check your URL and driver config');
}
if (empty($config['url']) && empty($config['database'])) {
throw new \InvalidArgumentException("URL and database not set, please check your configuration");
throw new \InvalidArgumentException('URL and database not set, please check your configuration');
}
}
}
10 changes: 4 additions & 6 deletions src/Database/LibSQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class LibSQLDatabase

protected bool $inTransaction = false;

public function __construct(protected array $config = [])
{
}
public function __construct(protected array $config = []) {}

public function init(): void
{
Expand All @@ -28,8 +26,8 @@ public function init(): void
'memory' => $this->createLibSQL(':memory:'),
'remote' => $this->createLibSQL("libsql:$path;authToken={$this->config['authToken']}"),
'remote_replica' => $this->createLibSQL([
"url" => $path,
"authToken" => $this->config['authToken'],
'url' => $path,
'authToken' => $this->config['authToken'],
'syncUrl' => $this->config['syncUrl'],
'syncInterval' => $this->config['syncInterval'],
'read_your_writes' => $this->config['readYourWrites'],
Expand Down Expand Up @@ -143,7 +141,7 @@ private function setConnectionMode(string $path, string $url = '', string $token
$this->connection_mode = 'remote_replica';
} elseif (str_starts_with($path, 'file:') !== false) {
$this->connection_mode = 'local';
} elseif (! empty($path) && ! empty($token) && $remoteOnly === true) {
} elseif (! empty($path) && ! empty($token) && $remoteOnly === true) {
$this->connection_mode = 'remote';
} elseif ($path === 'memory') {
$this->connection_mode = 'memory';
Expand Down
56 changes: 29 additions & 27 deletions tests/Feature/ConnectionsSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use LibSQL;
use Mockery;
use PHPUnit\Framework\Attributes\DataProvider;
use Turso\Driver\Laravel\Database\LibSQLConnection;
use Turso\Driver\Laravel\Database\LibSQLConnector;
use Turso\Driver\Laravel\Database\LibSQLDatabase;
use Mockery;
use Turso\Driver\Laravel\Exceptions\ConfigurationIsNotFound;

class ConnectionsSetupTest extends TestCase
Expand Down Expand Up @@ -40,6 +40,7 @@ public function testInvalidConfig(?string $defaultConnection, array $config, str
DB::connection('libsql');
} catch (\Exception $e) {
$this->assertEquals($error, $e->getMessage());

return;
}
}
Expand All @@ -60,7 +61,7 @@ public static function invalidConfigsProvider(): \Generator
'prefix' => '',
'database' => null,
],
'error' => 'URL and database not set, please check your configuration'
'error' => 'URL and database not set, please check your configuration',
];
yield 'url should be correct for file' => [
'defaultConnection' => 'libsql',
Expand All @@ -76,7 +77,7 @@ public static function invalidConfigsProvider(): \Generator
'prefix' => '',
'database' => null,
],
'error' => 'Got driver - file, please check your URL and driver config'
'error' => 'Got driver - file, please check your URL and driver config',
];
}

Expand Down Expand Up @@ -107,10 +108,10 @@ public function testConnectionInMemory(): void
$this->assertInstanceOf(LibSQLDatabase::class, $pdo);
$this->assertEquals('local', $pdo->getDb()->mode);
$this->assertEquals('memory', $pdo->getConnectionMode());
$result = $connection->select("PRAGMA database_list");
$result = $connection->select('PRAGMA database_list');
$this->assertNotEmpty($result);
$result = $pdo->query("SELECT sqlite_version()");
$this->assertNotEmpty($result, "Failed to query SQLite version");
$result = $pdo->query('SELECT sqlite_version()');
$this->assertNotEmpty($result, 'Failed to query SQLite version');
}

public function testConnectionLocalFile(): void
Expand Down Expand Up @@ -139,10 +140,10 @@ public function testConnectionLocalFile(): void
$this->assertInstanceOf(LibSQLDatabase::class, $pdo);
$this->assertEquals('local', $pdo->getDb()->mode);
$this->assertEquals('local', $pdo->getConnectionMode());
$result = $connection->select("PRAGMA database_list");
$result = $connection->select('PRAGMA database_list');
$this->assertNotEmpty($result);
$result = $pdo->query("SELECT sqlite_version()");
$this->assertNotEmpty($result, "Failed to query SQLite version");
$result = $pdo->query('SELECT sqlite_version()');
$this->assertNotEmpty($result, 'Failed to query SQLite version');

$this->assertTrue(File::exists('tests/_files/test.db'), 'No file created or wrong path');
}
Expand All @@ -159,26 +160,26 @@ public function testConnectionRemoteReplica(): void
'encryptionKey' => '',
'remoteOnly' => false,
'prefix' => '',
'database' => 'database.sqlite'
'database' => 'database.sqlite',
];
$expectedLibSQLParams = [
"url" => 'file:tests/_files/database.sqlite',
"authToken" => $config['authToken'],
'url' => 'file:tests/_files/database.sqlite',
'authToken' => $config['authToken'],
'syncUrl' => $config['syncUrl'],
'syncInterval' => $config['syncInterval'],
'read_your_writes' => $config['readYourWrites'],
'encryptionKey' => $config['encryptionKey'],
];
$constructorConfig = [
"driver" => "libsql",
"authToken" => "your-database-auth-token-from-turso",
"syncUrl" => "your-database-url-from-turso",
"syncInterval" => 5,
"readYourWrites" => true,
"encryptionKey" => "",
"remoteOnly" => false,
"prefix" => "",
"database" => "file:tests/_files/database.sqlite",
'driver' => 'libsql',
'authToken' => 'your-database-auth-token-from-turso',
'syncUrl' => 'your-database-url-from-turso',
'syncInterval' => 5,
'readYourWrites' => true,
'encryptionKey' => '',
'remoteOnly' => false,
'prefix' => '',
'database' => 'file:tests/_files/database.sqlite',
];

$mockLibSQLDatabase = $this->getMockBuilder(LibSQLDatabase::class)
Expand Down Expand Up @@ -225,13 +226,13 @@ public function testConnectionRemote(): void
'remoteOnly' => true,
'prefix' => '',
];
$expectedLibSQLParams = "libsql:dbname=libsql://database-org.turso.io;authToken=your-database-auth-token-from-turso";
$expectedLibSQLParams = 'libsql:dbname=libsql://database-org.turso.io;authToken=your-database-auth-token-from-turso';
$constructorConfig = [
"driver" => "libsql",
"authToken" => "your-database-auth-token-from-turso",
"remoteOnly" => true,
"prefix" => "",
"database" => "dbname=libsql://database-org.turso.io",
'driver' => 'libsql',
'authToken' => 'your-database-auth-token-from-turso',
'remoteOnly' => true,
'prefix' => '',
'database' => 'dbname=libsql://database-org.turso.io',
];

$mockLibSQLDatabase = $this->getMockBuilder(LibSQLDatabase::class)
Expand Down Expand Up @@ -279,6 +280,7 @@ public function testNotFoundConfiguration()
DB::connection('libsql');
} catch (ConfigurationIsNotFound $e) {
$this->assertEquals('Connection not found!', $e->getMessage());

return;
}
}
Expand Down

0 comments on commit f147c87

Please sign in to comment.