Skip to content

Commit

Permalink
fix: connection now private , use getConnection()
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Sep 27, 2024
1 parent 03c8410 commit 31a9e90
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 49 deletions.
13 changes: 11 additions & 2 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Database
{
/**
* Store the instance of current connection
* @var \Doctrine\DBAL\Connection
* @var \Scrawler\Arca\Connection
*/
public Connection $connection;
private Connection $connection;
/**
* When $isFrozen is set to true tables are not updated/created
* @var bool
Expand Down Expand Up @@ -284,4 +284,13 @@ public function isUsingUUID() : bool
{
return $this->connection->isUsingUUID();
}

/**
* Returns the current connection
* @return Connection
*/
public function getConnection() : Connection
{
return $this->connection;
}
}
48 changes: 24 additions & 24 deletions tests/Unit/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use Doctrine\DBAL\Exception\DriverException;

beforeEach(function () {
db()->connection->executeStatement("DROP TABLE IF EXISTS user; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent_user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent_user; ");
});

it(" checks db()->isUsingUUID() function ", function ($useUUID) {
Expand All @@ -19,7 +19,7 @@

it(" checks db()->create() function ", function ($useUUID) {
$user = db($useUUID)->create('user');
$model = new \Scrawler\Arca\Model('user',db($useUUID)->connection);
$model = new \Scrawler\Arca\Model('user',db($useUUID)->getConnection());
$this->assertObjectEquals($model, $user);
})->with('useUUID');

Expand All @@ -33,7 +33,7 @@

$user->save();

$table= db($useUUID)->connection->getSchemaManager()->introspectTable('user');
$table= db($useUUID)->getConnection()->getSchemaManager()->introspectTable('user');
$requiredTable = new \Doctrine\DBAL\Schema\Table('user');
if (db($useUUID)->isUsingUUID()) {
$requiredTable->addColumn('id', 'string', array('length' => 36,'notnull' => true));
Expand All @@ -50,10 +50,10 @@

$actual = new \Doctrine\DBAL\Schema\Schema([$table]);
$required = new \Doctrine\DBAL\Schema\Schema([$requiredTable]);
$comparator = db($useUUID)->connection->getSchemaManager()->createComparator();
$comparator = db($useUUID)->getConnection()->getSchemaManager()->createComparator();
$diff = $comparator->compareSchemas($actual, $required);

$this->assertEmpty(db($useUUID)->connection->getPlatform()->getAlterSchemaSQL($diff));
$this->assertEmpty(db($useUUID)->getConnection()->getPlatform()->getAlterSchemaSQL($diff));
})->with('useUUID');


Expand All @@ -67,7 +67,7 @@
$id = $user->save();


$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -89,7 +89,7 @@
$parent->user = $user;
$id = $parent->save();

$stmt = db($useUUID)->connection->prepare("SELECT * FROM parent WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM parent WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand Down Expand Up @@ -118,9 +118,9 @@
$parent->ownUserList = [$user,$user_two];
$id = $parent->save();

$stmt = db($useUUID)->connection->prepare("SELECT * FROM parent WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM parent WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE parent_id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE parent_id = '".$id."'");
$result_child = $stmt->executeQuery()->fetchAllAssociative();
if (!db($useUUID)->isUsingUUID()) {
unset($result_child[0]['id']);
Expand Down Expand Up @@ -221,9 +221,9 @@
$parent->sharedUserList = [$user,$user_two];
$id = $parent->save();

$stmt = db($useUUID)->connection->prepare("SELECT * FROM parent WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM parent WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$stmt = db($useUUID)->connection->prepare("SELECT * FROM parent_user WHERE parent_id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM parent_user WHERE parent_id = '".$id."'");
$results_rel = $stmt->executeQuery()->fetchAllAssociative();
$rel_ids ='';
foreach ($results_rel as $relation) {
Expand All @@ -232,7 +232,7 @@
}

$rel_ids = substr($rel_ids, 0, -1);
$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE id IN (" . $rel_ids . ")");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE id IN (" . $rel_ids . ")");
$result_user = $stmt->executeQuery()->fetchAllAssociative();
if (!db($useUUID)->isUsingUUID()) {
unset($result_user[0]['id']);
Expand All @@ -254,7 +254,7 @@
$user->age = 44;
$id = $user->save();

$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -269,7 +269,7 @@
$id = createRandomUser($useUUID);
$user = db($useUUID)->getOne('user', $id);

$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -282,7 +282,7 @@
it("checks if db()->get() gets all record", function ($useUUID) {
populateRandomUser($useUUID);
$users = db($useUUID)->get('user');
$stmt = db($useUUID)->connection->prepare("SELECT * FROM user");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user");
$result = json_encode($stmt->executeQuery()->fetchAllAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -299,7 +299,7 @@
it("checks if db()->find() returns correct records", function () {
populateRandomUser();
$users = db()->find('user')->where('active = 1')->get();
$stmt = db()->connection->prepare("SELECT * FROM user WHERE active = 1");
$stmt = db()->getConnection()->prepare("SELECT * FROM user WHERE active = 1");
$result = json_encode($stmt->executeQuery()->fetchAllAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -310,9 +310,9 @@


it("checks if all public instance of database files are correct", function () {
$this->assertInstanceOf(\Scrawler\Arca\Connection::class, db()->connection);
$this->assertInstanceOf(\Doctrine\DBAL\Platforms\AbstractPlatform::class, db()->connection->getPlatform());
$this->assertInstanceOf(\Doctrine\DBAL\Schema\AbstractSchemaManager::class, db()->connection->getSchemaManager());
$this->assertInstanceOf(\Scrawler\Arca\Connection::class, db()->getConnection());
$this->assertInstanceOf(\Doctrine\DBAL\Platforms\AbstractPlatform::class, db()->getConnection()->getPlatform());
$this->assertInstanceOf(\Doctrine\DBAL\Schema\AbstractSchemaManager::class, db()->getConnection()->getSchemaManager());
});

it("checks db()->exec() function", function ($useUUID) {
Expand All @@ -327,7 +327,7 @@
$id = 2;
}

$stmt = db($useUUID)->connection->prepare("SELECT * FROM user where id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user where id = '".$id."'");
$result = $stmt->executeQuery()->fetchAssociative();
$this->assertEquals($result['name'], "john");
})->with('useUUID');
Expand All @@ -337,7 +337,7 @@
$user->name = fake()->name();
$user->save();

$stmt = db($useUUID)->connection->prepare("SELECT * FROM user");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user");
$result = $stmt->executeQuery()->fetchAllAssociative();

$actual = db($useUUID)->getAll("SELECT * FROM user");
Expand All @@ -350,7 +350,7 @@
$user->name = fake()->name();
$id = $user->save();
$user->delete();
$stmt = db($useUUID)->connection->prepare("SELECT * FROM user where id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user where id = '".$id."'");
$result = $stmt->executeQuery()->fetchAssociative();
$this->assertEmpty($result);
})->with('useUUID');
18 changes: 9 additions & 9 deletions tests/Unit/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use function Pest\Faker\fake;

beforeEach(function () {
db()->connection->executeStatement("DROP TABLE IF EXISTS user; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent_user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent_user; ");
});

it('tests DB::connect()',function(){
Expand All @@ -23,14 +23,14 @@

it('tests DB::create()',function(){
$user = \Scrawler\Arca\Facade\Database::create('user');
$model = new \Scrawler\Arca\Model('user',db()->connection);
$model = new \Scrawler\Arca\Model('user',db()->getConnection());
$this->assertObjectEquals($model, $user);
});

it('tests DB::get()',function(){
populateRandomUser();
$users = \Scrawler\Arca\Facade\Database::get('user');
$stmt = db()->connection->prepare("SELECT * FROM user");
$stmt = db()->getConnection()->prepare("SELECT * FROM user");
$result = json_encode($stmt->executeQuery()->fetchAllAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -44,7 +44,7 @@
$id = createRandomUser();
$user = \Scrawler\Arca\Facade\Database::getOne('user', $id);

$stmt = db()->connection->prepare("SELECT * FROM user WHERE id = '".$id."'");
$stmt = db()->getConnection()->prepare("SELECT * FROM user WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand All @@ -62,7 +62,7 @@
\Scrawler\Arca\Facade\Database::exec("insert into user (name) values ('john')");
$id = 2;

$stmt = db()->connection->prepare("SELECT * FROM user where id = '".$id."'");
$stmt = db()->getConnection()->prepare("SELECT * FROM user where id = '".$id."'");
$result = $stmt->executeQuery()->fetchAssociative();
$this->assertEquals($result['name'], "john");
});
Expand All @@ -72,15 +72,15 @@
$user->name = fake()->name();
$id = $user->save();
\Scrawler\Arca\Facade\Database::delete($user);
$stmt = db()->connection->prepare("SELECT * FROM user where id = '".$id."'");
$stmt = db()->getConnection()->prepare("SELECT * FROM user where id = '".$id."'");
$result = $stmt->executeQuery()->fetchAssociative();
$this->assertEmpty($result);
});

it('test DB::find()',function(){
populateRandomUser();
$users = \Scrawler\Arca\Facade\Database::find('user')->where('active = 1')->get();
$stmt = db()->connection->prepare("SELECT * FROM user WHERE active = 1");
$stmt = db()->getConnection()->prepare("SELECT * FROM user WHERE active = 1");
$result = json_encode($stmt->executeQuery()->fetchAllAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@


beforeEach(function () {
db()->connection->executeStatement("DROP TABLE IF EXISTS user; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent_user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent_user; ");
});

it("checks if model is properly populated on retrive", function ($useUUID) {
$id = createRandomUser($useUUID);
$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE id ='".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE id ='".$id."'");
$user = db($useUUID)->getOne('user', $id);
$result =$stmt->executeQuery()->fetchAssociative();
$this->assertEquals($user->name, $result['name']);
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
use function Pest\Faker\fake;

beforeEach(function () {
db()->connection->executeStatement("DROP TABLE IF EXISTS user; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent_user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent_user; ");
});

it("checks if db()->find()->first() returns first record", function ($useUUID) {
$id = createRandomUser($useUUID);
$user = db($useUUID)->find('user')->first();
$stmt = db($useUUID)->connection->prepare("SELECT * FROM user WHERE id = '".$id."'");
$stmt = db($useUUID)->getConnection()->prepare("SELECT * FROM user WHERE id = '".$id."'");
$result = json_encode($stmt->executeQuery()->fetchAssociative());
$this->assertJsonStringEqualsJsonString(
$result,
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/TableManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use function Pest\Faker\fake;

beforeEach(function () {
db()->connection->executeStatement("DROP TABLE IF EXISTS user; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->connection->executeStatement("DROP TABLE IF EXISTS parent_user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS user; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent; ");
db()->getConnection()->executeStatement("DROP TABLE IF EXISTS parent_user; ");
});

it('tests table manger update functionality',function($useUUID){
Expand All @@ -20,7 +20,7 @@
$user->rand = 'abc';
$id = $user->save();

$table= db($useUUID)->connection->getSchemaManager()->introspectTable('user');
$table= db($useUUID)->getConnection()->getSchemaManager()->introspectTable('user');
$requiredTable = new \Doctrine\DBAL\Schema\Table('user');
if (db($useUUID)->isUsingUUID()) {
$requiredTable->addColumn('id', 'string', array('length' => 36,'notnull' => true));
Expand All @@ -39,9 +39,9 @@

$actual = new \Doctrine\DBAL\Schema\Schema([$table]);
$required = new \Doctrine\DBAL\Schema\Schema([$requiredTable]);
$comparator = db()->connection->getSchemaManager()->createComparator();
$comparator = db()->getConnection()->getSchemaManager()->createComparator();
$diff = $comparator->compareSchemas($actual, $required);
//print_r($diff->toSql(db()->platform));

$this->assertEmpty(db($useUUID)->connection->getPlatform()->getAlterSchemaSQL($diff));
$this->assertEmpty(db($useUUID)->getConnection()->getPlatform()->getAlterSchemaSQL($diff));
})->with('useUUID');

0 comments on commit 31a9e90

Please sign in to comment.