Skip to content

Commit

Permalink
test: container factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Oct 10, 2024
1 parent 7b46be5 commit bf2f0ad
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 28 deletions.
8 changes: 0 additions & 8 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ public function __construct(
{
}

/**
* Set if the connection is using UUID.
*/
public function useUUID(bool $isUuid): void
{
$this->isUUID = $isUuid;
}

/**
* Get if the connection is using UUID.
*/
Expand Down
24 changes: 14 additions & 10 deletions src/Factory/DatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ class DatabaseFactory
{
private \Di\Container $container;

public function __construct(?\DI\ContainerBuilder $container = null)
public function __construct(?\DI\Container $container = null)
{
if (is_null($container)) {
$builder = new \DI\ContainerBuilder();
$this->container = new \DI\Container();
} else {
$builder = $container;
$this->container = $container;
}
$this->container = $builder->build();
}

/**
Expand All @@ -49,9 +48,14 @@ public function build(array $connectionParams): Database
*/
public function wireContainer(array $connectionParams): void
{
$this->createConfig($connectionParams['useUUID'] ?? false, $connectionParams['frozen'] ?? false);
unset($connectionParams['use_uuid']);
unset($connectionParams['frozen']);
if (isset($connectionParams['useUUID'])) {
$useUUID = $connectionParams['useUUID'];
} else {
$useUUID = false;
}

$this->createConfig($useUUID,);
unset($connectionParams['useUUID']);
$this->createConnection($connectionParams);
$this->createModelManager();
}
Expand All @@ -75,10 +79,10 @@ private function createModelManager(): void
});
}

private function createConfig(bool $useUUID = false, bool $frozen = false): void
private function createConfig(bool $useUUID): void
{
$this->container->set(Config::class, function () use ($useUUID, $frozen): Config {
return new Config($useUUID, $frozen);
$this->container->set(Config::class, function () use ($useUUID): Config {
return new Config($useUUID);
});
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Database/DatabaseSaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
});

it('checks if db()->save() function creates table', function ($useUUID) {

$user = db($useUUID)->create('user');
$user->name = fake()->name();
$user->email = fake()->email();
$user->dob = fake()->date();
$user->age = fake()->randomNumber(2, false);
$user->active = false;

$id = db($useUUID)->save($user);
assert(db($useUUID)->getConnection()->createSchemaManager()->tablesExist(['user']));

$user = db($useUUID)->getOne('user', $id);
$this->assertEquals($user->name, $user->name);
$this->assertEquals($user->email, $user->email);


$user = db($useUUID)->create('user');
$user->name = fake()->name();
$user->email = fake()->email();
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Database/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
db()->getConnection()->executeStatement('DROP TABLE IF EXISTS parent; ');
db()->getConnection()->executeStatement('DROP TABLE IF EXISTS parent_user; ');
db()->getConnection()->executeStatement('DROP TABLE IF EXISTS employee; ');
db()->unfreeze();
});

it(' checks db()->isUsingUUID() function ', function ($useUUID) {
Expand Down
11 changes: 2 additions & 9 deletions tests/Unit/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
});

it('tests DB::connect()', function () {
$connectionParams = [
'dbname' => 'test',
'user' => 'root',
'password' => 'root@1432',
'host' => '127.0.0.1',
'driver' => 'pdo_mysql',
'useUUID' => false,
];
$db = Scrawler\Arca\Facade\Database::connect($connectionParams);

$db = Scrawler\Arca\Facade\Database::connect(getConnectionParams());
$this->assertInstanceOf(Scrawler\Arca\Database::class, $db);
});

Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Factory/DatabaseFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
covers(Scrawler\Arca\Factory\DatabaseFactory::class);
covers(Scrawler\Arca\Config::class);
beforeEach(function () {
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 proper initialization of database', function ($useUUID) {
$factory = new Scrawler\Arca\Factory\DatabaseFactory();

$db = $factory->build(getConnectionParams($useUUID));
$this->assertInstanceOf(Scrawler\Arca\Database::class, $db);
$this->assertEquals($useUUID == 'UUID', $db->isUsingUUID());

$db = $factory->build(getConnectionParams($useUUID,false));
$this->assertInstanceOf(Scrawler\Arca\Database::class, $db);
$this->assertEquals(false, $db->isUsingUUID());


})->with('useUUID');

it('tests proper initialization of database with container provided', function ($useUUID) {
$factory = new Scrawler\Arca\Factory\DatabaseFactory(container: new \DI\Container());

$db = $factory->build(getConnectionParams($useUUID));
$this->assertInstanceOf(Scrawler\Arca\Database::class, $db);
$this->assertEquals($useUUID == 'UUID', $db->isUsingUUID());

$db = $factory->build(getConnectionParams($useUUID,false));
$this->assertInstanceOf(Scrawler\Arca\Database::class, $db);
$this->assertEquals(false, $db->isUsingUUID());


})->with('useUUID');

0 comments on commit bf2f0ad

Please sign in to comment.