ver. 0.251 - migrate Dictionaries to Domain/Controller and remove legacy classes
This commit is contained in:
159
tests/Unit/Domain/Dictionaries/DictionariesRepositoryTest.php
Normal file
159
tests/Unit/Domain/Dictionaries/DictionariesRepositoryTest.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Domain\Dictionaries;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Domain\Dictionaries\DictionariesRepository;
|
||||
|
||||
class DictionariesRepositoryTest extends TestCase
|
||||
{
|
||||
public function testFindReturnsUnitWithTranslations(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with('pp_units', '*', ['id' => 5])
|
||||
->willReturn(['id' => 5]);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('select')
|
||||
->with('pp_units_langs', '*', [
|
||||
'unit_id' => 5,
|
||||
'ORDER' => ['lang_id' => 'ASC', 'id' => 'ASC'],
|
||||
])
|
||||
->willReturn([
|
||||
['lang_id' => 1, 'text' => 'kg'],
|
||||
['lang_id' => 2, 'text' => 'kg'],
|
||||
]);
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
$unit = $repository->find(5);
|
||||
|
||||
$this->assertIsArray($unit);
|
||||
$this->assertEquals(5, $unit['id']);
|
||||
$this->assertArrayHasKey('languages', $unit);
|
||||
$this->assertEquals('kg', $unit['languages'][1]['text']);
|
||||
}
|
||||
|
||||
public function testFindReturnsNullWhenUnitNotFound(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
$mockDb->method('get')->willReturn(false);
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
$this->assertNull($repository->find(999));
|
||||
}
|
||||
|
||||
public function testSaveInsertsNewUnitAndTranslationsForStringLanguageId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
// insert: 1x pp_units + 1x pp_units_langs
|
||||
$mockDb->expects($this->exactly(2))
|
||||
->method('insert');
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('id')
|
||||
->willReturn(12);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with('pp_units_langs', 'id', [
|
||||
'AND' => [
|
||||
'unit_id' => 12,
|
||||
'lang_id' => 'pl',
|
||||
],
|
||||
])
|
||||
->willReturn(false);
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
|
||||
$savedId = $repository->save([
|
||||
'translations' => [
|
||||
'pl' => ['text' => 'szt.'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(12, $savedId);
|
||||
}
|
||||
|
||||
public function testDeleteRemovesUnitAndTranslations(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->expects($this->exactly(2))
|
||||
->method('delete')
|
||||
->withConsecutive(
|
||||
['pp_units_langs', ['unit_id' => 7]],
|
||||
['pp_units', ['id' => 7]]
|
||||
)
|
||||
->willReturnOnConsecutiveCalls(
|
||||
$this->createMock(\PDOStatement::class),
|
||||
$this->createMock(\PDOStatement::class)
|
||||
);
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
$this->assertTrue($repository->delete(7));
|
||||
}
|
||||
|
||||
public function testGetUnitNameByIdReturnsTextFromDatabase(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with('pp_units_langs', 'text', [
|
||||
'AND' => [
|
||||
'unit_id' => 2,
|
||||
'lang_id' => 1,
|
||||
],
|
||||
])
|
||||
->willReturn('kg');
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
$this->assertSame('kg', $repository->getUnitNameById(2, 1));
|
||||
}
|
||||
|
||||
public function testGetUnitNameByIdSupportsStringLanguageId(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('get')
|
||||
->with('pp_units_langs', 'text', [
|
||||
'AND' => [
|
||||
'unit_id' => 2,
|
||||
'lang_id' => 'pl',
|
||||
],
|
||||
])
|
||||
->willReturn('kg');
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
$this->assertSame('kg', $repository->getUnitNameById(2, 'pl'));
|
||||
}
|
||||
|
||||
public function testAllUnitsReturnsArrayIndexedById(): void
|
||||
{
|
||||
$mockDb = $this->createMock(\medoo::class);
|
||||
|
||||
$stmt = $this->createMock(\PDOStatement::class);
|
||||
$stmt->expects($this->once())
|
||||
->method('fetchAll')
|
||||
->with(\PDO::FETCH_ASSOC)
|
||||
->willReturn([
|
||||
['id' => 3, 'text' => 'kg'],
|
||||
['id' => 4, 'text' => 'l'],
|
||||
]);
|
||||
|
||||
$mockDb->expects($this->once())
|
||||
->method('query')
|
||||
->willReturn($stmt);
|
||||
|
||||
$repository = new DictionariesRepository($mockDb);
|
||||
$units = $repository->allUnits();
|
||||
|
||||
$this->assertArrayHasKey(3, $units);
|
||||
$this->assertSame('kg', $units[3]['text']);
|
||||
$this->assertArrayHasKey(4, $units);
|
||||
}
|
||||
}
|
||||
64
tests/Unit/admin/Controllers/DictionariesControllerTest.php
Normal file
64
tests/Unit/admin/Controllers/DictionariesControllerTest.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace Tests\Unit\admin\Controllers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use admin\Controllers\DictionariesController;
|
||||
use Domain\Dictionaries\DictionariesRepository;
|
||||
|
||||
class DictionariesControllerTest extends TestCase
|
||||
{
|
||||
private $mockRepository;
|
||||
private $controller;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->mockRepository = $this->createMock(DictionariesRepository::class);
|
||||
$this->controller = new DictionariesController($this->mockRepository);
|
||||
}
|
||||
|
||||
public function testConstructorAcceptsRepository(): void
|
||||
{
|
||||
$controller = new DictionariesController($this->mockRepository);
|
||||
$this->assertInstanceOf(DictionariesController::class, $controller);
|
||||
}
|
||||
|
||||
public function testHasListMethod(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'list'));
|
||||
}
|
||||
|
||||
public function testHasEditMethod(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'edit'));
|
||||
}
|
||||
|
||||
public function testHasSaveMethod(): void
|
||||
{
|
||||
$this->assertTrue(method_exists($this->controller, 'save'));
|
||||
}
|
||||
|
||||
public function testHasDeleteMethod(): void
|
||||
{
|
||||
$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 testConstructorRequiresDictionariesRepository(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(DictionariesController::class);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$params = $constructor->getParameters();
|
||||
|
||||
$this->assertCount(1, $params);
|
||||
$this->assertEquals('Domain\Dictionaries\DictionariesRepository', $params[0]->getType()->getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user