code cleanup

This commit is contained in:
tracer 2022-10-08 10:16:52 +02:00
parent 4199faecb4
commit 552eeb0c25
10 changed files with 90 additions and 360 deletions

View File

@ -6,20 +6,16 @@
6. [DynDNS](#6-dyndns)
7. [Conclusion](#7-conclusion)
I'm currently in the process to convert this to a symfony app with a UX Turbo interface.
Don't use this code right now.
Use the latest release instead.
NOTICE: This documentation is not current as of September 2022.
After I finished the refactoring I'll upgrade it.
// important: Migration to v2022.2
ALTER TABLE `panels` ADD `self` INT NOT NULL DEFAULT '0' AFTER `apikey`;
ALTER TABLE `panels` CHANGE `self` `self` BOOLEAN NOT NULL DEFAULT FALSE;
ALTER TABLE `panels` ADD `apikey_prefix` VARCHAR(8) NOT NULL AFTER `apikey`;
<a name="overview"></a>
# 1. Overview

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL);
use Exception;
use PDO;
use PDOException;
/**
*
*/
class ApiKeys
{
public function __construct(private DatabaseConnection $databaseConnection)
{}
}

View File

@ -1,14 +0,0 @@
<?php
namespace App\Enums;
/**
*
*/
enum PanelType
{
case Panel;
case Custom;
case Undefined;
}

View File

@ -1 +0,0 @@
<?php

View File

@ -1,23 +0,0 @@
<?php
namespace App\Controller;
use PHPUnit\Framework\TestCase;
/**
*
*/
class RequestControllerTest extends TestCase
{
public function setUp(): void
{
}
public function tearDown(): void
{
}
}

View File

@ -1,161 +0,0 @@
<?php
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Controller\DomainController;
use App\Entity\Domain;
use DI\Container;
use DI\ContainerBuilder;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use function DI\autowire;
/**
*
*/
class DomainRepositoryTest extends TestCase
{
private Container $container;
private DomainRepository $domainRepository;
private DomainController $domainController;
private string $localZoneFile;
private string $localZonesDir;
private string $namedConfLocalFile;
private Logger $log;
/**
* @param int|string $dataName
*
* @throws \Exception
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct(name: $name, data: $data, dataName: $dataName);
$dateFormat = "Y-m-d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 3) . '/bindAPI.test.log');
$stream->setFormatter(formatter: $formatter);
$this->log = new Logger(name: 'bindAPI');
$this->log->pushHandler(handler: $stream);
$this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/';
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
//$this->zoneCachePath = '/var/cache/bind/';
// read config TODO use .env file instead?
$configFile = dirname(path: __DIR__, levels: 3) . "/config.json";
$configJSON = file_get_contents(filename: $configFile);
$config = json_decode(json: $configJSON, associative: true);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $config),
DomainController::class => autowire()
->constructorParameter(parameter: 'config', value: $config)
->constructorParameter(parameter: 'log', value: $this->log),
DomainRepository::class => autowire()
->constructorParameter(parameter: 'config', value: $config)
->constructorParameter(parameter: 'log', value: $this->log),
]);
$this->container = $containerBuilder->build();
$this->domainRepository = $this->container->get(name: DomainRepository::class);
$this->domainController = $this->container->get(name: DomainController::class);
}
public function setUp(): void
{
$this->log->info(message: 'Started DomainRepositoryTest');
}
public function tearDown(): void
{
$this->log->info(message: 'Finished DomainRepositoryTest');
}
/**
* @throws \DI\NotFoundException
* @throws \DI\DependencyException
*/
public function testInsert()
{
$domain = new Domain(name: 'inserttest.org', a: '1.2.3.4', aaaa: '1bad::babe');
$this->domainRepository->insert(domain: $domain);
$this->domainController->createSlaveZoneFile(domain: $domain);
// now get the persisted domain with id
$domainTest = $this->domainRepository->findByName(name: 'inserttest.org');
$this->assertIsNotBool(actual: $domainTest);
$this->assertEquals(expected: 'inserttest.org', actual: $domainTest->getName());
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
$this->assertStringContainsString(needle: $this->localZoneFile, haystack: $namedConfLocal);
} else {
$this->fail(message: 'No permissions: ' . $this->namedConfLocalFile);
}
$this->assertNotFalse(condition: fileperms(filename: $this->localZoneFile));
$localZones = file_get_contents(filename: $this->localZoneFile);
$this->assertStringContainsString(needle: $domainTest->getName(), haystack: $localZones);
$zoneFile = $this->localZonesDir . $domain->getName();
$this->assertFileExists(filename: $zoneFile);
// clean up
$this->domainRepository->delete(domain: $domainTest);
}
/**
* @throws \DI\NotFoundException
* @throws \DI\DependencyException
*/
public function testDelete()
{
$domain = new Domain(name: 'inserttest.org', a: '1.2.3.4', aaaa: '1bad::babe');
$this->domainRepository->insert(domain: $domain);
$this->domainController->createSlaveZoneFile(domain: $domain);
$domainTest = $this->domainRepository->findByName(name: 'inserttest.org');
$this->assertIsNotBool(actual: $domainTest);
$this->assertEquals(expected: 'inserttest.org', actual: $domainTest->getName());
// domain is valid and created
// now delete and check for cleanup
$this->domainRepository->delete(domain: $domainTest);
$this->domainController->deleteZone(domain: $domainTest);
// check zone is removed
$this->assertNotFalse(condition: fileperms(filename: $this->localZoneFile));
$localZones = file_get_contents(filename: $this->localZoneFile);
$this->assertStringNotContainsString(needle: $domainTest->getName(), haystack: $localZones);
$zoneFile = $this->localZonesDir . $domain->getName();
$this->assertFileDoesNotExist(filename: $zoneFile);
}
}

