Updated to KH 3.3 and improved

This commit is contained in:
Deon George
2013-04-13 16:17:56 +10:00
parent 6f50463ec7
commit 6f7913d363
1551 changed files with 96188 additions and 29813 deletions

View File

@@ -4,13 +4,14 @@
* Tests the Arr lib that's shipped with kohana
*
* @group kohana
* @group kohana.arr
* @group kohana.core
* @group kohana.core.arr
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ArrTest extends Unittest_TestCase
@@ -76,6 +77,36 @@ class Kohana_ArrTest extends Unittest_TestCase
'not in stock',
array('carrot cake' => 'in stock', 'humble pie' => 'not in stock'),
),
array(
// Source Array
array('level1' => array('level2a' => 'value 1', 'level2b' => 'value 2')),
// Paths to extract
array('level1.level2a', 'level1.level2b'),
// Default
NULL,
// Expected Result
array('level1' => array('level2a' => 'value 1', 'level2b' => 'value 2')),
),
array(
// Source Array
array('level1a' => array('level2a' => 'value 1'), 'level1b' => array('level2b' => 'value 2')),
// Paths to extract
array('level1a', 'level1b.level2b'),
// Default
NULL,
// Expected Result
array('level1a' => array('level2a' => 'value 1'), 'level1b' => array('level2b' => 'value 2')),
),
array(
// Source Array
array('level1a' => array('level2a' => 'value 1'), 'level1b' => array('level2b' => 'value 2')),
// Paths to extract
array('level1a', 'level1b.level2b', 'level1c', 'level1d.notfound'),
// Default
'default',
// Expected Result
array('level1a' => array('level2a' => 'value 1'), 'level1b' => array('level2b' => 'value 2'), 'level1c' => 'default', 'level1d' => array('notfound' => 'default')),
),
);
}
@@ -85,13 +116,13 @@ class Kohana_ArrTest extends Unittest_TestCase
* @test
* @dataProvider provider_extract
* @param array $array
* @param array $keys
* @param array $paths
* @param mixed $default
* @param array $expected
*/
public function test_extract(array $array, array $keys, $default, $expected)
public function test_extract(array $array, array $paths, $default, $expected)
{
$array = Arr::extract($array, $keys, $default);
$array = Arr::extract($array, $paths, $default);
$this->assertSame(count($expected), count($array));
$this->assertSame($expected, $array);
@@ -242,14 +273,19 @@ class Kohana_ArrTest extends Unittest_TestCase
),
// See how it merges sub-arrays with numerical indexes
array(
array(array('test1','test3'), array('test2','test4')),
array(array('test1'), array('test2'), array('test3')),
array(array('test1'), array('test2')),
array(array('test3'), array('test4')),
array(array('test2'), array('test3')),
),
array(
array(array('test1','test3'), array('test2','test4')),
array(array('test1'), array('test2')),
array(array('test3'), array('test4')),
array(array(array('test1')), array(array('test2')), array(array('test3'))),
array(array(array('test1')), array(array('test2'))),
array(array(array('test2')), array(array('test3'))),
),
array(
array('a' => array('test1','test2'), 'b' => array('test2','test3')),
array('a' => array('test1'), 'b' => array('test2')),
array('a' => array('test2'), 'b' => array('test3')),
),
array(
array('digits' => array(0, 1, 2, 3)),
@@ -279,6 +315,73 @@ class Kohana_ArrTest extends Unittest_TestCase
array('foo' => array('bar')),
array('foo' => 'bar'),
),
// data set #9
// Associative, Associative
array(
array('a' => 'K', 'b' => 'K', 'c' => 'L'),
array('a' => 'J', 'b' => 'K'),
array('a' => 'K', 'c' => 'L'),
),
// Associative, Indexed
array(
array('a' => 'J', 'b' => 'K', 'L'),
array('a' => 'J', 'b' => 'K'),
array('K', 'L'),
),
// Associative, Mixed
array(
array('a' => 'J', 'b' => 'K', 'K', 'c' => 'L'),
array('a' => 'J', 'b' => 'K'),
array('K', 'c' => 'L'),
),
// data set #12
// Indexed, Associative
array(
array('J', 'K', 'a' => 'K', 'c' => 'L'),
array('J', 'K'),
array('a' => 'K', 'c' => 'L'),
),
// Indexed, Indexed
array(
array('J', 'K', 'L'),
array('J', 'K'),
array('K', 'L'),
),
// Indexed, Mixed
array(
array('K', 'K', 'c' => 'L'),
array('J', 'K'),
array('K', 'c' => 'L'),
),
// data set #15
// Mixed, Associative
array(
array('a' => 'K', 'K', 'c' => 'L'),
array('a' => 'J', 'K'),
array('a' => 'K', 'c' => 'L'),
),
// Mixed, Indexed
array(
array('a' => 'J', 'K', 'L'),
array('a' => 'J', 'K'),
array('J', 'L'),
),
// Mixed, Mixed
array(
array('a' => 'K', 'L'),
array('a' => 'J', 'K'),
array('a' => 'K', 'L'),
),
// Bug #3141
array(
array('servers' => array(array('1.1.1.1', 4730), array('2.2.2.2', 4730))),
array('servers' => array(array('1.1.1.1', 4730))),
array('servers' => array(array('2.2.2.2', 4730))),
),
);
}
@@ -422,11 +525,11 @@ class Kohana_ArrTest extends Unittest_TestCase
{
$range = Arr::range($step, $max);
$this->assertSame((int) floor($max / $step), count($range));
$this->assertSame( (int) floor($max / $step), count($range));
$current = $step;
foreach($range as $key => $value)
foreach ($range as $key => $value)
{
$this->assertSame($key, $value);
$this->assertSame($current, $key);
@@ -509,8 +612,47 @@ class Kohana_ArrTest extends Unittest_TestCase
public function provider_map()
{
return array(
array('strip_tags', array('<p>foobar</p>'), array('foobar')),
array('strip_tags', array(array('<p>foobar</p>'), array('<p>foobar</p>')), array(array('foobar'), array('foobar'))),
array('strip_tags', array('<p>foobar</p>'), NULL, array('foobar')),
array('strip_tags', array(array('<p>foobar</p>'), array('<p>foobar</p>')), NULL, array(array('foobar'), array('foobar'))),
array(
'strip_tags',
array(
'foo' => '<p>foobar</p>',
'bar' => '<p>foobar</p>',
),
NULL,
array(
'foo' => 'foobar',
'bar' => 'foobar',
),
),
array(
'strip_tags',
array(
'foo' => '<p>foobar</p>',
'bar' => '<p>foobar</p>',
),
array('foo'),
array(
'foo' => 'foobar',
'bar' => '<p>foobar</p>',
),
),
array(
array(
'strip_tags',
'trim',
),
array(
'foo' => '<p>foobar </p>',
'bar' => '<p>foobar</p>',
),
NULL,
array(
'foo' => 'foobar',
'bar' => 'foobar',
),
),
);
}
@@ -519,11 +661,11 @@ class Kohana_ArrTest extends Unittest_TestCase
* @test
* @dataProvider provider_map
*/
public function test_map($method, $source, $expected)
public function test_map($method, $source, $keys, $expected)
{
$this->assertSame(
$expected,
Arr::map($method, $source)
Arr::map($method, $source, $keys)
);
}

View File

@@ -1,168 +0,0 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for CLI
*
* Based on the Kohana-unittest test
*
* @group kohana
* @group kohana.cli
*
* @see CLI
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_CLITest extends Unittest_TestCase
{
/**
* Tell PHPUnit to isolate globals during tests
* @var boolean
*/
protected $backupGlobals = TRUE;
/**
* An array of arguments to put in $_SERVER['argv']
* @var array
*/
protected $options = array(
'--uri' => 'test/something',
'--we_are_cool',
'invalid option',
'--version' => '2.23',
'--important' => 'something=true',
'--name' => 'Jeremy Taylor',
);
/**
* Setup the enviroment for each test
*
* PHPUnit automatically backups up & restores global variables
*/
public function setUp()
{
parent::setUp();
$_SERVER['argv'] = array('index.php');
foreach($this->options as $option => $value)
{
if(is_string($option))
{
$_SERVER['argv'][] = $option.'='.$value;
}
else
{
$_SERVER['argv'][] = $value;
}
}
$_SERVER['argc'] = count($_SERVER['argv']);
}
/**
* If for some reason arc != count(argv) then we need
* to fail gracefully.
*
* This test ensures it will
*
* @test
*/
public function test_only_loops_over_available_arguments()
{
++$_SERVER['argc'];
$options = CLI::options('uri');
$this->assertSame(1, count($options));
}
/**
* Options should only parse arguments requested
*
* @test
*/
public function test_only_parses_wanted_arguments()
{
$options = CLI::options('uri');
$this->assertSame(1, count($options));
$this->assertArrayHasKey('uri', $options);
$this->assertSame($options['uri'], $this->options['--uri']);
}
/**
* Options should not parse invalid arguments (i.e. not starting with --_
*
* @test
*/
public function test_does_not_parse_invalid_arguments()
{
$options = CLI::options('uri', 'invalid');
$this->assertSame(1, count($options));
$this->assertArrayHasKey('uri', $options);
$this->assertArrayNotHasKey('invalid', $options);
}
/**
* Options should parse multiple arguments & values correctly
*
* @test
*/
public function test_parses_multiple_arguments()
{
$options = CLI::options('uri', 'version');
$this->assertSame(2, count($options));
$this->assertArrayHasKey('uri', $options);
$this->assertArrayHasKey('version', $options);
$this->assertSame($this->options['--uri'], $options['uri']);
$this->assertSame($this->options['--version'], $options['version']);
}
/**
* Options should parse arguments without specified values as NULL
*
* @test
*/
public function test_parses_arguments_without_value_as_null()
{
$options = CLI::options('uri', 'we_are_cool');
$this->assertSame(2, count($options));
$this->assertSame(NULL, $options['we_are_cool']);
}
/**
* If the argument contains an equals sign then it shouldn't be split
*
* @test
* @ticket 2642
*/
public function test_cli_only_splits_on_the_first_equals()
{
$options = CLI::options('important');
$this->assertSame(1, count($options));
$this->assertSame('something=true', reset($options));
}
/**
* Arguments enclosed with quote marks should be allowed to contain
* spaces
*
* @test
*/
public function test_value_includes_spaces_when_enclosed_with_quotes()
{
$options = CLI::options('name');
$this->assertSame(array('name' => 'Jeremy Taylor'), $options);
}
}

View File

@@ -0,0 +1,94 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests the Config file reader that's shipped with kohana
*
* @group kohana
* @group kohana.config
*
* @package Unittest
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @author Matt Button <matthew@sigswitch.com>
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Config_File_ReaderTest extends Kohana_Unittest_TestCase {
/**
* If we don't pass a directory to the reader then it should assume
* that we want to search the dir 'config' by default
*
* @test
* @covers Kohana_Config_File_Reader
*/
public function test_default_search_dir_is_config()
{
$reader = new Kohana_Config_File_Reader;
$this->assertAttributeSame('config', '_directory', $reader);
}
/**
* If we pass a directory to the constructor of the file reader it
* should change the search directory
*
* @test
* @covers Kohana_Config_File_Reader
*/
public function test_constructor_sets_search_dir_from_param()
{
$reader = new Kohana_Config_File_Reader('gafloog');
$this->assertAttributeSame('gafloog', '_directory', $reader);
}
/**
* If the config dir does not exist then the function should just
* return an empty array
*
* @test
* @covers Kohana_Config_File_Reader::load
*/
public function test_load_returns_empty_array_if_conf_dir_dnx()
{
$config = new Kohana_Config_File_Reader('gafloogle');
$this->assertSame(array(), $config->load('values'));
}
/**
* If the requested config group does not exist then the reader
* should return an empty array
*
* @test
* @covers Kohana_Config_File_Reader::load
*/
public function test_load_returns_empty_array_if_conf_dnx()
{
$config = new Kohana_Config_File_Reader;
$this->assertSame(array(), $config->load('gafloogle'));
}
/**
* Test that the load() function is actually loading the
* configuration from the files.
*
* @test
* @covers Kohana_Config_File_Reader::load
*/
public function test_loads_config_from_files()
{
$config = new Kohana_Config_File_Reader;
$values = $config->load('inflector');
// Due to the way the cascading filesystem works there could be
// any number of modifications to the system config in the
// actual output. Therefore to increase compatability we just
// check that we've got an array and that it's not empty
$this->assertNotSame(array(), $values);
$this->assertInternalType('array', $values);
}
}

View File

@@ -0,0 +1,192 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests the Config group lib
*
* @group kohana
* @group kohana.config
*
* @package Unittest
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @author Matt Button <matthew@sigswitch.com>
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Config_GroupTest extends Kohana_Unittest_TestCase
{
/**
* Create a mock Kohana_Config instance
*
* @return Kohana_Config
*/
public function get_mock_config()
{
return new Kohana_Config;
}
/**
* Gets a fresh instance of Kohana_Config_Group
*
* @param string $group Config Group name
* @param array $config Configuration
* @param Kohana_Config $instance Instance of Kohana_Config
* @return Kohana_Config_Group
*/
public function get_mock_group($group, $config = array(), $instance = NULL)
{
if ($instance === NULL)
{
$instance = $this->get_mock_config();
}
return new Kohana_Config_Group($instance, $group, $config);
}
/**
* The group name and group's config values should be loaded into the object
* by the constructor
*
* @test
* @covers Kohana_Config_Group
*/
public function test_loads_group_name_and_values_in_constructor()
{
$group_name = 'information';
$group_values = array('var' => 'value');
$group = $this->get_mock_group($group_name, $group_values);
// Now usually we'd just use assertAttributeSame, but that tries to get at protected properties
// by casting the object in question into an array. This usually works fine, but as Kohana_Config_Group
// is a subclass of ArrayObject, casting to an array returns the config items!
// Therefore we have to use this little workaround
$this->assertSame($group_name, $group->group_name());
$this->assertSame($group_values, $group->getArrayCopy());
}
/**
* A config group may not exist (or may not have any values) when it is loaded.
* The config group should allow for this situation and not complain
*
* @test
* @covers Kohana_Config_Group
*/
public function test_allows_empty_group_values()
{
$group = $this->get_mock_group('informatica');
$this->assertSame(array(), $group->getArrayCopy());
}
/**
* When get() is called it should fetch the config value specified
*
* @test
* @covers Kohana_Config_Group::get
*/
public function test_get_fetches_config_value()
{
$group = $this->get_mock_group('kohana', array('status' => 'awesome'));
$this->assertSame('awesome', $group->get('status'));
}
/**
* If a config option does not exist then get() should return the default value, which is
* NULL by default
*
* @test
* @covers Kohana_Config_Group::get
*/
public function test_get_returns_default_value_if_config_option_dnx()
{
$group = $this->get_mock_group('kohana');
$this->assertSame(NULL, $group->get('problems', NULL));
$this->assertSame('nada', $group->get('problems', 'nada'));
}
/**
* We should be able to modify existing configuration items using set()
*
* @test
* @covers Kohana_Config_Group::set
*/
public function test_set_modifies_existing_config()
{
$group = $this->get_mock_group('kohana', array('status' => 'pre-awesome'));
$group->set('status', 'awesome');
$this->assertSame('awesome', $group->get('status'));
}
/**
* If we modify the config via set() [$var] or ->$var then the change should be passed to
* the parent config instance so that the config writers can be notified.
*
* The modification to the config should also stick
*
* @test
* @covers Kohana_Config_Group::offsetSet
*/
public function test_writes_changes_to_config()
{
$mock = $this->getMock('Kohana_Config', array('_write_config'));
$mock
->expects($this->exactly(3))
->method('_write_config')
->with('kohana', 'status', $this->LogicalOr('totally', 'maybe', 'not'));
$group = $this->get_mock_group('kohana', array('status' => 'kool'), $mock);
$group['status'] = 'totally';
$group->status = 'maybe';
$group->set('status', 'not');
}
/**
* Calling as_array() should return the full array, inc. any modifications
*
* @test
* @covers Kohana_Config_Group::as_array
*/
public function test_as_array_returns_full_array()
{
$config = $this->get_mock_group('something', array('var' => 'value'));
$this->assertSame(array('var' => 'value'), $config->as_array());
// Now change some vars **ahem**
$config->var = 'LOLCAT';
$config->lolcat = 'IN UR CODE';
$this->assertSame(
array('var' => 'LOLCAT', 'lolcat' => 'IN UR CODE'),
$config->as_array()
);
// And if we remove an item it should be removed from the exported array
unset($config['lolcat']);
$this->assertSame(array('var' => 'LOLCAT'), $config->as_array());
}
/**
* Casting the object to a string should serialize the output of as_array
*
* @test
* @covers Kohana_Config_Group::__toString
*/
public function test_to_string_serializes_array_output()
{
$vars = array('kohana' => 'cool', 'unit_tests' => 'boring');
$config = $this->get_mock_group('hehehe', $vars);
$this->assertSame(serialize($vars), (string) $config);
}
}

View File

@@ -4,32 +4,20 @@
* Tests the Config lib that's shipped with kohana
*
* @group kohana
* @group kohana.config
* @group kohana.core
* @group kohana.core.config
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @author Matt Button <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ConfigTest extends Unittest_TestCase
{
/**
* Calling Config::instance() should return the global singleton
* which should persist
*
* @test
* @covers Config::instance
*/
public function test_instance_returns_singleton_instance()
{
$this->assertSame(Config::instance(), Config::instance());
$this->assertNotSame(new Config, Config::instance());
}
/**
* When a config object is initially created there should be
* no readers attached
@@ -37,11 +25,11 @@ class Kohana_ConfigTest extends Unittest_TestCase
* @test
* @covers Config
*/
public function test_initially_there_are_no_readers()
public function test_initially_there_are_no_sources()
{
$config = new Config;
$this->assertAttributeSame(array(), '_readers', $config);
$this->assertAttributeSame(array(), '_sources', $config);
}
/**
@@ -54,11 +42,11 @@ class Kohana_ConfigTest extends Unittest_TestCase
public function test_attach_adds_reader_and_returns_this()
{
$config = new Config;
$reader = $this->getMock('Config_Reader');
$reader = $this->getMock('Kohana_Config_Reader');
$this->assertSame($config, $config->attach($reader));
$this->assertAttributeContains($reader, '_readers', $config);
$this->assertAttributeContains($reader, '_sources', $config);
}
/**
@@ -72,15 +60,15 @@ class Kohana_ConfigTest extends Unittest_TestCase
{
$config = new Config;
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader');
$reader1 = $this->getMock('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader');
$config->attach($reader1);
$config->attach($reader2);
// Rather than do two assertContains we'll do an assertSame to assert
// the order of the readers
$this->assertAttributeSame(array($reader2, $reader1), '_readers', $config);
$this->assertAttributeSame(array($reader2, $reader1), '_sources', $config);
// Now we test using the second parameter
$config = new Config;
@@ -88,7 +76,7 @@ class Kohana_ConfigTest extends Unittest_TestCase
$config->attach($reader1);
$config->attach($reader2, TRUE);
$this->assertAttributeSame(array($reader2, $reader1), '_readers', $config);
$this->assertAttributeSame(array($reader2, $reader1), '_sources', $config);
}
/**
@@ -101,13 +89,13 @@ class Kohana_ConfigTest extends Unittest_TestCase
public function test_attach_can_add_reader_to_end_of_queue()
{
$config = new Config;
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader');
$reader1 = $this->getMock('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader');
$config->attach($reader1);
$config->attach($reader2, FALSE);
$this->assertAttributeSame(array($reader1, $reader2), '_readers', $config);
$this->assertAttributeSame(array($reader1, $reader2), '_sources', $config);
}
/**
@@ -124,20 +112,20 @@ class Kohana_ConfigTest extends Unittest_TestCase
// that has already been used then it just re-uses the first's name
// To get around this we have to specify a totally random name for the second mock object
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader', array(), array(), 'MY_AWESOME_READER');
$reader1 = $this->getMock('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader', array(), array(), 'MY_AWESOME_READER');
$config->attach($reader1);
$config->attach($reader2);
$this->assertSame($config, $config->detach($reader1));
$this->assertAttributeNotContains($reader1, '_readers', $config);
$this->assertAttributeContains($reader2, '_readers', $config);
$this->assertAttributeNotContains($reader1, '_sources', $config);
$this->assertAttributeContains($reader2, '_sources', $config);
$this->assertSame($config, $config->detach($reader2));
$this->assertAttributeNotContains($reader2, '_readers', $config);
$this->assertAttributeNotContains($reader2, '_sources', $config);
}
/**
@@ -149,11 +137,62 @@ class Kohana_ConfigTest extends Unittest_TestCase
public function test_detach_returns_this_even_when_reader_dnx()
{
$config = new Config;
$reader = $this->getMock('Config_Reader');
$reader = $this->getMock('Kohana_Config_Reader');
$this->assertSame($config, $config->detach($reader));
}
/**
* If we request a config variable with a dot path then
* Config::load() should load the group and return the requested variable
*
* @test
* @covers Config::load
*/
public function test_load_can_get_var_from_dot_path()
{
$config = new Config;
$reader = $this->getMock('Kohana_Config_Reader', array('load'));
$reader
->expects($this->once())
->method('load')
->with('beer')
->will($this->returnValue(array('stout' => 'Guinness')));
$config->attach($reader);
$this->assertSame('Guinness', $config->load('beer.stout'));
}
/**
* If we've already loaded a config group then the correct variable
* should be returned if we use the dot path notation to to request
* a var
*
* @test
* @covers Config::load
*/
public function test_load_can_get_var_from_dot_path_for_loaded_group()
{
$config = new Config;
$reader = $this->getMock('Kohana_Config_Reader', array('load'));
$reader
->expects($this->once())
->method('load')
->with('beer')
->will($this->returnValue(array('stout' => 'Guinness')));
$config->attach($reader);
$config->load('beer');
$this->assertSame('Guinness', $config->load('beer.stout'));
}
/**
* If load() is called and there are no readers present then it should throw
* a kohana exception
@@ -162,7 +201,7 @@ class Kohana_ConfigTest extends Unittest_TestCase
* @covers Config::load
* @expectedException Kohana_Exception
*/
public function test_load_throws_exception_if_there_are_no_readers()
public function test_load_throws_exception_if_there_are_no_sources()
{
// The following code should throw an exception and phpunit will catch / handle it
// (see the @expectedException doccomment)
@@ -172,74 +211,196 @@ class Kohana_ConfigTest extends Unittest_TestCase
}
/**
* When load() is called it should interrogate each reader in turn until a match
* is found
* Provides test data for test_load_throws_exception_if_no_group_is_given()
*
* @test
* @covers Config::load
* @return array
*/
public function test_load_interrogates_each_reader_until_group_found()
public function provider_load_throws_exception_if_no_group_is_given()
{
$config = new Config;
$config_group = 'groupy';
$reader1 = $this->getMock('Config_Reader', array('load'));
$reader1
->expects($this->once())
->method('load')
->with($config_group)
->will($this->returnValue(FALSE));
$reader2 = $this->getMock('Config_Reader', array('load'));
$reader2
->expects($this->once())
->method('load')
->with($config_group)
->will($this->returnValue($reader2));
$reader3 = $this->getMock('Config_Reader', array('load'));
$reader3->expects($this->never())->method('load');
$config->attach($reader1, FALSE);
$config->attach($reader2, FALSE);
$config->attach($reader3, FALSE);
// By asserting a return type we're making the test a little less brittle / less likely
// to break due to minor modifications
$this->assertInstanceOf('Config_Reader', $config->load($config_group));
return array(
array(NULL),
array(''),
array(array()),
array(array('foo' => 'bar')),
array(new StdClass),
);
}
/**
* Calling load() with a group that doesn't exist, should get it to use the last reader
* to create a new config group
* If an invalid group name is specified then an exception should be thrown.
*
* Invalid means it's either a non-string value, or empty
*
* @test
* @dataProvider provider_load_throws_exception_if_no_group_is_given
* @covers Config::load
* @expectedException Kohana_Exception
*/
public function test_load_throws_exception_if_invalid_group($value)
{
$config = new Kohana_Config;
$reader = $this->getMock('Kohana_Config_Reader');
$config->attach($reader);
$config->load($value);
}
/**
* Make sure that _write_config() passes the changed configuration to all
* writers in the queue
*
* @test
* @covers Kohana_Config
*/
public function test_write_config_passes_changed_config_to_all_writers()
{
$config = new Kohana_Config;
$reader1 = $this->getMock('Kohana_Config_Reader');
$writer1 = $this->getMock('Kohana_Config_Writer', array('write'));
$writer2 = $this->getMock('Kohana_Config_Writer', array('write'));
$writer1
->expects($this->once())
->method('write')
->with('some_group', 'key', 'value');
$writer2
->expects($this->once())
->method('write')
->with('some_group', 'key', 'value');
$config->attach($reader1)->attach($writer1)->attach($writer2);
$config->_write_config('some_group', 'key', 'value');
}
/**
* Config sources are stored in a stack, make sure that config at the bottom
* of the stack is overriden by config at the top
*
* @test
* @covers Config::load
*/
public function test_load_returns_new_config_group_if_one_dnx()
public function test_config_is_loaded_from_top_to_bottom_of_stack()
{
$config = new Config;
$group = 'my_group';
$group_name = 'lolumns';
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader', array('load'), array(), 'Config_Waffles');
$reader1 = $this->getMock('Kohana_Config_Reader', array('load'), array(), 'Unittest_Config_Reader_1');
$reader2 = $this->getMock('Kohana_Config_Reader', array('load'), array(), 'Unittest_Config_Reader_2');
// This is a slightly hacky way of doing it, but it works
$reader2
->expects($this->exactly(2))
$reader1
->expects($this->once())
->method('load')
->with($group)
->will($this->onConsecutiveCalls(
$this->returnValue(FALSE),
$this->returnValue(clone $reader2)
));
->with($group_name)
->will($this->returnValue(array('foo' => 'bar', 'kohana' => 'awesome', 'life' => array('normal', 'fated'))));
$config->attach($reader1)->attach($reader2);
$reader2
->expects($this->once())
->method('load')
->with($group_name)
->will($this->returnValue(array('kohana' => 'sweet', 'music' => 'tasteful', 'life' => array('extraordinary', 'destined'))));
$new_config = $config->load('my_group');
$config = new Kohana_Config;
$this->assertInstanceOf('Config_Waffles', $new_config);
// Slightly taboo, testing a different api!!
$this->assertSame(array(), $new_config->as_array());
// Attach $reader1 at the "top" and reader2 at the "bottom"
$config->attach($reader1)->attach($reader2, FALSE);
$this->assertSame(
array(
'kohana' => 'awesome',
'music' => 'tasteful',
'life' => array(
'extraordinary',
'destined',
'normal',
'fated',
),
'foo' => 'bar',
),
$config->load($group_name)->as_array()
);
}
/**
* load() should keep a record of what config groups have been requested and if
* a group is requested more than once the first instance should be returned
*
* @test
* @covers Config::load
*/
public function test_load_reuses_config_groups()
{
$reader = $this->getMock('Kohana_Config_Reader', array('load'));
$reader
->expects($this->once())
->method('load')
->with('something')
->will($this->returnValue(array()));
$config = new Kohana_Config;
$config->attach($reader);
$group = $config->load('something');
$this->assertSame($group, $config->load('something'));
}
/**
* When we call copy() we expect it to copy the merged config to all writers
*
* @TODO This test sucks due to limitations in the phpunit mock generator. MAKE THIS AWESOME AGAIN!
* @test
* @covers Kohana_Config::copy
*/
public function test_copy_copies_merged_config_to_all_writers()
{
$config = new Kohana_Config;
$reader1 = $this->getMock('Kohana_Config_Reader', array('load'));
$reader2 = $this->getMock('Kohana_Config_Reader', array('load'));
$reader1
->expects($this->once())
->method('load')
->with('something')
->will($this->returnValue(array('pie' => 'good', 'kohana' => 'awesome')));
$reader2
->expects($this->once())
->method('load')
->with('something')
->will($this->returnValue(array('kohana' => 'good')));
$writer1 = $this->getMock('Kohana_Config_Writer', array('write'));
$writer2 = $this->getMock('Kohana_Config_Writer', array('write'));
// Due to crazy limitations in phpunit's mocking engine we have to be fairly
// liberal here as to what order we receive the config items
// Good news is that order shouldn't matter *yay*
//
// Now save your eyes and skip the next... 13 lines!
$key = $this->logicalOr('pie', 'kohana');
$val = $this->logicalOr('good', 'awesome');
$writer1
->expects($this->exactly(2))
->method('write')
->with('something', clone $key, clone $val);
$writer2
->expects($this->exactly(2))
->method('write')
->with('something', clone $key, clone $val);
$config
->attach($reader1)->attach($reader2, FALSE)
->attach($writer1)->attach($writer2);
// Now let's get this thing going!
$config->copy('something');
}
}

View File

@@ -4,13 +4,14 @@
* Tests the cookie class
*
* @group kohana
* @group kohana.cookie
* @group kohana.core
* @group kohana.core.cookie
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_CookieTest extends Unittest_TestCase
@@ -20,7 +21,9 @@ class Kohana_CookieTest extends Unittest_TestCase
/**
* Sets up the environment
*/
// @codingStandardsIgnoreStart
public function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
@@ -30,7 +33,9 @@ class Kohana_CookieTest extends Unittest_TestCase
/**
* Tears down the environment
*/
// @codingStandardsIgnoreStart
public function tearDown()
// @codingStandardsIgnoreEnd
{
parent::tearDown();
@@ -63,6 +68,10 @@ class Kohana_CookieTest extends Unittest_TestCase
*/
public function test_set($key, $value, $exp, $expected)
{
if (headers_sent()) {
$this->markTestSkipped('Cannot test setting cookies as headers have already been sent');
}
$this->assertSame($expected, cookie::set($key, $value, $exp));
}
@@ -96,6 +105,10 @@ class Kohana_CookieTest extends Unittest_TestCase
*/
public function test_get($key, $value, $expected)
{
if (headers_sent()) {
$this->markTestSkipped('Cannot test setting cookies as headers have already been sent');
}
// Force $_COOKIE
if ($key !== NULL)
{
@@ -128,6 +141,10 @@ class Kohana_CookieTest extends Unittest_TestCase
*/
public function test_delete($key, $expected)
{
if (headers_sent()) {
$this->markTestSkipped('Cannot test setting cookies as headers have already been sent');
}
$this->assertSame($expected, cookie::delete($key));
}

View File

@@ -7,12 +7,13 @@
*
* @group kohana
* @group kohana.core
* @group kohana.core.core
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_CoreTest extends Unittest_TestCase
@@ -60,7 +61,7 @@ class Kohana_CoreTest extends Unittest_TestCase
public function test_find_file_no_extension()
{
// EXT is manually appened to the _file name_, not passed as the extension
$path = Kohana::find_file('classes', $file = 'kohana/core'.EXT, FALSE);
$path = Kohana::find_file('classes', $file = 'Kohana/Core'.EXT, FALSE);
$this->assertInternalType('string', $path);
@@ -197,6 +198,7 @@ class Kohana_CoreTest extends Unittest_TestCase
*/
public function test_message($expected, $file, $key)
{
$this->markTestSkipped('This test is incredibly fragile and needs to be re-done');
$this->assertEquals($expected, Kohana::message($file, $key));
}
@@ -239,6 +241,48 @@ class Kohana_CoreTest extends Unittest_TestCase
error_reporting($error_level);
}
/**
* Provides test data for test_modules_sets_and_returns_valid_modules()
*
* @return array
*/
public function provider_modules_detects_invalid_modules()
{
return array(
array(array('unittest' => MODPATH.'fo0bar')),
array(array('unittest' => SMDPATH.'unittest', 'fo0bar' => MODPATH.'fo0bar')),
);
}
/**
* Tests Kohana::modules()
*
* @test
* @dataProvider provider_modules_detects_invalid_modules
* @expectedException Kohana_Exception
* @param boolean $source Input for Kohana::modules
*
*/
public function test_modules_detects_invalid_modules($source)
{
$modules = Kohana::modules();
try
{
Kohana::modules($source);
}
catch(Exception $e)
{
// Restore modules
Kohana::modules($modules);
throw $e;
}
// Restore modules
Kohana::modules($modules);
}
/**
* Provides test data for test_modules_sets_and_returns_valid_modules()
*
@@ -248,8 +292,7 @@ class Kohana_CoreTest extends Unittest_TestCase
{
return array(
array(array(), array()),
array(array('unittest' => MODPATH.'fo0bar'), array()),
array(array('unittest' => MODPATH.'unittest'), array('unittest' => $this->dirSeparator(MODPATH.'unittest/'))),
array(array('unittest' => SMDPATH.'unittest'), array('unittest' => $this->dirSeparator(SMDPATH.'unittest/'))),
);
}

View File

@@ -4,13 +4,14 @@
* Tests Date class
*
* @group kohana
* @group kohana.date
* @group kohana.core
* @group kohana.core.date
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_DateTest extends Unittest_TestCase
@@ -20,7 +21,9 @@ class Kohana_DateTest extends Unittest_TestCase
/**
* Ensures we have a consistant timezone for testing.
*/
// @codingStandardsIgnoreStart
public function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
@@ -32,7 +35,9 @@ class Kohana_DateTest extends Unittest_TestCase
/**
* Restores original timezone after testing.
*/
// @codingStandardsIgnoreStart
public function tearDown()
// @codingStandardsIgnoreEnd
{
date_default_timezone_set($this->_original_timezone);
@@ -248,6 +253,10 @@ class Kohana_DateTest extends Unittest_TestCase
// Now we use our own format
// Binary date!
array('01/01/2010 01:00', '1AM 1st January 2010', 'd/m/Y H:i'),
// Timezones (see #3902)
array('2011-04-01 01:23:45 Antarctica/South_Pole', '2011-04-01 01:23:45', 'Y-m-d H:i:s e', 'Antarctica/South_Pole'),
array('2011-04-01 01:23:45 Antarctica/South_Pole', '2011-03-31 14:23:45 Europe/Paris', 'Y-m-d H:i:s e', 'Antarctica/South_Pole'),
array('2011-04-01 01:23:45 Antarctica/South_Pole', '@1301574225', 'Y-m-d H:i:s e', 'Antarctica/South_Pole'),
);
}
@@ -257,14 +266,15 @@ class Kohana_DateTest extends Unittest_TestCase
* @test
* @dataProvider provider_formatted_time
* @covers Date::formatted_time
* @ticket 3035
* @ticket 3035 3902
* @param string $expected Expected output
* @param string|integer $datetime_str The datetime timestamp / string
* @param string|null $timestamp_format The output format
* @param string|null $timezone The timezone identifier
*/
public function test_formatted_time($expected, $datetime_str, $timestamp_format = NULL)
public function test_formatted_time($expected, $datetime_str, $timestamp_format = NULL, $timezone = NULL)
{
$timestamp = Date::formatted_time($datetime_str, $timestamp_format);
$timestamp = Date::formatted_time($datetime_str, $timestamp_format, $timezone);
$this->assertSame($expected, $timestamp);
}

View File

@@ -6,13 +6,14 @@
* @TODO Use a virtual filesystem (see phpunit doc on mocking fs) for find_file etc.
*
* @group kohana
* @group kohana.debug
* @group kohana.core
* @group kohana.core.debug
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_DebugTest extends Unittest_TestCase
@@ -26,7 +27,7 @@ class Kohana_DebugTest extends Unittest_TestCase
public function provider_vars()
{
return array(
// $exception_type, $message, $is_cli, $expected
// $thing, $expected
array(array('foobar'), "<pre class=\"debug\"><small>array</small><span>(1)</span> <span>(\n 0 => <small>string</small><span>(6)</span> \"foobar\"\n)</span></pre>"),
);
}
@@ -58,8 +59,8 @@ class Kohana_DebugTest extends Unittest_TestCase
'SYSPATH'.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.'kohana.php'
),
array(
MODPATH.$this->dirSeparator('unittest/classes/kohana/unittest/runner').EXT,
$this->dirSeparator('MODPATH/unittest/classes/kohana/unittest/runner').EXT
SMDPATH.$this->dirSeparator('unittest/classes/kohana/unittest/runner').EXT,
$this->dirSeparator('SMDPATH/unittest/classes/kohana/unittest/runner').EXT
),
);
}
@@ -86,13 +87,25 @@ class Kohana_DebugTest extends Unittest_TestCase
public function provider_dump()
{
return array(
array('foobar', 128, '<small>string</small><span>(6)</span> "foobar"'),
array('foobar', 2, '<small>string</small><span>(6)</span> "fo&nbsp;&hellip;"'),
array(NULL, 128, '<small>NULL</small>'),
array(TRUE, 128, '<small>bool</small> TRUE'),
array(array('foobar'), 128, "<small>array</small><span>(1)</span> <span>(\n 0 => <small>string</small><span>(6)</span> \"foobar\"\n)</span>"),
array(new StdClass, 128, "<small>object</small> <span>stdClass(0)</span> <code>{\n}</code>"),
array("fo\x6F\xFF\x00bar\x8F\xC2\xB110", 128, '<small>string</small><span>(10)</span> "foobar±10"'),
array('foobar', 128, 10, '<small>string</small><span>(6)</span> "foobar"'),
array('foobar', 2, 10, '<small>string</small><span>(6)</span> "fo&nbsp;&hellip;"'),
array(NULL, 128, 10, '<small>NULL</small>'),
array(TRUE, 128, 10, '<small>bool</small> TRUE'),
array(array('foobar'), 128, 10, "<small>array</small><span>(1)</span> <span>(\n 0 => <small>string</small><span>(6)</span> \"foobar\"\n)</span>"),
array(new StdClass, 128, 10, "<small>object</small> <span>stdClass(0)</span> <code>{\n}</code>"),
array("fo\x6F\xFF\x00bar\x8F\xC2\xB110", 128, 10, '<small>string</small><span>(10)</span> "foobar±10"'),
array(array('level1' => array('level2' => array('level3' => array('level4' => array('value' => 'something'))))), 128, 4,
'<small>array</small><span>(1)</span> <span>(
"level1" => <small>array</small><span>(1)</span> <span>(
"level2" => <small>array</small><span>(1)</span> <span>(
"level3" => <small>array</small><span>(1)</span> <span>(
"level4" => <small>array</small><span>(1)</span> (
...
)
)</span>
)</span>
)</span>
)</span>'),
);
}
@@ -106,8 +119,8 @@ class Kohana_DebugTest extends Unittest_TestCase
* @param object $exception exception to test
* @param string $expected expected output
*/
public function test_dump($input, $length, $expected)
public function test_dump($input, $length, $limit, $expected)
{
$this->assertEquals($expected, Debug::dump($input, $length));
$this->assertEquals($expected, Debug::dump($input, $length, $limit));
}
}

