refactor layouts module to domain/di and prepare 0.256 release
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
110
tests/Unit/Domain/Layouts/LayoutsRepositoryTest.php
Normal file
110
tests/Unit/Domain/Layouts/LayoutsRepositoryTest.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,25 @@ use PHPUnit\Framework\TestCase;
|
||||
use admin\Controllers\ArticlesController;
|
||||
use Domain\Article\ArticleRepository;
|
||||
use Domain\Languages\LanguagesRepository;
|
||||
use Domain\Layouts\LayoutsRepository;
|
||||
|
||||
class ArticlesControllerTest extends TestCase
|
||||
{
|
||||
private $mockRepository;
|
||||
private $mockLanguagesRepository;
|
||||
private $mockLayoutsRepository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mockRepository = $this->createMock(ArticleRepository::class);
|
||||
$this->mockLanguagesRepository = $this->createMock(LanguagesRepository::class);
|
||||
$this->controller = new ArticlesController($this->mockRepository, $this->mockLanguagesRepository);
|
||||
$this->mockLayoutsRepository = $this->createMock(LayoutsRepository::class);
|
||||
$this->controller = new ArticlesController(
|
||||
$this->mockRepository,
|
||||
$this->mockLanguagesRepository,
|
||||
$this->mockLayoutsRepository
|
||||
);
|
||||
}
|
||||
|
||||
public function testCanCreateController(): void
|
||||
@@ -26,7 +33,11 @@ class ArticlesControllerTest extends TestCase
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new ArticlesController($this->mockRepository, $this->mockLanguagesRepository);
|
||||
$controller = new ArticlesController(
|
||||
$this->mockRepository,
|
||||
$this->mockLanguagesRepository,
|
||||
$this->mockLayoutsRepository
|
||||
);
|
||||
$this->assertInstanceOf(ArticlesController::class, $controller);
|
||||
}
|
||||
|
||||
@@ -69,8 +80,9 @@ class ArticlesControllerTest extends TestCase
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(2, $params);
|
||||
$this->assertCount(3, $params);
|
||||
$this->assertEquals('Domain\Article\ArticleRepository', $params[0]->getType()->getName());
|
||||
$this->assertEquals('Domain\Languages\LanguagesRepository', $params[1]->getType()->getName());
|
||||
$this->assertEquals('Domain\Layouts\LayoutsRepository', $params[2]->getType()->getName());
|
||||
}
|
||||
}
|
||||
|
||||
56
tests/Unit/admin/Controllers/LayoutsControllerTest.php
Normal file
56
tests/Unit/admin/Controllers/LayoutsControllerTest.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace Tests\Unit\admin\Controllers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use admin\Controllers\LayoutsController;
|
||||
use Domain\Layouts\LayoutsRepository;
|
||||
use Domain\Languages\LanguagesRepository;
|
||||
|
||||
class LayoutsControllerTest extends TestCase
|
||||
{
|
||||
private $mockRepository;
|
||||
private $mockLanguagesRepository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mockRepository = $this->createMock(LayoutsRepository::class);
|
||||
$this->mockLanguagesRepository = $this->createMock(LanguagesRepository::class);
|
||||
$this->controller = new LayoutsController($this->mockRepository, $this->mockLanguagesRepository);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new LayoutsController($this->mockRepository, $this->mockLanguagesRepository);
|
||||
$this->assertInstanceOf(LayoutsController::class, $controller);
|
||||
}
|
||||
|
||||
public function testHasMainActionMethods(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'list'));
|
||||
$this->assertTrue(method_exists($this->controller, 'edit'));
|
||||
$this->assertTrue(method_exists($this->controller, 'save'));
|
||||
$this->assertTrue(method_exists($this->controller, 'delete'));
|
||||
}
|
||||
|
||||
public function testActionMethodReturnTypes(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->controller);
|
||||
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('list')->getReturnType());
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('edit')->getReturnType());
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('save')->getReturnType());
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('delete')->getReturnType());
|
||||
}
|
||||
|
||||
public function testConstructorRequiresLayoutsRepository(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(LayoutsController::class);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(2, $params);
|
||||
$this->assertEquals('Domain\Layouts\LayoutsRepository', $params[0]->getType()->getName());
|
||||
$this->assertEquals('Domain\Languages\LanguagesRepository', $params[1]->getType()->getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user