Implemented a few unit tests for the new classes.

This commit is contained in:
Abel Hoogeveen 2016-06-07 15:51:28 +02:00
parent 8298975c98
commit 7aff99c701
4 changed files with 316 additions and 16 deletions

View File

@ -466,14 +466,14 @@ class URI {
$total_segments = "total_{$which}s";
$segment_array = "{$which}_array";
if ($this->total_segments() < $n)
if ($this->$total_segments() < $n)
{
return (count($default) === 0)
? array()
: array_fill_keys($default, NULL);
}
$segments = array_slice($this->segment_array(), ($n - 1));
$segments = array_slice($this->$segment_array(), ($n - 1));
$i = 0;
$lastval = '';
$retval = array();
@ -591,7 +591,7 @@ class URI {
$trailing = '';
}
return $leading.$this->which($n).$trailing;
return $leading.$this->$which($n).$trailing;
}
// --------------------------------------------------------------------
@ -653,17 +653,4 @@ class URI {
{
return $this->uri_string;
}
// --------------------------------------------------------------------
/**
* Fetch Re-routed URI string
*
* @return string
*/
public function ruri_string()
{
return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments);
}
}

100
tests/core_outputTest.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/**
* FuzeWorks.
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
*
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
use FuzeWorks\Factory;
/**
* Class OutputTest.
*
* Core testing suite, will test basic output class functionality
*/
class outputTest extends CoreTestAbstract {
public $output;
protected $_output_data = '';
public function setUp()
{
$this->_output_data =<<<HTML
<html>
<head>
<title>Basic HTML</title>
</head>
<body>
Test
</body>
</html>
HTML;
Factory::getInstance()->config->main->charset = 'UTF-8';
$this->output = Factory::getInstance()->output;
}
// --------------------------------------------------------------------
public function test_set_get_append_output()
{
$append = "<!-- comment /-->\n";
$this->assertEquals(
$this->_output_data.$append,
$this->output
->set_output($this->_output_data)
->append_output("<!-- comment /-->\n")
->get_output()
);
}
// --------------------------------------------------------------------
public function test_get_content_type()
{
$this->assertEquals('text/html', $this->output->get_content_type());
}
// --------------------------------------------------------------------
public function test_get_header()
{
$this->assertNull($this->output->get_header('Non-Existent-Header'));
// TODO: Find a way to test header() values as well. Currently,
// PHPUnit prevents this by not using output buffering.
$this->output->set_content_type('text/plain', 'WINDOWS-1251');
$this->assertEquals(
'text/plain; charset=WINDOWS-1251',
$this->output->get_header('content-type')
);
}
}

182
tests/core_uriTest.php Normal file
View File