View File

@@ -4,12 +4,13 @@
* Tests Kohana Exception Class
*
* @group kohana
* @group kohana.exception
* @group kohana.core
* @group kohana.core.exception
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ExceptionTest extends Unittest_TestCase
@@ -31,8 +32,14 @@ class Kohana_ExceptionTest extends Unittest_TestCase
array(array(':a :b', array(':a' => 'c', ':b' => 'd')), 'c d', 0),
array(array(':a', NULL, 5), ':a', 5),
// #3358
array(array(':a', NULL, '3F000'), ':a', '3F000'),
// #3404
array(array(':a', NULL, '42S22'), ':a', '42S22'),
// #3927
array(array(':a', NULL, 'b'), ':a', 'b'),
// #4039
array(array(':a', NULL, '25P01'), ':a', '25P01'),
);
}

View File

@@ -4,13 +4,14 @@
* Test for feed helper
*
* @group kohana
* @group kohana.feed
* @group kohana.core
* @group kohana.core.feed
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_FeedTest extends Unittest_TestCase
@@ -39,12 +40,9 @@ class Kohana_FeedTest extends Unittest_TestCase
*/
public function test_parse($source, $expected)
{
if ( ! $this->hasInternet())
{
$this->markTestSkipped('An internet connection is required for this test');
}
$this->markTestSkipped('We don\'t go to the internet for tests.');
$this->assertEquals($expected, count(feed::parse($source)));
$this->assertEquals($expected, count(Feed::parse($source)));
}
/**
@@ -115,11 +113,11 @@ class Kohana_FeedTest extends Unittest_TestCase
{
$this->setEnvironment($enviroment);
$this->assertTag($matcher_item, feed::create($info, $items), '', FALSE);
$this->assertTag($matcher_item, Feed::create($info, $items), '', FALSE);
foreach ($matchers_image as $matcher_image)
{
$this->assertTag($matcher_image, feed::create($info, $items), '', FALSE);
$this->assertTag($matcher_image, Feed::create($info, $items), '', FALSE);
}
}
}

View File

@@ -4,13 +4,14 @@
* Tests Kohana File helper
*
* @group kohana
* @group kohana.url
* @group kohana.core
* @group kohana.core.url
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_FileTest extends Unittest_TestCase
@@ -24,7 +25,7 @@ class Kohana_FileTest extends Unittest_TestCase
{
return array(
// $value, $result
array(Kohana::find_file('classes', 'file')),
array(Kohana::find_file('classes', 'File')),
array(Kohana::find_file('tests', 'test_data/github', 'png')),
);
}
@@ -39,6 +40,9 @@ class Kohana_FileTest extends Unittest_TestCase
*/
public function test_mime($input)
{
$this->markTestSkipped(
'This test doesn\'t do anything useful!'
);
$this->assertSame(1, preg_match('/^(?:application|audio|image|message|multipart|text|video)\/[a-z.+0-9-]+$/i', File::mime($input)));
}

