ver. 0.292: ShopProduct + ShopPaymentMethod + ShopPromotion + ShopStatuses + ShopTransport frontend migration to Domain
Full migration of front\factory\ — entire directory removed (all 20 classes migrated). ProductRepository +20 frontend methods, PromotionRepository +5 applyType methods, TransportRepository +4 cached methods, PaymentMethodRepository +cached frontend methods. Fix: broken transports_list() in ajax.php replaced with forPaymentMethod(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -176,4 +176,202 @@ class PromotionRepositoryTest extends TestCase
|
||||
$this->assertCount(1, $tree[0]['subcategories']);
|
||||
$this->assertSame(11, (int)$tree[0]['subcategories'][0]['id']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Frontend: basket promotion logic (migrated from front\factory\ShopPromotion)
|
||||
// =========================================================================
|
||||
|
||||
private function mockPromotion(array $data): object
|
||||
{
|
||||
return new class($data) {
|
||||
private $data;
|
||||
public function __construct($data) { $this->data = $data; }
|
||||
public function __get($key) { return isset($this->data[$key]) ? $this->data[$key] : null; }
|
||||
};
|
||||
}
|
||||
|
||||
private function makeBasket(array $items): array
|
||||
{
|
||||
$basket = [];
|
||||
foreach ($items as $i => $item) {
|
||||
$basket[$i] = array_merge(['product-id' => $item['id']], $item);
|
||||
}
|
||||
return $basket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test applyTypeWholeBasket — rabat na cały koszyk
|
||||
*/
|
||||
public function testApplyTypeWholeBasketAppliesDiscountToAll(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
// productCategoriesFront zwraca kategorie
|
||||
$mockDb->method('get')->willReturn(null); // parent_id = null
|
||||
$mockStmt = $this->createMock(\PDOStatement::class);
|
||||
$mockStmt->method('fetchAll')->willReturn([['category_id' => 1]]);
|
||||
$mockDb->method('query')->willReturn($mockStmt);
|
||||
|
||||
$promotion = $this->mockPromotion([
|
||||
'discount_type' => 1,
|
||||
'amount' => 10,
|
||||
'include_coupon' => 0,
|
||||
'include_product_promo' => 0,
|
||||
]);
|
||||
|
||||
$basket = $this->makeBasket([
|
||||
['id' => 1],
|
||||
['id' => 2],
|
||||
]);
|
||||
|
||||
$repository = new PromotionRepository($mockDb);
|
||||
$result = $repository->applyTypeWholeBasket($basket, $promotion);
|
||||
|
||||
$this->assertSame(1, $result[0]['discount_type']);
|
||||
$this->assertSame(10, $result[0]['discount_amount']);
|
||||
$this->assertSame(1, $result[1]['discount_type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test applyTypeCategoriesOr — rabat na produkty z kat. 1 lub 2
|
||||
*/
|
||||
public function testApplyTypeCategoriesOrAppliesDiscountToMatchingCategories(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->method('get')->willReturn(null);
|
||||
$mockStmt = $this->createMock(\PDOStatement::class);
|
||||
$mockStmt->method('fetchAll')->willReturn([['category_id' => 5]]);
|
||||
$mockDb->method('query')->willReturn($mockStmt);
|
||||
|
||||
$promotion = $this->mockPromotion([
|
||||
'categories' => json_encode([5]),
|
||||
'condition_categories' => json_encode([10]),
|
||||
'discount_type' => 1,
|
||||
'amount' => 15,
|
||||
'include_coupon' => 1,
|
||||
'include_product_promo' => 0,
|
||||
]);
|
||||
|
||||
$basket = $this->makeBasket([['id' => 1]]);
|
||||
|
||||
$repository = new PromotionRepository($mockDb);
|
||||
$result = $repository->applyTypeCategoriesOr($basket, $promotion);
|
||||
|
||||
$this->assertSame(1, $result[0]['discount_type']);
|
||||
$this->assertSame(15, $result[0]['discount_amount']);
|
||||
$this->assertSame(1, $result[0]['discount_include_coupon']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test applyTypeCategoryCondition — rabat na kat. I jeśli kat. II w koszyku
|
||||
*/
|
||||
public function testApplyTypeCategoryConditionAppliesWhenConditionMet(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$callCount = 0;
|
||||
$mockDb->method('get')->willReturn(null);
|
||||
|
||||
$mockStmt1 = $this->createMock(\PDOStatement::class);
|
||||
$mockStmt1->method('fetchAll')->willReturnOnConsecutiveCalls(
|
||||
[['category_id' => 10]], // product 1 — condition category
|
||||
[['category_id' => 5]], // product 2 — target category
|
||||
[['category_id' => 10]], // product 1 — check for discount (not matching target)
|
||||
[['category_id' => 5]] // product 2 — check for discount (matching target)
|
||||
);
|
||||
$mockDb->method('query')->willReturn($mockStmt1);
|
||||
|
||||
$promotion = $this->mockPromotion([
|
||||
'categories' => json_encode([5]),
|
||||
'condition_categories' => json_encode([10]),
|
||||
'discount_type' => 1,
|
||||
'amount' => 20,
|
||||
'include_coupon' => 0,
|
||||
'include_product_promo' => 0,
|
||||
]);
|
||||
|
||||
$basket = $this->makeBasket([
|
||||
['id' => 1],
|
||||
['id' => 2],
|
||||
]);
|
||||
|
||||
$repository = new PromotionRepository($mockDb);
|
||||
$result = $repository->applyTypeCategoryCondition($basket, $promotion);
|
||||
|
||||
// Produkt 2 (kat. 5) powinien mieć rabat
|
||||
$this->assertSame(1, $result[1]['discount_type']);
|
||||
$this->assertSame(20, $result[1]['discount_amount']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test applyTypeCategoryCondition — brak rabatu gdy warunek niespełniony
|
||||
*/
|
||||
public function testApplyTypeCategoryConditionNoDiscountWhenConditionNotMet(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->method('get')->willReturn(null);
|
||||
$mockStmt = $this->createMock(\PDOStatement::class);
|
||||
$mockStmt->method('fetchAll')->willReturn([['category_id' => 99]]); // nie pasuje do condition_categories
|
||||
$mockDb->method('query')->willReturn($mockStmt);
|
||||
|
||||
$promotion = $this->mockPromotion([
|
||||
'categories' => json_encode([5]),
|
||||
'condition_categories' => json_encode([10]),
|
||||
'discount_type' => 1,
|
||||
'amount' => 20,
|
||||
'include_coupon' => 0,
|
||||
'include_product_promo' => 0,
|
||||
]);
|
||||
|
||||
$basket = $this->makeBasket([['id' => 1]]);
|
||||
|
||||
$repository = new PromotionRepository($mockDb);
|
||||
$result = $repository->applyTypeCategoryCondition($basket, $promotion);
|
||||
|
||||
$this->assertArrayNotHasKey('discount_type', $result[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test applyTypeCategoriesAnd — rabat gdy oba warunki spełnione
|
||||
*/
|
||||
public function testApplyTypeCategoriesAndAppliesWhenBothConditionsMet(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->method('get')->willReturn(null);
|
||||
|
||||
$mockStmt = $this->createMock(\PDOStatement::class);
|
||||
$mockStmt->method('fetchAll')->willReturnOnConsecutiveCalls(
|
||||
[['category_id' => 10]], // product 1 — condition_categories ✓
|
||||
[['category_id' => 5]], // product 2 — categories ✓ (condition_2)
|
||||
[['category_id' => 10]], // product 1 — check categories ✓ (condition check)
|
||||
[['category_id' => 5]], // product 2 — check categories ✓ (condition check)
|
||||
[['category_id' => 10]], // product 1 — discount assignment
|
||||
[['category_id' => 5]] // product 2 — discount assignment
|
||||
);
|
||||
$mockDb->method('query')->willReturn($mockStmt);
|
||||
|
||||
$promotion = $this->mockPromotion([
|
||||
'categories' => json_encode([5]),
|
||||
'condition_categories' => json_encode([10]),
|
||||
'discount_type' => 1,
|
||||
'amount' => 25,
|
||||
'include_coupon' => 1,
|
||||
'include_product_promo' => 0,
|
||||
]);
|
||||
|
||||
$basket = $this->makeBasket([
|
||||
['id' => 1],
|
||||
['id' => 2],
|
||||
]);
|
||||
|
||||
$repository = new PromotionRepository($mockDb);
|
||||
$result = $repository->applyTypeCategoriesAnd($basket, $promotion);
|
||||
|
||||
$this->assertSame(1, $result[0]['discount_type']);
|
||||
$this->assertSame(25, $result[0]['discount_amount']);
|
||||
$this->assertSame(1, $result[1]['discount_type']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user