Repository: briannesbitt/Carbon (PHP)

Issue:
withTestNow leaves the effect of setTestNow in place if the closure throws an exception
<!--
    🛑 DON'T REMOVE ME.
    This issue template apply to all
      - bug reports,
      - feature proposals,
      - and documentation requests

    Having all those informations will allow us to know exactly
    what you expect and answer you faster and precisely (answer
    that matches your Carbon version, PHP version and usage).
    
    Note: Comments between <!- - and - -> won't appear in the final
    issue (See [Preview] tab).
-->
Hello,

I encountered an issue with the following code:
```php
try {
    Carbon::withTestNow(
        '1989-02-15 00:00:00',
        function () {
            throw new Exception();
        }
    );
} catch (Exception $e) {
    printf("%s\n", Carbon::now());
}
```

Carbon version: 2.57.0
PHP version: 8.0.8

<!--
    Run the command `composer show nesbot/carbon`
    to get "versions"
    Use `echo phpversion();`
    to get PHP version.

    Some issues can depends on your context, settings,
    macros, timezone, language. So to be sure the code
    you give is enough to reproduce your bug, try it
    first in:
    https://try-carbon.herokuapp.com/?theme=xcode&export&embed

    You can use the [Options] button to change the version
    then when you get the bug with this editor, you can use
    the [Export] button, copy the link of the opened tab,
    then paste it in the issue. Then we can immediatly get
    your issue.
-->


I expected to get:

```
(current date and time)
```
<!--
    Always give your expectations. Each use has their owns.
    You may want daylight saving time to be taken into account,
    someone else want it to be ignored. You may want timezone
    to be used in comparisons, someone else may not, etc.
-->

But I actually get:

```
1989-02-15 00:00:00
```
<!--
    If you did not succeed to get the same result in
    https://try-carbon.herokuapp.com/ then precise the
    result you get.
-->

Thanks!



Discussion:

Patch:
diff --git a/src/Carbon/Traits/Test.php b/src/Carbon/Traits/Test.php
index c86faa74c6..61e6266994 100644
--- a/src/Carbon/Traits/Test.php
+++ b/src/Carbon/Traits/Test.php
@@ -129,8 +129,12 @@ public static function setTestNowAndTimezone($testNow = null, $tz = null)
     public static function withTestNow($testNow = null, $callback = null)
     {
         static::setTestNow($testNow);
-        $result = $callback();
-        static::setTestNow();
+
+        try {
+            $result = $callback();
+        } finally {
+            static::setTestNow();
+        }
 
         return $result;
     }


Test patch:
diff --git a/tests/Carbon/TestingAidsTest.php b/tests/Carbon/TestingAidsTest.php
index b5052a4382..5d86882a6d 100644
--- a/tests/Carbon/TestingAidsTest.php
+++ b/tests/Carbon/TestingAidsTest.php
@@ -326,4 +326,20 @@ public function testWithTestNow()
         $currentTime = Carbon::now();
         $this->assertNotEquals($testNow, $currentTime->format('Y-m-d H:i:s'));
     }
+
+    public function testWithTestNowWithException()
+    {
+        $testNow = '2020-09-16 10:20:00';
+
+        try {
+            Carbon::withTestNow($testNow, static function () {
+                throw new \Exception();
+            });
+        } catch (\Exception $e) {
+            // ignore
+        }
+
+        $currentTime = Carbon::now();
+        $this->assertNotEquals($testNow, $currentTime->format('Y-m-d H:i:s'));
+    }
 }
diff --git a/tests/CarbonImmutable/TestingAidsTest.php b/tests/CarbonImmutable/TestingAidsTest.php
index 1a873393c5..014f10cd41 100644
--- a/tests/CarbonImmutable/TestingAidsTest.php
+++ b/tests/CarbonImmutable/TestingAidsTest.php
@@ -279,4 +279,20 @@ public function testWithTestNow()
         $currentTime = Carbon::now();
         $this->assertNotEquals($testNow, $currentTime->format('Y-m-d H:i:s'));
     }
+
+    public function testWithTestNowWithException()
+    {
+        $testNow = '2020-09-16 10:20:00';
+
+        try {
+            Carbon::withTestNow($testNow, static function () {
+                throw new \Exception();
+            });
+        } catch (\Exception $e) {
+            // ignore
+        }
+
+        $currentTime = Carbon::now();
+        $this->assertNotEquals($testNow, $currentTime->format('Y-m-d H:i:s'));
+    }
 }