View File

@@ -4,13 +4,14 @@
* Tests Kohana Form helper
*
* @group kohana
* @group kohana.form
* @group kohana.core
* @group kohana.core.form
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_FormTest extends Unittest_TestCase
@@ -19,10 +20,13 @@ class Kohana_FormTest extends Unittest_TestCase
* Defaults for this test
* @var array
*/
// @codingStandardsIgnoreStart
protected $environmentDefault = array(
'Kohana::$base_url' => '/',
'HTTP_HOST' => 'kohanaframework.org',
'Kohana::$index_file' => '',
);
// @codingStandardsIgnoreEnd
/**
* Provides test data for test_open()
@@ -32,11 +36,22 @@ class Kohana_FormTest extends Unittest_TestCase
public function provider_open()
{
return array(
// $value, $result
array(NULL, NULL, '<form action="/" method="post" accept-charset="utf-8">'),
array('foo', NULL),
array('', NULL),
array('foo', array('method' => 'get')),
array(
array('', NULL),
array('action' => '')
),
array(
array(NULL, NULL),
array('action' => '')
),
array(
array('foo', NULL),
array('action' => '/foo')
),
array(
array('foo', array('method' => 'get')),
array('action' => '/foo', 'method' => 'get')
),
);
}
@@ -48,22 +63,22 @@ class Kohana_FormTest extends Unittest_TestCase
* @param boolean $input Input for Form::open
* @param boolean $expected Output for Form::open
*/
public function test_open($action, $attributes)
public function test_open($input, $expected)
{
list($action, $attributes) = $input;
$tag = Form::open($action, $attributes);
$matcher = array(
'tag' => 'form',
// Default attributes
'attributes' => array(
'method' => 'post',
'method' => 'post',
'accept-charset' => 'utf-8',
),
);
if ($attributes !== NULL)
{
$matcher['attributes'] = $attributes + $matcher['attributes'];
}
$matcher['attributes'] = $expected + $matcher['attributes'];
$this->assertTag($matcher, $tag);
}

View File

@@ -4,22 +4,30 @@
* Tests HTML
*
* @group kohana
* @group kohana.html
* @group kohana.core
* @group kohana.core.html
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_HTMLTest extends Unittest_TestCase
{
/**
* Defaults for this test
* @var array
*/
// @codingStandardsIgnoreStart
protected $environmentDefault = array(
'Kohana::$base_url' => '/kohana/',
'Kohana::$index_file' => 'index.php',
'HTML::$strict' => TRUE,
'HTTP_HOST' => 'www.kohanaframework.org',
);
// @codingStandardsIgnoreStart
/**
* Provides test data for test_attributes()
@@ -31,20 +39,29 @@ class Kohana_HTMLTest extends Unittest_TestCase
return array(
array(
array('name' => 'field', 'random' => 'not_quite', 'id' => 'unique_field'),
array(),
' id="unique_field" name="field" random="not_quite"'
),
array(
array('invalid' => NULL),
array(),
''
),
array(
array(),
array(),
''
),
array(
array('name' => 'field', 'checked'),
array(),
' name="field" checked="checked"',
),
array(
array('id' => 'disabled_field', 'disabled'),
array('HTML::$strict' => FALSE),
' id="disabled_field" disabled',
),
);
}
@@ -54,10 +71,13 @@ class Kohana_HTMLTest extends Unittest_TestCase
* @test
* @dataProvider provider_attributes
* @param array $attributes Attributes to use
* @param array $options Environment options to use
* @param string $expected Expected output
*/
public function test_attributes($attributes, $expected)
public function test_attributes(array $attributes, array $options, $expected)
{
$this->setEnvironment($options);
$this->assertSame(
$expected,
HTML::attributes($attributes)
@@ -90,6 +110,13 @@ class Kohana_HTMLTest extends Unittest_TestCase
'https',
FALSE
),
array(
'<script type="text/javascript" src="https://www.kohanaframework.org/kohana/my/script.js"></script>',
'/my/script.js', // Test absolute paths
NULL,
'https',
FALSE
),
);
}
@@ -149,6 +176,23 @@ class Kohana_HTMLTest extends Unittest_TestCase
'https',
TRUE
),
array(
'<link type="text/css" href="https://www.kohanaframework.org/kohana/index.php/my/style.css" rel="stylesheet" />',
'/my/style.css',
array(),
'https',
TRUE
),
array(
// #4283: http://dev.kohanaframework.org/issues/4283
'<link type="text/css" href="https://www.kohanaframework.org/kohana/index.php/my/style.css" rel="stylesheet/less" />',
'my/style.css',
array(
'rel' => 'stylesheet/less'
),
'https',
TRUE
),
);
}
@@ -171,34 +215,6 @@ class Kohana_HTMLTest extends Unittest_TestCase
);
}
/**
* Provides test data for test_obfuscate
*
* @return array Array of test data
*/
public function provider_obfuscate()
{
return array(
array('something crazy'),
array('me@google.com'),
);
}
/**
* Tests HTML::obfuscate
*
* @test
* @dataProvider provider_obfuscate
* @param string $string The string to obfuscate
*/
public function test_obfuscate($string)
{
$this->assertNotSame(
$string,
HTML::obfuscate($string)
);
}
/**
* Provides test data for test_anchor
*
@@ -239,6 +255,41 @@ class Kohana_HTMLTest extends Unittest_TestCase
'https',
TRUE,
),
array(
'<a href="https://www.kohanaframework.org/kohana/index.php/users/example">Kohana</a>',
array(),
'users/example',
'Kohana',
NULL,
'https',
),
array(
'<a href="https://www.kohanaframework.org/kohana/index.php/users/example">Kohana</a>',
array(),
'users/example',
'Kohana',
NULL,
'https',
TRUE,
),
array(
'<a href="https://www.kohanaframework.org/kohana/users/example">Kohana</a>',
array(),
'users/example',
'Kohana',
NULL,
'https',
FALSE,
),
array(
'<a href="https://www.kohanaframework.org/kohana/users/example">Kohana</a>',
array(),
'/users/example',
'Kohana',
NULL,
'https',
FALSE,
),
);
}
@@ -248,7 +299,7 @@ class Kohana_HTMLTest extends Unittest_TestCase
* @test
* @dataProvider provider_anchor
*/
public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = FALSE)
public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = TRUE)
{
// $this->setEnvironment($options);
@@ -287,7 +338,15 @@ class Kohana_HTMLTest extends Unittest_TestCase
'My picture file',
'ftp',
FALSE
)
),
array(
'<a href="ftp://www.kohanaframework.org/kohana/mypic.png">My picture file</a>',
array(),
'/mypic.png',
'My picture file',
'ftp',
FALSE
),
);
}

View File

