Multiple implementations of the same back-end application. The aim is to provide quick, side-by-side comparisons of different technologies (languages, frameworks, libraries) while preserving consistent business logic across all implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

207 lines
6.1 KiB

<?php
declare(strict_types=1);
namespace AutoStore\Tests\Infrastructure\Repositories;
use AutoStore\Application\Exceptions\ApplicationException;
use AutoStore\Domain\Entities\Item;
use AutoStore\Infrastructure\Repositories\FileItemRepository;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class FileItemRepositoryTest extends TestCase
{
private FileItemRepository $repository;
private string $testStoragePath;
private LoggerInterface&\PHPUnit\Framework\MockObject\MockObject $logger;
protected function setUp(): void
{
$this->testStoragePath = $GLOBALS['test_storage_path'];
// Clean up any existing test files
array_map('unlink', glob("$this->testStoragePath/*.json"));
$this->logger = $this->createMock(LoggerInterface::class);
$this->repository = new FileItemRepository($this->testStoragePath, $this->logger);
}
protected function tearDown(): void
{
// Clean up test files
array_map('unlink', glob("$this->testStoragePath/*.json"));
}
public function testSaveShouldCreateFileForNewItem(): void
{
$item = new Item(
'test-id',
'Test Item',
new DateTimeImmutable('+1 day'),
'http://example.com/order',
'user-id'
);
$this->repository->save($item);
$filePath = $this->testStoragePath . '/items.json';
$this->assertFileExists($filePath);
$fileContent = file_get_contents($filePath);
$data = json_decode($fileContent, true);
$this->assertIsArray($data);
$this->assertCount(1, $data);
// Compare the essential fields
$this->assertEquals($item->getId(), $data[$item->getId()]['id']);
$this->assertEquals($item->getName(), $data[$item->getId()]['name']);
$this->assertEquals($item->getOrderUrl(), $data[$item->getId()]['orderUrl']);
$this->assertEquals($item->getUserId(), $data[$item->getId()]['userId']);
}
public function testFindByIdShouldReturnItemIfExists(): void
{
$item = new Item(
'test-id',
'Test Item',
new DateTimeImmutable('+1 day'),
'http://example.com/order',
'user-id'
);
$this->repository->save($item);
$foundItem = $this->repository->findById('test-id');
$this->assertNotNull($foundItem);
$this->assertSame($item->getId(), $foundItem->getId());
$this->assertSame($item->getName(), $foundItem->getName());
}
public function testFindByIdShouldReturnNullIfNotExists(): void
{
$foundItem = $this->repository->findById('non-existent-id');
$this->assertNull($foundItem);
}
public function testFindByUserIdShouldReturnItemsForUser(): void
{
$item1 = new Item(
'test-id-1',
'Test Item 1',
new DateTimeImmutable('+1 day'),
'http://example.com/order1',
'user-id-1'
);
$item2 = new Item(
'test-id-2',
'Test Item 2',
new DateTimeImmutable('+2 days'),
'http://example.com/order2',
'user-id-2'
);
$item3 = new Item(
'test-id-3',
'Test Item 3',
new DateTimeImmutable('+3 days'),
'http://example.com/order3',
'user-id-1'
);
$this->repository->save($item1);
$this->repository->save($item2);
$this->repository->save($item3);
$userItems = $this->repository->findByUserId('user-id-1');
$this->assertCount(2, $userItems);
$this->assertContainsEquals($item1->getId(), array_map(fn($i) => $i->getId(), $userItems));
$this->assertContainsEquals($item3->getId(), array_map(fn($i) => $i->getId(), $userItems));
}
public function testFindAllShouldReturnAllItems(): void
{
$item1 = new Item(
'test-id-1',
'Test Item 1',
new DateTimeImmutable('+1 day'),
'http://example.com/order1',
'user-id-1'
);
$item2 = new Item(
'test-id-2',
'Test Item 2',
new DateTimeImmutable('+2 days'),
'http://example.com/order2',
'user-id-2'
);
$this->repository->save($item1);
$this->repository->save($item2);
$allItems = $this->repository->findAll();
$this->assertCount(2, $allItems);
$this->assertContainsEquals($item1->getId(), array_map(fn($i) => $i->getId(), $allItems));
$this->assertContainsEquals($item2->getId(), array_map(fn($i) => $i->getId(), $allItems));
}
public function testDeleteShouldRemoveItem(): void
{
$item = new Item(
'test-id',
'Test Item',
new DateTimeImmutable('+1 day'),
'http://example.com/order',
'user-id'
);
$this->repository->save($item);
$this->repository->delete('test-id');
$foundItem = $this->repository->findById('test-id');
$this->assertNull($foundItem);
}
public function testDeleteShouldThrowExceptionForNonExistentItem(): void
{
$this->expectException(ApplicationException::class);
$this->expectExceptionMessage("Item 'non-existent-id' not found");
$this->repository->delete('non-existent-id');
}
public function testFindExpiredItemsShouldReturnOnlyExpiredItems(): void
{
$expiredItem = new Item(
'expired-id',
'Expired Item',
new DateTimeImmutable('-1 day'),
'http://example.com/expired-order',
'user-id'
);
$validItem = new Item(
'valid-id',
'Valid Item',
new DateTimeImmutable('+1 day'),
'http://example.com/valid-order',
'user-id'
);
$this->repository->save($expiredItem);
$this->repository->save($validItem);
$expiredItems = $this->repository->findExpiredItems();
$this->assertCount(1, $expiredItems);
$this->assertSame($expiredItem->getId(), $expiredItems[0]->getId());
}
}