Skip to content

Commit 3ea03e8

Browse files
freekmurzeclaude
andcommitted
Use static:: in storeWebhook and cover custom webhook_model (refs #253)
storeWebhook() already respects a configured custom webhook_model: the self::create() call is a forwarding static call, so Eloquent's __callStatic (which uses new static) resolves to the configured model and its table/connection. Switch to static:: to make that intent explicit and add a regression test storing through a custom model on its own table. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 83e50b9 commit 3ea03e8

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/Models/WebhookCall.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ class WebhookCall extends Model
4848

4949
public static function storeWebhook(WebhookConfig $config, Request $request): WebhookCall
5050
{
51-
$headers = self::headersToStore($config, $request);
52-
$payload = self::buildPayloadFromRequest($request);
51+
$headers = static::headersToStore($config, $request);
52+
$payload = static::buildPayloadFromRequest($request);
5353

54-
return self::create([
54+
return static::create([
5555
'name' => $config->name,
5656
'url' => $request->fullUrl(),
5757
'headers' => $headers,
@@ -65,7 +65,7 @@ protected static function buildPayloadFromRequest(Request $request): array
6565
$payload = $request->input();
6666

6767
if ($request->allFiles()) {
68-
$payload['attachments'] = self::processRequestFiles($request->allFiles());
68+
$payload['attachments'] = static::processRequestFiles($request->allFiles());
6969
}
7070

7171
return $payload;
@@ -76,11 +76,11 @@ protected static function processRequestFiles(array $files): array
7676
return collect($files)
7777
->flatMap(function ($fieldFiles) {
7878
if (! is_array($fieldFiles)) {
79-
return [self::processUploadedFile($fieldFiles)];
79+
return [static::processUploadedFile($fieldFiles)];
8080
}
8181

8282
return collect($fieldFiles)->map(function ($file) {
83-
return self::processUploadedFile($file);
83+
return static::processUploadedFile($file);
8484
});
8585
})
8686
->toArray();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\WebhookClient\Tests\TestClasses;
4+
5+
use Spatie\WebhookClient\Models\WebhookCall;
6+
7+
class CustomTableWebhookCall extends WebhookCall
8+
{
9+
protected $table = 'custom_webhook_calls';
10+
}

tests/WebhookCallModelTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

3+
use Illuminate\Database\Schema\Blueprint;
34
use Illuminate\Http\Request;
45
use Illuminate\Http\UploadedFile;
6+
use Illuminate\Support\Facades\Schema;
57
use Illuminate\Support\Facades\Storage;
68
use Spatie\WebhookClient\Models\WebhookCall;
9+
use Spatie\WebhookClient\Tests\TestClasses\CustomTableWebhookCall;
710
use Spatie\WebhookClient\WebhookConfig;
811

912
beforeEach(function () {
@@ -31,6 +34,39 @@
3134
expect($webhookCall->payload)->not->toHaveKey('attachments');
3235
});
3336

37+
it('stores the webhook using the configured webhook model table', function () {
38+
Schema::create('custom_webhook_calls', function (Blueprint $table) {
39+
$table->id();
40+
$table->string('name');
41+
$table->string('url');
42+
$table->json('headers')->nullable();
43+
$table->json('payload')->nullable();
44+
$table->json('exception')->nullable();
45+
$table->timestamps();
46+
});
47+
48+
$this->webhookConfig = new WebhookConfig([
49+
'name' => 'test',
50+
'signing_secret' => 'secret',
51+
'signature_header_name' => 'Signature',
52+
'signature_validator' => \Spatie\WebhookClient\SignatureValidator\DefaultSignatureValidator::class,
53+
'webhook_profile' => \Spatie\WebhookClient\WebhookProfile\ProcessEverythingWebhookProfile::class,
54+
'webhook_response' => \Spatie\WebhookClient\WebhookResponse\DefaultRespondsTo::class,
55+
'webhook_model' => CustomTableWebhookCall::class,
56+
'process_webhook_job' => \Spatie\WebhookClient\Tests\TestClasses\ProcessWebhookJobTestClass::class,
57+
'store_headers' => [],
58+
]);
59+
60+
$request = Request::create('/test', 'POST', ['key' => 'value']);
61+
62+
$webhookCall = CustomTableWebhookCall::storeWebhook($this->webhookConfig, $request);
63+
64+
expect($webhookCall)->toBeInstanceOf(CustomTableWebhookCall::class);
65+
expect($webhookCall->getTable())->toBe('custom_webhook_calls');
66+
expect(CustomTableWebhookCall::count())->toBe(1);
67+
expect(WebhookCall::count())->toBe(0);
68+
});
69+
3470
it('can store webhook with single file', function () {
3571
Storage::fake('local');
3672

0 commit comments

Comments
 (0)