@ -0,0 +1,182 @@
<?php
/**
* FuzeWorks.
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
*
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
use FuzeWorks\Factory;
/**
* Class URITest.
*
* Core testing suite, will test URI class functionality
*/
class uriTest extends CoreTestAbstract {
public function setUp()
{
$this->uri = new Mock_Core_URI();
}
// --------------------------------------------------------------------
public function test_filter_uri_passing()
{
$this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-');
$str = 'abc01239~%.:_-';
$this->uri->filter_uri($str);
}
// --------------------------------------------------------------------
/**
* @expectedException FuzeWorks\UriException
*/
public function test_filter_uri_throws_error()
{
$this->uri->config->routing->enable_query_strings = false;
$this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-');
$segment = '$this()'; // filter_uri() accepts by reference
$this->uri->filter_uri($segment);
}
// --------------------------------------------------------------------
public function test_segment()
{
$this->uri->segments = array(1 => 'controller');
$this->assertEquals($this->uri->segment(1), 'controller');
$this->assertEquals($this->uri->segment(2, 'default'), 'default');
}
// --------------------------------------------------------------------
public function test_rsegment()
{
$this->uri->rsegments = array(1 => 'method');
$this->assertEquals($this->uri->rsegment(1), 'method');
$this->assertEquals($this->uri->rsegment(2, 'default'), 'default');
}
// --------------------------------------------------------------------
public function test_uri_to_assoc()
{
$this->uri->segments = array('a', '1', 'b', '2', 'c', '3');
$this->assertEquals(
array('a' => '1', 'b' => '2', 'c' => '3'),
$this->uri->uri_to_assoc(1)
);
$this->assertEquals(
array('b' => '2', 'c' => '3'),
$this->uri->uri_to_assoc(3)
);
$this->uri->keyval = array(); // reset cache
$this->uri->segments = array('a', '1', 'b', '2', 'c');
$this->assertEquals(
array('a' => '1', 'b' => '2', 'c' => FALSE),
$this->uri->uri_to_assoc(1)
);
$this->uri->keyval = array(); // reset cache
$this->uri->segments = array('a', '1');
// test default
$this->assertEquals(
array('a' => '1', 'b' => FALSE),
$this->uri->uri_to_assoc(1, array('a', 'b'))
);
}
// --------------------------------------------------------------------
public function test_ruri_to_assoc()
{
$this->uri->rsegments = array('x', '1', 'y', '2', 'z', '3');
$this->assertEquals(
array('x' => '1', 'y' => '2', 'z' => '3'),
$this->uri->ruri_to_assoc(1)
);
$this->assertEquals(
array('y' => '2', 'z' => '3'),
$this->uri->ruri_to_assoc(3)
);
$this->uri->keyval = array(); // reset cache
$this->uri->rsegments = array('x', '1', 'y', '2', 'z');
$this->assertEquals(
array('x' => '1', 'y' => '2', 'z' => FALSE),
$this->uri->ruri_to_assoc(1)
);
$this->uri->keyval = array(); // reset cache
$this->uri->rsegments = array('x', '1');
// test default
$this->assertEquals(
array('x' => '1', 'y' => FALSE),
$this->uri->ruri_to_assoc(1, array('x', 'y'))
);
}
// --------------------------------------------------------------------
public function test_assoc_to_uri()
{
//$this->uri->config->set_item('uri_string_slashes', 'none');
$this->assertEquals('a/1/b/2', $this->uri->assoc_to_uri(array('a' => '1', 'b' => '2')));
}
// --------------------------------------------------------------------
public function test_slash_segment()
{
$this->uri->segments[1] = 'segment';
$this->uri->rsegments[1] = 'segment';
$this->assertEquals('/segment/', $this->uri->slash_segment(1, 'both'));
$this->assertEquals('/segment/', $this->uri->slash_rsegment(1, 'both'));
$a = '/segment';
$this->assertEquals('/segment', $this->uri->slash_segment(1, 'leading'));
$this->assertEquals('/segment', $this->uri->slash_rsegment(1, 'leading'));
$this->assertEquals('segment/', $this->uri->slash_segment(1, 'trailing'));
$this->assertEquals('segment/', $this->uri->slash_rsegment(1, 'trailing'));
}
}

31
tests/mocks/core/uri.php Normal file
View File

@ -0,0 +1,31 @@
<?php
use FuzeWorks\Uri;
use FuzeWorks\Factory;
class Mock_Core_URI extends Uri {
public function __construct()
{
$this->config = Factory::getInstance()->config;
// set predictable config values
$this->config->main->index_page = 'index.php';
$this->config->main->base_url = 'http://example.com/';
$this->config->main->application_prefix = 'MY_';
$this->config->routing->enable_query_strings = false;
$this->config->routing->permitted_uri_chars = 'a-z 0-9~%.:_\-';
if ($this->config->routing->enable_query_strings !== TRUE OR is_cli())
{
$this->_permitted_uri_chars = $this->config->routing->permitted_uri_chars;
}
}
public function _set_permitted_uri_chars($value)
{
$this->_permitted_uri_chars = $value;
}
}