View File

@ -1,61 +0,0 @@
<?php
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Entity\Nameserver;
use DI\Container;
use DI\ContainerBuilder;
use PHPUnit\Framework\TestCase;
use function DI\autowire;
/**
*
*/
class NameserverRepositoryTest extends TestCase
{
private Container $container;
private NameserverRepository $nameserverRepository;
/**
* @param int|string $dataName
*
* @throws \Exception
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct(name: $name, data: $data, dataName: $dataName);
// read config TODO use .env file instead?
$configFile = dirname(path: __DIR__, levels: 3) . "/config.json";
$configJSON = file_get_contents(filename: $configFile);
$config = json_decode(json: $configJSON, associative: true);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $config),
]);
$this->container = $containerBuilder->build();
$this->nameserverRepository = $this->container->get(name: NameserverRepository::class);
}
/**
* @throws \DI\NotFoundException
* @throws \DI\DependencyException
*/
public function testInsert()
{
$nameserver = new Nameserver(name: 'inserttest.org', a: '1.2.3.4', aaaa: '1bad::babe');
$this->nameserverRepository->insert(nameserver: $nameserver);
$nameservertest = $this->nameserverRepository->findByName(name: 'inserttest.org');
$this->assertIsNotBool(actual: $nameserver);
$this->assertEquals(expected: 'inserttest.org', actual: $nameservertest->getName());
// clean up
$this->nameserverRepository->delete(id: $nameservertest->getId());
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace App\Repository;
use App\Controller\BindApiTestController;
use PHPUnit\Framework\TestCase;
class PanelRepositoryTest extends BindApiTestController
{
public function setUp(): void
{
}
public function tearDown(): void
{
}
public function testInsert()
{
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace Unit\Controller;
use App\Controller\ConfigController;
use App\Controller\DatabaseConnection;
use App\Controller\DomainController;
use App\Repository\DomainRepository;
use App\Repository\NameserverRepository;
use DI\Container;
use DI\ContainerBuilder;
use Exception;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use function DI\autowire;
/**
*
*/
class BindApiControllerTest extends TestCase
{
protected Container $container;
protected NameserverRepository $nameserverRepository;
protected DomainRepository $domainRepository;
protected DomainController $domainController;
protected Logger $logger;
/**
* @param int|string $dataName
*
* @throws Exception
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct(name: $name, data: $data, dataName: $dataName);
// init the logger
$dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
$debug = (new ConfigController)->getConfig(configKey: 'debug');
if ($debug) {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 3) . '/bindAPI.test.log', level: Level::Debug);
} else {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 3) . '/bindAPI.test.log', level: Level::Info);
}
$stream->setFormatter(formatter: $formatter);
$this->logger = new Logger(name: 'bindAPI');
$this->logger->pushHandler(handler: $stream);
$this->logger->debug(message: 'bindAPI started');
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
//DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $config),
DomainController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger),
DomainRepository::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger),
]);
$this->container = $containerBuilder->build();
$this->nameserverRepository = $this->container->get(name: NameserverRepository::class);
$this->domainRepository = $this->container->get(name: DomainRepository::class);
$this->domainController = $this->container->get(name: DomainController::class);
}
public function testFoo(): void
{
self::assertEquals(expected: true, actual: true);
}
}

View File

@ -1,44 +0,0 @@
<?php
namespace Unit\Controller;
use App\Controller\DatabaseConnection;
use App\Repository\NameserverRepository;
use DI\Container;
use DI\ContainerBuilder;
use Exception;
use PHPUnit\Framework\TestCase;
use function DI\autowire;
/**
*
*/
class BindApiTestController extends TestCase
{
protected Container $container;
protected NameserverRepository $nameserverRepository;
/**
* @param int|string $dataName
*
* @throws Exception
* @internal This method is not covered by the backward compatibility promise for PHPUnit
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct(name: $name, data: $data, dataName: $dataName);
// read config TODO use .env file instead?
$configFile = dirname(path: __DIR__) . "/config.json";
$configJSON = file_get_contents(filename: $configFile);
$config = json_decode(json: $configJSON, associative: true);
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $config),
]);
$this->container = $containerBuilder->build();
$this->nameserverRepository = $this->container->get(name: NameserverRepository::class);
}
}