<?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());
	}
	
}
