Skip to content

Commit

Permalink
Make sure unguarded() does not change unguarded state on exception (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroennoten authored and taylorotwell committed Apr 18, 2016
1 parent b45df80 commit 712332c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2266,11 +2266,11 @@ public static function unguarded(callable $callback)

static::unguard();

$result = $callback();

static::reguard();

return $result;
try {
return $callback();
} finally {
static::reguard();
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,18 @@ public function testUnguardedCallDoesNotChangeUnguardedState()
Model::reguard();
}

public function testUnguardedCallDoesNotChangeUnguardedStateOnException()
{
try {
Model::unguarded(function () {
throw new Exception;
});
} catch (Exception $e) {
// ignore the exception
}
$this->assertFalse(Model::isUnguarded());
}

public function testHasOneCreatesProperRelation()
{
$model = new EloquentModelStub;
Expand Down

0 comments on commit 712332c

Please sign in to comment.