@@ -0,0 +1,87 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests HTTP
*
* @group kohana
* @group kohana.core
* @group kohana.core.http
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_HTTPTest extends Unittest_TestCase {
/**
* Defaults for this test
* @var array
*/
// @codingStandardsIgnoreStart
protected $environmentDefault = array(
'Kohana::$base_url' => '/kohana/',
'Kohana::$index_file' => 'index.php',
'HTTP_HOST' => 'www.example.com',
);
// @codingStandardsIgnoreEnd
/**
* Provides test data for test_attributes()
*
* @return array
*/
public function provider_redirect()
{
return array(
array(
'http://www.example.org/',
301,
'HTTP_Exception_301',
'http://www.example.org/'
),
array(
'/page_one',
302,
'HTTP_Exception_302',
'http://www.example.com/kohana/index.php/page_one'
),
array(
'page_two',
303,
'HTTP_Exception_303',
'http://www.example.com/kohana/index.php/page_two'
),
);
}
/**
* Tests HTTP::redirect()
*
* @test
* @dataProvider provider_redirect
* @param array $location Location to redirect to
* @param array $code HTTP Code to use for the redirect
* @param string $expected_exception Expected exception
* @param string $expected_location Expected exception
*/
public function test_redirect($location, $code, $expected_exception, $expected_location)
{
try
{
HTTP::redirect($location, $code);
}
catch (HTTP_Exception_Redirect $e)
{
$response = $e->get_response();
$this->assertInstanceOf($expected_exception, $e);
$this->assertEquals($expected_location, $response->headers('Location'));
return;
}
$this->fail('HTTP_Exception_Redirect not thrown');
}
}

View File

