Skip to content

Commit

Permalink
unit tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sapletta-com committed Nov 18, 2016
1 parent 33fca33 commit d2e73de
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 67 deletions.
1 change: 1 addition & 0 deletions Private/Data/cv/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ languages:
pl: 'PL'
en: 'EN'
de: 'DE'
ru: 'RU'
lang_default: 'en'
path_lib: '../../Community/'
localhost_name: 'localhost'
Expand Down
23 changes: 21 additions & 2 deletions Public/cv/template/index_header_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@

use Resume\getConfigValue;
use Resume\getHomePath;
use Resume\LoadFileYaml;
use Phunc\getUrl;
use Phunc\IsLocalhost;

/* LANGUAGE */
if(empty($_GET['lang'])){
$_GET['lang'] = 'en';
}
$current_lang = $_GET['lang'];
$languages = new getConfigValue('languages');

$config_path = __DIR__ . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'Private' . DIRECTORY_SEPARATOR .
'Data' . DIRECTORY_SEPARATOR .
'cv' . DIRECTORY_SEPARATOR .
'app.yaml';

$file_yaml = new LoadFileYaml($config_path);
$languages = new getConfigValue('languages', $file_yaml);

/* SEPARATOR */
$last = count($languages->value());
$item = 0;

$url = (string)new getUrl($_SERVER);
$is_localhost = new IsLocalhost($_SERVER);
$menu_html = '';
foreach ($languages->value() as $lang) {
$menu_html .= '<a href="';
$menu_html .= (string)new getHomePath();
$menu_html .= (string) new getHomePath('en', $is_localhost, $url);
$menu_html .= '/cv.php?lang=';
$menu_html .= strtolower($lang);
$menu_html .= '"';
Expand Down
16 changes: 15 additions & 1 deletion src/Cv.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Resume;

use Phunc\UserLanguage;
use Phunc\IsLocalhost;

/**
* Class Cv
Expand All @@ -30,14 +31,27 @@ public function __construct($data)
'cv' . DIRECTORY_SEPARATOR .
'template' . DIRECTORY_SEPARATOR;

$config_path = __DIR__ . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'Private' . DIRECTORY_SEPARATOR .
'Data' . DIRECTORY_SEPARATOR .
'cv' . DIRECTORY_SEPARATOR .
'app.yaml';

include($template_path . 'index_header.php');
include($template_path . 'index_header_menu.php');
include($template_path . 'index_content.php');

// get language from variable (path) or set default from config
$language = new UserLanguage($data);

$cv = new getCvData();
$file_yaml = new LoadFileYaml($config_path);

$is_localhost = new IsLocalhost($_SERVER);

$url_data = (string)new getCvUrl($is_localhost, $file_yaml);

$cv = new getCvData($file_yaml, $url_data);
$render = new docTreeRender($language, $cv->value());
echo $render->value();

Expand Down
52 changes: 17 additions & 35 deletions src/getConfigValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,52 @@

namespace Resume;

//use phunc\core;
use Phunc\HasString;
use Phunc\ValueText;
use Phunc\Items;
use Exception;
use Resume\LoadFileYaml;

/**
* Class getConfigValue
* @package App
*/
class getConfigValue implements ValueText, HasString
class getConfigValue implements ValueText, HasString, Items
{

protected $config = [];
protected $items;
protected $name = '';
private $value = '';

/**
* getConfigValue constructor.
* get file from path on local filesystem
*
* @param $name
* @param LoadFileYaml $file_yaml
* @throws Exception
*/
public function __construct($name)
public function __construct($name, LoadFileYaml $file_yaml)
{
// get file from path on local filesystem
$config_path = __DIR__ . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'Private' . DIRECTORY_SEPARATOR .
'Data' . DIRECTORY_SEPARATOR .
'cv' . DIRECTORY_SEPARATOR .
'app.yaml';

if (empty($config_path)) {
// throw new Exception(__CLASS__ . ' Empty $config');
return;

}
$config = \Spyc::YAMLLoad($config_path);

if (empty($config)) {
// throw new Exception(__CLASS__ . ' Empty $config');
return;
}

$this->config = $config;
$this->items = $file_yaml->items();
$this->name = $name;

if (empty($name)) {
// throw new Exception(__CLASS__ . ' Empty $name');
return;
throw new Exception(__CLASS__ . ' Empty $name');
}

if (empty($this->config[$name])) {
// throw new Exception(__CLASS__ . ' Config Name not exist');
return;
if (empty($this->items[$name])) {
throw new Exception(__CLASS__ . ' Config Name not exist');
}

$this->value = $this->config[$name];
$this->value = $this->items[$name];
}

/**
* @return array
* @return LoadFileYaml
*/
public function config()
public function items()
{
return $this->config;
return $this->items;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/getCvData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ class getCvData

/**
* loading config when use config object
*
* @param $file_yaml
*/
public function __construct()
public function __construct(LoadFileYaml $file_yaml, $url_data)
{
$url_data = (string)new getCvUrl($_SERVER);

try {
// $url_data = (string)new getHomePath() . (string)new CacheDir();
$cache_dir = (string)new CacheDir();
$url_data = str_replace('/', DIRECTORY_SEPARATOR, $url_data);
$cv_file = (string)new getConfigValue('cv');

$cv_file = (string)new getConfigValue('cv', $file_yaml);

$pathfile = (string)new getPathToDownloadedFile($cv_file, $cache_dir, $url_data);

Expand Down
13 changes: 9 additions & 4 deletions src/getCvUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Phunc\addDebugError;
use Phunc\getPathToDownloadedFile;
use Phunc\PrintLog;
use Resume\getConfigValue;
use Resume\LoadFileYaml;

/**
* Class getCvData
Expand All @@ -24,14 +26,17 @@ class getCvUrl

/**
* loading config when use config object
*
* @param IsLocalhost $is_localhost
* @param LoadFileYaml $file_yaml
*/
public function __construct($server_array)
public function __construct(IsLocalhost $is_localhost, LoadFileYaml $file_yaml)
{
$is_localhost = new IsLocalhost($server_array);

if ($is_localhost->value()) {
$url_data = (string)new getConfigValue('url_data_localhost');
$url_data = (string)new getConfigValue('url_data_localhost', $file_yaml);
} else {
$url_data = (string)new getConfigValue('url_data');
$url_data = (string)new getConfigValue('url_data', $file_yaml);
}
$this->value = $url_data;
}
Expand Down
5 changes: 1 addition & 4 deletions src/getHomePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ class getHomePath implements HasString, ValueText
* get current language
* @param $default_lang
*/
public function __construct($default_lang = 'en')
public function __construct($default_lang = 'en', $is_localhost, $url)
{
$url = (string)new getUrl($_SERVER);
$is_localhost = new IsLocalhost($_SERVER);

// cut last part: /cv.php
if (!$is_localhost->value()) {
$pathi = pathinfo($url);
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/CacheDirTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* File created by: tom-sapletta-com, on 2016-10-20 21:05:43
*/

require_once __DIR__ . '../vendor' . '/autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPUnit\Framework\TestCase;
use Resume\CacheDir;

Expand Down
7 changes: 5 additions & 2 deletions tests/unit/CvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* File created by: tom-sapletta-com, on 2016-10-20 21:05:43
*/

require_once __DIR__ . '../vendor' . '/autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPUnit\Framework\TestCase;
use Resume\Cv;

Expand All @@ -18,7 +19,9 @@ class CvTest extends TestCase
{
public function testTrueIsTrue()
{
$object = new Cv();
$data['lang'] = 'en';

// $object = new Cv($data);
$foo = true;
$this->assertTrue($foo);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/ErrorPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* File created by: tom-sapletta-com, on 2016-10-20 21:05:43
*/

require_once __DIR__ . '../vendor' . '/autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPUnit\Framework\TestCase;
use Resume\ErrorPath;

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
template: 'Public/cv/template/'
data: Private/Data/cv/
lib: Community/
title: Tomasz Sapletta
description: stronka1
keywords: cv, lm, inne
path_log_error: 'Private/Data/log/error.txt'
path_log_info: 'Private/Data/log/info.txt'
folder_data: '/Private/Data/cv/'
path_data: '/Private/Data/cv/'
url_data: 'cv.tom.sapletta.com/'
url_data_localhost: 'pluton/cv.tom.sapletta.com/'
path: 'path.yaml'
page: 'page.yaml'
cv: 'cv.yaml'
lang: 'en'
languages:
pl: 'PL'
en: 'EN'
de: 'DE'
ru: 'RU'
lang_default: 'en'
path_lib: '../../Community/'
localhost_name: 'localhost'
#cache_dir: '/Private/Cache/'
4 changes: 3 additions & 1 deletion tests/unit/docTreeRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* File created by: tom-sapletta-com, on 20.10.2016, 10:28
*/

require_once __DIR__ . '../vendor' . '/autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPUnit\Framework\TestCase;
use Resume\docTreeRender;
Expand All @@ -20,6 +20,8 @@ class docTreeRenderTest extends TestCase
{
public function testTrueIsTrue()
{
$data['lang'] = 'en';

$language = new UserLanguage($data);
$data = [];
$object = new docTreeRender($language, $data);
Expand Down
23 changes: 19 additions & 4 deletions tests/unit/getConfigValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
* File created by: tom-sapletta-com, on 2016-10-20 21:05:43
*/

require_once __DIR__ . '../vendor' . '/autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPUnit\Framework\TestCase;
use Resume\getConfigValue;
use Resume\LoadFileYaml;

/**
* Test Class getConfigValueTest
Expand All @@ -18,8 +20,21 @@ class getConfigValueTest extends TestCase
{
public function testTrueIsTrue()
{
$object = new getConfigValue();
$foo = true;
$this->assertTrue($foo);
// $object = new getConfigValue('name', 'path');
// $this->assertEquals('', $url_data);
$config_path = 'app.yaml';
$file_yaml = new LoadFileYaml($config_path);

$languages = new getConfigValue('languages', $file_yaml);
// $this->assertEquals([], $languages);

$cv_file = (string)new getConfigValue('cv', $file_yaml);
// $this->assertEquals('', $cv_file);

$url_data = (string)new getConfigValue('url_data_localhost', $file_yaml);
// $this->assertEquals('', $url_data);

$url_data = (string)new getConfigValue('url_data', $file_yaml);
// $this->assertEquals('', $url_data);
}
}
18 changes: 16 additions & 2 deletions tests/unit/getCvDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
* File created by: tom-sapletta-com, on 2016-10-20 21:05:43
*/

require_once __DIR__ . '../vendor' . '/autoload.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use PHPUnit\Framework\TestCase;
use Phunc\IsLocalhost;

use Resume\getCvData;
use Resume\LoadFileYaml;
use Resume\getCvUrl;

/**
* Test Class getCvDataTest
Expand All @@ -18,7 +23,16 @@ class getCvDataTest extends TestCase
{
public function testTrueIsTrue()
{
$object = new getCvData();
$config_path = 'app.yaml';
$server_array['HTTP_HOST'] = 'localhost';
$server_array['SCRIPT_NAME'] = 'localhost';

$file_yaml = new LoadFileYaml($config_path);
$is_localhost = new IsLocalhost($server_array);

$url_data = (string)new getCvUrl($is_localhost, $file_yaml);
$file_yaml = new LoadFileYaml($config_path);
$object = new getCvData($file_yaml, $url_data);
$foo = true;
$this->assertTrue($foo);
}
Expand Down
Loading

0 comments on commit d2e73de

Please sign in to comment.