refactor languages module to domain/controller and release 0.254 update package
This commit is contained in:
116
tests/Unit/Domain/Languages/LanguagesRepositoryTest.php
Normal file
116
tests/Unit/Domain/Languages/LanguagesRepositoryTest.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Domain\Languages;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Domain\Languages\LanguagesRepository;
|
||||
|
||||
class LanguagesRepositoryTest extends TestCase
|
||||
{
|
||||
public function testLanguageDetailsReturnsArrayOrNull(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with('pp_langs', '*', ['id' => 'pl'])
|
||||
->willReturn(['id' => 'pl', 'name' => 'Polski']);
|
||||
|
||||
$repository = new LanguagesRepository($mockDb);
|
||||
$language = $repository->languageDetails('pl');
|
||||
|
||||
$this->assertIsArray($language);
|
||||
$this->assertSame('pl', $language['id']);
|
||||
}
|
||||
|
||||
public function testLanguagesListReturnsArray(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('select')
|
||||
->with('pp_langs', '*', ['ORDER' => ['o' => 'ASC']])
|
||||
->willReturn([
|
||||
['id' => 'pl', 'name' => 'Polski', 'status' => 1],
|
||||
]);
|
||||
|
||||
$repository = new LanguagesRepository($mockDb);
|
||||
$list = $repository->languagesList(false);
|
||||
|
||||
$this->assertCount(1, $list);
|
||||
$this->assertSame('pl', $list[0]['id']);
|
||||
}
|
||||
|
||||
public function testSaveLanguageRejectsInvalidLanguageId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$repository = new LanguagesRepository($mockDb);
|
||||
|
||||
$this->assertNull($repository->saveLanguage('pol', 'Polski', 1, 1, 1));
|
||||
$this->assertNull($repository->saveLanguage('p1', 'Polski', 1, 1, 1));
|
||||
}
|
||||
|
||||
public function testSaveTranslationInsertsNewTranslationAndReturnsId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('insert')
|
||||
->with('pp_langs_translations', ['text' => 'Hello'])
|
||||
->willReturn($this->createMock(\PDOStatement::class));
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('id')
|
||||
->willReturn(15);
|
||||
|
||||
$mockDb->expects($this->exactly(2))
|
||||
->method('update')
|
||||
->withConsecutive(
|
||||
['pp_langs_translations', ['pl' => 'Czesc'], ['id' => 15]],
|
||||
['pp_langs_translations', ['en' => 'Hello'], ['id' => 15]]
|
||||
);
|
||||
|
||||
$repository = new LanguagesRepository($mockDb);
|
||||
$savedId = $repository->saveTranslation(0, 'Hello', ['pl' => 'Czesc', 'en' => 'Hello']);
|
||||
|
||||
$this->assertSame(15, $savedId);
|
||||
}
|
||||
|
||||
public function testDeleteTranslationReturnsBoolean(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->expects($this->once())
|
||||
->method('delete')
|
||||
->with('pp_langs_translations', ['id' => 5])
|
||||
->willReturn($this->createMock(\PDOStatement::class));
|
||||
|
||||
$repository = new LanguagesRepository($mockDb);
|
||||
$this->assertTrue($repository->deleteTranslation(5));
|
||||
}
|
||||
|
||||
public function testListForAdminReturnsItemsAndTotal(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$countStmt = $this->createMock(\PDOStatement::class);
|
||||
$countStmt->expects($this->once())
|
||||
->method('fetchAll')
|
||||
->willReturn([[1]]);
|
||||
|
||||
$dataStmt = $this->createMock(\PDOStatement::class);
|
||||
$dataStmt->expects($this->once())
|
||||
->method('fetchAll')
|
||||
->willReturn([
|
||||
['id' => 'pl', 'name' => 'Polski', 'status' => 1, 'start' => 1, 'o' => 1],
|
||||
]);
|
||||
|
||||
$mockDb->expects($this->exactly(2))
|
||||
->method('query')
|
||||
->willReturnOnConsecutiveCalls($countStmt, $dataStmt);
|
||||
|
||||
$repository = new LanguagesRepository($mockDb);
|
||||
$result = $repository->listForAdmin(['name' => '', 'status' => '', 'start' => ''], 'o', 'ASC', 1, 15);
|
||||
|
||||
$this->assertSame(1, $result['total']);
|
||||
$this->assertCount(1, $result['items']);
|
||||
$this->assertSame('pl', $result['items'][0]['id']);
|
||||
}
|
||||
}
|
||||
|
||||
63
tests/Unit/admin/Controllers/LanguagesControllerTest.php
Normal file
63
tests/Unit/admin/Controllers/LanguagesControllerTest.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace Tests\Unit\admin\Controllers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use admin\Controllers\LanguagesController;
|
||||
use Domain\Languages\LanguagesRepository;
|
||||
|
||||
class LanguagesControllerTest extends TestCase
|
||||
{
|
||||
private $mockRepository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mockRepository = $this->createMock(LanguagesRepository::class);
|
||||
$this->controller = new LanguagesController($this->mockRepository);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new LanguagesController($this->mockRepository);
|
||||
$this->assertInstanceOf(LanguagesController::class, $controller);
|
||||
}
|
||||
|
||||
public function testHasMainActionMethods(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'list'));
|
||||
$this->assertTrue(method_exists($this->controller, 'view_list'));
|
||||
$this->assertTrue(method_exists($this->controller, 'language_edit'));
|
||||
$this->assertTrue(method_exists($this->controller, 'language_save'));
|
||||
$this->assertTrue(method_exists($this->controller, 'language_delete'));
|
||||
$this->assertTrue(method_exists($this->controller, 'translation_list'));
|
||||
$this->assertTrue(method_exists($this->controller, 'translation_edit'));
|
||||
$this->assertTrue(method_exists($this->controller, 'translation_save'));
|
||||
$this->assertTrue(method_exists($this->controller, 'translation_delete'));
|
||||
}
|
||||
|
||||
public function testActionMethodReturnTypes(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->controller);
|
||||
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('list')->getReturnType());
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('view_list')->getReturnType());
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('language_edit')->getReturnType());
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('language_save')->getReturnType());
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('language_delete')->getReturnType());
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('translation_list')->getReturnType());
|
||||
$this->assertEquals('string', (string)$reflection->getMethod('translation_edit')->getReturnType());
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('translation_save')->getReturnType());
|
||||
$this->assertEquals('void', (string)$reflection->getMethod('translation_delete')->getReturnType());
|
||||
}
|
||||
|
||||
public function testConstructorRequiresLanguagesRepository(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(LanguagesController::class);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(1, $params);
|
||||
$this->assertEquals('Domain\Languages\LanguagesRepository', $params[0]->getType()->getName());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user