Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Packages\\Tag\\Tests\\": "packages/Tag/tests/"
"Packages\\Tag\\Tests\\": "packages/Tag/tests/",
"Packages\\Category\\Tests\\": "packages/Category/tests/"
}
},
"scripts": {
Expand Down Expand Up @@ -147,4 +148,4 @@
},
"minimum-stability": "stable",
"prefer-stable": true
}
}
2 changes: 0 additions & 2 deletions packages/Category/database/seeders/CategorySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
use Illuminate\Database\Seeder;
use Packages\Category\Models\Category;
use Packages\Category\Models\CategoryProfile;
use Packages\React\Models\Follow;

class CategorySeeder extends Seeder
{
public function run(): void
{
Category::factory(10)->create()->each(function ($category) {
CategoryProfile::factory()->for($category)->create();
Follow::factory(3)->forModel($category)->create();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Packages\Category\Tests\Feature\Database\Seeders;

use Packages\Category\Database\Seeders\CategorySeeder;
use Packages\Category\Models\Category;
use Packages\Category\Models\CategoryProfile;
use Packages\Category\Tests\TestCase;

class CategorySeederTest extends TestCase
{
public function test_it_seeds_categories(): void
{
$this->seed(CategorySeeder::class);

$this->assertDatabaseCount(Category::class, 10);

$this->assertDatabaseCount(CategoryProfile::class, 10);

Category::all()->each(function (Category $category) {
$this->assertNotNull(
$category->profile,
);
});

CategoryProfile::all()->each(function (CategoryProfile $profile) {
$this->assertDatabaseHas(Category::class, [
'id' => $profile->category_id,
]);
});

$this->assertEquals(
10,
Category::has('profile')->count(),
);

$this->assertEquals(
Category::count(),
CategoryProfile::count()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Packages\Category\Tests\Feature\Http\Controllers;

use App\Services\SeoService;
use Honeystone\Seo\Contracts\BuildsMetadata;
use Inertia\Testing\AssertableInertia as Assert;
use Mockery;
use Packages\Category\Data\CategoryData;
use Packages\Category\Data\CategoryProfileData;
use Packages\Category\Services\CategoryService;
use Packages\Category\Tests\TestCase;
use Packages\Tag\Data\TagData;
use Symfony\Component\HttpFoundation\Response;

class CategoryControllerTest extends TestCase
{
public function test_it_displays_category_detail_page(): void
{
$categoryId = 1;
$slug = 'test-category';

$categoryData = new CategoryData(
id: $categoryId,
name: 'Test Category',
slug: $slug,
followers: 0,
isFollowing: false
);

$profileData = new CategoryProfileData(
id: 10,
description: 'Test Description',
color: '#fff'
);

$tagData = new TagData(
id: 5,
name: 'Test Tag',
slug: 'test-tag',
followers: 0,
isFollowing: false
);

$tags = collect([$tagData]);

$categoryService = Mockery::mock(CategoryService::class);
$categoryService->shouldReceive('getId')
->once()
->with($slug)
->andReturn($categoryId);

$categoryService->shouldReceive('getData')
->once()
->with($categoryId)
->andReturn($categoryData);

$categoryService->shouldReceive('getProfileData')
->once()
->with($categoryId)
->andReturn($profileData);

$categoryService->shouldReceive('getArticlesCount')
->once()
->with($categoryId)
->andReturn(10);

$categoryService->shouldReceive('getNewsCount')
->once()
->with($categoryId)
->andReturn(5);

$categoryService->shouldReceive('listTags')
->once()
->with($categoryId)
->andReturn($tags);

$this->app->instance(CategoryService::class, $categoryService);

$metadata = Mockery::mock(BuildsMetadata::class);
$metadata->shouldReceive('generate')->once()->andReturn('seo-html');

$seoService = Mockery::mock(SeoService::class);
$seoService->shouldReceive('getCategorySeo')
->once()
->with($categoryData, $profileData)
->andReturn($metadata);

$this->app->instance(SeoService::class, $seoService);

$response = $this->get(route('category.view', $slug));

$response->assertStatus(Response::HTTP_OK);

$response->assertInertia(
fn (Assert $page) => $page
->component('Category/Detail')

->where('category.id', $categoryId)
->where('category.slug', $slug)
->where('category.name', 'Test Category')

->where('profile.description', 'Test Description')
->where('profile.color', '#fff')

->where('articles', 10)
->where('news', 5)

->has('tags', 1)
->where('tags.0.id', 5)
->where('tags.0.name', 'Test Tag')
->where('tags.0.slug', 'test-tag')
);

$this->assertArrayHasKey('seo', $response->original->getData());
}
}
10 changes: 10 additions & 0 deletions packages/Category/tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Packages\Category\Tests;

use Tests\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
107 changes: 107 additions & 0 deletions packages/Category/tests/Unit/Data/CategoryDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Packages\Category\Tests\Unit\Data;

use Illuminate\Support\Facades\Auth;
use Mockery;
use Packages\Category\Data\CategoryData;
use Packages\Category\Models\Category;
use Packages\Category\Tests\TestCase;

class CategoryDataTest extends TestCase
{
public function test_it_can_be_instantiated_with_defaults(): void
{
$data = new CategoryData(
id: 1,
name: 'Test Category',
slug: 'test-category',
followers: 10,
isFollowing: true
);

$this->assertSame(1, $data->id);
$this->assertSame('Test Category', $data->name);
$this->assertSame('test-category', $data->slug);
$this->assertSame(10, $data->followers);
$this->assertTrue($data->isFollowing);

$this->assertSame('category', $data->type);
}

public function test_it_creates_data_from_model_without_id_by_default(): void
{
$category = Category::factory()->create([
'name' => 'Inertia',
'slug' => 'inertia',
]);

$categoryData = CategoryData::fromModel($category);

$this->assertSame(0, $categoryData->id);
$this->assertSame('Inertia', $categoryData->name);
$this->assertSame('inertia', $categoryData->slug);
$this->assertSame('category', $categoryData->type);
}

public function test_it_sets_id_when_flag_is_true(): void
{
$category = Category::factory()->create([
'name' => 'Laravel',
'slug' => 'laravel',
]);

$categoryData = CategoryData::fromModel($category, true);

$this->assertSame($category->id, $categoryData->id);
$this->assertSame('Laravel', $categoryData->name);
$this->assertSame('laravel', $categoryData->slug);
$this->assertSame('category', $categoryData->type);
}

public function test_it_uses_model_methods_for_followers_and_follow_state(): void
{
$userId = 123;
Auth::shouldReceive('id')->once()->andReturn($userId);

$category = Mockery::mock(Category::class)->makePartial();
$category->id = 99;
$category->name = 'PHP';
$category->slug = 'php';

$category->shouldReceive('followersCount')
->once()
->andReturn(42);

$category->shouldReceive('isFollowedBy')
->once()
->with($userId)
->andReturn(true);

$data = CategoryData::fromModel($category, true);

$this->assertSame(99, $data->id);
$this->assertSame(42, $data->followers);
$this->assertTrue($data->isFollowing);
}

public function test_it_handles_guest_user(): void
{
Auth::shouldReceive('id')->once()->andReturn(null);

$category = Mockery::mock(Category::class)->makePartial();
$category->id = 1;
$category->name = 'Guest';
$category->slug = 'guest';

$category->shouldReceive('followersCount')->once()->andReturn(0);
$category->shouldReceive('isFollowedBy')
->once()
->with(null)
->andReturn(false);

$data = CategoryData::fromModel($category);

$this->assertFalse($data->isFollowing);
}
}
34 changes: 34 additions & 0 deletions packages/Category/tests/Unit/Data/CategoryProfileDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Packages\Category\Tests\Unit\Data;

use Packages\Category\Data\CategoryProfileData;
use Packages\Category\Models\CategoryProfile;
use Packages\Category\Tests\TestCase;

class CategoryProfileDataTest extends TestCase
{
public function test_it_can_be_instantiated_with_valid_data(): void
{
$data = new CategoryProfileData(
id: 1,
description: 'Test Description',
color: '#ffffff',
);

$this->assertSame(1, $data->id);
$this->assertSame('Test Description', $data->description);
$this->assertSame('#ffffff', $data->color);
}

public function test_it_sets_id_when_flag_is_true(): void
{
$profile = CategoryProfile::factory()->create();

$data = CategoryProfileData::fromModel($profile, true);

$this->assertSame($profile->id, $data->id);
$this->assertSame($profile->description, $data->description);
$this->assertSame($profile->color, $data->color);
}
}
Loading
Loading