refactor layouts module to domain/di and prepare 0.256 release

This commit is contained in:
2026-02-12 22:54:47 +01:00
parent 58a41691e6
commit b0a2f4be0e
24 changed files with 970 additions and 315 deletions

View File

@@ -112,5 +112,37 @@ class LanguagesRepositoryTest extends TestCase
$this->assertCount(1, $result['items']);
$this->assertSame('pl', $result['items'][0]['id']);
}
}
public function testDefaultLanguageIdReturnsLanguageWithStartFlag(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('select')
->with('pp_langs', '*', ['ORDER' => ['o' => 'ASC']])
->willReturn([
['id' => 'en', 'start' => 0],
['id' => 'pl', 'start' => 1],
]);
$repository = new LanguagesRepository($mockDb);
$this->assertSame('pl', $repository->defaultLanguageId());
}
public function testDefaultLanguageIdFallsBackToFirstLanguageOrPl(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->exactly(2))
->method('select')
->with('pp_langs', '*', ['ORDER' => ['o' => 'ASC']])
->willReturnOnConsecutiveCalls(
[
['id' => 'en', 'start' => 0],
],
[]
);
$repository = new LanguagesRepository($mockDb);
$this->assertSame('en', $repository->defaultLanguageId());
$this->assertSame('pl', $repository->defaultLanguageId());
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Tests\Unit\Domain\Layouts;
use PHPUnit\Framework\TestCase;
use Domain\Layouts\LayoutsRepository;
class LayoutsRepositoryTest extends TestCase
{
public function testFindReturnsLayoutWithRelations(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->with('pp_layouts', '*', ['id' => 5])
->willReturn(['id' => 5, 'name' => 'Main']);
$mockDb->expects($this->exactly(2))
->method('select')
->withConsecutive(
['pp_layouts_pages', 'page_id', ['layout_id' => 5]],
['pp_layouts_categories', 'category_id', ['layout_id' => 5]]
)
->willReturnOnConsecutiveCalls([10, 11], [2, 3]);
$repository = new LayoutsRepository($mockDb);
$layout = $repository->find(5);
$this->assertSame(5, $layout['id']);
$this->assertSame([10, 11], $layout['pages']);
$this->assertSame([2, 3], $layout['categories']);
}
public function testDeleteReturnsFalseWhenOnlyOneLayoutExists(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('count')
->with('pp_layouts')
->willReturn(1);
$repository = new LayoutsRepository($mockDb);
$this->assertFalse($repository->delete(1));
}
public function testFindReturnsDefaultLayoutWhenRecordDoesNotExist(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->with('pp_layouts', '*', ['id' => 999])
->willReturn(false);
$repository = new LayoutsRepository($mockDb);
$layout = $repository->find(999);
$this->assertSame(0, $layout['id']);
$this->assertSame([], $layout['pages']);
$this->assertSame([], $layout['categories']);
}
public function testSaveInsertsNewLayoutAndReturnsId(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('insert')
->with('pp_layouts', $this->arrayHasKey('name'));
$mockDb->expects($this->once())
->method('id')
->willReturn(9);
$mockDb->expects($this->exactly(2))
->method('delete')
->withConsecutive(
['pp_layouts_pages', ['layout_id' => 9]],
['pp_layouts_categories', ['layout_id' => 9]]
)
->willReturn(true);
$repository = new LayoutsRepository($mockDb);
$savedId = $repository->save([
'name' => 'Nowy szablon',
'status' => 0,
'categories_default' => 0,
]);
$this->assertSame(9, $savedId);
}
public function testListAllReturnsArray(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('select')
->with('pp_layouts', '*', ['ORDER' => ['name' => 'ASC']])
->willReturn([
['id' => 1, 'name' => 'Default'],
]);
$repository = new LayoutsRepository($mockDb);
$rows = $repository->listAll();
$this->assertCount(1, $rows);
$this->assertSame('Default', $rows[0]['name']);
}
}