|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Jobs; |
| 4 | + |
| 5 | +use App\Jobs\SyncPluginReleases; |
| 6 | +use App\Models\Plugin; |
| 7 | +use App\Models\PluginVersion; |
| 8 | +use App\Services\SatisService; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Illuminate\Support\Facades\Http; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class SyncPluginReleasesTest extends TestCase |
| 14 | +{ |
| 15 | + use RefreshDatabase; |
| 16 | + |
| 17 | + public function test_it_updates_latest_version_on_plugin_when_new_releases_are_synced(): void |
| 18 | + { |
| 19 | + Http::fake([ |
| 20 | + 'api.github.com/repos/acme/test-plugin/releases*' => Http::response([ |
| 21 | + [ |
| 22 | + 'id' => 1, |
| 23 | + 'tag_name' => 'v1.0.0', |
| 24 | + 'body' => 'Initial release', |
| 25 | + 'target_commitish' => 'abc123', |
| 26 | + 'published_at' => '2026-01-01T00:00:00Z', |
| 27 | + ], |
| 28 | + [ |
| 29 | + 'id' => 2, |
| 30 | + 'tag_name' => 'v1.1.0', |
| 31 | + 'body' => 'New features', |
| 32 | + 'target_commitish' => 'def456', |
| 33 | + 'published_at' => '2026-02-01T00:00:00Z', |
| 34 | + ], |
| 35 | + ]), |
| 36 | + ]); |
| 37 | + |
| 38 | + $plugin = Plugin::factory()->create([ |
| 39 | + 'name' => 'acme/test-plugin', |
| 40 | + 'repository_url' => 'https://github.com/acme/test-plugin', |
| 41 | + 'latest_version' => '0.9.0', |
| 42 | + ]); |
| 43 | + |
| 44 | + $satisService = $this->mock(SatisService::class); |
| 45 | + |
| 46 | + $job = new SyncPluginReleases($plugin, triggerSatisBuild: false); |
| 47 | + $job->handle($satisService); |
| 48 | + |
| 49 | + $plugin->refresh(); |
| 50 | + |
| 51 | + $this->assertEquals('1.1.0', $plugin->latest_version); |
| 52 | + $this->assertCount(2, $plugin->versions); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_it_does_not_update_latest_version_when_no_new_releases(): void |
| 56 | + { |
| 57 | + Http::fake([ |
| 58 | + 'api.github.com/repos/acme/test-plugin/releases*' => Http::response([ |
| 59 | + [ |
| 60 | + 'id' => 1, |
| 61 | + 'tag_name' => 'v1.0.0', |
| 62 | + 'body' => 'Initial release', |
| 63 | + 'target_commitish' => 'abc123', |
| 64 | + 'published_at' => '2026-01-01T00:00:00Z', |
| 65 | + ], |
| 66 | + ]), |
| 67 | + ]); |
| 68 | + |
| 69 | + $plugin = Plugin::factory()->create([ |
| 70 | + 'name' => 'acme/test-plugin', |
| 71 | + 'repository_url' => 'https://github.com/acme/test-plugin', |
| 72 | + 'latest_version' => '1.0.0', |
| 73 | + ]); |
| 74 | + |
| 75 | + // Pre-create the version so nothing is "new" |
| 76 | + PluginVersion::create([ |
| 77 | + 'plugin_id' => $plugin->id, |
| 78 | + 'version' => '1.0.0', |
| 79 | + 'tag_name' => 'v1.0.0', |
| 80 | + 'github_release_id' => '1', |
| 81 | + 'published_at' => '2026-01-01T00:00:00Z', |
| 82 | + ]); |
| 83 | + |
| 84 | + $satisService = $this->mock(SatisService::class); |
| 85 | + |
| 86 | + $job = new SyncPluginReleases($plugin, triggerSatisBuild: false); |
| 87 | + $job->handle($satisService); |
| 88 | + |
| 89 | + $plugin->refresh(); |
| 90 | + |
| 91 | + $this->assertEquals('1.0.0', $plugin->latest_version); |
| 92 | + } |
| 93 | +} |
0 commit comments