ver. 0.274 - ShopProduct mass_edit + tree UI cleanup
This commit is contained in:
@@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
59
tests/Unit/admin/Controllers/ShopProductControllerTest.php
Normal file
59
tests/Unit/admin/Controllers/ShopProductControllerTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
namespace Tests\Unit\admin\Controllers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use admin\Controllers\ShopProductController;
|
||||
use Domain\Product\ProductRepository;
|
||||
|
||||
class ShopProductControllerTest extends TestCase
|
||||
{
|
||||
private $repository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(ProductRepository::class);
|
||||
$this->controller = new ShopProductController($this->repository);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new ShopProductController($this->repository);
|
||||
$this->assertInstanceOf(ShopProductController::class, $controller);
|
||||
}
|
||||
|
||||
public function testHasMassEditActionMethods(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'mass_edit'));
|
||||
$this->assertTrue(method_exists($this->controller, 'mass_edit_save'));
|
||||
$this->assertTrue(method_exists($this->controller, 'get_products_by_category'));
|
||||
}
|
||||
|
||||
public function testMassEditReturnsString(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->controller);
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('mass_edit')->getReturnType());
|
||||
}
|
||||
|
||||
public function testMassEditSaveReturnsVoid(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->controller);
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('mass_edit_save')->getReturnType());
|
||||
}
|
||||
|
||||
public function testGetProductsByCategoryReturnsVoid(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->controller);
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('get_products_by_category')->getReturnType());
|
||||
}
|
||||
|
||||
public function testConstructorRequiresProductRepository(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(ShopProductController::class);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(1, $params);
|
||||
$this->assertEquals('Domain\Product\ProductRepository', $params[0]->getType()->getName());
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,7 @@ if (!class_exists('S')) {
|
||||
public static function clear_product_cache($id) {}
|
||||
public static function send_email($to, $subject, $body) { return true; }
|
||||
public static function remove_special_chars($str) { return str_ireplace(['\'', '"', ',', ';', '<', '>'], ' ', $str); }
|
||||
public static function normalize_decimal($val, $precision = 2) { return round((float)$val, $precision); }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user