Newwebshop/tests/Unit/ProductTest.php

355 lines
9.8 KiB
PHP

<?php
/**
* Copyright seit 2024 Webshop System
*
* Unit-Tests für Product-Klasse
*
* @author Webshop System
* @license GPL v3
*/
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
class ProductTest extends TestCase
{
private $conn;
protected $product;
protected function setUp(): void
{
parent::setUp();
$this->product = new Product();
// Test-Datenbank-Verbindung
$connectionParams = [
'dbname' => getenv('DB_DATABASE') ?: 'freeshop_test',
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
'host' => getenv('DB_HOST') ?: 'db',
'driver' => 'pdo_mysql',
'port' => getenv('DB_PORT') ?: 3306,
'charset' => 'utf8mb4',
];
try {
$this->conn = DriverManager::getConnection($connectionParams);
$this->setupTestData();
} catch (Exception $e) {
$this->markTestSkipped('Datenbankverbindung nicht verfügbar: ' . $e->getMessage());
}
}
protected function tearDown(): void
{
if ($this->conn) {
$this->cleanupTestData();
}
$this->product = null;
parent::tearDown();
}
private function setupTestData()
{
// Test-Kategorie erstellen
$this->conn->executeStatement('
INSERT INTO ws_category (name, description, active)
VALUES (?, ?, ?)
', ['Test Kategorie', 'Test Beschreibung', 1]);
// Test-Produkte erstellen
$this->conn->executeStatement('
INSERT INTO ws_product (name, description, price, category_id, active)
VALUES (?, ?, ?, ?, ?)
', ['Test Produkt 1', 'Test Beschreibung 1', 19.99, 1, 1]);
$this->conn->executeStatement('
INSERT INTO ws_product (name, description, price, category_id, active)
VALUES (?, ?, ?, ?, ?)
', ['Test Produkt 2', 'Test Beschreibung 2', 29.99, 1, 1]);
}
private function cleanupTestData()
{
$this->conn->executeStatement('DELETE FROM ws_product WHERE name LIKE ?', ['Test Produkt%']);
$this->conn->executeStatement('DELETE FROM ws_category WHERE name = ?', ['Test Kategorie']);
}
/**
* Test Product Constructor
*/
public function testProductConstructor()
{
$product = new Product(1);
$this->assertInstanceOf(Product::class, $product);
$this->assertEquals(1, $product->id);
}
/**
* Test Product Definition
*/
public function testProductDefinition()
{
$this->assertIsArray(Product::$definition);
$this->assertArrayHasKey('table', Product::$definition);
$this->assertArrayHasKey('primary', Product::$definition);
$this->assertArrayHasKey('fields', Product::$definition);
$this->assertEquals('product', Product::$definition['table']);
$this->assertEquals('id_product', Product::$definition['primary']);
}
/**
* Test Product Fields
*/
public function testProductFields()
{
$this->product->name = 'Test Product';
$this->product->reference = 'TEST-001';
$this->product->price = 29.99;
$this->product->active = true;
$this->assertEquals('Test Product', $this->product->name);
$this->assertEquals('TEST-001', $this->product->reference);
$this->assertEquals(29.99, $this->product->price);
$this->assertTrue($this->product->active);
}
/**
* Test Product Validation
*/
public function testProductValidation()
{
$this->product->name = 'Valid Product Name';
$this->product->reference = 'VALID-REF';
$this->product->price = 19.99;
$this->assertTrue($this->product->validateFields());
}
/**
* Test Product Save
*/
public function testProductSave()
{
$this->product->name = 'Test Save Product';
$this->product->reference = 'SAVE-001';
$this->product->price = 39.99;
$this->product->active = true;
// Mock database operations
$this->assertTrue($this->product->add());
}
/**
* Test Product Update
*/
public function testProductUpdate()
{
$this->product->id = 1;
$this->product->name = 'Updated Product';
$this->product->price = 49.99;
// Mock database operations
$this->assertTrue($this->product->update());
}
/**
* Test Product Delete
*/
public function testProductDelete()
{
$this->product->id = 1;
// Mock database operations
$this->assertTrue($this->product->delete());
}
/**
* Test Product Search
*/
public function testProductSearch()
{
$products = Product::searchByName('test');
$this->assertIsArray($products);
}
/**
* Test Product Get By Reference
*/
public function testProductGetByReference()
{
$product = Product::getByReference('TEST-REF');
$this->assertInstanceOf(Product::class, $product);
}
/**
* Test Product Price Calculation
*/
public function testProductPriceCalculation()
{
$this->product->price = 100.00;
$this->product->tax_rate = 19.0;
$priceWithTax = $this->product->getPrice(true);
$priceWithoutTax = $this->product->getPrice(false);
$this->assertEquals(119.00, $priceWithTax);
$this->assertEquals(100.00, $priceWithoutTax);
}
/**
* Test Product Stock Management
*/
public function testProductStockManagement()
{
$this->product->quantity = 50;
$this->product->out_of_stock = 0;
$this->assertTrue($this->product->checkQty(10));
$this->assertFalse($this->product->checkQty(60));
}
/**
* Test Product Categories
*/
public function testProductCategories()
{
$this->product->id = 1;
$categories = $this->product->getCategories();
$this->assertIsArray($categories);
}
/**
* Test Product Images
*/
public function testProductImages()
{
$this->product->id = 1;
$images = $this->product->getImages(1);
$this->assertIsArray($images);
}
/**
* Test Product Features
*/
public function testProductFeatures()
{
$this->product->id = 1;
$features = $this->product->getFeatures();
$this->assertIsArray($features);
}
/**
* Test Product Combinations
*/
public function testProductCombinations()
{
$this->product->id = 1;
$combinations = $this->product->getAttributeCombinations();
$this->assertIsArray($combinations);
}
/**
* Test Product Webservice
*/
public function testProductWebservice()
{
$this->product->id = 1;
$this->product->name = 'Webservice Test Product';
$wsFields = $this->product->getWebserviceObjectList('', '', '', '');
$this->assertIsArray($wsFields);
}
/**
* Test Product Cache
*/
public function testProductCache()
{
$this->product->id = 1;
$this->product->name = 'Cache Test Product';
// Test cache operations
$this->assertTrue($this->product->update());
$this->assertTrue($this->product->delete());
}
/**
* Test Product Multi Shop
*/
public function testProductMultiShop()
{
$this->product->id = 1;
$this->product->id_shop_list = [1, 2];
$this->assertIsArray($this->product->id_shop_list);
$this->assertCount(2, $this->product->id_shop_list);
}
/**
* Test Product Associations
*/
public function testProductAssociations()
{
$this->product->id = 1;
// Test category association
$this->product->id_category_default = 1;
$this->assertEquals(1, $this->product->id_category_default);
// Test manufacturer association
$this->product->id_manufacturer = 1;
$this->assertEquals(1, $this->product->id_manufacturer);
// Test supplier association
$this->product->id_supplier = 1;
$this->assertEquals(1, $this->product->id_supplier);
}
/**
* Test Product SEO
*/
public function testProductSEO()
{
$this->product->link_rewrite = 'test-product';
$this->product->meta_title = 'Test Product SEO';
$this->product->meta_description = 'Test product description for SEO';
$this->assertEquals('test-product', $this->product->link_rewrite);
$this->assertEquals('Test Product SEO', $this->product->meta_title);
$this->assertEquals('Test product description for SEO', $this->product->meta_description);
}
/**
* Test Product Shipping
*/
public function testProductShipping()
{
$this->product->width = 10.5;
$this->product->height = 5.2;
$this->product->depth = 3.1;
$this->product->weight = 0.5;
$this->assertEquals(10.5, $this->product->width);
$this->assertEquals(5.2, $this->product->height);
$this->assertEquals(3.1, $this->product->depth);
$this->assertEquals(0.5, $this->product->weight);
}
/**
* Test Product Availability
*/
public function testProductAvailability()
{
$this->product->available_for_order = true;
$this->product->show_price = true;
$this->product->online_only = false;
$this->assertTrue($this->product->available_for_order);
$this->assertTrue($this->product->show_price);
$this->assertFalse($this->product->online_only);
}
}