|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Features; |
| 4 | + |
| 5 | +use Laravel\Folio\Folio; |
| 6 | +use Sentry\Laravel\Integration; |
| 7 | +use Illuminate\Config\Repository; |
| 8 | +use Sentry\Laravel\Tests\TestCase; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | + |
| 11 | +class FolioPackageIntegrationTest extends TestCase |
| 12 | +{ |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + if (!class_exists(Folio::class)) { |
| 16 | + $this->markTestSkipped('Laravel Folio package is not installed.'); |
| 17 | + } |
| 18 | + |
| 19 | + parent::setUp(); |
| 20 | + } |
| 21 | + |
| 22 | + protected function defineRoutes($router): void |
| 23 | + { |
| 24 | + Folio::path(__DIR__ . '/../../stubs/folio')->uri('/folio'); |
| 25 | + } |
| 26 | + |
| 27 | + protected function defineEnvironment($app): void |
| 28 | + { |
| 29 | + parent::defineEnvironment($app); |
| 30 | + |
| 31 | + tap($app['config'], static function (Repository $config) { |
| 32 | + // This is done to prevent noise from the database queries in the breadcrumbs |
| 33 | + $config->set('sentry.breadcrumbs.sql_queries', false); |
| 34 | + |
| 35 | + $config->set('database.default', 'inmemory'); |
| 36 | + $config->set('database.connections.inmemory', [ |
| 37 | + 'driver' => 'sqlite', |
| 38 | + 'database' => ':memory:', |
| 39 | + ]); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + protected function defineDatabaseMigrations(): void |
| 44 | + { |
| 45 | + $this->loadLaravelMigrations(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testFolioBreadcrumbIsRecorded(): void |
| 49 | + { |
| 50 | + $this->get('/folio'); |
| 51 | + |
| 52 | + $this->assertCount(1, $this->getCurrentBreadcrumbs()); |
| 53 | + |
| 54 | + $lastBreadcrumb = $this->getLastBreadcrumb(); |
| 55 | + |
| 56 | + $this->assertEquals('folio.route', $lastBreadcrumb->getCategory()); |
| 57 | + $this->assertEquals('navigation', $lastBreadcrumb->getType()); |
| 58 | + $this->assertEquals('/folio/index', $lastBreadcrumb->getMessage()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testFolioRouteUpdatesIntegrationTransaction(): void |
| 62 | + { |
| 63 | + $this->get('/folio/post/123')->assertOk(); |
| 64 | + |
| 65 | + $this->assertEquals('/folio/post/{id}', Integration::getTransaction()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testFolioRouteUpdatesPerformanceTransaction(): void |
| 69 | + { |
| 70 | + $transaction = $this->startTransaction(); |
| 71 | + |
| 72 | + $this->get('/folio/post/123')->assertOk(); |
| 73 | + |
| 74 | + $this->assertEquals('/folio/post/{id}', $transaction->getName()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testFolioTransactionNameForRouteWithSingleSegmentParamater(): void |
| 78 | + { |
| 79 | + $this->get('/folio/post/123')->assertOk(); |
| 80 | + |
| 81 | + $this->assertEquals('/folio/post/{id}', Integration::getTransaction()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testFolioTransactionNameForRouteWithMultipleSegmentParameter(): void |
| 85 | + { |
| 86 | + $this->get('/folio/posts/1/2/3')->assertOk(); |
| 87 | + |
| 88 | + $this->assertEquals('/folio/posts/{...ids}', Integration::getTransaction()); |
| 89 | + } |
| 90 | + |
| 91 | + public function testFolioTransactionNameForRouteWithRouteModelBoundSegmentParameter(): void |
| 92 | + { |
| 93 | + $user = FolioPackageIntegrationUserModel::create([ |
| 94 | + 'name' => 'John Doe', |
| 95 | + |
| 96 | + 'password' => 'secret', |
| 97 | + ]); |
| 98 | + |
| 99 | + $this->get("/folio/user/{$user->id}")->assertOk(); |
| 100 | + |
| 101 | + // This looks a little odd, but that is because we want to make the route model binding work in our tests |
| 102 | + // normally this would look like `/folio/user/{User}` instead, see: https://laravel.com/docs/10.x/folio#route-model-binding. |
| 103 | + $this->assertEquals('/folio/user/{.Sentry.Features.FolioPackageIntegrationUserModel}', Integration::getTransaction()); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class FolioPackageIntegrationUserModel extends Model |
| 108 | +{ |
| 109 | + protected $table = 'users'; |
| 110 | + protected $guarded = false; |
| 111 | +} |
0 commit comments