ver. 0.274 - ShopProduct mass_edit + tree UI cleanup

This commit is contained in:
2026-02-15 11:41:04 +01:00
parent 3bac7616e7
commit eb8e8fed36
22 changed files with 905 additions and 251 deletions

View File

@@ -347,4 +347,167 @@ class ProductRepositoryTest extends TestCase
$result = $repository->archive(1);
$this->assertIsBool($result);
}
/**
* Test allProductsForMassEdit - zwraca mapę id => name
*/
public function testAllProductsForMassEditReturnsMap()
{
$mockDb = $this->createMock(\medoo::class);
$callIndex = 0;
$mockDb->method('get')
->willReturnCallback(function($table, $column, $where) use (&$callIndex) {
$callIndex++;
// 1. domyślny język
if ($table === 'pp_langs') return 'pl';
// 2. nazwa produktu 1
if ($table === 'pp_shop_products_langs' && $where['AND']['product_id'] === 1) return 'Produkt A';
// 3. nazwa produktu 2
if ($table === 'pp_shop_products_langs' && $where['AND']['product_id'] === 2) return 'Produkt B';
return false;
});
$mockDb->method('select')
->with('pp_shop_products', 'id', ['parent_id' => null])
->willReturn([1, 2]);
$repository = new ProductRepository($mockDb);
$result = $repository->allProductsForMassEdit();
$this->assertIsArray($result);
$this->assertCount(2, $result);
$this->assertEquals('Produkt A', $result[1]);
$this->assertEquals('Produkt B', $result[2]);
}
/**
* Test allProductsForMassEdit - pusta lista gdy brak produktów
*/
public function testAllProductsForMassEditEmptyWhenNoProducts()
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('get')->willReturn('pl');
$mockDb->method('select')->willReturn([]);
$repository = new ProductRepository($mockDb);
$result = $repository->allProductsForMassEdit();
$this->assertIsArray($result);
$this->assertEmpty($result);
}
/**
* Test getProductsByCategory - zwraca listę ID
*/
public function testGetProductsByCategoryReturnsList()
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('select')
->with('pp_shop_products_categories', 'product_id', ['category_id' => 5])
->willReturn([10, 20, 30]);
$repository = new ProductRepository($mockDb);
$result = $repository->getProductsByCategory(5);
$this->assertIsArray($result);
$this->assertCount(3, $result);
$this->assertEquals([10, 20, 30], $result);
}
/**
* Test getProductsByCategory - pusta lista gdy brak produktów w kategorii
*/
public function testGetProductsByCategoryReturnsEmptyArray()
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('select')->willReturn([]);
$repository = new ProductRepository($mockDb);
$result = $repository->getProductsByCategory(999);
$this->assertIsArray($result);
$this->assertEmpty($result);
}
/**
* Test applyDiscountPercent - zwraca null gdy produkt nie istnieje
*/
public function testApplyDiscountPercentReturnsNullForInvalidProduct()
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('get')->willReturn(false);
$repository = new ProductRepository($mockDb);
$result = $repository->applyDiscountPercent(999, 10.0);
$this->assertNull($result);
}
/**
* Test applyDiscountPercent - poprawny wynik z rabatem
*/
public function testApplyDiscountPercentReturnsCorrectPrices()
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('get')
->willReturn([
'vat' => 23,
'price_brutto' => '100.00',
'price_netto' => '81.30',
]);
$mockDb->method('update')
->willReturn($this->createMock(\PDOStatement::class));
// query zwracający puste kombinacje
$mockStmt = $this->createMock(\PDOStatement::class);
$mockStmt->method('fetchAll')->willReturn([]);
$mockDb->method('query')->willReturn($mockStmt);
$repository = new ProductRepository($mockDb);
$result = $repository->applyDiscountPercent(1, 10.0);
$this->assertIsArray($result);
$this->assertArrayHasKey('price_brutto', $result);
$this->assertArrayHasKey('price_brutto_promo', $result);
$this->assertEquals(100.00, $result['price_brutto']);
$this->assertEquals(90.00, $result['price_brutto_promo']);
}
/**
* Test applyDiscountPercent - rabat 0% nie tworzy ceny promocyjnej
*/
public function testApplyDiscountPercentZeroPercentNullsPromo()
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('get')
->willReturn([
'vat' => 23,
'price_brutto' => '100.00',
'price_netto' => '81.30',
]);
$mockDb->method('update')
->willReturn($this->createMock(\PDOStatement::class));
$mockStmt = $this->createMock(\PDOStatement::class);
$mockStmt->method('fetchAll')->willReturn([]);
$mockDb->method('query')->willReturn($mockStmt);
$repository = new ProductRepository($mockDb);
$result = $repository->applyDiscountPercent(1, 0.0);
$this->assertIsArray($result);
$this->assertNull($result['price_brutto_promo']);
}
}