ver. 0.290: ShopCoupon + ShopOrder frontend migration to Domain + Controllers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -265,4 +265,153 @@ class CouponRepositoryTest extends TestCase
|
||||
$this->assertCount(1, $tree[0]['subcategories']);
|
||||
$this->assertSame(11, (int)$tree[0]['subcategories'][0]['id']);
|
||||
}
|
||||
|
||||
public function testFindByNameReturnsObjectWhenFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with('pp_shop_coupon', '*', ['name' => 'RABAT10'])
|
||||
->willReturn([
|
||||
'id' => '5',
|
||||
'name' => 'RABAT10',
|
||||
'status' => '1',
|
||||
'used' => '0',
|
||||
'type' => '1',
|
||||
'amount' => '10.00',
|
||||
'one_time' => '1',
|
||||
'include_discounted_product' => '0',
|
||||
'categories' => '[1,2]',
|
||||
'used_count' => '0',
|
||||
]);
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$result = $repository->findByName('RABAT10');
|
||||
|
||||
$this->assertIsObject($result);
|
||||
$this->assertSame(5, $result->id);
|
||||
$this->assertSame('RABAT10', $result->name);
|
||||
$this->assertSame(1, $result->status);
|
||||
$this->assertSame(0, $result->used);
|
||||
$this->assertSame(1, $result->type);
|
||||
$this->assertSame('10.00', $result->amount);
|
||||
$this->assertSame(1, $result->one_time);
|
||||
$this->assertSame(0, $result->include_discounted_product);
|
||||
$this->assertSame('[1,2]', $result->categories);
|
||||
$this->assertSame(0, $result->used_count);
|
||||
}
|
||||
|
||||
public function testFindByNameReturnsNullWhenNotFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->willReturn(null);
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$this->assertNull($repository->findByName('NIEISTNIEJACY'));
|
||||
}
|
||||
|
||||
public function testFindByNameReturnsNullForEmptyName(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->never())->method('get');
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$this->assertNull($repository->findByName(''));
|
||||
$this->assertNull($repository->findByName(' '));
|
||||
}
|
||||
|
||||
public function testIsAvailableReturnsTrueForActiveCoupon(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$repository = new CouponRepository($mockDb);
|
||||
|
||||
$coupon = (object)['id' => 1, 'status' => 1, 'used' => 0];
|
||||
$this->assertTrue($repository->isAvailable($coupon));
|
||||
}
|
||||
|
||||
public function testIsAvailableReturnsFalseForUsedCoupon(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$repository = new CouponRepository($mockDb);
|
||||
|
||||
$coupon = (object)['id' => 1, 'status' => 1, 'used' => 1];
|
||||
$this->assertFalse($repository->isAvailable($coupon));
|
||||
}
|
||||
|
||||
public function testIsAvailableReturnsFalseForInactiveCoupon(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$repository = new CouponRepository($mockDb);
|
||||
|
||||
$coupon = (object)['id' => 1, 'status' => 0, 'used' => 0];
|
||||
$this->assertFalse($repository->isAvailable($coupon));
|
||||
}
|
||||
|
||||
public function testIsAvailableReturnsFalseForNullCoupon(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$repository = new CouponRepository($mockDb);
|
||||
|
||||
$this->assertFalse($repository->isAvailable(null));
|
||||
}
|
||||
|
||||
public function testIsAvailableWorksWithArray(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$repository = new CouponRepository($mockDb);
|
||||
|
||||
$this->assertTrue($repository->isAvailable(['id' => 1, 'status' => 1, 'used' => 0]));
|
||||
$this->assertFalse($repository->isAvailable(['id' => 0, 'status' => 1, 'used' => 0]));
|
||||
}
|
||||
|
||||
public function testMarkAsUsedCallsUpdate(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('update')
|
||||
->willReturnCallback(function ($table, $row, $where) {
|
||||
$this->assertSame('pp_shop_coupon', $table);
|
||||
$this->assertSame(1, $row['used']);
|
||||
$this->assertArrayHasKey('date_used', $row);
|
||||
$this->assertSame(['id' => 10], $where);
|
||||
});
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$repository->markAsUsed(10);
|
||||
}
|
||||
|
||||
public function testMarkAsUsedSkipsInvalidId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->never())->method('update');
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$repository->markAsUsed(0);
|
||||
}
|
||||
|
||||
public function testIncrementUsedCountCallsUpdate(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('update')
|
||||
->willReturnCallback(function ($table, $row, $where) {
|
||||
$this->assertSame('pp_shop_coupon', $table);
|
||||
$this->assertSame(1, $row['used_count[+]']);
|
||||
$this->assertSame(['id' => 7], $where);
|
||||
});
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$repository->incrementUsedCount(7);
|
||||
}
|
||||
|
||||
public function testIncrementUsedCountSkipsInvalidId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->never())->method('update');
|
||||
|
||||
$repository = new CouponRepository($mockDb);
|
||||
$repository->incrementUsedCount(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,4 +91,141 @@ class OrderRepositoryTest extends TestCase
|
||||
$this->assertSame(2, $result['items'][0]['total_orders']);
|
||||
$this->assertSame(1, $result['items'][0]['paid']);
|
||||
}
|
||||
|
||||
// --- Frontend method tests ---
|
||||
|
||||
public function testFindIdByHashReturnsIdWhenFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')
|
||||
->with('pp_shop_orders', 'id', ['hash' => 'abc123'])
|
||||
->willReturn('42');
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$this->assertSame(42, $repository->findIdByHash('abc123'));
|
||||
}
|
||||
|
||||
public function testFindIdByHashReturnsNullWhenNotFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')->willReturn(null);
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$this->assertNull($repository->findIdByHash('nonexistent'));
|
||||
}
|
||||
|
||||
public function testFindIdByHashReturnsNullForEmptyHash(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->never())->method('get');
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$this->assertNull($repository->findIdByHash(''));
|
||||
$this->assertNull($repository->findIdByHash(' '));
|
||||
}
|
||||
|
||||
public function testFindHashByIdReturnsHashWhenFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')
|
||||
->with('pp_shop_orders', 'hash', ['id' => 42])
|
||||
->willReturn('abc123hash');
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$this->assertSame('abc123hash', $repository->findHashById(42));
|
||||
}
|
||||
|
||||
public function testFindHashByIdReturnsNullForInvalidId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->never())->method('get');
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$this->assertNull($repository->findHashById(0));
|
||||
$this->assertNull($repository->findHashById(-1));
|
||||
}
|
||||
|
||||
public function testOrderDetailsFrontendByIdReturnsArrayWithProducts(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')
|
||||
->willReturn(['id' => 1, 'number' => '2026/02/001', 'coupon_id' => null]);
|
||||
$mockDb->method('select')
|
||||
->willReturn([['product_id' => 10, 'name' => 'Test Product']]);
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$result = $repository->orderDetailsFrontend(1);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertSame(1, $result['id']);
|
||||
$this->assertIsArray($result['products']);
|
||||
$this->assertCount(1, $result['products']);
|
||||
}
|
||||
|
||||
public function testOrderDetailsFrontendByHashReturnsArrayWithProducts(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')
|
||||
->willReturn(['id' => 5, 'number' => '2026/02/005', 'hash' => 'testhash']);
|
||||
$mockDb->method('select')
|
||||
->willReturn([]);
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$result = $repository->orderDetailsFrontend(null, 'testhash');
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertSame(5, $result['id']);
|
||||
$this->assertIsArray($result['products']);
|
||||
}
|
||||
|
||||
public function testOrderDetailsFrontendReturnsNullWhenNotFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')->willReturn(null);
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$this->assertNull($repository->orderDetailsFrontend(999));
|
||||
$this->assertNull($repository->orderDetailsFrontend(null, 'nonexistent'));
|
||||
}
|
||||
|
||||
public function testGenerateOrderNumberFormatsCorrectly(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$resultSet = new class {
|
||||
public function fetchAll(): array
|
||||
{
|
||||
return [[5]];
|
||||
}
|
||||
};
|
||||
|
||||
$mockDb->method('query')->willReturn($resultSet);
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$number = $repository->generateOrderNumber();
|
||||
|
||||
$expectedPrefix = date('Y/m') . '/';
|
||||
$this->assertStringStartsWith($expectedPrefix, $number);
|
||||
$this->assertSame($expectedPrefix . '006', $number);
|
||||
}
|
||||
|
||||
public function testGenerateOrderNumberStartsAt001(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$resultSet = new class {
|
||||
public function fetchAll(): array
|
||||
{
|
||||
return [[null]];
|
||||
}
|
||||
};
|
||||
|
||||
$mockDb->method('query')->willReturn($resultSet);
|
||||
|
||||
$repository = new OrderRepository($mockDb);
|
||||
$number = $repository->generateOrderNumber();
|
||||
|
||||
$expectedPrefix = date('Y/m') . '/';
|
||||
$this->assertSame($expectedPrefix . '001', $number);
|
||||
}
|
||||
}
|
||||
|
||||
40
tests/Unit/front/Controllers/ShopCouponControllerTest.php
Normal file
40
tests/Unit/front/Controllers/ShopCouponControllerTest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace Tests\Unit\front\Controllers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use front\Controllers\ShopCouponController;
|
||||
use Domain\Coupon\CouponRepository;
|
||||
|
||||
class ShopCouponControllerTest extends TestCase
|
||||
{
|
||||
private $repository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(CouponRepository::class);
|
||||
$this->controller = new ShopCouponController($this->repository);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new ShopCouponController($this->repository);
|
||||
$this->assertInstanceOf(ShopCouponController::class, $controller);
|
||||
}
|
||||
|
||||
public function testHasMainActionMethods(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'useCoupon'));
|
||||
$this->assertTrue(method_exists($this->controller, 'deleteCoupon'));
|
||||
}
|
||||
|
||||
public function testConstructorRequiresCouponRepository(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(ShopCouponController::class);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(1, $params);
|
||||
$this->assertEquals('Domain\Coupon\CouponRepository', $params[0]->getType()->getName());
|
||||
}
|
||||
}
|
||||
43
tests/Unit/front/Controllers/ShopOrderControllerTest.php
Normal file
43
tests/Unit/front/Controllers/ShopOrderControllerTest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace Tests\Unit\front\Controllers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use front\Controllers\ShopOrderController;
|
||||
use Domain\Order\OrderRepository;
|
||||
|
||||
class ShopOrderControllerTest extends TestCase
|
||||
{
|
||||
private $repository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(OrderRepository::class);
|
||||
$this->controller = new ShopOrderController($this->repository);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new ShopOrderController($this->repository);
|
||||
$this->assertInstanceOf(ShopOrderController::class, $controller);
|
||||
}
|
||||
|
||||
public function testHasMainActionMethods(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'paymentConfirmation'));
|
||||
$this->assertTrue(method_exists($this->controller, 'paymentStatusTpay'));
|
||||
$this->assertTrue(method_exists($this->controller, 'paymentStatusPrzelewy24pl'));
|
||||
$this->assertTrue(method_exists($this->controller, 'paymentStatusHotpay'));
|
||||
$this->assertTrue(method_exists($this->controller, 'orderDetails'));
|
||||
}
|
||||
|
||||
public function testConstructorRequiresOrderRepository(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(ShopOrderController::class);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(1, $params);
|
||||
$this->assertEquals('Domain\Order\OrderRepository', $params[0]->getType()->getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user