fedi-feed-router/tests/Feature/Http/Controllers/Api/V1/ArticlesControllerTest.php

118 lines
3.4 KiB
PHP

<?php
namespace Tests\Feature\Http\Controllers\Api\V1;
use App\Models\Article;
use App\Models\Feed;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ArticlesControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_returns_successful_response(): void
{
$response = $this->getJson('/api/v1/articles');
$response->assertStatus(200)
->assertJsonStructure([
'success',
'data' => [
'articles',
'pagination' => [
'current_page',
'last_page',
'per_page',
'total',
'from',
'to',
],
'settings' => [
'publishing_approvals_enabled',
],
],
'message',
]);
}
public function test_index_returns_articles_with_pagination(): void
{
$feed = Feed::factory()->create();
Article::factory()->count(25)->create(['feed_id' => $feed->id]);
$response = $this->getJson('/api/v1/articles?per_page=10');
$response->assertStatus(200)
->assertJson([
'success' => true,
'data' => [
'pagination' => [
'per_page' => 10,
'total' => 25,
'last_page' => 3,
],
],
]);
$this->assertCount(10, $response->json('data.articles'));
}
public function test_index_respects_per_page_limit(): void
{
$feed = Feed::factory()->create();
Article::factory()->count(10)->create(['feed_id' => $feed->id]);
// Test max limit of 100
$response = $this->getJson('/api/v1/articles?per_page=150');
$response->assertStatus(200)
->assertJson([
'success' => true,
'data' => [
'pagination' => [
'per_page' => 100, // Should be capped at 100
],
],
]);
}
public function test_index_orders_articles_by_created_at_desc(): void
{
$feed = Feed::factory()->create();
$firstArticle = Article::factory()->create([
'feed_id' => $feed->id,
'created_at' => now()->subHours(2),
'title' => 'First Article',
]);
$secondArticle = Article::factory()->create([
'feed_id' => $feed->id,
'created_at' => now()->subHour(),
'title' => 'Second Article',
]);
$response = $this->getJson('/api/v1/articles');
$response->assertStatus(200);
$articles = $response->json('data.articles');
$this->assertEquals('Second Article', $articles[0]['title']);
$this->assertEquals('First Article', $articles[1]['title']);
}
public function test_index_includes_settings(): void
{
$response = $this->getJson('/api/v1/articles');
$response->assertStatus(200)
->assertJsonStructure([
'data' => [
'settings' => [
'publishing_approvals_enabled',
],
],
]);
}
}