jonnybarnes.uk/tests/Feature/LikesTest.php
Jonny Barnes e08763c526
Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org,
webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id
lookup, contact avatar/h-card fetch, profile image download, and the
CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http,
and enables Http::preventStrayRequests() globally in tests so any
un-faked outbound call now fails loudly instead of silently hitting the
network.

The CloudConvert retry-until-finished middleware in AppServiceProvider
is replaced by a plain polling loop in SaveScreenshot, which also fixes
a latent bug where the old middleware decoded a response object instead
of its body. Bridgy syndication jobs keep their tries=1/no-retry
semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers
(Header, UriResolver, Utils) stay in SendWebMentions since Http has no
equivalent for them.

Every affected test file's Guzzle MockHandler/HandlerStack boilerplate
is replaced with Http::fake()/Http::sequence().

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
2026-07-26 09:12:01 +01:00

181 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Jobs\ProcessLike;
use App\Models\Like;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;
use Jonnybarnes\WebmentionsParser\Authorship;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
use Tests\TestToken;
class LikesTest extends TestCase
{
use RefreshDatabase;
use TestToken;
#[Test]
public function likes_page_has_correct_view(): void
{
$response = $this->get('/likes');
$response->assertViewIs('likes.index');
}
#[Test]
public function single_like_page_has_correct_view(): void
{
$like = Like::factory()->create();
$response = $this->get('/likes/'.$like->id);
$response->assertViewIs('likes.show');
}
#[Test]
public function check_like_created_from_micropub_api_requests(): void
{
Queue::fake();
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->getToken(),
])->json('POST', '/api/post', [
'type' => ['h-entry'],
'properties' => [
'like-of' => ['https://example.org/blog-post'],
],
]);
$response->assertJson(['response' => 'created']);
Queue::assertPushed(ProcessLike::class);
$this->assertDatabaseHas('likes', ['url' => 'https://example.org/blog-post']);
}
#[Test]
public function check_like_created_from_micropub_web_requests(): void
{
Queue::fake();
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->getToken(),
])->post('/api/post', [
'h' => 'entry',
'like-of' => 'https://example.org/blog-post',
]);
$response->assertStatus(201);
Queue::assertPushed(ProcessLike::class);
$this->assertDatabaseHas('likes', ['url' => 'https://example.org/blog-post']);
}
#[Test]
public function like_with_simple_author(): void
{
$like = new Like;
$like->url = 'http://example.org/note/id';
$like->save();
$id = $like->id;
$job = new ProcessLike($like);
$content = <<<'END'
<html>
<body>
<div class="h-entry">
<div class="e-content">
A post that I like.
</div>
by <span class="p-author">Fred Bloggs</span>
</div>
</body>
</html>
END;
Http::fake([
'*' => Http::response($content, 200),
]);
$authorship = new Authorship;
$job->handle($authorship);
$this->assertEquals('Fred Bloggs', Like::find($id)->author_name);
}
#[Test]
public function like_with_h_card(): void
{
$like = new Like;
$like->url = 'http://example.org/note/id';
$like->save();
$id = $like->id;
$job = new ProcessLike($like);
$content = <<<'END'
<html>
<body>
<div class="h-entry">
<div class="e-content">
A post that I like.
</div>
by
<div class="p-author h-card">
<span class="p-name">Fred Bloggs</span>
<a class="u-url" href="https://fredd.blog/gs"></a>
</div>
</div>
</body>
</html>
END;
Http::fake([
'*' => Http::response($content, 200),
]);
$authorship = new Authorship;
$job->handle($authorship);
$this->assertEquals('Fred Bloggs', Like::find($id)->author_name);
}
#[Test]
public function like_without_microformats(): void
{
$like = new Like;
$like->url = 'http://example.org/note/id';
$like->save();
$id = $like->id;
$job = new ProcessLike($like);
$content = <<<'END'
<html>
<body>
<div>
I liked a post
</div>
</body>
</html>
END;
Http::fake([
'*' => Http::response($content, 200),
]);
$authorship = new Authorship;
$job->handle($authorship);
$this->assertNull(Like::find($id)->author_name);
}
#[Test]
public function unknown_like_gives_not_found_response(): void
{
$response = $this->get('/likes/202');
$response->assertNotFound();
}
}