@@ -1,164 +0,0 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for Kohana_HTTP_Header_Value
*
* @group kohana
* @group kohana.http
* @group kohana.http.header
* @group kohana.http.header.value
*
* @see CLI
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_HTTP_Header_ValueTest extends Unittest_TestCase
{
/**
* If the header value is composed of a key value pair then the parser
* should return an array of key => value
*
* @test
*/
public function test_parser_returns_array_if_header_contains_key_val()
{
$key_val = Kohana_HTTP_Header_Value::parse_key_value('name=kohana');
$this->assertSame(array('name' => 'kohana'), $key_val);
}
/**
* If the parser is passed a header which doesn't contain a key then it should
* return the header key as a string
*
* @test
* @covers Kohana_HTTP_Header_Value::parse_key_value
*/
public function test_parser_returns_key_as_array_if_value_isnt_present()
{
$this->assertSame(
array('kohana'),
Kohana_HTTP_Header_Value::parse_key_value('kohana')
);
}
/**
* Provides test data for test_parsing_only_splits_on_first_separator()
*
* @return array
*/
public function provider_test_parsing_only_splits_on_first_separator()
{
return array(
array('='),
array('+')
);
}
/**
* If we pass in a string containing multiple occurences of the separator then
* the string should only be split on the first occurence
*
* @test
* @dataProvider provider_test_parsing_only_splits_on_first_separator
* @covers Kohana_HTTP_Header_Value::parse_key_value
*/
public function test_parsing_only_splits_on_first_separator($separator)
{
$key = 'some_value';
$val = 'pie,pizza'.$separator.'cheese';
$key_value = Kohana_HTTP_Header_Value::parse_key_value($key.$separator.$val, $separator);
$this->assertSame(array($key => $val), $key_value);
}
/**
* Provides test data
*
* @return array
*/
public function provider_constructor_throws_exception_if_header_value_type_not_allowed()
{
return array(
array(42),
array(new ArrayObject),
);
}
/**
* If the constructor is passed a value of type other than string|array then it should
* throw a HTTP_Exception_500
*
* @test
* @dataProvider provider_constructor_throws_exception_if_header_value_type_not_allowed
* @expectedException HTTP_Exception_500
* @param mixed The header value to pass to the constructor
*/
public function test_constructor_throws_exception_if_header_value_type_not_allowed($header)
{
new Kohana_HTTP_Header_Value($header);
}
/**
* When the constructor is passed an array of values it should extract the appropriate values
* and set them in the object's properties
*
* @test
* @covers Kohana_HTTP_Header_Value
*/
public function test_constructor_allows_and_parses_header_in_array_format()
{
$input = array(
'key' => 'name',
'value' => 'kohana',
'properties' => array(
'ttl' => '60'
)
);
$header = new Kohana_HTTP_Header_Value($input);
$this->assertSame($input['key'], $header->key);
$this->assertSame($input['value'], $header->value);
$this->assertSame($input['properties'], $header->properties);
}
/**
* Provides test data
*
* @return array
*/
public function provider_compiles_down_to_valid_header_when_cast_to_string()
{
return array(
// Basic test, try and achieve what the doccomment says it can do
array(
'name=value; property; another_property=property_value',
array(
'key' => 'name',
'value' => 'value',
'properties' => array('property', 'another_property' => 'property_value')
),
),
);
}
/**
* When we cast a Kohana_HTTP_Header_Value object to a string it should generate
* a valid header
*
* @test
* @covers Kohana_HTTP_Header_Value::__toString
* @dataProvider provider_compiles_down_to_valid_header_when_cast_to_string
* @param string Expected compiled header
* @param array|string The compiled header
*/
public function test_compiles_down_to_valid_header_when_cast_to_string($expected, $input)
{
$header = new Kohana_HTTP_Header_Value($input);
$this->assertSame($expected, (string) $header);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -4,44 +4,54 @@
* Tests Kohana i18n class
*
* @group kohana
* @group kohana.i18n
* @group kohana.core
* @group kohana.core.i18n
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_I18nTest extends Unittest_TestCase
{
class Kohana_I18nTest extends Unittest_TestCase {
/**
* Default values for the environment, see setEnvironment
* @var array
*/
// @codingStandardsIgnoreStart
protected $environmentDefault = array(
'I18n::$lang' => 'en-us',
);
// @codingStandardsIgnoreEnd
/**
* Provides test data for test_lang()
*
*
* @return array
*/
public function provider_lang()
{
return array(
// $value, $result
// $input, $expected_result
array(NULL, 'en-us'),
array('es-es', 'es-es'),
array(NULL, 'es-es'),
);
}
/**
* Tests i18n::lang()
* Tests I18n::lang()
*
* @test
* @dataProvider provider_lang
* @param boolean $input Input for File::mime
* @param boolean $expected Output for File::mime
* @param boolean $input Input for I18n::lang
* @param boolean $expected Output for I18n::lang
*/
public function test_open($input, $expected)
public function test_lang($input, $expected_result)
{
$this->assertSame($expected, I18n::lang($input));
$this->assertSame($expected_result, I18n::lang($input));
$this->assertSame($expected_result, I18n::lang());
}
/**

View File

@@ -4,13 +4,14 @@
* Tests Kohana inflector class
*
* @group kohana
* @group kohana.inflector
* @group kohana.core
* @group kohana.core.inflector
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_InflectorTest extends Unittest_TestCase
@@ -174,9 +175,9 @@ class Kohana_InflectorTest extends Unittest_TestCase
*
* @test
* @dataProvider provider_decamelize
* @param string Camelized string
* @param string Glue
* @param string Expected string
* @param string $input Camelized string
* @param string $glue Glue
* @param string $expected Expected string
*/
public function test_decamelize($input, $glue, $expected)
{

View File

@@ -4,13 +4,14 @@
* Tests Kohana Logging API
*
* @group kohana
* @group kohana.logging
* @group kohana.core
* @group kohana.core.logging
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Matt Button <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_LogTest extends Unittest_TestCase

View File

@@ -4,13 +4,14 @@
* This test only really exists for code coverage.
*
* @group kohana
* @group kohana.model
* @group kohana.core
* @group kohana.core.model
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ModelTest extends Unittest_TestCase

View File

@@ -3,13 +3,14 @@
/**
* Tests Num
*
* @group kohana
* @group kohana.num
* @group kohana
* @group kohana.core
* @group kohana.core.num
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_NumTest extends Unittest_TestCase
@@ -19,17 +20,21 @@ class Kohana_NumTest extends Unittest_TestCase
/**
* SetUp test enviroment
*/
// @codingStandardsIgnoreStart
public function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
setlocale(LC_ALL, 'English');
setlocale(LC_ALL, 'en_US.utf8');
}
/**
* Tear down environment
*/
// @codingStandardsIgnoreStart
public function tearDown()
// @codingStandardsIgnoreEnd
{
parent::tearDown();
@@ -194,7 +199,7 @@ class Kohana_NumTest extends Unittest_TestCase
{
foreach (array(Num::ROUND_HALF_UP, Num::ROUND_HALF_DOWN, Num::ROUND_HALF_EVEN, Num::ROUND_HALF_ODD) as $i => $mode)
{
$this->assertSame($expected[$i], Num::round($input, $precision, $mode, false));
$this->assertSame($expected[$i], Num::round($input, $precision, $mode, FALSE));
}
}
}

View File

@@ -4,31 +4,40 @@
* Unit tests for request class
*
* @group kohana
* @group kohana.request
* @group kohana.core
* @group kohana.core.request
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_RequestTest extends Unittest_TestCase
{
protected $_inital_request;
// @codingStandardsIgnoreStart
public function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
$this->_initial_request = Request::$initial;
Request::$initial = new Request('/');
}
// @codingStandardsIgnoreStart
public function tearDown()
// @codingStandardsIgnoreEnd
{
Request::$initial = $this->_initial_request;
parent::tearDown();
}
public function test_initial()
{
$original = array(
'Kohana::$is_cli' => Kohana::$is_cli,
'Request::$initial' => Request::$initial,
'Request::$client_ip' => Request::$client_ip,
'Request::$user_agent' => Request::$user_agent,
'_SERVER' => $_SERVER,
'_GET' => $_GET,
'_POST' => $_POST,
);
$this->setEnvironment(array(
'Kohana::$is_cli' => FALSE,
'Request::$initial' => NULL,
'Request::$client_ip' => NULL,
'Request::$user_agent' => NULL,
@@ -53,7 +62,7 @@ class Kohana_RequestTest extends Unittest_TestCase
$this->assertEquals(Request::$user_agent, 'whatever (Mozilla 5.0/compatible)');
$this->assertEquals($request->protocol(), 'http');
$this->assertEquals($request->protocol(), 'HTTP/1.1');
$this->assertEquals($request->referrer(), 'http://example.com/');
@@ -62,51 +71,24 @@ class Kohana_RequestTest extends Unittest_TestCase
$this->assertEquals($request->query(), array());
$this->assertEquals($request->post(), array());
$this->setEnvironment($original);
}
/**
* Tests that an initial request won't use an external client
*
* @expectedException HTTP_Exception_404
* Tests that the allow_external flag prevents an external request.
*
* @return null
*/
public function test_initial_request_only_loads_internal()
public function test_disable_external_tests()
{
$this->setEnvironment(
array(
'Kohana::$is_cli' => FALSE,
'Request::$initial' => NULL,
)
);
$request = new Request('http://www.google.com/');
}
$request = new Request('http://www.google.com/', array(), FALSE);
/**
* Tests that with an empty request, cli requests are routed properly
*
* @return null
*/
public function test_empty_cli_requests_route_properly()
{
$this->setEnvironment(
array(
'Kohana::$is_cli' => TRUE,
'Request::$initial' => NULL,
)
);
$route = new Route('(<controller>(/<action>))');
$route->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
$request = Request::factory(TRUE, NULL, array($route));
$response = $request->execute();
$this->assertEquals(FALSE, $request->is_external());
}
/**
@@ -131,7 +113,7 @@ class Kohana_RequestTest extends Unittest_TestCase
{
$request = Request::factory($uri);
$this->assertInstanceOf($client_class, $request->get_client());
$this->assertInstanceOf($client_class, $request->client());
}
/**
@@ -141,8 +123,17 @@ class Kohana_RequestTest extends Unittest_TestCase
*/
public function test_param()
{
$route = new Route('(<controller>(/<action>(/<id>)))');
$uri = 'foo/bar/id';
$request = Request::factory($uri);
$request = Request::factory($uri, NULL, TRUE, array($route));
// We need to execute the request before it has matched a route
try
{
$request->execute();
}
catch (Exception $e) {}
$this->assertArrayHasKey('id', $request->param());
$this->assertArrayNotHasKey('foo', $request->param());
@@ -160,51 +151,18 @@ class Kohana_RequestTest extends Unittest_TestCase
$route = new Route('(<uri>)', array('uri' => '.+'));
$route->defaults(array('controller' => 'foobar', 'action' => 'index'));
$request = Request::factory('foobar', NULL, array($route));
$request = Request::factory('foobar', NULL, TRUE, array($route));
// We need to execute the request before it has matched a route
try
{
$request->execute();
}
catch (Exception $e) {}
$this->assertSame('foobar', $request->param('uri'));
}
/**
* Provides data for Request::create_response()
*/
public function provider_create_response()
{
return array(
array('foo/bar', TRUE, TRUE),
array('foo/bar', FALSE, FALSE)
);
}
/**
* Ensures a request creates an empty response, and binds correctly
*
* @test
* @dataProvider provider_create_response
*/
public function test_create_response($uri, $bind, $equality)
{
$request = Request::factory($uri);
$response = $request->create_response($bind);
$this->assertEquals(($request->response() === $response), $equality);
}
/**
* Tests Request::response()
*
* @test
*/
public function test_response()
{
$request = Request::factory('foo/bar');
$response = $request->create_response(FALSE);
$this->assertEquals($request->response(), NULL);
$this->assertEquals(($request->response($response) === $request), TRUE);
$this->assertEquals(($request->response() === $response), TRUE);
}
/**
* Tests Request::method()
*
@@ -228,9 +186,29 @@ class Kohana_RequestTest extends Unittest_TestCase
{
$request = Request::factory(''); // This should always match something, no matter what changes people make
// We need to execute the request before it has matched a route
try
{
$request->execute();
}
catch (Exception $e) {}
$this->assertInstanceOf('Route', $request->route());
}
/**
* Tests Request::route()
*
* @test
*/
public function test_route_is_not_set_before_execute()
{
$request = Request::factory(''); // This should always match something, no matter what changes people make
// The route should be NULL since the request has not been executed yet
$this->assertEquals($request->route(), NULL);
}
/**
* Tests Request::accept_type()
*
@@ -284,17 +262,13 @@ class Kohana_RequestTest extends Unittest_TestCase
return array(
array(
'foo/bar',
array(),
'http',
TRUE,
'http://localhost/kohana/foo/bar'
),
array(
'foo',
array('action' => 'bar'),
'http',
TRUE,
'http://localhost/kohana/foo/bar'
'http://localhost/kohana/foo'
),
);
}
@@ -305,146 +279,24 @@ class Kohana_RequestTest extends Unittest_TestCase
* @test
* @dataProvider provider_url
* @covers Request::url
* @param string $route the route to use
* @param array $params params to pass to route::uri
* @param string $uri the uri to use
* @param string $protocol the protocol to use
* @param array $expected The string we expect
*/
public function test_url($uri, $params, $protocol, $is_cli, $expected)
public function test_url($uri, $protocol, $expected)
{
if ( ! isset($_SERVER['argc']))
{
$_SERVER['argc'] = 1;
}
$this->setEnvironment(array(
'Kohana::$base_url' => '/kohana/',
'_SERVER' => array('HTTP_HOST' => 'localhost', 'argc' => $_SERVER['argc']),
'Kohana::$index_file' => FALSE,
'Kohana::$is_cli' => $is_cli,
));
$this->assertEquals(Request::factory($uri)->url($params, $protocol), $expected);
}
/**
* Tests that request caching works
*
* @return null
*/
public function test_cache()
{
/**
* Sets up a mock cache object, asserts that:
*
* 1. The cache set() method gets called
* 2. The cache get() method will return the response above when called
*/
$cache = $this->getMock('Cache_File', array('get', 'set'), array(), 'Cache');
$cache->expects($this->once())
->method('set');
$foo = Request::factory('', $cache);
$response = $foo->create_response(TRUE);
$response->headers('Cache-Control', 'max-age=100');
$foo->response($response);
$foo->execute();
/**
* Set up a mock response object to test with
*/
$response = $this->getMock('Response');
$response->expects($this->any())
->method('body')
->will($this->returnValue('Foo'));
$cache->expects($this->any())
->method('get')
->will($this->returnValue($response));
$foo = Request::factory('', $cache)->execute();
$this->assertSame('Foo', $foo->body());
}
/**
* Data provider for test_set_cache
*
* @return array
*/
public function provider_set_cache()
{
return array(
array(
array('cache-control' => 'no-cache'),
array('no-cache' => NULL),
FALSE,
),
array(
array('cache-control' => 'no-store'),
array('no-store' => NULL),
FALSE,
),
array(
array('cache-control' => 'max-age=100'),
array('max-age' => '100'),
TRUE
),
array(
array('cache-control' => 'private'),
array('private' => NULL),
FALSE
),
array(
array('cache-control' => 'private, max-age=100'),
array('private' => NULL, 'max-age' => '100'),
FALSE
),
array(
array('cache-control' => 'private, s-maxage=100'),
array('private' => NULL, 's-maxage' => '100'),
TRUE
),
array(
array(
'expires' => date('m/d/Y', strtotime('-1 day')),
),
array(),
FALSE
),
array(
array(
'expires' => date('m/d/Y', strtotime('+1 day')),
),
array(),
TRUE
),
array(
array(),
array(),
TRUE
),
);
}
/**
* Tests the set_cache() method
*
* @test
* @dataProvider provider_set_cache
*
* @return null
*/
public function test_set_cache($headers, $cache_control, $expected)
{
/**
* Set up a mock response object to test with
*/
$response = $this->getMock('Response');
$response->expects($this->any())
->method('parse_cache_control')
->will($this->returnValue($cache_control));
$response->expects($this->any())
->method('headers')
->will($this->returnValue($headers));
$request = new Request_Client_Internal;
$this->assertEquals($request->set_cache($response), $expected);
$this->assertEquals(Request::factory($uri)->url($protocol), $expected);
}
/**
@@ -456,16 +308,16 @@ class Kohana_RequestTest extends Unittest_TestCase
{
return array(
array(
'http',
'http',
'http/1.1',
'HTTP/1.1',
),
array(
'FTP',
'ftp',
'FTP',
),
array(
'hTTps',
'https',
'hTTp/1.0',
'HTTP/1.0',
),
);
}
@@ -485,7 +337,7 @@ class Kohana_RequestTest extends Unittest_TestCase
$result = $request->protocol($protocol);
// Test the set value
$this->assertSame($request->protocol(), $expected);
$this->assertSame($expected, $request->protocol());
// Test the return value
$this->assertTrue($request instanceof $result);
@@ -546,7 +398,7 @@ class Kohana_RequestTest extends Unittest_TestCase
public function provider_uri_only_trimed_on_internal()
{
$old_request = Request::$initial;
Request::$initial = new Request('foo/bar');
Request::$initial = new Request(TRUE);
$result = array(
array(
@@ -564,6 +416,14 @@ class Kohana_RequestTest extends Unittest_TestCase
array(
new Request('foo/bar'),
'foo/bar'
),
array(
new Request('/'),
'/'
),
array(
new Request(''),
'/'
)
);
@@ -606,22 +466,6 @@ class Kohana_RequestTest extends Unittest_TestCase
)
);
if (extension_loaded('http'))
{
$provider[] = array(
array(
'proxyhost' => 'http://localhost:8080',
'proxytype' => HTTP_PROXY_HTTP,
'redirect' => 2
),
array(
'proxyhost' => 'http://localhost:8080',
'proxytype' => HTTP_PROXY_HTTP,
'redirect' => 2
)
);
}
return $provider;
}
@@ -637,11 +481,10 @@ class Kohana_RequestTest extends Unittest_TestCase
*/
public function test_options_set_to_external_client($settings, $expected)
{
$request = Request::factory('http://www.kohanaframework.org');
$request_client = $request->get_client();
$request_client = Request_Client_External::factory(array(), 'Request_Client_Curl');
// Test for empty array
$this->assertSame($request_client->options(), array());
$this->assertSame(array(), $request_client->options());
// Test that set works as expected
$this->assertSame($request_client->options($settings), $request_client);
@@ -692,7 +535,7 @@ class Kohana_RequestTest extends Unittest_TestCase
{
foreach ($headers as $key => $expected_value)
{
$this->assertSame((string) $request->headers($key), $expected_value);
$this->assertSame( (string) $request->headers($key), $expected_value);
}
}
@@ -705,20 +548,20 @@ class Kohana_RequestTest extends Unittest_TestCase
{
return array(
array(
new Request('foo/bar'),
Request::factory(),
array(
'content-type' => 'application/x-www-form-urlencoded',
'x-test-header' => 'foo'
),
"content-type: application/x-www-form-urlencoded\r\nx-test-header: foo\r\n\n"
"Content-Type: application/x-www-form-urlencoded\r\nX-Test-Header: foo\r\n\r\n"
),
array(
new Request('foo/bar'),
Request::factory(),
array(
'content-type' => 'application/json',
'x-powered-by' => 'kohana'
),
"content-type: application/json\r\nx-powered-by: kohana\r\n\n"
"Content-Type: application/json\r\nX-Powered-By: kohana\r\n\r\n"
)
);
}
@@ -808,46 +651,70 @@ class Kohana_RequestTest extends Unittest_TestCase
}
/**
* Provider for test_uri_without_query_parameters
* Provides data for test_client
*
* @return array
*/
public function provider_uri_without_query_parameters()
public function provider_client()
{
$internal_client = new Request_Client_Internal;
$external_client = new Request_Client_Stream;
return array(
array(
new Request('foo/bar?foo=bar&bar=foo'),
array(),
'foo/bar'
new Request('http://kohanaframework.org'),
$internal_client,
$internal_client
),
array(
new Request('foo/bar'),
array('bar' => 'foo', 'foo' => 'bar'),
'foo/bar'
),
array(
new Request('foo/bar'),
array(),
'foo/bar'
$external_client,
$external_client
)
);
}
/**
* Tests that the [Request::uri()] method does not return
* query parameters
*
* @dataProvider provider_uri_without_query_parameters
* Tests the getter/setter for request client
*
* @param Request request
* @param array query
* @param string expected
* @dataProvider provider_client
*
* @param Request $request
* @param Request_Client $client
* @param Request_Client $expected
* @return void
*/
public function test_uri_without_query_parameters(Request $request, $query, $expected)
public function test_client(Request $request, Request_Client $client, Request_Client $expected)
{
$request->query($query);
$this->assertSame($expected, $request->uri());
$request->client($client);
$this->assertSame($expected, $request->client());
}
} // End Kohana_RequestTest
/**
* Tests that the Request constructor passes client params on to the
* Request_Client once created.
*/
public function test_passes_client_params()
{
$request = Request::factory('http://example.com/', array(
'follow' => TRUE,
'strict_redirect' => FALSE
));
$client = $request->client();
$this->assertEquals($client->follow(), TRUE);
$this->assertEquals($client->strict_redirect(), FALSE);
}
} // End Kohana_RequestTest
class Controller_Kohana_RequestTest_Dummy extends Controller
{
public function action_index()
{
}
} // End Kohana_RequestTest

View File

@@ -4,40 +4,17 @@
* Unit tests for response class
*
* @group kohana
* @group kohana.response
* @group kohana.core
* @group kohana.core.response
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ResponseTest extends Unittest_TestCase
{
/**
* Ensures that Kohana::$expose adds the x-powered-by header and
* makes sure it's set to the correct Kohana Framework string
*
* @test
*/
public function test_expose()
{
Kohana::$expose = TRUE;
$response = new Response;
$headers = $response->send_headers()->headers();
$this->assertArrayHasKey('x-powered-by', (array) $headers);
if (isset($headers['x-powered-by']))
{
$this->assertSame($headers['x-powered-by']->value, 'Kohana Framework '.Kohana::VERSION.' ('.Kohana::CODENAME.')');
}
Kohana::$expose = FALSE;
$response = new Response;
$headers = $response->send_headers()->headers();
$this->assertArrayNotHasKey('x-powered-by', (array) $headers);
}
/**
* Provider for test_body
*
@@ -201,19 +178,18 @@ class Kohana_ResponseTest extends Unittest_TestCase
*/
public function test_send_headers_cli()
{
if (Kohana::$is_cli)
if (headers_sent())
{
$content_type = 'application/json';
$response = new Response;
$response->headers('content-type', $content_type)
->send_headers();
$this->markTestSkipped('Cannot test this feature as headers have already been sent!');
}
$content_type = 'application/json';
$response = new Response;
$response->headers('content-type', $content_type)
->send_headers();
$this->assertFalse(headers_sent());
$this->assertFalse(headers_sent());
}
else
{
$this->markTestSkipped('Unable to perform test outside of CLI mode');
}
}
/**
@@ -229,16 +205,4 @@ class Kohana_ResponseTest extends Unittest_TestCase
$headers = $response->send_headers()->headers();
$this->assertSame($content_type, (string) $headers['content-type']);
}
/**
* Tests that the default content type is sent if not set
*
* @test
*/
public function test_default_content_type_when_not_set()
{
$response = new Response;
$headers = $response->send_headers()->headers();
$this->assertSame(Kohana::$content_type.'; charset='.Kohana::$charset, (string) $headers['content-type']);
}
}

View File

@@ -4,13 +4,14 @@
* Description of RouteTest
*
* @group kohana
* @group kohana.route
* @group kohana.core
* @group kohana.core.route
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
@@ -21,7 +22,9 @@ class Kohana_RouteTest extends Unittest_TestCase
/**
* Remove all caches
*/
// @codingStandardsIgnoreStart
public function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
@@ -31,7 +34,9 @@ class Kohana_RouteTest extends Unittest_TestCase
/**
* Removes cache files created during tests
*/
// @codingStandardsIgnoreStart
public function tearDown()
// @codingStandardsIgnoreEnd
{
parent::tearDown();
@@ -113,6 +118,34 @@ class Kohana_RouteTest extends Unittest_TestCase
$this->assertEquals($routes, Route::all());
}
/**
* Check appending cached routes. See http://dev.kohanaframework.org/issues/4347
*
* @test
* @covers Route::cache
*/
public function test_cache_append_routes()
{
$cached = Route::all();
// First we create the cache
Route::cache(TRUE);
// Now lets modify the "current" routes
Route::set('nonsensical_route', 'flabbadaga/ding_dong');
$modified = Route::all();
// Then try and load said cache
$this->assertTrue(Route::cache(NULL, TRUE));
// Check the route cache flag
$this->assertTrue(Route::$cache);
// And if all went ok the nonsensical route should exist with the other routes...
$this->assertEquals(Route::all(), $cached + $modified);
}
/**
* Route::cache() should return FALSE if cached routes could not be found
*
@@ -163,7 +196,6 @@ class Kohana_RouteTest extends Unittest_TestCase
{
return array(
array('<controller>/<action>', '<controller>/<action>'),
array(array('Route_Holder', 'default_callback'), array('Route_Holder', 'default_callback')),
);
}
@@ -220,7 +252,6 @@ class Kohana_RouteTest extends Unittest_TestCase
{
return array(
array('projects/(<project_id>/(<controller>(/<action>(/<id>))))', 'apple/pie'),
array(array('Route_Holder', 'default_callback'), 'apple/pie'),
);
}
@@ -236,7 +267,14 @@ class Kohana_RouteTest extends Unittest_TestCase
{
$route = new Route($uri);
$this->assertSame(FALSE, $route->matches($match));
// Mock a request class with the $match uri
$stub = $this->getMock('Request', array('uri'), array($match));
$stub->expects($this->any())
->method('uri')
// Request::uri() called by Route::matches() will return $match
->will($this->returnValue($match));
$this->assertSame(FALSE, $route->matches($stub));
}
/**
@@ -250,13 +288,7 @@ class Kohana_RouteTest extends Unittest_TestCase
array(
'(<controller>(/<action>(/<id>)))',
'welcome/index',
'welcome',
'index',
),
array(
array('Route_Holder', 'matches_returns_array_of_parameters_on_successful_match'),
'apple/pie',
'welcome',
'Welcome',
'index',
),
);
@@ -275,7 +307,14 @@ class Kohana_RouteTest extends Unittest_TestCase
{
$route = new Route($uri);
$matches = $route->matches($m);
// Mock a request class with the $m uri
$request = $this->getMock('Request', array('uri'), array($m));
$request->expects($this->any())
->method('uri')
// Request::uri() called by Route::matches() will return $m
->will($this->returnValue($m));
$matches = $route->matches($request);
$this->assertInternalType('array', $matches);
$this->assertArrayHasKey('controller', $matches);
@@ -297,8 +336,8 @@ class Kohana_RouteTest extends Unittest_TestCase
array(
'<controller>(/<action>(/<id>))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
array('controller' => 'Welcome', 'action' => 'index'),
'Welcome',
'index',
'unit/test/1',
array(
@@ -306,13 +345,13 @@ class Kohana_RouteTest extends Unittest_TestCase
'action' => 'test',
'id' => '1'
),
'welcome',
'Welcome',
),
array(
'(<controller>(/<action>(/<id>)))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'Welcome',
'index',
'unit/test/1',
array(
@@ -322,20 +361,34 @@ class Kohana_RouteTest extends Unittest_TestCase
),
'',
),
/**
* Specifying this should cause controller and action to show up
* refs #4113
*/
array(
array('Route_Holder', 'default_return_callback'),
'(<controller>(/<action>(/<id>)))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'Welcome',
'index',
'unit/test/1',
'welcome/index/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'',
),
array(
'<controller>(/<action>(/<id>))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'Welcome',
'index',
'welcome/foo',
array(
'action' => 'foo',
),
'welcome',
),
);
}
@@ -353,7 +406,15 @@ class Kohana_RouteTest extends Unittest_TestCase
$route = new Route($uri, $regex);
$route->defaults($defaults);
$matches = $route->matches($default_uri);
$this->assertSame($defaults, $route->defaults());
// Mock a request class
$request = $this->getMock('Request', array('uri'), array($default_uri));
$request->expects($this->any())
->method('uri')
->will($this->returnValue($default_uri));
$matches = $route->matches($request);
$this->assertInternalType('array', $matches);
$this->assertArrayHasKey('controller', $matches);
@@ -366,6 +427,27 @@ class Kohana_RouteTest extends Unittest_TestCase
$this->assertSame($default_uri, $route->uri());
}
/**
* Optional params should not be used if what is passed in is identical
* to the default.
*
* refs #4116
*
* @test
* @covers Route::uri
*/
public function test_defaults_are_not_used_if_param_is_identical()
{
$route = new Route('(<controller>(/<action>(/<id>)))');
$route->defaults(array(
'controller' => 'welcome',
'action' => 'index'
));
$this->assertSame('', $route->uri(array('controller' => 'welcome')));
$this->assertSame('welcome2', $route->uri(array('controller' => 'welcome2')));
}
/**
* Provider for test_required_parameters_are_needed
*
@@ -379,11 +461,6 @@ class Kohana_RouteTest extends Unittest_TestCase
'admin',
'admin/users/add',
),
array(
array('Route_Holder', 'required_parameters_are_needed'),
'admin',
'admin/users/add',
),
);
}
@@ -399,13 +476,31 @@ class Kohana_RouteTest extends Unittest_TestCase
{
$route = new Route($uri);
$this->assertFalse($route->matches(''));
// Mock a request class that will return empty uri
$request = $this->getMock('Request', array('uri'), array(''));
$request->expects($this->any())
->method('uri')
->will($this->returnValue(''));
$matches = $route->matches($matches_route1);
$this->assertFalse($route->matches($request));
// Mock a request class that will return route1
$request = $this->getMock('Request', array('uri'), array($matches_route1));
$request->expects($this->any())
->method('uri')
->will($this->returnValue($matches_route1));
$matches = $route->matches($request);
$this->assertInternalType('array', $matches);
$matches = $route->matches($matches_route2);
// Mock a request class that will return route2 uri
$request = $this->getMock('Request', array('uri'), array($matches_route2));
$request->expects($this->any())
->method('uri')
->will($this->returnValue($matches_route2));
$matches = $route->matches($request);
$this->assertInternalType('array', $matches);
// $this->assertSame(5, count($matches));
@@ -427,12 +522,6 @@ class Kohana_RouteTest extends Unittest_TestCase
'info/about_us',
array('some' => 'random', 'params' => 'to confuse'),
),
array(
array('Route_Holder', 'reverse_routing_returns_routes_uri_if_route_is_static'),
'info/about_us',
'info/about_us',
array('some' => 'random', 'params' => 'to confuse'),
),
);
}
@@ -467,11 +556,6 @@ class Kohana_RouteTest extends Unittest_TestCase
NULL,
array('action' => 'awesome-action'),
),
array(
array('Route_Holder', 'default_return_callback'),
'<controller>(/<action)',
array('action' => 'awesome-action'),
),
);
}
@@ -526,21 +610,6 @@ class Kohana_RouteTest extends Unittest_TestCase
'id' => 'god',
),
),
array(
array('Route_Holder', 'default_return_callback'),
'<controller>/<action>(/<id>)',
'users/edit',
array(
'controller' => 'users',
'action' => 'edit',
),
'users/edit/god',
array(
'controller' => 'users',
'action' => 'edit',
'id' => 'god',
),
),
);
}
@@ -712,4 +781,60 @@ class Kohana_RouteTest extends Unittest_TestCase
$this->assertSame($expected_uri, Route::get('test')->uri());
}
/**
* Provider for test_route_filter_modify_params
*
* @return array
*/
public function provider_route_filter_modify_params()
{
return array(
array(
'<controller>/<action>',
array(
'controller' => 'Test',
'action' => 'same',
),
array('Route_Holder', 'route_filter_modify_params_array'),
'test/different',
array(
'controller' => 'Test',
'action' => 'modified',
),
),
array(
'<controller>/<action>',
array(
'controller' => 'test',
'action' => 'same',
),
array('Route_Holder', 'route_filter_modify_params_false'),
'test/fail',
FALSE,
),
);
}
/**
* Tests that route filters can modify parameters
*
* @covers Route::filter
* @dataProvider provider_route_filter_modify_params
*/
public function test_route_filter_modify_params($route, $defaults, $filter, $uri, $expected_params)
{
$route = new Route($route);
// Mock a request class
$request = $this->getMock('Request', array('uri'), array($uri));
$request->expects($this->any())
->method('uri')
->will($this->returnValue($uri));
$params = $route->defaults($defaults)->filter($filter)->matches($request);
$this->assertSame($expected_params, $params);
}
}

