Skip to content

Commit

Permalink
Merge pull request #139 from thenotsoft/add_test_to_sequence_default_…
Browse files Browse the repository at this point in the history
…value

Add test to sequence default value
  • Loading branch information
wolfy-j authored Nov 27, 2020
2 parents 43c7313 + 01f195b commit 6c3f993
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
96 changes: 96 additions & 0 deletions tests/ORM/Driver/Postgres/SequenceDefaultValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Driver\Postgres;

use Cycle\ORM\Heap\Heap;
use Cycle\ORM\Schema;
use Cycle\ORM\Select;
use Cycle\ORM\Tests\BaseTest;
use Cycle\ORM\Tests\Fixtures\NotDeletedConstrain;
use Cycle\ORM\Tests\Fixtures\SequenceDefaultValueMapper;
use Cycle\ORM\Tests\Fixtures\User;
use Cycle\ORM\Tests\Traits\TableTrait;
use Cycle\ORM\Transaction;

class SequenceDefaultValueTest extends BaseTest
{
use TableTrait;

public const DRIVER = 'postgres';

public function setUp(): void
{
parent::setUp();

$this->clearSequence();
$this->createSequence();

$this->makeTable(
'user',
[
'id' => 'primary',
'email' => 'string',
'balance' => 'float',
'user_code' => 'int',
'deleted_at' => 'datetime,null',
]
);

$this->orm = $this->withSchema(
new Schema(
[
User::class => [
Schema::ROLE => 'user',
Schema::MAPPER => SequenceDefaultValueMapper::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => ['id', 'email', 'balance', 'user_code', 'deleted_at'],
Schema::TYPECAST => [
'id' => 'int',
'balance' => 'float',
'user_code' => 'int',
'deleted_at' => 'datetime'
],
Schema::SCHEMA => [],
Schema::RELATIONS => [],
Schema::CONSTRAIN => NotDeletedConstrain::class
]
]
)
);
}

public function tearDown(): void
{
$this->clearSequence();

parent::tearDown();
}

public function testCreate(): void
{
$u = new User();
$u->email = 'test@email.com';
$u->balance = 199;

(new Transaction($this->orm))->persist($u)->run();

$s = new Select($this->orm->withHeap(new Heap()), User::class);
$data = $s->fetchData();

$this->assertIsInt($data[0]['user_code']);
}

private function createSequence(): void
{
$this->getDatabase()->query('CREATE SEQUENCE user_code_seq INCREMENT 1 START 1 MINVALUE 1');
}

private function clearSequence(): void
{
$this->getDatabase()->execute('DROP SEQUENCE IF EXISTS user_code_seq CASCADE');
}
}
23 changes: 23 additions & 0 deletions tests/ORM/Fixtures/SequenceDefaultValueMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Fixtures;

use Cycle\ORM\Command\ContextCarrierInterface;
use Cycle\ORM\Heap\Node;
use Cycle\ORM\Heap\State;
use Cycle\ORM\Mapper\Mapper;
use Spiral\Database\Injection\Fragment;

class SequenceDefaultValueMapper extends Mapper
{
public function queueCreate($entity, Node $node, State $state): ContextCarrierInterface
{
$command = parent::queueCreate($entity, $node, $state);

$command->register('user_code', new Fragment('nextval(\'user_code_seq\')'), true);

return $command;
}
}
1 change: 1 addition & 0 deletions tests/ORM/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User implements ImagedInterface
public $id;
public $email;
public $balance;
public $user_code;

/** @var Profile */
public $profile;
Expand Down

0 comments on commit 6c3f993

Please sign in to comment.