- Dodano testy: SettingsRepositoryTest, LanguagesRepositoryTest, UserRepositoryTest - Infrastruktura: phpunit.xml, composer.json (phpunit/phpunit ^10), tests/bootstrap.php - Bootstrap stuby: \Shared\Cache\CacheHandler (in-memory), \S - Zaktualizowano docs/TESTING.md dla cmsPRO - Paczka: updates/1.60/ver_1.691.zip + manifest Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
102 lines
2.8 KiB
PHP
102 lines
2.8 KiB
PHP
<?php
|
|
namespace Tests\Unit\Domain\Settings;
|
|
|
|
use Domain\Settings\SettingsRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class SettingsRepositoryTest extends TestCase
|
|
{
|
|
private function mockDb(): object
|
|
{
|
|
return $this->createMock(\medoo::class);
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
\Shared\Cache\CacheHandler::reset();
|
|
}
|
|
|
|
// --- allSettings ---
|
|
|
|
public function testAllSettingsReturnsMappedArray(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->method('select')->willReturn([
|
|
['param' => 'site_name', 'value' => 'Test CMS'],
|
|
['param' => 'email', 'value' => 'admin@test.pl'],
|
|
]);
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$result = $repo->allSettings();
|
|
|
|
$this->assertSame('Test CMS', $result['site_name']);
|
|
$this->assertSame('admin@test.pl', $result['email']);
|
|
}
|
|
|
|
public function testAllSettingsReturnsEmptyArrayWhenDbReturnsNull(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->method('select')->willReturn(null);
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$this->assertSame([], $repo->allSettings());
|
|
}
|
|
|
|
public function testAllSettingsUsesCache(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->expects($this->never())->method('select');
|
|
|
|
\CacheHandlerStub::store('settings_details', ['cached' => '1']);
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$result = $repo->allSettings();
|
|
|
|
$this->assertSame('1', $result['cached']);
|
|
}
|
|
|
|
// --- update ---
|
|
|
|
public function testUpdateCallsDbUpdateWhenParamExists(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->method('count')->willReturn(1);
|
|
$db->expects($this->once())->method('update')->willReturn(true);
|
|
$db->expects($this->never())->method('insert');
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$this->assertTrue($repo->update('site_name', 'Nowa Nazwa'));
|
|
}
|
|
|
|
public function testUpdateCallsDbInsertWhenParamMissing(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->method('count')->willReturn(0);
|
|
$db->expects($this->once())->method('insert')->willReturn(true);
|
|
$db->expects($this->never())->method('update');
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$repo->update('new_param', 'value');
|
|
}
|
|
|
|
// --- visitCounter ---
|
|
|
|
public function testVisitCounterReturnsValue(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->method('get')->willReturn('1234');
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$this->assertSame('1234', $repo->visitCounter());
|
|
}
|
|
|
|
public function testVisitCounterReturnsNullWhenEmpty(): void
|
|
{
|
|
$db = $this->mockDb();
|
|
$db->method('get')->willReturn(null);
|
|
|
|
$repo = new SettingsRepository($db);
|
|
$this->assertNull($repo->visitCounter());
|
|
}
|
|
}
|