Skip to content

Commit 1e495ab

Browse files
committed
Merge branch 'develop' into feature/controllers
2 parents 09eec81 + e4cb3da commit 1e495ab

13 files changed

+111
-0
lines changed

core/exceptions/file_not_found.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Core\Exceptions;
3+
4+
class FileNotFound extends \Exception {
5+
public $message;
6+
public function __construct($file) {
7+
$this->message = "File: $file not found";
8+
}
9+
}

core/loader.php

+12
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ public function model($model_name) {
1717

1818
return new $model_name();
1919
}
20+
21+
public function view($view_file, array $params=[]) {
22+
$path_to_view = ENVIRONMENT_VIEWS . "$view_file";
23+
if (!file_exists($path_to_view)) {
24+
throw new \Core\Exceptions\FileNotFound($path_to_view);
25+
}
26+
27+
ob_start();
28+
extract($params);
29+
include($path_to_view);
30+
return ob_get_clean();
31+
}
2032
}

core/views/template.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Template {
4+
5+
}

index.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_once('core/exceptions/method_not_found.php');
66
require_once('core/exceptions/method_not_callable.php');
77
require_once('core/exceptions/no_abstraction_of_base.php');
8+
require_once('core/exceptions/file_not_found.php');
89
require_once('core/Parser.php');
910
require_once('core/loader.php');
1011
require_once('core/input.php');
@@ -14,6 +15,7 @@
1415
// TODO: set ENVIRONMENT constants to either admin or 'frontend'
1516
define('ENVIRONMENT', 'not yet impleneted');
1617
define('ENVIRONMENT_CONTROLLERS', 'controllers/');
18+
define('ENVIRONMENT_VIEWS', 'views/');
1719

1820
//TODO: Add configurable default controller
1921
$controller = isset($_GET['controller']) ? $_GET['controller'] : 'Welcome';

tests/Test.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
require_once('core/exceptions/method_not_found.php');
66
require_once('core/exceptions/method_not_callable.php');
77
require_once('core/exceptions/no_abstraction_of_base.php');
8+
require_once('core/exceptions/file_not_found.php');
89
require_once('core/Parser.php');
910
require_once('core/loader.php');
1011
require_once('core/Input.php');
12+
require_once('core/views/template.php');
1113

1214
define('ENVIRONMENT', 'test');
1315
define('ENVIRONMENT_CONTROLLERS', 'tests/controllers/');
1416
define('ENVIRONMENT_MODELS', 'tests/models/');
17+
define('ENVIRONMENT_VIEWS', 'tests/views/');
18+
1519
foreach (glob('tests/*.php') as $file) {
1620
$filename = pathinfo($file, PATHINFO_FILENAME);
1721
if ($filename == pathinfo(__file__, PATHINFO_FILENAME)) {

tests/exceptions.php

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use \Core\Exceptions\MethodNotFound as MethodNotFound;
44
use \Core\Exceptions\MethodNotCallable as MethodNotCallable;
55
use \Core\Exceptions\NoAbstractionOfBase as NoAbstractionOfBase;
6+
use \Core\Exceptions\FileNotFound as FileNotFound;
67

78
class ExceptionsTest extends PHPUnit_Framework_TestCase {
89
public function test_class_not_found() {
@@ -25,4 +26,9 @@ public function test_no_abstraction_of_base() {
2526
$this->assertEquals($exception->message, 'MyController is no abstraction of base.');
2627
}
2728

29+
public function test_file_not_found() {
30+
$exception = new FileNotFound('view.php');
31+
$this->assertEquals($exception->message, 'File: view.php not found');
32+
}
33+
2834
}

tests/loader.php

+54
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,58 @@ public function test_duplicate_models() {
2828
$first_model->a_very_random_value = 1337;
2929
$this->assertFalse(isset($second_model->a_very_random_value));
3030
}
31+
32+
public function test_load_view() {
33+
$loader = new Loader();
34+
$view_content = file_get_contents('tests/views/myview.php');
35+
$this->assertSame($view_content, $loader->view('myview.php'));
36+
}
37+
38+
public function test_view_doesnt_exists() {
39+
$this->setExpectedException('Core\Exceptions\FileNotFound');
40+
$loader = new Loader();
41+
$loader->view('nonExisting.adadawdakdawd');
42+
}
43+
44+
public function test_view_passing_values() {
45+
$loader = new Loader();
46+
$view_content = $loader->view('multiply.php', ['two' => 2, 'three' => 3]);
47+
$this->assertEquals($view_content, 6);
48+
}
49+
50+
public function test_view_pass_object() {
51+
$my_object = new DummyClass();
52+
$loader = new Loader();
53+
$view_content = $loader->view('object.php', ['object' => $my_object]);
54+
$this->assertEquals($view_content, 10);
55+
}
56+
57+
public function test_view_pass_function() {
58+
$function = function($a, $b) {
59+
return $a + $b;
60+
};
61+
62+
$loader = new Loader();
63+
$view_content = $loader->view('function.php', ['function' => $function]);
64+
$this->assertEquals($view_content, 2);
65+
}
66+
67+
public function test_view_html() {
68+
$loader = new Loader();
69+
$view_content = '<!DOCTYPE html><html><head></head><body>hai</body></html>';
70+
71+
$this->assertEquals($loader->view('normal.html'), $view_content);
72+
}
73+
74+
public function test_view_html_with_params() {
75+
$loader = new Loader();
76+
$view_content = '<!DOCTYPE html><html><head></head><body>1</body></html>';
77+
$this->assertEquals($loader->view('withphp.html', ['i' => 1]), $view_content);
78+
}
3179
}
80+
81+
class DummyClass {
82+
public function sum(array $numbers) {
83+
return array_sum($numbers);
84+
}
85+
}

tests/views/function.php

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
echo $function(1, 1);

tests/views/multiply.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
echo $two * $three;
3+
?>

tests/views/myview.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>haiya</title>
5+
</head>
6+
<body>
7+
content
8+
</body>
9+
</html>

tests/views/normal.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head></head><body>hai</body></html>

tests/views/object.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
echo $object->sum([1, 2, 3, 4]);
3+
?>

tests/views/withphp.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head></head><body><?=$i?></body></html>

0 commit comments

Comments
 (0)