|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Models\Plugin; |
| 6 | +use App\Services\PluginSyncService; |
| 7 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +class PluginSyncServiceTest extends TestCase |
| 12 | +{ |
| 13 | + use RefreshDatabase; |
| 14 | + |
| 15 | + public function test_sync_extracts_mobile_min_version_from_composer_data(): void |
| 16 | + { |
| 17 | + $composerJson = json_encode([ |
| 18 | + 'name' => 'acme/test-plugin', |
| 19 | + 'require' => [ |
| 20 | + 'nativephp/mobile' => '^3.0.0', |
| 21 | + ], |
| 22 | + ]); |
| 23 | + |
| 24 | + Http::fake([ |
| 25 | + 'api.github.com/repos/acme/test-plugin/contents/composer.json' => Http::response([ |
| 26 | + 'content' => base64_encode($composerJson), |
| 27 | + ]), |
| 28 | + 'api.github.com/repos/acme/test-plugin/contents/nativephp.json' => Http::response([], 404), |
| 29 | + 'raw.githubusercontent.com/*' => Http::response('', 404), |
| 30 | + 'api.github.com/repos/acme/test-plugin/releases/latest' => Http::response([], 404), |
| 31 | + 'api.github.com/repos/acme/test-plugin/tags*' => Http::response([]), |
| 32 | + 'api.github.com/repos/acme/test-plugin/contents/LICENSE*' => Http::response([], 404), |
| 33 | + ]); |
| 34 | + |
| 35 | + $plugin = Plugin::factory()->create([ |
| 36 | + 'name' => 'acme/test-plugin', |
| 37 | + 'repository_url' => 'https://github.com/acme/test-plugin', |
| 38 | + 'mobile_min_version' => null, |
| 39 | + ]); |
| 40 | + |
| 41 | + $service = new PluginSyncService; |
| 42 | + $result = $service->sync($plugin); |
| 43 | + |
| 44 | + $this->assertTrue($result); |
| 45 | + $this->assertEquals('^3.0.0', $plugin->fresh()->mobile_min_version); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_sync_sets_mobile_min_version_to_null_when_not_in_composer_data(): void |
| 49 | + { |
| 50 | + $composerJson = json_encode([ |
| 51 | + 'name' => 'acme/test-plugin', |
| 52 | + 'require' => [ |
| 53 | + 'php' => '^8.2', |
| 54 | + ], |
| 55 | + ]); |
| 56 | + |
| 57 | + Http::fake([ |
| 58 | + 'api.github.com/repos/acme/test-plugin/contents/composer.json' => Http::response([ |
| 59 | + 'content' => base64_encode($composerJson), |
| 60 | + ]), |
| 61 | + 'api.github.com/repos/acme/test-plugin/contents/nativephp.json' => Http::response([], 404), |
| 62 | + 'raw.githubusercontent.com/*' => Http::response('', 404), |
| 63 | + 'api.github.com/repos/acme/test-plugin/releases/latest' => Http::response([], 404), |
| 64 | + 'api.github.com/repos/acme/test-plugin/tags*' => Http::response([]), |
| 65 | + 'api.github.com/repos/acme/test-plugin/contents/LICENSE*' => Http::response([], 404), |
| 66 | + ]); |
| 67 | + |
| 68 | + $plugin = Plugin::factory()->create([ |
| 69 | + 'name' => 'acme/test-plugin', |
| 70 | + 'repository_url' => 'https://github.com/acme/test-plugin', |
| 71 | + 'mobile_min_version' => '^2.0.0', |
| 72 | + ]); |
| 73 | + |
| 74 | + $service = new PluginSyncService; |
| 75 | + $result = $service->sync($plugin); |
| 76 | + |
| 77 | + $this->assertTrue($result); |
| 78 | + $this->assertNull($plugin->fresh()->mobile_min_version); |
| 79 | + } |
| 80 | +} |
0 commit comments