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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user