Skip to content

Commit

Permalink
Add unit tests for default values with output
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Feb 8, 2024
1 parent ecf1082 commit 4e2017f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Driver/SQLServer/Query/SQLServerInsertQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ public function run(): mixed

public function getTokens(): array
{
return [
'table' => $this->table,
return parent::getTokens() + [
'return' => $this->returningColumns,
'columns' => $this->columns,
'values' => $this->values,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public function testMultipleReturningWithFragment(): void
);
}

public function testReturningWithDefaultValues(): void
{
$insert = $this->database->insert()->into('table')->values([])->returning('created_at');

$this->assertSameQuery(
'INSERT INTO {table} OUTPUT INSERTED.[created_at] DEFAULT VALUES',
$insert
);
}

public function testReturningValuesFromDatabase(): void
{
$schema = $this->schema('returning_values');
Expand Down Expand Up @@ -124,6 +134,27 @@ public function testReturningSingleValueFromDatabase(): void
$this->assertNotFalse(\strtotime($returning));
}

public function testReturningValuesFromDatabaseWithDefaultValuesInsert(): void
{
$schema = $this->schema('returning_value');
$schema->primary('id');
$schema->datetime('created_at', defaultValue: SQLServerColumn::DATETIME_NOW);
$schema->datetime('updated_at', defaultValue: SQLServerColumn::DATETIME_NOW);
$schema->save();

$returning = $this->database
->insert('returning_value')
->values([])
->returning('updated_at', new Fragment('INSERTED.[created_at] as [created]'))
->run();

$this->assertIsString($returning['created']);
$this->assertNotFalse(\strtotime($returning['created']));

$this->assertIsString($returning['updated_at']);
$this->assertNotFalse(\strtotime($returning['updated_at']));
}

public function testCustomReturningShouldContainColumns(): void
{
$this->expectException(BuilderException::class);
Expand Down

0 comments on commit 4e2017f

Please sign in to comment.