Files
shopPRO/tests/Unit/Domain/Layouts/LayoutsRepositoryTest.php
Jacek Pyziak 8162df7356 ver. 0.286: Layouts, Menu, Pages frontend migration to Domain
- Add 6 frontend methods to LayoutsRepository (Redis cache, 3-level fallback)
- Add 6 frontend methods to PagesRepository (Redis cache, recursive pages)
- Create front\Views\Menu (clean VIEW replacing front\view\Menu)
- Delete front\factory\Layouts, Menu, Pages + front\view\Menu + dead submenu.php
- Fix null $lang_id TypeError in check_url_params() (remove string type hint + ?? '')
- Optimize Helpers::htacces() from 3 layout calls to 1
- Tests: 470 OK, 1484 assertions (+16 new)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 08:21:53 +01:00

218 lines
6.8 KiB
PHP

<?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']);
}
// ── Frontend methods tests ──────────────────────────────────
public function testCategoryDefaultLayoutIdReturnsId(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->with('pp_layouts', 'id', ['categories_default' => 1])
->willReturn(7);
$repo = new LayoutsRepository($mockDb);
$this->assertSame(7, $repo->categoryDefaultLayoutId());
}
public function testCategoryDefaultLayoutIdReturnsNullWhenNone(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->willReturn(null);
$repo = new LayoutsRepository($mockDb);
$this->assertNull($repo->categoryDefaultLayoutId());
}
public function testGetDefaultLayoutReturnsLayoutFromDb(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->with('pp_layouts', '*', ['status' => 1])
->willReturn(['id' => 3, 'name' => 'Main', 'html' => '<h1>Test</h1>']);
$repo = new LayoutsRepository($mockDb);
$layout = $repo->getDefaultLayout();
$this->assertSame(3, $layout['id']);
$this->assertSame('Main', $layout['name']);
}
public function testGetDefaultLayoutReturnsNullWhenNoLayout(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->willReturn(null);
$repo = new LayoutsRepository($mockDb);
$this->assertNull($repo->getDefaultLayout());
}
public function testGetArticleLayoutReturnsLayoutFromDb(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->with(
'pp_layouts',
['[><]pp_articles' => ['id' => 'layout_id']],
'*',
['pp_articles.id' => 10]
)
->willReturn(['id' => 2, 'name' => 'Article Layout', 'html' => '<div>art</div>']);
$repo = new LayoutsRepository($mockDb);
$layout = $repo->getArticleLayout(10);
$this->assertSame(2, $layout['id']);
}
public function testGetArticleLayoutReturnsNullWhenNoLayout(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->once())
->method('get')
->willReturn(null);
$repo = new LayoutsRepository($mockDb);
$this->assertNull($repo->getArticleLayout(999));
}
public function testGetActiveLayoutFallsBackToDefault(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->expects($this->exactly(2))
->method('get')
->willReturnOnConsecutiveCalls(
null,
['id' => 1, 'name' => 'Default', 'html' => '<body>']
);
$repo = new LayoutsRepository($mockDb);
$layout = $repo->getActiveLayout(99);
$this->assertSame(1, $layout['id']);
$this->assertSame('Default', $layout['name']);
}
public function testGetActiveLayoutReturnsNullWhenNothingFound(): void
{
$mockDb = $this->createMock(\medoo::class);
$mockDb->method('get')->willReturn(null);
$repo = new LayoutsRepository($mockDb);
$this->assertNull($repo->getActiveLayout(99));
}
}