View File

@@ -4,12 +4,12 @@
* Tests Kohana_Security
*
* @group kohana
* @group kohana.security
* @group kohana.core
* @group kohana.core.security
*
* @package Kohana
* @category Tests
*/
class Kohana_SecurityTest extends Unittest_TestCase
{
/**
@@ -67,6 +67,17 @@ class Kohana_SecurityTest extends Unittest_TestCase
*/
public function provider_csrf_token()
{
// Unfortunately this data provider has to use the session in order to
// generate its data. If headers have already been sent then this method
// throws an error, even if the test is does not run. If we return an
// empty array then this also causes an error, so the only way to get
// around it is to return an array of misc data and have the test skip
// if headers have been sent. It's annoying this hack has to be
// implemented, but the security code isn't exactly brilliantly
// implemented. Ideally we'd be able to inject a session instance
if (headers_sent())
return array(array('', '', 0));
$array = array();
for ($i = 0; $i <= 4; $i++)
{
@@ -85,6 +96,10 @@ class Kohana_SecurityTest extends Unittest_TestCase
*/
public function test_csrf_token($expected, $input, $iteration)
{
if (headers_sent()) {
$this->markTestSkipped('Headers have already been sent, session not available');
}
Security::$token_name = 'token_'.$iteration;
$this->assertSame(TRUE, $input);
$this->assertSame($expected, Security::token(FALSE));

View File

@@ -4,13 +4,14 @@
* Tests the session class
*
* @group kohana
* @group kohana.session
* @group kohana.core
* @group kohana.core.session
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_SessionTest extends Unittest_TestCase
@@ -21,7 +22,9 @@ class Kohana_SessionTest extends Unittest_TestCase
*
* @return Session
*/
// @codingStandardsIgnoreStart
public function getMockSession(array $config = array())
// @codingStandardsIgnoreEnd
{
return $this->getMockForAbstractClass('Session', array($config));
}

View File

@@ -4,7 +4,8 @@
* Tests the kohana text class (Kohana_Text)
*
* @group kohana
* @group kohana.text
* @group kohana.core
* @group kohana.core.text
*
* @package Kohana
* @category Tests
@@ -15,7 +16,9 @@ class Kohana_TextTest extends Unittest_TestCase
/**
* Sets up the test enviroment
*/
// @codingStandardsIgnoreStart
function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
@@ -486,6 +489,19 @@ class Kohana_TextTest extends Unittest_TestCase
'Look at me <a href="http://www.google.com">http://www.google.com</a>',
'Look at me <a href="http://www.google.com">http://www.google.com</a>',
),
// Punctuation at the end of the URL
array(
'Wow <a href="http://www.google.com">http://www.google.com</a>!',
'Wow http://www.google.com!',
),
array(
'Zomg <a href="http://www.google.com">www.google.com</a>!',
'Zomg www.google.com!',
),
array(
'Well this, <a href="http://www.google.com">www.google.com</a>, is cool',
'Well this, www.google.com, is cool',
),
// @issue 3190
array(
'<a href="http://www.google.com/">www.google.com</a>',
@@ -495,6 +511,32 @@ class Kohana_TextTest extends Unittest_TestCase
'<a href="http://www.google.com/">www.google.com</a> <a href="http://www.google.com/">http://www.google.com/</a>',
'<a href="http://www.google.com/">www.google.com</a> http://www.google.com/',
),
// @issue 3436
array(
'<strong><a href="http://www.google.com/">http://www.google.com/</a></strong>',
'<strong>http://www.google.com/</strong>',
),
// @issue 4208, URLs with a path
array(
'Foobar <a href="http://www.google.com/analytics">www.google.com/analytics</a> cake',
'Foobar www.google.com/analytics cake',
),
array(
'Look at this <a href="http://www.google.com/analytics">www.google.com/analytics</a>!',
'Look at this www.google.com/analytics!',
),
array(
'Path <a href="http://www.google.com/analytics">http://www.google.com/analytics</a> works?',
'Path http://www.google.com/analytics works?',
),
array(
'Path <a href="http://www.google.com/analytics">http://www.google.com/analytics</a>',
'Path http://www.google.com/analytics',
),
array(
'Path <a href="http://www.google.com/analytics">www.google.com/analytics</a>',
'Path www.google.com/analytics',
),
);
}
@@ -592,7 +634,7 @@ class Kohana_TextTest extends Unittest_TestCase
foreach ($emails as $email)
{
$this->assertNotContains($email, $linked_text);
$this->assertContains('&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email, $linked_text);
}
}

View File

@@ -4,13 +4,14 @@
* Tests URL
*
* @group kohana
* @group kohana.url
* @group kohana.core
* @group kohana.core.url
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_URLTest extends Unittest_TestCase
@@ -19,12 +20,14 @@ class Kohana_URLTest extends Unittest_TestCase
* Default values for the environment, see setEnvironment
* @var array
*/
// @codingStandardsIgnoreStart
protected $environmentDefault = array(
'Kohana::$base_url' => '/kohana/',
'Kohana::$index_file'=> 'index.php',
'HTTP_HOST' => 'example.com',
'_GET' => array(),
);
// @codingStandardsIgnoreEnd
/**
* Provides test data for test_base()
@@ -46,7 +49,7 @@ class Kohana_URLTest extends Unittest_TestCase
array('ftp', TRUE, 'ftp://example.com/kohana/index.php/'),
// Test for automatic protocol detection, protocol = TRUE
array(TRUE, TRUE, 'cli://example.com/kohana/index.php/', array('HTTPS' => FALSE)),
array(TRUE, TRUE, 'cli://example.com/kohana/index.php/', array('HTTPS' => FALSE, 'Request::$initial' => Request::factory('/')->protocol('cli'))),
// Change base url'
array('https', FALSE, 'https://example.com/kohana/', array('Kohana::$base_url' => 'omglol://example.com/kohana/')),
@@ -247,6 +250,10 @@ class Kohana_URLTest extends Unittest_TestCase
array(array(), '?key=1', array('key' => TRUE)),
array(array('_GET' => array('sort' => 'down')), '?sort=down&key=1', array('key' => TRUE)),
array(array('_GET' => array('sort' => 'down')), '?sort=down&key=0', array('key' => FALSE)),
// @issue 4240
array(array('_GET' => array('foo' => array('a' => 100))), '?foo%5Ba%5D=100&foo%5Bb%5D=bar', array('foo' => array('b' => 'bar'))),
array(array('_GET' => array('a' => 'a')), '?a=b', array('a' => 'b')),
);
}

View File

@@ -3,12 +3,13 @@
* Tests Kohana_UTF8 class
*
* @group kohana
* @group kohana.utf8
* @group kohana.core
* @group kohana.core.utf8
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_UTF8Test extends Unittest_TestCase
@@ -483,7 +484,7 @@ class Kohana_UTF8Test extends Unittest_TestCase
* Tests UTF8::str_pad error
*
* @test
* @expectedException Exception
* @expectedException UTF8_Exception
*/
public function test_str_pad_error()
{

View File

@@ -4,13 +4,14 @@
* Tests Kohana upload class
*
* @group kohana
* @group kohana.upload
* @group kohana.core
* @group kohana.core.upload
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_UploadTest extends Unittest_TestCase
@@ -78,7 +79,7 @@ class Kohana_UploadTest extends Unittest_TestCase
* @param string $field the files field to test
* @param string $bytes valid bite size
* @param array $environment set the $_FILES array
* @param $expected what to expect
* @param bool $expected what to expect
*/
public function test_size($field, $bytes, $environment, $expected)
{

View File

@@ -4,13 +4,14 @@
* Tests the Valid class
*
* @group kohana
* @group kohana.valid
* @group kohana.core
* @group kohana.core.valid
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ValidTest extends Unittest_TestCase
@@ -29,7 +30,11 @@ class Kohana_ValidTest extends Unittest_TestCase
array('!"£$(G$W£(HFW£F(HQ)"n', FALSE),
// UTF-8 tests
array('あいうえお', TRUE, TRUE),
array('¥', FALSE, TRUE)
array('¥', FALSE, TRUE),
// Empty test
array('', FALSE, FALSE),
array(NULL, FALSE, FALSE),
array(FALSE, FALSE, FALSE),
);
}
@@ -58,18 +63,22 @@ class Kohana_ValidTest extends Unittest_TestCase
{
return array(
array('abcd1234', TRUE),
array('abcd', TRUE),
array('1234', TRUE),
array('abc123&^/-', FALSE),
array('abcd', TRUE),
array('1234', TRUE),
array('abc123&^/-', FALSE),
// UTF-8 tests
array('あいうえお', TRUE, TRUE),
array('零一二三四五', TRUE, TRUE),
array('あい四五£^£^', FALSE, TRUE),
// Empty test
array('', FALSE, FALSE),
array(NULL, FALSE, FALSE),
array(FALSE, FALSE, FALSE),
);
}
/**
* Tests Valid::alpha_numberic()
* Tests Valid::alpha_numeric()
*
* Checks whether a string consists of alphabetical characters and numbers only.
*
@@ -93,10 +102,14 @@ class Kohana_ValidTest extends Unittest_TestCase
{
return array(
array('abcdef', TRUE),
array('12345', TRUE),
array('abcd1234', TRUE),
array('abcd1234-', TRUE),
array('abc123&^/-', FALSE)
array('12345', TRUE),
array('abcd1234', TRUE),
array('abcd1234-', TRUE),
array('abc123&^/-', FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -144,6 +157,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('blarg',FALSE),
array('in the year 2000',FALSE),
array('324824',FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -169,9 +186,16 @@ class Kohana_ValidTest extends Unittest_TestCase
public function provider_decimal()
{
return array(
array('45.1664', 3, NULL, FALSE),
array('45.1664', 4, NULL, TRUE),
array('45.1664', 4, 2, TRUE),
// Empty test
array('', 2, NULL, FALSE),
array(NULL, 2, NULL, FALSE),
array(FALSE, 2, NULL, FALSE),
array('45.1664', 3, NULL, FALSE),
array('45.1664', 4, NULL, TRUE),
array('45.1664', 4, 2, TRUE),
array('-45.1664', 4, NULL, TRUE),
array('+45.1664', 4, NULL, TRUE),
array('-45.1664', 3, NULL, FALSE),
);
}
@@ -207,6 +231,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('abcd1234', FALSE),
array('-5', FALSE),
array(-5, FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -251,7 +279,11 @@ class Kohana_ValidTest extends Unittest_TestCase
array('GGGGGG', FALSE),
array('AbCdEf', TRUE),
array('000', TRUE),
array('DEF', TRUE)
array('DEF', TRUE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -278,14 +310,18 @@ class Kohana_ValidTest extends Unittest_TestCase
{
return array(
array('4222222222222', 'visa', TRUE),
array('4012888888881881', 'visa', TRUE),
array('4012888888881881', NULL, TRUE),
array('4012888888881881', array('mastercard', 'visa'), TRUE),
array('4012888888881881', array('discover', 'mastercard'), FALSE),
array('4012888888881881', 'mastercard', FALSE),
array('5105105105105100', 'mastercard', TRUE),
array('6011111111111117', 'discover', TRUE),
array('6011111111111117', 'visa', FALSE)
array('4012888888881881', 'visa', TRUE),
array('4012888888881881', NULL, TRUE),
array('4012888888881881', array('mastercard', 'visa'), TRUE),
array('4012888888881881', array('discover', 'mastercard'), FALSE),
array('4012888888881881', 'mastercard', FALSE),
array('5105105105105100', 'mastercard', TRUE),
array('6011111111111117', 'discover', TRUE),
array('6011111111111117', 'visa', FALSE),
// Empty test
array('', NULL, FALSE),
array(NULL, NULL, FALSE),
array(FALSE, NULL, FALSE),
);
}
@@ -321,6 +357,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('6011111111111117X', FALSE),
array('6011111111111117 ', FALSE),
array('WORD ', FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -352,13 +392,21 @@ class Kohana_ValidTest extends Unittest_TestCase
array('foo', TRUE, FALSE),
array('foo', FALSE, FALSE),
array('foo@bar', TRUE, TRUE),
// RFC is less strict than the normal regex, presumably to allow
// admin@localhost, therefore we IGNORE IT!!!
array('foo@bar', FALSE, FALSE),
array('foo@bar.com', FALSE, TRUE),
array('foo@barcom:80', FALSE, FALSE),
array('foo@bar.sub.com', FALSE, TRUE),
array('foo+asd@bar.sub.com', FALSE, TRUE),
array('foo.asd@bar.sub.com', FALSE, TRUE),
// RFC says 254 length max #4011
array(Text::random(NULL, 200).'@'.Text::random(NULL, 50).'.com', FALSE, FALSE),
// Empty test
array('', TRUE, FALSE),
array(NULL, TRUE, FALSE),
array(FALSE, TRUE, FALSE),
);
}
@@ -391,7 +439,11 @@ class Kohana_ValidTest extends Unittest_TestCase
return array(
array('google.com', TRUE),
// Don't anybody dare register this...
array('DAWOMAWIDAIWNDAIWNHDAWIHDAIWHDAIWOHDAIOHDAIWHD.com', FALSE)
array('DAWOMAWIDAIWNDAIWNHDAWIHDAIWHDAIWOHDAIOHDAIWHD.com', FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -437,7 +489,15 @@ class Kohana_ValidTest extends Unittest_TestCase
{
return array(
array('somestring', 10, TRUE),
array('somestring', 11, FALSE),
array('anotherstring', 13, TRUE),
// Empty test
array('', 10, FALSE),
array(NULL, 10, FALSE),
array(FALSE, 10, FALSE),
// Test array of allowed lengths
array('somestring', array(1, 3, 5, 7, 9, 10), TRUE),
array('somestring', array(1, 3, 5, 7, 9), FALSE),
);
}
@@ -475,6 +535,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('1', '1', TRUE),
array(1, '1', FALSE),
array('011', 011, FALSE),
// Empty test
array('', 123, FALSE),
array(NULL, 123, FALSE),
array(FALSE, 123, FALSE),
);
}
@@ -506,11 +570,15 @@ class Kohana_ValidTest extends Unittest_TestCase
return array(
array('75.125.175.50', FALSE, TRUE),
// PHP 5.3.6 fixed a bug that allowed 127.0.0.1 as a public ip: http://bugs.php.net/53150
array('127.0.0.1', FALSE, version_compare(PHP_VERSION, '5.3.6', '<')),
array('256.257.258.259', FALSE, FALSE),
array('255.255.255.255', FALSE, FALSE),
array('192.168.0.1', FALSE, FALSE),
array('192.168.0.1', TRUE, TRUE)
array('127.0.0.1', FALSE, version_compare(PHP_VERSION, '5.3.6', '<')),
array('256.257.258.259', FALSE, FALSE),
array('255.255.255.255', FALSE, FALSE),
array('192.168.0.1', FALSE, FALSE),
array('192.168.0.1', TRUE, TRUE),
// Empty test
array('', TRUE, FALSE),
array(NULL, TRUE, FALSE),
array(FALSE, TRUE, FALSE),
);
}
@@ -544,7 +612,11 @@ class Kohana_ValidTest extends Unittest_TestCase
// Exceeds
array('KOHANARULLLES', 2, FALSE),
// Under
array('CakeSucks', 10, TRUE)
array('CakeSucks', 10, TRUE),
// Empty test
array('', -10, FALSE),
array(NULL, -10, FALSE),
array(FALSE, -10, FALSE),
);
}
@@ -577,7 +649,11 @@ class Kohana_ValidTest extends Unittest_TestCase
return array(
array('This is obviously long enough', 10, TRUE),
array('This is not', 101, FALSE),
array('This is on the borderline', 25, TRUE)
array('This is on the borderline', 25, TRUE),
// Empty test
array('', 10, FALSE),
array(NULL, 10, FALSE),
array(FALSE, 10, FALSE),
);
}
@@ -668,6 +744,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('-5.', TRUE),
array('.', FALSE),
array('1.2.3', FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
}
@@ -695,8 +775,8 @@ class Kohana_ValidTest extends Unittest_TestCase
{
return array(
array('0163634840', NULL, TRUE),
array('+27173634840', NULL, TRUE),
array('123578', NULL, FALSE),
array('+27173634840', NULL, TRUE),
array('123578', NULL, FALSE),
// Some uk numbers
array('01234456778', NULL, TRUE),
array('+0441234456778', NULL, FALSE),
@@ -704,6 +784,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('+44 20-7031-3000', array(12), TRUE),
// BT Corporate
array('020 7356 5000', NULL, TRUE),
// Empty test
array('', NULL, FALSE),
array(NULL, NULL, FALSE),
array(FALSE, NULL, FALSE),
);
}
@@ -733,6 +817,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('123456789', '/[0-9]++/', TRUE),
array('£$%£%', '/[abc]/', FALSE),
array('Good evening', '/hello/', FALSE),
// Empty test
array('', '/hello/', FALSE),
array(NULL, '/hello/', FALSE),
array(FALSE, '/hello/', FALSE),
);
}
@@ -743,9 +831,9 @@ class Kohana_ValidTest extends Unittest_TestCase
*
* @test
* @dataProvider provider_regex
* @param string Value to test against
* @param string Valid pcre regular expression
* @param bool Does the value match the expression?
* @param string $value Value to test against
* @param string $regex Valid pcre regular expression
* @param bool $expected Does the value match the expression?
*/
public function test_regex($value, $regex, $expected)
{
@@ -761,12 +849,19 @@ class Kohana_ValidTest extends Unittest_TestCase
public function provider_range()
{
return array(
array(1, 0, 2, TRUE),
array(-1, -5, 0, TRUE),
array(-1, 0, 1, FALSE),
array(1, 0, 0, FALSE),
array(2147483647, 0, 200000000000000, TRUE),
array(-2147483647, -2147483655, 2147483645, TRUE)
array(1, 0, 2, NULL, TRUE),
array(-1, -5, 0, NULL, TRUE),
array(-1, 0, 1, NULL, FALSE),
array(1, 0, 0, NULL, FALSE),
array(2147483647, 0, 200000000000000, NULL, TRUE),
array(-2147483647, -2147483655, 2147483645, NULL, TRUE),
// #4043
array(2, 0, 10, 2, TRUE),
array(3, 0, 10, 2, FALSE),
// Empty test
array('', 5, 10, NULL, FALSE),
array(NULL, 5, 10, NULL, FALSE),
array(FALSE, 5, 10, NULL, FALSE),
);
}
@@ -782,11 +877,11 @@ class Kohana_ValidTest extends Unittest_TestCase
* @param integer $max Upper bound
* @param boolean $expected Is Number within the bounds of $min && $max
*/
public function test_range($number, $min, $max, $expected)
public function test_range($number, $min, $max, $step, $expected)
{
$this->AssertSame(
$expected,
Valid::range($number, $min, $max)
Valid::range($number, $min, $max, $step)
);
}
@@ -824,6 +919,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array('http://127.0.0.1.1', FALSE),
array('http://user:@127.0.0.1', FALSE),
array("http://finalnewline.com\n", FALSE),
// Empty test
array('', FALSE),
array(NULL, FALSE),
array(FALSE, FALSE),
);
$data[] = array('http://'.str_repeat('123456789.', 25).'com/', TRUE); // 253 chars
@@ -857,6 +956,10 @@ class Kohana_ValidTest extends Unittest_TestCase
array(array('a' => 'hello', 'b' => 'hello'), 'a', 'b', TRUE),
array(array('a' => 'hello', 'b' => 'hello '), 'a', 'b', FALSE),
array(array('a' => '1', 'b' => 1), 'a', 'b', FALSE),
// Empty test
array(array('a' => '', 'b' => 'hello'), 'a', 'b', FALSE),
array(array('a' => NULL, 'b' => 'hello'), 'a', 'b', FALSE),
array(array('a' => FALSE, 'b' => 'hello'), 'a', 'b', FALSE),
);
}

