Added Core and test

This commit is contained in:
Kevin Frantz 2018-07-14 16:52:17 +02:00
parent d79616c3ae
commit b11cea5337
3 changed files with 138 additions and 0 deletions

73
src/core/Core.php Normal file
View File

@ -0,0 +1,73 @@
<?php
namespace core;
use entity\user\UserInterface;
/**
*
* @author kevinfrantz
*
*/
final class Core implements CoreInterface
{
const DATABASE_USERNAME = 'devuser';
const DATABASE_PASSWORD = 'devpass';
const DATABASE_NAME = 'test_db';
const DATABASE_PORT = '3306';
const DATABASE_HOST = 'codingchallengeonlineshop_db_1';
/**
* @var \Twig_Environment
*/
private $twig;
/**
* @var UserInterface
*/
private $user;
/**
* @var \PDO
*/
private $database;
public function __construct(){
$this->initTwig();
$this->initDatabase();
}
private function initTwig():void{
$loader = new \Twig_Loader_Filesystem(__DIR__.'/../template');
$this->twig = new \Twig_Environment($loader);
}
private function initDatabase():void{
$this->database = new \PDO('mysql:host='.self::DATABASE_HOST.';dbname='.self::DATABASE_NAME.';port='.self::DATABASE_PORT, self::DATABASE_USERNAME, self::DATABASE_PASSWORD);
}
public function getDatabase(): \PDO
{
return $this->database;
}
public function getTwig(): \Twig_Environment
{
return $this->twig;
}
public function getUser(): ?UserInterface
{
return $this->user;
}
public function setUser(?UserInterface $user = null): void
{
$this->user = $user;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace core;
use entity\user\UserInterface;
/**
*
* @author kevinfrantz
*
*/
interface CoreInterface
{
public function getDatabase():\PDO;
public function getTwig():\Twig_Environment;
public function getUser():?UserInterface;
public function setUser(?UserInterface $user = null):void;
}

44
src/core/CoreTest.php Normal file
View File

@ -0,0 +1,44 @@
<?php
namespace core;
use PHPUnit\Framework\TestCase;
use entity\user\User;
/**
*
* @author kevinfrantz
*
*/
class CoreTest extends TestCase
{
/**
*
* @var Core
*/
protected $core;
/**
*
* @var User
*/
protected $user;
protected function setUp():void{
$this->core = new Core();
$this->user = new User();
$this->core->setUser($this->user);
}
public function testTwig():void{
$this->assertInstanceOf(\Twig_Environment::class, $this->core->getTwig());
}
public function testDatabase():void{
$this->assertInstanceOf(\PDO::class, $this->core->getDatabase());
}
public function testUser():void{
$this->assertEquals($this->user, $this->core->getUser());
}
}