laravel-shop/tests/Feature/CommandProductExamplesTest.php

81 lines
3.4 KiB
PHP
Raw Normal View History

2025-12-02 09:18:20 +00:00
<?php
namespace Blax\Shop\Tests\Feature;
use Blax\Shop\Console\Commands\ShopAddExampleProducts;
use Blax\Shop\Models\Product;
use Blax\Shop\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
2025-12-24 18:40:10 +00:00
use PHPUnit\Framework\Attributes\Test;
2025-12-02 09:18:20 +00:00
class CommandProductExamplesTest extends TestCase
{
use RefreshDatabase;
2025-12-24 18:40:10 +00:00
#[Test]
2025-12-02 09:18:20 +00:00
public function it_creates_example_products_and_related_data(): void
{
$this->artisan(ShopAddExampleProducts::class, ['--clean' => true, '--count' => 2])
->assertExitCode(0);
2025-12-17 12:04:58 +00:00
// Parent products (no parent_id) should be 6 types * 2 count = 12
2025-12-02 09:18:20 +00:00
$parents = Product::whereNull('parent_id')->get();
2025-12-17 12:04:58 +00:00
$this->assertCount(12, $parents, 'Expected 12 parent example products (6 types * 2 each)');
2025-12-02 09:18:20 +00:00
2025-12-17 12:04:58 +00:00
// Total products should include variations, grouped children, and pool items
$this->assertGreaterThanOrEqual(30, Product::count(), 'Expected at least 30 total products including children');
2025-12-02 09:18:20 +00:00
2025-12-17 12:04:58 +00:00
// Categories are created (6 hotel categories) and attached to parents
$this->assertGreaterThanOrEqual(6, \Blax\Shop\Models\ProductCategory::count(), 'Expected at least 6 example categories');
2025-12-02 09:18:20 +00:00
// Each product (including variants/children) must have a default price
/** @var Product $p */
foreach (Product::all() as $p) {
$this->assertTrue($p->defaultPrice()->exists(), 'Each product should have a default price');
}
$variation = Product::whereNotNull('parent_id')->first();
$this->assertNotNull($variation, 'There should be at least one variation');
// Localization for name is populated
$this->assertNotEmpty(Product::first()->getLocalized('name'));
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-12-02 09:18:20 +00:00
public function it_cleans_existing_examples_when_option_provided(): void
{
// Seed examples
$this->artisan(ShopAddExampleProducts::class, ['--clean' => true, '--count' => 1])->assertExitCode(0);
$this->assertGreaterThan(0, Product::where('slug', 'like', 'example-%')->count());
// Clean again (count=0 will create categories but no products)
$this->artisan(ShopAddExampleProducts::class, ['--clean' => true, '--count' => 0])->assertExitCode(0);
2025-12-17 12:04:58 +00:00
// All example products removed, categories recreated (6 hotel categories)
2025-12-02 09:18:20 +00:00
$this->assertEquals(0, Product::where('slug', 'like', 'example-%')->count());
2025-12-17 12:04:58 +00:00
$this->assertEquals(6, \Blax\Shop\Models\ProductCategory::where('slug', 'like', 'example-%')->count());
2025-12-02 09:18:20 +00:00
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-12-02 09:18:20 +00:00
public function it_honors_the_count_option_for_each_type(): void
{
$this->artisan(ShopAddExampleProducts::class, ['--clean' => true, '--count' => 3])
->assertExitCode(0);
2025-12-17 12:04:58 +00:00
// For each of the 6 types, expect 3 parent products
2025-12-02 09:18:20 +00:00
$parents = Product::whereNull('parent_id')->get();
2025-12-17 12:04:58 +00:00
$this->assertCount(18, $parents);
2025-12-02 09:18:20 +00:00
$byType = $parents->groupBy('type');
$this->assertEquals(3, $byType['simple']->count());
$this->assertEquals(3, $byType['variable']->count());
$this->assertEquals(3, $byType['grouped']->count());
$this->assertEquals(3, $byType['external']->count());
2025-12-17 12:04:58 +00:00
$this->assertEquals(3, $byType['booking']->count());
$this->assertEquals(3, $byType['pool']->count());
2025-12-02 09:18:20 +00:00
// Sanity: external products do not manage stock
$this->assertTrue($byType['external']->every(fn($p) => $p->manage_stock === false));
}
}