Skip to content

Commit

Permalink
Add extra test
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Apr 23, 2024
1 parent a9e0655 commit 3b2f67d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
52 changes: 49 additions & 3 deletions tests/BacktraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
use Spatie\Backtrace\Backtrace;
use Spatie\Backtrace\Frame;
use Spatie\Backtrace\Tests\TestClasses\FakeArgumentReducer;
use Spatie\Backtrace\Tests\TestClasses\LaravelSerializeableClosureThrow;
use Spatie\Backtrace\Tests\TestClasses\LaravelSerializableClosureCallThrow;
use Spatie\Backtrace\Tests\TestClasses\LaravelSerializableClosureThrow;
use Spatie\Backtrace\Tests\TestClasses\ThrowAndReturnExceptionAction;
use Spatie\Backtrace\Tests\TestClasses\TraceArguments;

Expand Down Expand Up @@ -224,7 +225,11 @@ public function it_can_get_a_backtrace_from_a_throwable()
/** @test */
public function it_can_handle_a_laravel_serializable_closure_via_throwable()
{
$throwable = LaravelSerializeableClosureThrow::getThrowable();
if (version_compare(PHP_VERSION, '8.1', '<')) {
$this->markTestSkipped('Enums are only supported in PHP 8.1+');
}

$throwable = LaravelSerializableClosureThrow::getThrowable();

$frames = Backtrace::createForThrowable($throwable)->frames();

Expand All @@ -235,7 +240,7 @@ public function it_can_handle_a_laravel_serializable_closure_via_throwable()

$this->assertEquals(2, $firstFrame->lineNumber);
$this->assertEquals('{closure}', $firstFrame->method);
$this->assertEquals(LaravelSerializeableClosureThrow::class, $firstFrame->class);
$this->assertEquals(LaravelSerializableClosureThrow::class, $firstFrame->class);
$this->assertTrue($firstFrame->applicationFrame);

$firstFrameSnippet = $firstFrame->getSnippetAsString(5);
Expand All @@ -250,6 +255,47 @@ public function it_can_handle_a_laravel_serializable_closure_via_throwable()
);
}

/** @test */
public function it_can_handle_a_laravel_serializable_closure_via_call_throwable()
{
if (version_compare(PHP_VERSION, '8.1', '<')) {
$this->markTestSkipped('Enums are only supported in PHP 8.1+');
}

$throwable = LaravelSerializableClosureCallThrow::getThrowable();

$frames = Backtrace::createForThrowable($throwable)->frames();

/** @var Frame $firstFrame */
$firstFrame = $frames[0];

$this->assertEquals(29, $firstFrame->lineNumber);
$this->assertEquals('throw', $firstFrame->method);
$this->assertEquals(LaravelSerializableClosureCallThrow::class, $firstFrame->class);
$this->assertEquals(realpath(__DIR__.'/../tests/TestClasses/LaravelSerializableClosureCallThrow.php'), $firstFrame->file);
$this->assertTrue($firstFrame->applicationFrame);


/** @var Frame $secondFrame */
$secondFrame = $frames[1];

$this->assertEquals(2, $secondFrame->lineNumber);
$this->assertEquals('{closure}', $secondFrame->method);
$this->assertEquals(LaravelSerializableClosureCallThrow::class, $secondFrame->class);
$this->assertTrue($secondFrame->applicationFrame);

$secondFrameSnippet = $secondFrame->getSnippetAsString(5);

$this->assertEquals(
<<<'EOT'
1 laravel-serializable-closure://function () {
2 self::throw();
3 }
EOT,
$secondFrameSnippet
);
}

/** @test */
public function it_can_get_the_index_of_the_first_application_frame()
{
Expand Down
31 changes: 31 additions & 0 deletions tests/TestClasses/LaravelSerializableClosureCallThrow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\Backtrace\Tests\TestClasses;

use Exception;
use Laravel\SerializableClosure\SerializableClosure;
use Throwable;

class LaravelSerializableClosureCallThrow
{
public static function getThrowable(): Throwable
{
$closure = function () {
self::throw();
};

$serialized = serialize(new SerializableClosure($closure));
$closure = unserialize($serialized)->getClosure();

try {
$closure();
} catch (Throwable $exception) {
return $exception;
}
}

public static function throw()
{
throw new Exception('This is a test exception from a serialized closure');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Laravel\SerializableClosure\SerializableClosure;
use Throwable;

class LaravelSerializeableClosureThrow
class LaravelSerializableClosureThrow
{
public static function getThrowable(): Throwable
{
Expand All @@ -23,4 +23,5 @@ public static function getThrowable(): Throwable
return $exception;
}
}

}

0 comments on commit 3b2f67d

Please sign in to comment.