Skip to content

Commit

Permalink
refactor: use where condition in hasOne hasMany
Browse files Browse the repository at this point in the history
- Tests: 56, Assertions: 17, Skipped: 42
  • Loading branch information
SonyPradana committed Feb 21, 2024
1 parent 45dd40a commit 97262b4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/System/Database/MyModel/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,13 @@ public function hasOne(string $table, string $ref = 'id')
{
$ref = MyQuery::from($this->table_name, $this->pdo)
->select([$table . '.*'])
->join(InnerJoin::ref($table, $this->primery_key, $ref))
->equal($this->primery_key, $this->indentifer[$this->primery_key])
->single()
;
->join(InnerJoin::ref($table, $this->primery_key, $ref));

return new CollectionImmutable($ref);
foreach ($this->indentifer as $column_name => $value) {
$ref->in($column_name, $value);
}

return new CollectionImmutable($ref->single());
}

/**
Expand All @@ -266,13 +267,13 @@ public function hasMany(string $table, string $ref = 'id')
{
$ref = MyQuery::from($this->table_name, $this->pdo)
->select([$table . '.*'])
->join(InnerJoin::ref($table, $this->primery_key, $ref))
->equal($this->primery_key, $this->indentifer[$this->primery_key])
->get()
->immutable()
;
->join(InnerJoin::ref($table, $this->primery_key, $ref));

foreach ($this->indentifer as $column_name => $value) {
$ref->in($column_name, $value);
}

return new CollectionImmutable($ref);
return new CollectionImmutable($ref->get()->immutable());
}

/**
Expand Down
84 changes: 81 additions & 3 deletions tests/DataBase/Model/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace System\Test\Database\Model;

use System\Database\MyModel\Model;
use System\Database\MyQuery\Insert;
use System\Test\Database\BaseConnection;

final class BaseModelTest extends BaseConnection
Expand Down Expand Up @@ -37,6 +38,47 @@ public function user(bool $read = true): User
return $user;
}

private function createProfileSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `profiles` (
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`gender` varchar(10) NOT NULL,
PRIMARY KEY (`user`)
)')
->execute();
}

private function createProfiles($profiles): bool
{
return (new Insert('profiles', $this->pdo))
->rows($profiles)
->execute();
}

private function createOrderSchema(): bool
{
return $this
->pdo
->query('CREATE TABLE `orders` (
`id` varchar(3) NOT NULL,
`user` varchar(32) NOT NULL,
`name` varchar(100) NOT NULL,
`type` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
)')
->execute();
}

private function createOrders($orders): bool
{
return (new Insert('orders', $this->pdo))
->rows($orders)
->execute();
}

/**
* @test
*
Expand Down Expand Up @@ -114,7 +156,18 @@ public function itCanGetFirst()
*/
public function itCanGetHasOne()
{
$this->markTestSkipped('tdd');
// profile
$profile = [
'user' => 'taylor',
'name' => 'taylor otwell',
'gender' => 'male',
];
$this->createProfileSchema();
$this->createProfiles([$profile]);

$user = $this->user();
$result = $user->hasOne('profiles', 'user');
$this->assertEquals($profile, $result->toArray());
}

/**
Expand All @@ -124,7 +177,26 @@ public function itCanGetHasOne()
*/
public function itCanGetHasMany()
{
$this->markTestSkipped('tdd');
// order
$order = [
[
'id' => '1',
'user' => 'taylor',
'name' => 'order 1',
'type' => 'gadget',
], [
'id' => '3',
'user' => 'taylor',
'name' => 'order 2',
'type' => 'gadget',
],
];
$this->createOrderSchema();
$this->createOrders($order);

$user = $this->user();
$result = $user->hasMany('orders', 'user');
$this->assertEquals($order, $result->toArray());
}

/**
Expand Down Expand Up @@ -159,7 +231,13 @@ public function itCanCheckisDirty()
*/
public function itCanGetChangeColumn()
{
$this->markTestSkipped('tdd');
$user = $this->user();
$this->assertEquals([], $user->changes(), 'original fresh data');
// modify
$user->setter('stat', 75);
$this->assertEquals([
'stat' => 75,
], $user->changes(), 'change first column');
}

/**
Expand Down

0 comments on commit 97262b4

Please sign in to comment.