232 lines
7.7 KiB
PHP
232 lines
7.7 KiB
PHP
<?php
|
|
namespace Tests\Unit\Domain\Order;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Domain\Order\OrderRepository;
|
|
|
|
class OrderRepositoryTest extends TestCase
|
|
{
|
|
public function testOrderStatusesReturnsMappedArray(): void
|
|
{
|
|
$mockDb = $this->createMock(\medoo::class);
|
|
$mockDb->method('select')
|
|
->willReturnCallback(function ($table, $columns, $where) {
|
|
if ($table === 'pp_shop_statuses') {
|
|
return [
|
|
['id' => 0, 'status' => 'Nowe'],
|
|
['id' => 4, 'status' => 'W realizacji'],
|
|
];
|
|
}
|
|
|
|
return [];
|
|
});
|
|
|
|
$repository = new OrderRepository($mockDb);
|
|
$statuses = $repository->orderStatuses();
|
|
|
|
$this->assertIsArray($statuses);
|
|
$this->assertSame('Nowe', $statuses[0]);
|
|
$this->assertSame('W realizacji', $statuses[4]);
|
|
}
|
|
|
|
public function testNextAndPrevOrderIdReturnNullForInvalidInput(): void
|
|
{
|
|
$mockDb = $this->createMock(\medoo::class);
|
|
$mockDb->expects($this->never())->method('get');
|
|
|
|
$repository = new OrderRepository($mockDb);
|
|
|
|
$this->assertNull($repository->nextOrderId(0));
|
|
$this->assertNull($repository->prevOrderId(0));
|
|
}
|
|
|
|
public function testListForAdminReturnsItemsAndTotal(): void
|
|
{
|
|
$mockDb = $this->createMock(\medoo::class);
|
|
|
|
$resultSetCount = new class {
|
|
public function fetchAll(): array
|
|
{
|
|
return [[3]];
|
|
}
|
|
};
|
|
|
|
$resultSetData = new class {
|
|
public function fetchAll(): array
|
|
{
|
|
return [
|
|
[
|
|
'id' => 11,
|
|
'number' => '2026/02/001',
|
|
'date_order' => '2026-02-15 10:00:00',
|
|
'client' => 'Jan Kowalski',
|
|
'order_email' => 'jan@example.com',
|
|
'address' => 'Testowa 1, 00-000 Warszawa',
|
|
'status' => 0,
|
|
'client_phone' => '111222333',
|
|
'transport' => 'Kurier',
|
|
'payment_method' => 'Przelew',
|
|
'summary' => '123.45',
|
|
'paid' => 1,
|
|
'total_orders' => 2,
|
|
],
|
|
];
|
|
}
|
|
};
|
|
|
|
$callIndex = 0;
|
|
$mockDb->method('query')
|
|
->willReturnCallback(function () use (&$callIndex, $resultSetCount, $resultSetData) {
|
|
$callIndex++;
|
|
return $callIndex === 1 ? $resultSetCount : $resultSetData;
|
|
});
|
|
|
|
$repository = new OrderRepository($mockDb);
|
|
$result = $repository->listForAdmin([], 'date_order', 'DESC', 1, 15);
|
|
|
|
$this->assertSame(3, $result['total']);
|
|
$this->assertCount(1, $result['items']);
|
|
$this->assertSame(11, $result['items'][0]['id']);
|
|
$this->assertSame('2026/02/001', $result['items'][0]['number']);
|
|
$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);
|
|
}
|
|
}
|