Skip to content

Commit

Permalink
Ensure an aborted load is not finalised.
Browse files Browse the repository at this point in the history
  • Loading branch information
courtney-miles committed Sep 17, 2018
1 parent b384e3d commit ed8da3a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Load/DatabaseLoader/DatabaseLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public function isAborted(): bool

public function finalise(): void
{
if (!$this->hasBegun()) {
throw new DatabaseLoaderException('Unable to finalise when loading has not begun.');
}

if ($this->isAborted()) {
throw new DatabaseLoaderException('Unable to finalise when loading has been aborted.');
}

$this->flush();
$this->stagedLoad->commit();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Load/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public function begin(): void;

public function hasBegun(): bool;

public function finalise(): void;

public function abort(): void;

public function isAborted(): bool;

public function finalise(): void;
}
4 changes: 3 additions & 1 deletion src/Stage/FinaliseLoadStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function __construct(LoaderInterface $loader)

public function __invoke(Slurp $slurp): Slurp
{
$this->loader->finalise();
if (!$this->loader->isAborted()) {
$this->loader->finalise();
}

return $slurp;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Slurp/Load/DatabaseLoader/DatabaseLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,25 @@ public function testExceptionWhenAbortBeforeBegin()

$databaseLoader->abort();
}

public function testExceptionWhenFinaliseBeforeBegin()
{
$this->expectException(DatabaseLoaderException::class);

$databaseLoader = new DatabaseLoader('', [], $this->mockLoaderFactory, 1);

$databaseLoader->finalise();
}

public function testExceptionWhenFinaliseAfterAbort()
{
$this->expectException(DatabaseLoaderException::class);

$databaseLoader = new DatabaseLoader('', [], $this->mockLoaderFactory, 1);

$databaseLoader->begin();
$databaseLoader->abort();

$databaseLoader->finalise();
}
}
11 changes: 11 additions & 0 deletions tests/Slurp/Stage/FinaliseLoadStageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function setUp()
parent::setUp();

$this->mockLoader = \Mockery::mock(LoaderInterface::class);
$this->mockLoader->shouldReceive('isAborted')
->andReturn(false)
->byDefault();
$this->mockSlurp = \Mockery::mock(Slurp::class);

$this->stage = new FinaliseLoadStage($this->mockLoader);
Expand All @@ -50,4 +53,12 @@ public function testFinaliseLoadOnInvoke()

$this->assertSame($this->mockSlurp, ($this->stage)($this->mockSlurp));
}

public function testsDoNotFinaliseIfAborted()
{
$this->mockLoader->shouldReceive('isAborted')
->andReturn(true);
$this->mockLoader->shouldReceive('finalise')
->never();
}
}

0 comments on commit ed8da3a

Please sign in to comment.