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 committed Apr 16, 2016
1 parent b45df80 commit d256d9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2266,9 +2266,11 @@ public static function unguarded(callable $callback)

static::unguard();

$result = $callback();

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

return $result;
}
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 d256d9b

Please sign in to comment.