Skip to content

Commit

Permalink
Add test for stored procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
harrygulliford committed Nov 2, 2021
1 parent 69aeb1d commit 1f81b69
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,16 @@ public function it_can_limit_results()
/** @test */
public function it_can_execute_stored_procedures()
{
$this->markTestSkipped('This test needs to be written.');
$firstNumber = random_int(1, 10);
$secondNumber = random_int(1, 10);

$result = DB::query()
->fromProcedure('MULTIPLY', [
$firstNumber, $secondNumber,
])
->first()
->RESULT;

$this->assertEquals($firstNumber * $secondNumber, $result);
}
}
29 changes: 29 additions & 0 deletions tests/Support/MigrateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public function setUp(): void
$this->dropTables();
$this->createTables();

$this->dropProcedure();
$this->createProcedure();

MigrationState::$migrated = true;
}
}
Expand Down Expand Up @@ -71,4 +74,30 @@ public function dropTables(): void
}
}
}

public function createProcedure()
{
DB::select(
'CREATE PROCEDURE MULTIPLY (a INTEGER, b INTEGER)
RETURNS (result INTEGER)
AS BEGIN
result = a * b;
SUSPEND;
END'
);
}

public function dropProcedure()
{
try {
DB::select('DROP PROCEDURE MULTIPLY');
} catch (QueryException $e) {
// Suppress the "table does not exist" exception, as we want to
// replicate dropIfExists() functionality without using the Schema
// class.
if (! Str::contains($e->getMessage(), 'not found')) {
throw $e;
}
}
}
}

0 comments on commit 1f81b69

Please sign in to comment.