Skip to content

Commit

Permalink
refactor: renamed some methods, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 24, 2024
1 parent 9cbc003 commit 8bdbcc0
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 23 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/admin/instances.edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$instanceId = Filter::filterInput(INPUT_GET, 'instance_id', FILTER_VALIDATE_INT);

$instance = new Instance($faqConfig);
$instanceData = $instance->getInstanceById($instanceId, 'array');
$instanceData = $instance->getById($instanceId, 'array');

$templateVars = [
'ad_menu_instances' => Translation::get('ad_menu_instances'),
Expand Down
8 changes: 4 additions & 4 deletions phpmyfaq/admin/instances.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$updatedData->setComment(Filter::filterInput(INPUT_POST, 'comment', FILTER_SANITIZE_SPECIAL_CHARS));

// Original data
$originalData = $currentClient->getInstanceById($instanceId);
$originalData = $currentClient->getById($instanceId);

if ($originalData->url !== $updatedData->getUrl()) {
$moveInstance = true;
Expand All @@ -65,7 +65,7 @@
if (is_null($updatedData->getUrl())) {
echo Alert::danger('ad_entryins_fail', $faqConfig->getDb()->error());
} else {
if ($updatedClient->updateInstance($instanceId, $updatedData)) {
if ($updatedClient->update($instanceId, $updatedData)) {
if ($moveInstance) {
$updatedClient->moveClientFolder($originalData->url, $updatedData->getUrl());
$updatedClient->deleteClientFolder($originalData->url);
Expand All @@ -78,15 +78,15 @@
}

$masterConfig = [];
foreach ($instance->getAllInstances() as $site) {
foreach ($instance->getAll() as $site) {
$masterConfig[$site->id] = $instance->getInstanceConfig($site->id)['isMaster'];
}

$templateVars = [
'userPermInstanceAdd' => $user->perm->hasPermission($user->getUserId(), PermissionType::INSTANCE_ADD->value),
'multisiteFolderIsWritable' => is_writable(PMF_ROOT_DIR . DIRECTORY_SEPARATOR . 'multisite'),
'ad_instance_add' => Translation::get('ad_instance_add'),
'allInstances' => $instance->getAllInstances(),
'allInstances' => $instance->getAll(),
'csrfTokenDeleteInstance' => Token::getInstance()->getTokenString('delete-instance'),
'csrfTokenAddInstance' => Token::getInstance()->getTokenString('add_instance'),
'masterConfig' => $masterConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function add(Request $request): JsonResponse
->setComment($comment);

$faqInstance = new Instance($configuration);
$instanceId = $faqInstance->addInstance($data);
$instanceId = $faqInstance->create($data);

$faqInstanceClient = new Client($configuration);
$faqInstanceClient->createClient($faqInstance);
Expand Down Expand Up @@ -134,7 +134,7 @@ public function add(Request $request): JsonResponse

Database::setTablePrefix($databaseConfiguration->getPrefix());
} else {
$faqInstance->removeInstance($instanceId);
$faqInstance->delete($instanceId);
return $this->json(['error' => 'Cannot create instance.'], Response::HTTP_BAD_REQUEST);
}

Expand Down Expand Up @@ -168,11 +168,11 @@ public function delete(Request $request): JsonResponse
if (null !== $instanceId) {
$client = new Client($configuration);
$client->setFileSystem(new Filesystem());
$clientData = $client->getInstanceById($instanceId);
$clientData = $client->getById($instanceId);
if (
1 !== $instanceId &&
$client->deleteClientFolder($clientData->url) &&
$client->removeInstance($instanceId)
$client->delete($instanceId)
) {
$payload = ['deleted' => $instanceId];
return $this->json($payload, Response::HTTP_OK);
Expand Down
26 changes: 13 additions & 13 deletions phpmyfaq/src/phpMyFAQ/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public function __construct(protected Configuration $configuration)
*
* @return int $id
*/
public function addInstance(InstanceEntity $instanceEntity): int
public function create(InstanceEntity $instance): int
{
$this->setId($this->configuration->getDb()->nextId(Database::getTablePrefix() . 'faqinstances', 'id'));

$insert = sprintf(
"INSERT INTO %sfaqinstances VALUES (%d, '%s', '%s', '%s', %s, %s)",
Database::getTablePrefix(),
$this->getId(),
$this->configuration->getDb()->escape($instanceEntity->getUrl()),
$this->configuration->getDb()->escape($instanceEntity->getInstance()),
$this->configuration->getDb()->escape($instanceEntity->getComment()),
$this->configuration->getDb()->escape($instance->getUrl()),
$this->configuration->getDb()->escape($instance->getInstance()),
$this->configuration->getDb()->escape($instance->getComment()),
$this->configuration->getDb()->now(),
$this->configuration->getDb()->now()
);
Expand Down Expand Up @@ -94,7 +94,7 @@ public function setId(int $id): void
*
* @return stdClass[]
*/
public function getAllInstances(): array
public function getAll(): array
{
$select = sprintf(
'SELECT * FROM %sfaqinstances ORDER BY id',
Expand All @@ -109,7 +109,7 @@ public function getAllInstances(): array
/**
* Returns the instance.
*/
public function getInstanceById(int $id): object
public function getById(int $id): object
{
$select = sprintf(
'SELECT * FROM %sfaqinstances WHERE id = %d',
Expand All @@ -125,25 +125,25 @@ public function getInstanceById(int $id): object
/**
* Updates the instance data.
*/
public function updateInstance(int $id, InstanceEntity $instanceEntity): bool
public function update(int $id, InstanceEntity $instance): bool
{
$update = sprintf(
"UPDATE %sfaqinstances SET instance = '%s', comment = '%s', url = '%s' WHERE id = %d",
Database::getTablePrefix(),
$this->configuration->getDb()->escape($instanceEntity->getInstance()),
$this->configuration->getDb()->escape($instanceEntity->getComment()),
$this->configuration->getDb()->escape($instanceEntity->getUrl()),
$this->configuration->getDb()->escape($instance->getInstance()),
$this->configuration->getDb()->escape($instance->getComment()),
$this->configuration->getDb()->escape($instance->getUrl()),
$id
);

return $this->configuration->getDb()->query($update);
return (bool) $this->configuration->getDb()->query($update);
}

/**
* Deletes an instance.
*
*/
public function removeInstance(int $id): bool
public function delete(int $id): bool
{
$deletes = [
sprintf(
Expand Down Expand Up @@ -185,7 +185,7 @@ public function addConfig(string $name, string $value): mixed
$this->configuration->getDb()->escape(trim($value))
);

return $this->configuration->getDb()->query($insert);
return (bool) $this->configuration->getDb()->query($insert);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Setup/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ public function startInstall(array|null $setup = null): void
->setInstance($link->getSystemRelativeUri('setup/index.php'))
->setComment('phpMyFAQ ' . System::getVersion());
$faqInstance = new Instance($configuration);
$faqInstance->addInstance($instanceEntity);
$faqInstance->create($instanceEntity);

$faqInstanceMaster = new Master($configuration);
$faqInstanceMaster->createMaster($faqInstance);
Expand Down
120 changes: 120 additions & 0 deletions tests/phpMyFAQ/InstanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace phpMyFAQ;

use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Entity\InstanceEntity;
use PHPUnit\Framework\TestCase;

class InstanceTest extends TestCase
{
private Instance $instance;
protected function setUp(): void
{
parent::setUp();

$dbHandle = new Sqlite3();
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$configuration = new Configuration($dbHandle);

$this->instance = new Instance($configuration);
}

public function testCreate(): void
{
$instance = new InstanceEntity();
$instance
->setUrl('http://two.localhost')
->setInstance('Second localhost')
->setComment('Test instance');

$this->assertEquals(2, $this->instance->create($instance));
$this->instance->delete(2);
}

public function testGetAll(): void
{
$instances = $this->instance->getAll();
$this->assertCount(1, $instances); // Only one instance is created by default

$instance = new InstanceEntity();
$instance
->setUrl('http://two.localhost')
->setInstance('Second localhost')
->setComment('Test instance');
$this->instance->create($instance);

$this->assertCount(2, $this->instance->getAll());
$this->instance->delete(2);
}

public function testGetById(): void
{
$instance = new InstanceEntity();
$instance
->setUrl('http://two.localhost')
->setInstance('Second localhost')
->setComment('Test instance');
$id = $this->instance->create($instance);

$instance = $this->instance->getById($id);
$this->assertEquals('http://two.localhost', $instance->url);
$this->assertEquals('Second localhost', $instance->instance);
$this->assertEquals('Test instance', $instance->comment);

$this->instance->delete($id);
}

public function testUpdate(): void
{
$instance = new InstanceEntity();
$instance
->setUrl('http://two.localhost')
->setInstance('Second localhost')
->setComment('Test instance');
$id = $this->instance->create($instance);

$instance
->setUrl('http://three.localhost')
->setInstance('Third localhost')
->setComment('Test instance');
$this->assertTrue($this->instance->update($id, $instance));

$instance = $this->instance->getById($id);
$this->assertEquals('http://three.localhost', $instance->url);
$this->assertEquals('Third localhost', $instance->instance);
$this->assertEquals('Test instance', $instance->comment);

$this->instance->delete($id);
}

public function testAddConfig(): void
{
$instance = new InstanceEntity();
$instance
->setUrl('http://two.localhost')
->setInstance('Second localhost')
->setComment('Test instance');
$id = $this->instance->create($instance);

$this->instance->addConfig('foo', 'bar');
$this->assertEquals('bar', $this->instance->getConfig('foo'));

$this->instance->delete($id);
}

public function testGetInstanceConfig(): void
{
$instance = new InstanceEntity();
$instance
->setUrl('http://two.localhost')
->setInstance('Second localhost')
->setComment('Test instance');
$id = $this->instance->create($instance);

$this->instance->addConfig('foo', 'bar');
$this->assertEquals(['foo' => 'bar'], $this->instance->getInstanceConfig($id));

$this->instance->delete($id);
}
}

0 comments on commit 8bdbcc0

Please sign in to comment.