View File

@@ -4,13 +4,14 @@
* Tests the Validation lib that's shipped with Kohana
*
* @group kohana
* @group kohana.validation
* @group kohana.core
* @group kohana.core.validation
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ValidationTest extends Unittest_TestCase
@@ -37,7 +38,7 @@ class Kohana_ValidationTest extends Unittest_TestCase
$this->assertSame(
$values,
$instance->as_array()
$instance->data()
);
}
@@ -72,7 +73,7 @@ class Kohana_ValidationTest extends Unittest_TestCase
);
}
$this->assertSame($copy_data, $copy->as_array());
$this->assertSame($copy_data, $copy->data());
}
/**
@@ -165,6 +166,27 @@ class Kohana_ValidationTest extends Unittest_TestCase
$this->assertAttributeSame(array(':foo' => 'some other value'), '_bound', $validation);
}
/**
* We should be able to used bound variables in callbacks
*
* @test
* @covers Validation::check
*/
public function test_bound_callback()
{
$data = array(
'kung fu' => 'fighting',
'fast' => 'cheetah',
);
$validation = new Validation($data);
$validation->bind(':class', 'Valid')
// Use the bound value in a callback
->rule('fast', array(':class', 'max_length'), array(':value', 2));
// The rule should have run and check() should fail
$this->assertSame($validation->check(), FALSE);
}
/**
* Provides test data for test_check
*
@@ -292,6 +314,42 @@ class Kohana_ValidationTest extends Unittest_TestCase
$this->assertSame($expected, $validation->check());
}
/**
* Tests Validation::check()
*
* @test
* @covers Validation::check
*/
public function test_check_stops_when_error_added_by_callback()
{
$validation = new Validation(array(
'foo' => 'foo',
));
$validation
->rule('foo', array($this, '_validation_callback'), array(':validation'))
// This rule should never run
->rule('foo', 'min_length', array(':value', 20));
$validation->check();
$errors = $validation->errors();
$expected = array(
'foo' => array(
0 => '_validation_callback',
1 => NULL,
),
);
$this->assertSame($errors, $expected);
}
public function _validation_callback(Validation $object)
{
// Simply add the error
$object->error('foo', '_validation_callback');
}
/**
* Provides test data for test_errors()
*
@@ -385,7 +443,7 @@ class Kohana_ValidationTest extends Unittest_TestCase
$current = i18n::lang();
i18n::lang('es');
foreach($rules as $field => $field_rules)
foreach ($rules as $field => $field_rules)
{
$validation->rules($field, $field_rules);
}
@@ -465,9 +523,9 @@ class Kohana_ValidationTest extends Unittest_TestCase
$validation = Validation::factory(array('foo' => 'bar'))
->rule('something', 'not_empty');
$before = $validation->as_array();
$before = $validation->data();
$validation->check();
$after = $validation->as_array();
$after = $validation->data();
$expected = array('foo' => 'bar');
@@ -484,7 +542,7 @@ class Kohana_ValidationTest extends Unittest_TestCase
public function test_object_parameters_not_in_messages()
{
$validation = Validation::factory(array('foo' => 'foo'))
->rule('bar', 'matches', array(':validation', 'foo', ':field'));
->rule('bar', 'matches', array(':validation', ':field', 'foo'));
$validation->check();
$errors = $validation->errors('validation');
@@ -492,4 +550,127 @@ class Kohana_ValidationTest extends Unittest_TestCase
$this->assertSame($expected, $errors);
}
/**
* Tests Validation::as_array()
*
* @test
* @covers Validation::as_array
*/
public function test_as_array_returns_original_array()
{
$data = array(
'one' => 'hello',
'two' => 'world',
'ten' => '',
);
$validation = Validation::factory($data);
$this->assertSame($data, $validation->as_array());
}
/**
* Tests Validation::data()
*
* @test
* @covers Validation::data
*/
public function test_data_returns_original_array()
{
$data = array(
'one' => 'hello',
'two' => 'world',
'ten' => '',
);
$validation = Validation::factory($data);
$this->assertSame($data, $validation->data());
}
// @codingStandardsIgnoreStart
public function test_offsetExists()
// @codingStandardsIgnoreEnd
{
$array = array(
'one' => 'Hello',
'two' => 'World',
'ten' => NULL,
);
$validation = Validation::factory($array);
$this->assertTrue(isset($validation['one']));
$this->assertFalse(isset($validation['ten']));
$this->assertFalse(isset($validation['five']));
}
// @codingStandardsIgnoreStart
public function test_offsetSet_throws_exception()
// @codingStandardsIgnoreEnd
{
$this->setExpectedException('Kohana_Exception');
$validation = Validation::factory(array());
// Validation is read-only
$validation['field'] = 'something';
}
// @codingStandardsIgnoreStart
public function test_offsetGet()
// @codingStandardsIgnoreEnd
{
$array = array(
'one' => 'Hello',
'two' => 'World',
'ten' => NULL,
);
$validation = Validation::factory($array);
$this->assertSame($array['one'], $validation['one']);
$this->assertSame($array['two'], $validation['two']);
$this->assertSame($array['ten'], $validation['ten']);
}
// @codingStandardsIgnoreStart
public function test_offsetUnset()
// @codingStandardsIgnoreEnd
{
$this->setExpectedException('Kohana_Exception');
$validation = Validation::factory(array(
'one' => 'Hello, World!',
));
// Validation is read-only
unset($validation['one']);
}
/**
* http://dev.kohanaframework.org/issues/4365
*
* @test
* @covers Validation::errors
*/
public function test_error_type_check()
{
$array = array(
'email' => 'not an email address',
);
$validation = Validation::factory($array)
->rule('email', 'not_empty')
->rule('email', 'email')
;
$validation->check();
$errors = $validation->errors('tests/validation/error_type_check');
$this->assertSame($errors, $validation->errors('validation'));
}
}

View File

@@ -4,12 +4,13 @@
* Tests the View class
*
* @group kohana
* @group kohana.view
* @group kohana.core
* @group kohana.core.view
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ViewTest extends Unittest_TestCase
@@ -21,12 +22,14 @@ class Kohana_ViewTest extends Unittest_TestCase
*
* @return null
*/
// @codingStandardsIgnoreStart
public static function setupBeforeClass()
// @codingStandardsIgnoreEnd
{
self::$old_modules = Kohana::modules();
$new_modules = self::$old_modules+array(
'test_views' => realpath(__DIR__.'/../test_data/')
'test_views' => realpath(dirname(__FILE__).'/../test_data/')
);
Kohana::modules($new_modules);
}
@@ -36,7 +39,9 @@ class Kohana_ViewTest extends Unittest_TestCase
*
* @return null
*/
// @codingStandardsIgnoreStart
public static function teardownAfterClass()
// @codingStandardsIgnoreEnd
{
Kohana::modules(self::$old_modules);
}
@@ -70,9 +75,9 @@ class Kohana_ViewTest extends Unittest_TestCase
$view = new View($path);
$this->assertSame(FALSE, $expects_exception);
}
catch(Kohana_View_Exception $e)
catch(View_Exception $e)
{
$this->assertSame(TRUE, $expects_exception);
}
}
}
}

View File

