149 lines
5.1 KiB
PHP
149 lines
5.1 KiB
PHP
<?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']);
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|