Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Database/Helpers/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 3.0.0
*/

use Quantum\Database\Database;
use Quantum\Di\Di;

/**
* Gets the Database instance from DI
*/
function db(): Database
Comment thread
armanist marked this conversation as resolved.
{
if (!Di::isRegistered(Database::class)) {
Di::register(Database::class);
}

return Di::get(Database::class);
}
8 changes: 1 addition & 7 deletions src/Database/Traits/RelationalTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace Quantum\Database\Traits;

use Quantum\Database\Database;
use Quantum\Di\Di;
use Quantum\Di\Exceptions\DiException;
use ReflectionException;

Expand Down Expand Up @@ -87,10 +85,6 @@ public static function queryLog(): array
*/
protected static function resolveQuery(string $method, string $query = '', array $parameters = [])
{
if (!Di::isRegistered(Database::class)) {
Di::register(Database::class);
}

return Di::get(Database::class)->getOrmClass()::$method($query, $parameters);
return db()->getOrmClass()::$method($query, $parameters);
}
}
8 changes: 1 addition & 7 deletions src/Database/Traits/TransactionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

use Quantum\Database\Exceptions\DatabaseException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Database\Database;
use ReflectionException;
use Quantum\Di\Di;
use Throwable;

/**
Expand Down Expand Up @@ -64,11 +62,7 @@ public static function rollback(): void
*/
protected static function resolveTransaction(string $method)
{
if (!Di::isRegistered(Database::class)) {
Di::register(Database::class);
}

$db = Di::get(Database::class)->getOrmClass();
$db = db()->getOrmClass();

if (!method_exists($db, $method)) {
throw DatabaseException::methodNotSupported($method, self::class);
Expand Down
7 changes: 1 addition & 6 deletions src/Migration/MigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Quantum\Storage\FileSystem;
use Quantum\Database\Database;
use ReflectionException;
use Quantum\Di\Di;

/**
* Class MigrationManager
Expand Down Expand Up @@ -77,11 +76,7 @@ public function __construct()
{
$this->fs = FileSystemFactory::get();

if (!Di::isRegistered(Database::class)) {
Di::register(Database::class);
}

$this->db = Di::get(Database::class);
$this->db = db();

$this->tableFactory = new TableFactory();

Expand Down
8 changes: 1 addition & 7 deletions src/Model/Factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
use Quantum\Model\Exceptions\ModelException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Database\Database;
use Quantum\Model\DbModel;
use Quantum\Model\Model;
use ReflectionException;
use Quantum\Di\Di;

/**
* Class ModelFactory
Expand Down Expand Up @@ -106,11 +104,7 @@ protected static function createOrmInstance(
array $foreignKeys = [],
array $hidden = []
): DbalInterface {
if (!Di::isRegistered(Database::class)) {
Di::register(Database::class);
}

$ormClass = Di::get(Database::class)->getOrmClass();
$ormClass = db()->getOrmClass();

$instance = new $ormClass(
$table,
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Database/Helpers/DbHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Quantum\Tests\Unit\Database\Helpers;

use Quantum\Tests\Unit\AppTestCase;
use Quantum\Database\Database;
use Quantum\Di\Di;

class DbHelperTest extends AppTestCase
{
public function testDbHelperReturnsDatabaseInstance(): void
{
$this->assertInstanceOf(Database::class, db());
}

public function testDbHelperReturnsSameInstance(): void
{
$first = db();
$second = db();

$this->assertSame($first, $second);
}

public function testDbHelperLazilyRegistersDatabase(): void
{
Di::resetContainer();

$this->assertFalse(Di::isRegistered(Database::class));
$this->assertInstanceOf(Database::class, db());
$this->assertTrue(Di::isRegistered(Database::class));
}
}
2 changes: 2 additions & 0 deletions tests/Unit/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public function testRenderWithLayout(): void

$this->assertIsString($renderedView);

$renderedView = str_replace("\n", PHP_EOL, $renderedView);

$this->assertEquals('<html>' . PHP_EOL . '<head></head>' . PHP_EOL . '<body>' . PHP_EOL . '<p>Hello World, this is rendered html view</p></body>' . PHP_EOL . '</html>' . PHP_EOL, $renderedView);
}

Expand Down
Loading