@@ -0,0 +1,488 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for generic Request_Client class
*
* @group kohana
* @group kohana.core
* @group kohana.core.request
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Andrew Coulton
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_ClientTest extends Unittest_TestCase
{
protected $_inital_request;
protected static $_original_routes;
// @codingStandardsIgnoreStart - PHPUnit does not follow standards
/**
* Sets up a new route to ensure that we have a matching route for our
* Controller_RequestClientDummy class.
*/
public static function setUpBeforeClass()
{
// @codingStandardsIgnoreEnd
parent::setUpBeforeClass();
// Set a new Route to the ClientTest controller as the first route
// This requires reflection as the API for editing defined routes is limited
$route_class = new ReflectionClass('Route');
$routes_prop = $route_class->getProperty('_routes');
$routes_prop->setAccessible(TRUE);
self::$_original_routes = $routes_prop->getValue('Route');
$routes = array(
'ko_request_clienttest' => new Route('<controller>/<action>/<data>',array('data'=>'.+'))
) + self::$_original_routes;
$routes_prop->setValue('Route',$routes);
}
// @codingStandardsIgnoreStart - PHPUnit does not follow standards
/**
* Resets the application's routes to their state prior to this test case
*/
public static function tearDownAfterClass()
{
// @codingStandardsIgnoreEnd
// Reset routes
$route_class = new ReflectionClass('Route');
$routes_prop = $route_class->getProperty('_routes');
$routes_prop->setAccessible(TRUE);
$routes_prop->setValue('Route',self::$_original_routes);
parent::tearDownAfterClass();
}
// @codingStandardsIgnoreStart - PHPUnit does not follow standards
public function setUp()
{
// @codingStandardsIgnoreEnd
parent::setUp();
$this->_initial_request = Request::$initial;
Request::$initial = new Request('/');
}
// @codingStandardsIgnoreStart - PHPUnit does not follow standards
public function tearDown()
{
// @codingStandardsIgnoreEnd
Request::$initial = $this->_initial_request;
parent::tearDown();
}
/**
* Generates an internal URI to the [Controller_RequestClientDummy] shunt
* controller - the URI contains an encoded form of the required server
* response.
*
* @param string $status HTTP response code to issue
* @param array $headers HTTP headers to send with the response
* @param string $body A string to send back as response body (included in the JSON response)
* @return string
*/
protected function _dummy_uri($status, $headers, $body)
{
$data = array(
'status' => $status,
'header' => $headers,
'body' => $body
);
return "/requestclientdummy/fake".'/'.urlencode(http_build_query($data));
}
/**
* Shortcut method to generate a simple redirect URI - the first request will
* receive a redirect with the given HTTP status code and the second will
* receive a 200 response. The 'body' data value in the first response will
* be 'not-followed' and in the second response it will be 'followed'. This
* allows easy assertion that a redirect has taken place.
*
* @param string $status HTTP response code to issue
* @return string
*/
protected function _dummy_redirect_uri($status)
{
return $this->_dummy_uri($status,
array('Location' => $this->_dummy_uri(200, NULL, 'followed')),
'not-followed');
}
/**
* Provider for test_follows_redirects
* @return array
*/
public function provider_follows_redirects()
{
return array(
array(TRUE, $this->_dummy_uri(200, NULL, 'not-followed'), 'not-followed'),
array(TRUE, $this->_dummy_redirect_uri(200), 'not-followed'),
array(TRUE, $this->_dummy_redirect_uri(302), 'followed'),
array(FALSE, $this->_dummy_redirect_uri(302), 'not-followed'),
);
}
/**
* Tests that the client optionally follows properly formed redirects
*
* @dataProvider provider_follows_redirects
*
* @param bool $follow Option value to set
* @param string $request_url URL to request initially (contains data to set up redirect etc)
* @param string $expect_body Body text expected in the eventual result
*/
public function test_follows_redirects($follow, $request_url, $expect_body)
{
$response = Request::factory($request_url,
array('follow' => $follow))
->execute();
$data = json_decode($response->body(), TRUE);
$this->assertEquals($expect_body, $data['body']);
}
/**
* Tests that only specified headers are resent following a redirect
*/
public function test_follows_with_headers()
{
$response = Request::factory(
$this->_dummy_redirect_uri(301),
array(
'follow' => TRUE,
'follow_headers' => array('Authorization', 'X-Follow-With-Value')
))
->headers(array(
'Authorization' => 'follow',
'X-Follow-With-Value' => 'follow',
'X-Not-In-Follow' => 'no-follow'
))
->execute();
$data = json_decode($response->body(),TRUE);
$headers = $data['rq_headers'];
$this->assertEquals('followed', $data['body']);
$this->assertEquals('follow', $headers['authorization']);
$this->assertEquals('follow', $headers['x-follow-with-value']);
$this->assertFalse(isset($headers['x-not-in-follow']), 'X-Not-In-Follow should not be passed to next request');
}
/**
* Provider for test_follows_with_strict_method
*
* @return array
*/
public function provider_follows_with_strict_method()
{
return array(
array(201, NULL, Request::POST, Request::GET),
array(301, NULL, Request::GET, Request::GET),
array(302, TRUE, Request::POST, Request::POST),
array(302, FALSE, Request::POST, Request::GET),
array(303, NULL, Request::POST, Request::GET),
array(307, NULL, Request::POST, Request::POST),
);
}
/**
* Tests that the correct method is used (allowing for the strict_redirect setting)
* for follow requests.
*
* @dataProvider provider_follows_with_strict_method
*
* @param string $status_code HTTP response code to fake
* @param bool $strict_redirect Option value to set
* @param string $orig_method Request method for the original request
* @param string $expect_method Request method expected for the follow request
*/
public function test_follows_with_strict_method($status_code, $strict_redirect, $orig_method, $expect_method)
{
$response = Request::factory($this->_dummy_redirect_uri($status_code),
array(
'follow' => TRUE,
'strict_redirect' => $strict_redirect
))
->method($orig_method)
->execute();
$data = json_decode($response->body(), TRUE);
$this->assertEquals('followed', $data['body']);
$this->assertEquals($expect_method, $data['rq_method']);
}
/**
* Provider for test_follows_with_body_if_not_get
*
* @return array
*/
public function provider_follows_with_body_if_not_get()
{
return array(
array('GET','301',NULL),
array('POST','303',NULL),
array('POST','307','foo-bar')
);
}
/**
* Tests that the original request body is sent when following a redirect
* (unless redirect method is GET)
*
* @dataProvider provider_follows_with_body_if_not_get
* @depends test_follows_with_strict_method
* @depends test_follows_redirects
*
* @param string $original_method Request method to use for the original request
* @param string $status Redirect status that will be issued
* @param string $expect_body Expected value of body() in the second request
*/
public function test_follows_with_body_if_not_get($original_method, $status, $expect_body)
{
$response = Request::factory($this->_dummy_redirect_uri($status),
array('follow' => TRUE))
->method($original_method)
->body('foo-bar')
->execute();
$data = json_decode($response->body(), TRUE);
$this->assertEquals('followed', $data['body']);
$this->assertEquals($expect_body, $data['rq_body']);
}
/**
* Provider for test_triggers_header_callbacks
*
* @return array
*/
public function provider_triggers_header_callbacks()
{
return array(
// Straightforward response manipulation
array(
array('X-test-1' =>
function($request, $response, $client)
{
$response->body(json_encode(array('body'=>'test1-body-changed')));
return $response;
}),
$this->_dummy_uri(200, array('X-test-1' => 'foo'), 'test1-body'),
'test1-body-changed'
),
// Subsequent request execution
array(
array('X-test-2' =>
function($request, $response, $client)
{
return Request::factory($response->headers('X-test-2'));
}),
$this->_dummy_uri(200,
array('X-test-2' => $this->_dummy_uri(200, NULL, 'test2-subsequent-body')),
'test2-orig-body'),
'test2-subsequent-body'
),
// No callbacks triggered
array(
array('X-test-3' =>
function ($request, $response, $client)
{
throw new Exception("Unexpected execution of X-test-3 callback");
}),
$this->_dummy_uri(200, array('X-test-1' => 'foo'), 'test3-body'),
'test3-body'
),
// Callbacks not triggered once a previous callback has created a new response
array(
array(
'X-test-1' =>
function($request, $response, $client)
{
return Request::factory($response->headers('X-test-1'));
},
'X-test-2' =>
function($request, $response, $client)
{
return Request::factory($response->headers('X-test-2'));
}
),
$this->_dummy_uri(200,
array(
'X-test-1' => $this->_dummy_uri(200, NULL, 'test1-subsequent-body'),
'X-test-2' => $this->_dummy_uri(200, NULL, 'test2-subsequent-body')
),
'test2-orig-body'),
'test1-subsequent-body'
),
// Nested callbacks are supported if callback creates new request
array(
array(
'X-test-1' =>
function($request, $response, $client)
{
return Request::factory($response->headers('X-test-1'));
},
'X-test-2' =>
function($request, $response, $client)
{
return Request::factory($response->headers('X-test-2'));
}
),
$this->_dummy_uri(200,
array(
'X-test-1' => $this->_dummy_uri(
200,
array('X-test-2' => $this->_dummy_uri(200, NULL, 'test2-subsequent-body')),
'test1-subsequent-body'),
),
'test-orig-body'),
'test2-subsequent-body'
),
);
}
/**
* Tests that header callbacks are triggered in sequence when specific headers
* are present in the response
*
* @dataProvider provider_triggers_header_callbacks
*
* @param array $callbacks Array of header callbacks
* @param array $headers Headers that will be received in the response
* @param string $expect_body Response body content to expect
*/
public function test_triggers_header_callbacks($callbacks, $uri, $expect_body)
{
$response = Request::factory($uri,
array('header_callbacks' => $callbacks))
->execute();
$data = json_decode($response->body(), TRUE);
$this->assertEquals($expect_body, $data['body']);
}
/**
* Tests that the Request_Client is protected from too many recursions of
* requests triggered by header callbacks.
*
*/
public function test_deep_recursive_callbacks_are_aborted()
{
$uri = $this->_dummy_uri('200', array('x-cb' => '1'), 'body');
// Temporary property to track requests
$this->requests_executed = 0;
try
{
$response = Request::factory(
$uri,
array(
'header_callbacks' => array(
'x-cb' =>
function ($request, $response, $client)
{
$client->callback_params('testcase')->requests_executed++;
// Recurse into a new request
return Request::factory($request->uri());
}),
'max_callback_depth' => 2,
'callback_params' => array(
'testcase' => $this,
)
))
->execute();
}
catch (Request_Client_Recursion_Exception $e)
{
// Verify that two requests were executed
$this->assertEquals(2, $this->requests_executed);
return;
}
$this->fail('Expected Request_Client_Recursion_Exception was not thrown');
}
/**
* Header callback for testing that arbitrary callback_params are available
* to the callback.
*
* @param Request $request
* @param Response $response
* @param Request_Client $client
*/
public function callback_assert_params($request, $response, $client)
{
$this->assertEquals('foo', $client->callback_params('constructor_param'));
$this->assertEquals('bar', $client->callback_params('setter_param'));
$response->body('assertions_ran');
}
/**
* Test that arbitrary callback_params can be passed to the callback through
* the Request_Client and are assigned to subsequent requests
*/
public function test_client_can_hold_params_for_callbacks()
{
// Test with param in constructor
$request = Request::factory(
$this->_dummy_uri(
302,
array('Location' => $this->_dummy_uri('200',array('X-cb'=>'1'), 'followed')),
'not-followed'),
array(
'follow' => TRUE,
'header_callbacks' => array(
'x-cb' => array($this, 'callback_assert_params'),
'location' => 'Request_Client::on_header_location',
),
'callback_params' => array(
'constructor_param' => 'foo'
)
));
// Test passing param to setter
$request->client()->callback_params('setter_param', 'bar');
// Callback will throw assertion exceptions when executed
$response = $request->execute();
$this->assertEquals('assertions_ran', $response->body());
}
} // End Kohana_Request_ClientTest
/**
* Dummy controller class that acts as a shunt - passing back request information
* in the response to allow inspection.
*/
class Controller_RequestClientDummy extends Controller {
/**
* Takes a urlencoded 'data' parameter from the route and uses it to craft a
* response. Redirect chains can be tested by passing another encoded uri
* as a location header with an appropriate status code.
*/
public function action_fake()
{
parse_str(urldecode($this->request->param('data')), $data);
$this->response->status(Arr::get($data, 'status', 200));
$this->response->headers(Arr::get($data, 'header', array()));
$this->response->body(json_encode(array(
'body'=> Arr::get($data,'body','ok'),
'rq_headers' => $this->request->headers(),
'rq_body' => $this->request->body(),
'rq_method' => $this->request->method(),
)));
}
} // End Controller_RequestClientDummy

View File

@@ -0,0 +1,191 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for external request client
*
* @group kohana
* @group kohana.request
* @group kohana.request.client
* @group kohana.request.client.external
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Client_ExternalTest extends Unittest_TestCase {
/**
* Provider for test_factory()
*
* @return array
*/
public function provider_factory()
{
Request_Client_External::$client = 'Request_Client_Stream';
$return = array(
array(
array(),
NULL,
'Request_Client_Stream'
),
array(
array(),
'Request_Client_Stream',
'Request_Client_Stream'
)
);
if (extension_loaded('curl'))
{
$return[] = array(
array(),
'Request_Client_Curl',
'Request_Client_Curl'
);
}
if (extension_loaded('http'))
{
$return[] = array(
array(),
'Request_Client_HTTP',
'Request_Client_HTTP'
);
}
return $return;
}
/**
* Tests the [Request_Client_External::factory()] method
*
* @dataProvider provider_factory
*
* @param array $params params
* @param string $client client
* @param Request_Client_External $expected expected
* @return void
*/
public function test_factory($params, $client, $expected)
{
$this->assertInstanceOf($expected, Request_Client_External::factory($params, $client));
}
/**
* Data provider for test_options
*
* @return array
*/
public function provider_options()
{
return array(
array(
NULL,
NULL,
array()
),
array(
array('foo' => 'bar', 'stfu' => 'snafu'),
NULL,
array('foo' => 'bar', 'stfu' => 'snafu')
),
array(
'foo',
'bar',
array('foo' => 'bar')
),
array(
array('foo' => 'bar'),
'foo',
array('foo' => 'bar')
)
);
}
/**
* Tests the [Request_Client_External::options()] method
*
* @dataProvider provider_options
*
* @param mixed $key key
* @param mixed $value value
* @param array $expected expected
* @return void
*/
public function test_options($key, $value, $expected)
{
// Create a mock external client
$client = new Request_Client_Stream;
$client->options($key, $value);
$this->assertSame($expected, $client->options());
}
/**
* Data provider for test_execute
*
* @return array
*/
public function provider_execute()
{
$json = '{"foo": "bar", "snafu": "stfu"}';
$post = array('foo' => 'bar', 'snafu' => 'stfu');
return array(
array(
'application/json',
$json,
array(),
array(
'content-type' => 'application/json',
'body' => $json
)
),
array(
'application/json',
$json,
$post,
array(
'content-type' => 'application/x-www-form-urlencoded',
'body' => http_build_query($post, NULL, '&')
)
)
);
}
/**
* Tests the [Request_Client_External::_send_message()] method
*
* @dataProvider provider_execute
*
* @return void
*/
public function test_execute($content_type, $body, $post, $expected)
{
$old_request = Request::$initial;
Request::$initial = TRUE;
// Create a mock Request
$request = new Request('http://kohanaframework.org/');
$request->method(HTTP_Request::POST)
->headers('content-type', $content_type)
->body($body)
->post($post);
$client = $this->getMock('Request_Client_External', array('_send_message'));
$client->expects($this->once())
->method('_send_message')
->with($request)
->will($this->returnValue($this->getMock('Response')));
$request->client($client);
$this->assertInstanceOf('Response', $request->execute());
$this->assertSame($expected['body'], $request->body());
$this->assertSame($expected['content-type'], (string) $request->headers('content-type'));
Request::$initial = $old_request;
}
}

View File

@@ -0,0 +1,68 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for internal request client
*
* @group kohana
* @group kohana.core
* @group kohana.core.request
* @group kohana.core.request.client
* @group kohana.core.request.client.internal
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Client_InternalTest extends Unittest_TestCase
{
public function provider_response_failure_status()
{
return array(
array('', 'Welcome', 'missing_action', 'Welcome/missing_action', 404),
array('kohana3', 'missing_controller', 'index', 'kohana3/missing_controller/index', 404),
array('', 'Template', 'missing_action', 'kohana3/Template/missing_action', 500),
);
}
/**
* Tests for correct exception messages
*
* @test
* @dataProvider provider_response_failure_status
*
* @return null
*/
public function test_response_failure_status($directory, $controller, $action, $uri, $expected)
{
// Mock for request object
$request = $this->getMock('Request', array('directory', 'controller', 'action', 'uri', 'response'), array($uri));
$request->expects($this->any())
->method('directory')
->will($this->returnValue($directory));
$request->expects($this->any())
->method('controller')
->will($this->returnValue($controller));
$request->expects($this->any())
->method('action')
->will($this->returnValue($action));
$request->expects($this->any())
->method('uri')
->will($this->returnValue($uri));
$request->expects($this->any())
->method('response')
->will($this->returnValue($this->getMock('Response')));
$internal_client = new Request_Client_Internal;
$response = $internal_client->execute($request);
$this->assertSame($expected, $response->status());
}
}

View File

@@ -1,89 +0,0 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for internal request client
*
* @group kohana
* @group kohana.request
* @group kohana.request.client
* @group kohana.request.client.internal
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Client_InternalTest extends Unittest_TestCase
{
public function provider_exceptions()
{
return array(
array('', 'welcome', 'missing_action', 'welcome/missing_action',
'The requested URL welcome/missing_action was not found on this server.'),
array('kohana3', 'missing_controller', 'index', 'kohana3/missing_controller/index',
'The requested URL kohana3/missing_controller/index was not found on this server.'),
array('', 'template', 'missing_action', 'kohana3/template/missing_action',
'Cannot create instances of abstract controller_template'),
);
}
/**
* Tests for correct exception messages
*
* @test
* @dataProvider provider_exceptions
*
* @return null
*/
public function test_exceptions($directory, $controller, $action, $uri, $expected)
{
// Mock for request object
$request = $this->getMock('Request', array(), array($uri));
$request->expects($this->any())
->method('directory')
->will($this->returnValue($directory));
$request->expects($this->any())
->method('controller')
->will($this->returnValue($controller));
$request->expects($this->any())
->method('action')
->will($this->returnValue($action));
$request->expects($this->any())
->method('uri')
->will($this->returnValue($uri));
$request->expects($this->any())
->method('response')
->will($this->returnValue($this->getMock('Response')));
$internal_client = new Request_Client_Internal;
try
{
$internal_client->execute($request);
}
catch(HTTP_Exception_404 $e)
{
if ($e->getMessage() !== $expected)
{
$this->fail('Was expecting "'.$expected.'" but got "'.$e->getMessage().'" instead.');
}
return;
}
catch(Kohana_Exception $e)
{
if ($e->getMessage() !== $expected)
{
$this->fail('Was expecting "'.$expected.'" but got "'.$e->getMessage().'" instead.');
}
return;
}
$this->fail('A HTTP_Exception_404 or Kohana_Exception exception was expected.');
}
}

View File

@@ -6,7 +6,7 @@
*
* @package Unittest
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Route_Holder
@@ -16,7 +16,7 @@ class Route_Holder
*/
public static function default_callback($uri)
{
}
/**
@@ -27,7 +27,7 @@ class Route_Holder
public static function default_return_callback($uri)
{
return array(
);
}
@@ -70,9 +70,31 @@ class Route_Holder
if ($uri == 'info/about_us')
{
return array(
);
}
}
/**
* Route callback for test route_filter_modify_params
*
* @return array
*/
public static function route_filter_modify_params_array(Route $route, $params)
{
$params['action'] = 'modified';
return $params;
}
/**
* Route callback for test route_filter_modify_params
*
* @return array
*/
public static function route_filter_modify_params_false(Route $route, $params)
{
return FALSE;
}
}