-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from thenotsoft/add_test_to_sequence_default_…
…value Add test to sequence default value
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters