Skip to content

Commit

Permalink
Add Tests for Joining tables on non-id fields
Browse files Browse the repository at this point in the history
  • Loading branch information
abbadon1334 committed Dec 15, 2022
1 parent 9ec3683 commit 2426c4d
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,63 @@ public function testExpressionJoin(): void
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
}

public function testExpressionJoinForeignCustomIdField(): void
{
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2],
3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1],
],
'contact' => [
1 => ['custom_id' => 1, 'contact_id' => 1, 'contact_phone' => '+123 smiths'],
2 => ['custom_id' => 2, 'contact_id' => 2, 'contact_phone' => '+321 sues'],
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m->addField('name');
$m->addField('gender');
$m->addField('surname');

$m->join('contact', [
'masterField' => 'contact_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'custom_id'
])->addField('contact_phone');

$mm2 = $m->load(1);
static::assertSame('John', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
$mm2 = $m->load(2);
static::assertSame('Sue', $mm2->get('name'));
static::assertSame('+321 sues', $mm2->get('contact_phone'));
$mm2 = $m->load(3);
static::assertSame('Peter', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));

$mm = clone $m;
$mm->addCondition($mm->expr('[name] = [surname]'));
$mm2 = $mm->tryLoad(1);
static::assertNull($mm2);
$mm2 = $mm->load(2);
static::assertSame('Sue', $mm2->get('name'));
static::assertSame('+321 sues', $mm2->get('contact_phone'));
$mm2 = $mm->tryLoad(3);
static::assertNull($mm2);

$mm = clone $m;
$mm->addCondition($mm->expr('\'+123 smiths\' = [contact_phone]'));
$mm2 = $mm->load(1);
static::assertSame('John', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
$mm2 = $mm->tryLoad(2);
static::assertNull($mm2);
$mm2 = $mm->load(3);
static::assertSame('Peter', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
}

public function testArrayCondition(): void
{
$this->setDb([
Expand Down
227 changes: 227 additions & 0 deletions tests/JoinSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Data\Exception;
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Platforms\MySQLPlatform;

Expand Down Expand Up @@ -762,4 +763,230 @@ public function testJoinActualFieldNamesAndPrefix(): void
],
], $this->getDb());
}

public function testJoinSavingForeignCustomIdFieldRaiseException(): void
{
$this->expectException(\Atk4\Data\Persistence\Sql\ExecuteException::class);

$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($master_model)->create();
$this->createMigrator($joined_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'foreignTable' => 'contact_join',
'masterField' => 'test_id',
'foreignField' => 'contact_id',
//'foreignModelIdField' => 'not_named_id',
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user = $user->load(1);

$user->set('name', 'John');
$user->set('contact_phone', '+321');

$user->save();
}

public function testJoinSavingForeignCustomIdField(): void
{
$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($master_model)->create();
$this->createMigrator($joined_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'masterField' => 'test_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'not_named_id',
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user = $user->load(1);

$user->set('name', 'John');
$user->set('contact_phone', '+321');

$user->save();

static::assertSame([
'user_join' => [
1 => ['id' => 1, 'name' => 'John', 'test_id' => '21'],
],
'contact_join' => [
1 => ['not_named_id' => 1, 'contact_id' => '21', 'contact_phone' => '+321'],
],
], [
'user_join' => [
1 => $master_model->load(1)->get(),
],
'contact_join' => [
1 => $joined_model->load(1)->get(),
],
]);
}

public function testJoinDeleteForeignCustomIdField(): void
{
$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$this->createMigrator($master_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($joined_model)->create();

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'masterField' => 'test_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'not_named_id',
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user->delete(1);

static::assertSame(null, $master_model->tryLoad(1));
static::assertSame(null, $joined_model->tryLoad(1));
}

public function testJoinDeleteForeignCustomIdFieldReverse(): void
{
$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$this->createMigrator($master_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($joined_model)->create();

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'masterField' => 'test_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'not_named_id',
'reverse' => true,
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user->delete(1);

static::assertSame(null, $master_model->tryLoad(1));
static::assertSame(null, $joined_model->tryLoad(1));
}
}

0 comments on commit 2426c4d

Please sign in to comment.