Update Kohana to 3.1.3.1

This commit is contained in:
Deon George
2011-05-16 22:47:16 +10:00
parent 8b658b497a
commit ff2370c368
401 changed files with 14070 additions and 10213 deletions

View File

@@ -4,14 +4,16 @@
* Tests the Arr lib that's shipped with kohana
*
* @group kohana
* @group kohana.arr
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_ArrTest extends Kohana_Unittest_TestCase
class Kohana_ArrTest extends Unittest_TestCase
{
/**
* Provides test data for test_callback()
@@ -95,6 +97,42 @@ Class Kohana_ArrTest extends Kohana_Unittest_TestCase
$this->assertSame($expected, $array);
}
/**
* Provides test data for test_pluck
*
* @return array
*/
public function provider_pluck()
{
return array(
array(
array(
array('id' => 20, 'name' => 'John Smith'),
array('name' => 'Linda'),
array('id' => 25, 'name' => 'Fred'),
),
'id',
array(20, 25)
),
);
}
/**
* Tests Arr::pluck()
*
* @test
* @dataProvider provider_pluck
* @param array $array
* @param string $key
* @param array $expected
*/
public function test_pluck(array $array, $key, $expected)
{
$array = Arr::pluck($array, $key);
$this->assertSame(count($expected), count($array));
$this->assertSame($expected, $array);
}
/**
* Provides test data for test_get()
@@ -208,6 +246,11 @@ Class Kohana_ArrTest extends Kohana_Unittest_TestCase
array(array('test1'), array('test2')),
array(array('test3'), array('test4')),
),
array(
array(array('test1','test3'), array('test2','test4')),
array(array('test1'), array('test2')),
array(array('test3'), array('test4')),
),
array(
array('digits' => array(0, 1, 2, 3)),
array('digits' => array(0, 1)),
@@ -318,6 +361,42 @@ Class Kohana_ArrTest extends Kohana_Unittest_TestCase
);
}
/**
* Provides test data for test_path()
*
* @return array
*/
public function provider_set_path()
{
return array(
// Tests returns normal values
array(array('foo' => 'bar'), array(), 'foo', 'bar'),
array(array('kohana' => array('is' => 'awesome')), array(), 'kohana.is', 'awesome'),
array(array('kohana' => array('is' => 'cool', 'and' => 'slow')),
array('kohana' => array('is' => 'cool')), 'kohana.and', 'slow'),
// Custom delimiters
array(array('kohana' => array('is' => 'awesome')), array(), 'kohana/is', 'awesome', '/'),
// Ensures set_path() casts ints to actual integers for keys
array(array('foo' => array('bar')), array('foo' => array('test')), 'foo.0', 'bar'),
);
}
/**
* Tests Arr::path()
*
* @test
* @dataProvider provider_set_path
* @param string $path The path to follow
* @param boolean $expected The expected value
* @param string $delimiter The path delimiter
*/
public function test_set_path($expected, $array, $path, $value, $delimiter = NULL)
{
Arr::set_path($array, $path, $value, $delimiter);
$this->assertSame($expected, $array);
}
/**
* Provides test data for test_range()
*
@@ -422,31 +501,6 @@ Class Kohana_ArrTest extends Kohana_Unittest_TestCase
);
}
/**
* Provides test data for test_binary_search
*
* @return array Test Data
*/
public function provider_binary_search()
{
return array(
array(2, 'john', array('mary', 'louise', 'john', 'kent'))
);
}
/**
*
* @test
* @dataProvider provider_binary_search
*/
public function test_binary_search($expected, $needle, $haystack, $sort = FALSE)
{
$this->assertSame(
$expected,
Arr::binary_search($needle, $haystack, $sort)
);
}
/**
* Provides test data for test_map
*

View File

@@ -9,13 +9,14 @@
* @group kohana.cli
*
* @see CLI
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_CLITest extends Kohana_Unittest_TestCase
class Kohana_CLITest extends Unittest_TestCase
{
/**

View File

@@ -4,28 +4,30 @@
* Tests the Config lib that's shipped with kohana
*
* @group kohana
* @group kohana.config
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @author Matt Button <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
class Kohana_ConfigTest extends Unittest_TestCase
{
/**
* Calling Kohana_Config::instance() should return the global singleton
* Calling Config::instance() should return the global singleton
* which should persist
*
* @test
* @covers Kohana_Config::instance
* @covers Config::instance
*/
public function test_instance_returns_singleton_instance()
{
$this->assertSame(Kohana_Config::instance(), Kohana_Config::instance());
$this->assertNotSame(new Kohana_Config, Kohana_Config::instance());
$this->assertSame(Config::instance(), Config::instance());
$this->assertNotSame(new Config, Config::instance());
}
/**
@@ -33,11 +35,11 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* no readers attached
*
* @test
* @covers Kohana_Config
* @covers Config
*/
public function test_initially_there_are_no_readers()
{
$config = new Kohana_Config;
$config = new Config;
$this->assertAttributeSame(array(), '_readers', $config);
}
@@ -47,12 +49,12 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* adds the specified reader to the config object
*
* @test
* @covers Kohana_Config::attach
* @covers Config::attach
*/
public function test_attach_adds_reader_and_returns_this()
{
$config = new Kohana_Config;
$reader = $this->getMock('Kohana_Config_Reader');
$config = new Config;
$reader = $this->getMock('Config_Reader');
$this->assertSame($config, $config->attach($reader));
@@ -64,14 +66,14 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* should prepend the reader to the front of the readers queue
*
* @test
* @covers Kohana_Config::attach
* @covers Config::attach
*/
public function test_attach_adds_reader_to_front_of_queue()
{
$config = new Kohana_Config;
$config = new Config;
$reader1 = $this->getMock('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader');
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader');
$config->attach($reader1);
$config->attach($reader2);
@@ -81,7 +83,7 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
$this->assertAttributeSame(array($reader2, $reader1), '_readers', $config);
// Now we test using the second parameter
$config = new Kohana_Config;
$config = new Config;
$config->attach($reader1);
$config->attach($reader2, TRUE);
@@ -94,13 +96,13 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* phpunit to append the reader rather than prepend
*
* @test
* @covers Kohana_Config::attach
* @covers Config::attach
*/
public function test_attach_can_add_reader_to_end_of_queue()
{
$config = new Kohana_Config;
$reader1 = $this->getMock('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader');
$config = new Config;
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader');
$config->attach($reader1);
$config->attach($reader2, FALSE);
@@ -112,19 +114,19 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* Calling detach() on a config object should remove it from the queue of readers
*
* @test
* @covers Kohana_Config::detach
* @covers Config::detach
*/
public function test_detach_removes_reader_and_returns_this()
{
$config = new Kohana_Config;
$config = new Config;
// Due to the way phpunit mock generator works if you try and mock a class
// 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('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader', array(), array(), 'MY_AWESOME_READER');
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader', array(), array(), 'MY_AWESOME_READER');
$config->attach($reader1);
$config->attach($reader2);
@@ -142,12 +144,12 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* detach() should return $this even if the specified reader does not exist
*
* @test
* @covers Kohana_Config::detach
* @covers Config::detach
*/
public function test_detach_returns_this_even_when_reader_dnx()
{
$config = new Kohana_Config;
$reader = $this->getMock('Kohana_Config_Reader');
$config = new Config;
$reader = $this->getMock('Config_Reader');
$this->assertSame($config, $config->detach($reader));
}
@@ -155,9 +157,9 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
/**
* If load() is called and there are no readers present then it should throw
* a kohana exception
*
*
* @test
* @covers Kohana_Config::load
* @covers Config::load
* @expectedException Kohana_Exception
*/
public function test_load_throws_exception_if_there_are_no_readers()
@@ -174,28 +176,28 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* is found
*
* @test
* @covers Kohana_Config::load
* @covers Config::load
*/
public function test_load_interrogates_each_reader_until_group_found()
{
$config = new Kohana_Config;
$config = new Config;
$config_group = 'groupy';
$reader1 = $this->getMock('Kohana_Config_Reader', array('load'));
$reader1 = $this->getMock('Config_Reader', array('load'));
$reader1
->expects($this->once())
->method('load')
->with($config_group)
->will($this->returnValue(FALSE));
$reader2 = $this->getMock('Kohana_Config_Reader', array('load'));
$reader2 = $this->getMock('Config_Reader', array('load'));
$reader2
->expects($this->once())
->method('load')
->with($config_group)
->will($this->returnValue($reader2));
$reader3 = $this->getMock('Kohana_Config_Reader', array('load'));
$reader3 = $this->getMock('Config_Reader', array('load'));
$reader3->expects($this->never())->method('load');
$config->attach($reader1, FALSE);
@@ -204,7 +206,7 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
// By asserting a return type we're making the test a little less brittle / less likely
// to break due to minor modifications
$this->assertInstanceOf('Kohana_Config_Reader', $config->load($config_group));
$this->assertInstanceOf('Config_Reader', $config->load($config_group));
}
/**
@@ -212,15 +214,15 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
* to create a new config group
*
* @test
* @covers Kohana_Config::load
* @covers Config::load
*/
public function test_load_returns_new_config_group_if_one_dnx()
{
$config = new Kohana_Config;
$config = new Config;
$group = 'my_group';
$reader1 = $this->getMock('Kohana_Config_Reader');
$reader2 = $this->getMock('Kohana_Config_Reader', array('load'), array(), 'Kohana_Config_Waffles');
$reader1 = $this->getMock('Config_Reader');
$reader2 = $this->getMock('Config_Reader', array('load'), array(), 'Config_Waffles');
// This is a slightly hacky way of doing it, but it works
$reader2
@@ -228,7 +230,7 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
->method('load')
->with($group)
->will($this->onConsecutiveCalls(
$this->returnValue(FALSE),
$this->returnValue(FALSE),
$this->returnValue(clone $reader2)
));
@@ -236,8 +238,7 @@ Class Kohana_ConfigTest extends Kohana_Unittest_TestCase
$new_config = $config->load('my_group');
$this->assertInstanceOf('Kohana_Config_Waffles', $new_config);
$this->assertInstanceOf('Config_Waffles', $new_config);
// Slightly taboo, testing a different api!!
$this->assertSame(array(), $new_config->as_array());
}

View File

@@ -4,15 +4,39 @@
* Tests the cookie class
*
* @group kohana
* @group kohana.cookie
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_CookieTest extends Kohana_Unittest_TestCase
class Kohana_CookieTest extends Unittest_TestCase
{
protected $_default_salt = 'AdaoidadnA£ASDNadnaoiwdnawd';
/**
* Sets up the environment
*/
public function setUp()
{
parent::setUp();
Cookie::$salt = $this->_default_salt;
}
/**
* Tears down the environment
*/
public function tearDown()
{
parent::tearDown();
Cookie::$salt = NULL;
}
/**
* Provides test data for test_set()
*
@@ -49,6 +73,10 @@ Class Kohana_CookieTest extends Kohana_Unittest_TestCase
*/
public function provider_get()
{
// setUp is called after the provider so we need to specify a
// salt here in order to use it in the provider
Cookie::$salt = $this->_default_salt;
return array(
array('foo', Cookie::salt('foo', 'bar').'~bar', 'bar'),
array('bar', Cookie::salt('foo', 'bar').'~bar', NULL),
@@ -70,7 +98,9 @@ Class Kohana_CookieTest extends Kohana_Unittest_TestCase
{
// Force $_COOKIE
if ($key !== NULL)
{
$_COOKIE[$key] = $value;
}
$this->assertSame($expected, cookie::get($key));
}
@@ -109,7 +139,7 @@ Class Kohana_CookieTest extends Kohana_Unittest_TestCase
public function provider_salt()
{
return array(
array('foo', 'bar', '795317c9df04d8061e6e134a9b3487dc9ad69117'),
array('foo', 'bar', 'b5773a6255d1deefc23f9f69bcc40fdc998e5802'),
);
}

View File

@@ -8,18 +8,19 @@
* @group kohana
* @group kohana.core
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_CoreTest extends Kohana_Unittest_TestCase
class Kohana_CoreTest extends Unittest_TestCase
{
/**
* Provides test data for test_sanitize()
*
*
* @return array
*/
public function provider_sanitize()
@@ -93,7 +94,7 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
$this->assertInternalType('array', $files);
$this->assertGreaterThan(3, count($files));
$this->assertSame(array(), Kohana::list_files('geshmuck'));
}
@@ -114,7 +115,7 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
/**
* Provides test data for testCache()
*
*
* @return array
*/
public function provider_cache()
@@ -145,14 +146,14 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
/**
* Provides test data for test_message()
*
*
* @return array
*/
public function provider_message()
{
return array(
// $value, $result
array(':field must not be empty', 'validate', 'not_empty'),
array(':field must not be empty', 'validation', 'not_empty'),
array(
array(
'alpha' => ':field must contain only letters',
@@ -161,25 +162,25 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
'color' => ':field must be a color',
'credit_card' => ':field must be a credit card number',
'date' => ':field must be a date',
'decimal' => ':field must be a decimal with :param1 places',
'decimal' => ':field must be a decimal with :param2 places',
'digit' => ':field must be a digit',
'email' => ':field must be a email address',
'email_domain' => ':field must contain a valid email domain',
'equals' => ':field must equal :param1',
'exact_length' => ':field must be exactly :param1 characters long',
'equals' => ':field must equal :param2',
'exact_length' => ':field must be exactly :param2 characters long',
'in_array' => ':field must be one of the available options',
'ip' => ':field must be an ip address',
'matches' => ':field must be the same as :param1',
'min_length' => ':field must be at least :param1 characters long',
'max_length' => ':field must be less than :param1 characters long',
'matches' => ':field must be the same as :param2',
'min_length' => ':field must be at least :param2 characters long',
'max_length' => ':field must not exceed :param2 characters long',
'not_empty' => ':field must not be empty',
'numeric' => ':field must be numeric',
'phone' => ':field must be a phone number',
'range' => ':field must be within the range of :param1 to :param2',
'range' => ':field must be within the range of :param2 to :param3',
'regex' => ':field does not match the required format',
'url' => ':field must be a url',
),
'validate', NULL,
'validation', NULL,
),
);
}
@@ -201,7 +202,7 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
/**
* Provides test data for test_error_handler()
*
*
* @return array
*/
public function provider_error_handler()
@@ -238,116 +239,9 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
error_reporting($error_level);
}
/**
* Provides test data for testExceptionHandler()
*
* @return array
*/
public function provider_exception_handler()
{
return array(
// $exception_type, $message, $is_cli, $expected
array('Kohana_Exception', 'hello, world!', TRUE, TRUE, 'hello, world!'),
array('ErrorException', 'hello, world!', TRUE, TRUE, 'hello, world!'),
// #3016
array('Kohana_Exception', '<hello, world!>', FALSE, TRUE, '&lt;hello, world!&gt;'),
);
}
/**
* Tests Kohana::exception_handler()
*
* @test
* @dataProvider provider_exception_handler
* @covers Kohana::exception_handler
* @param boolean $exception_type Exception type to throw
* @param boolean $message Message to pass to exception
* @param boolean $is_cli Use cli mode?
* @param boolean $expected Output for Kohana::exception_handler
* @param string $expexcted_message What to look for in the output string
*/
public function teste_exception_handler($exception_type, $message, $is_cli, $expected, $expected_message)
{
try
{
Kohana::$is_cli = $is_cli;
throw new $exception_type($message);
}
catch (Exception $e)
{
ob_start();
$this->assertEquals($expected, Kohana::exception_handler($e));
$view = ob_get_contents();
ob_clean();
$this->assertContains($expected_message, $view);
}
Kohana::$is_cli = TRUE;
}
/**
* Provides test data for test_debug()
*
* @return array
*/
public function provider_debug()
{
return array(
// $exception_type, $message, $is_cli, $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>"),
);
}
/**
* Tests Kohana::debug()
*
* @test
* @dataProvider provider_debug
* @covers Kohana::debug
* @param boolean $thing The thing to debug
* @param boolean $expected Output for Kohana::debug
*/
public function testdebug($thing, $expected)
{
$this->assertEquals($expected, Kohana::debug($thing));
}
/**
* Provides test data for testDebugPath()
*
* @return array
*/
public function provider_debug_path()
{
return array(
array(
Kohana::find_file('classes', 'kohana'),
'SYSPATH'.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.'kohana.php'
),
array(
Kohana::find_file('classes', $this->dirSeparator('kohana/unittest/runner')),
$this->dirSeparator('MODPATH/unittest/classes/kohana/unittest/runner.php')
),
);
}
/**
* Tests Kohana::debug_path()
*
* @test
* @dataProvider provider_debug_path
* @covers Kohana::debug_path
* @param boolean $path Input for Kohana::debug_path
* @param boolean $expected Output for Kohana::debug_path
*/
public function testDebugPath($path, $expected)
{
$this->assertEquals($expected, Kohana::debug_path($path));
}
/**
* Provides test data for test_modules_sets_and_returns_valid_modules()
*
*
* @return array
*/
public function provider_modules_sets_and_returns_valid_modules()
@@ -386,7 +280,7 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
}
/**
* To make the tests as portable as possible this just tests that
* To make the tests as portable as possible this just tests that
* you get an array of modules when you can Kohana::modules() and that
* said array contains unittest
*
@@ -422,69 +316,11 @@ class Kohana_CoreTest extends Kohana_Unittest_TestCase
// And make sure they're in the correct positions
$this->assertSame(APPPATH, reset($include_paths));
$this->assertSame(SYSPATH, end($include_paths));
foreach($modules as $module)
foreach ($modules as $module)
{
$this->assertContains($module, $include_paths);
}
}
/**
* Provides test data for test_exception_text()
*
* @return array
*/
public function provider_exception_text()
{
return array(
array(new Kohana_Exception('foobar'), $this->dirSeparator('Kohana_Exception [ 0 ]: foobar ~ SYSPATH/tests/kohana/CoreTest.php [ '.__LINE__.' ]')),
);
}
/**
* Tests Kohana::exception_text()
*
* @test
* @dataProvider provider_exception_text
* @covers Kohana::exception_text
* @param object $exception exception to test
* @param string $expected expected output
*/
public function test_exception_text($exception, $expected)
{
$this->assertEquals($expected, Kohana::exception_text($exception));
}
/**
* Provides test data for test_dump()
*
* @return array
*/
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"'),
);
}
/**
* Tests Kohana::dump()
*
* @test
* @dataProvider provider_dump
* @covers Kohana::dump
* @covers Kohana::_dump
* @param object $exception exception to test
* @param string $expected expected output
*/
public function test_dump($input, $length, $expected)
{
$this->assertEquals($expected, Kohana::dump($input, $length));
}
}

View File

@@ -2,16 +2,18 @@
/**
* Tests Date class
*
* @group kohana
*
* @package Unittest
* @group kohana
* @group kohana.date
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_DateTest extends Kohana_Unittest_TestCase
class Kohana_DateTest extends Unittest_TestCase
{
protected $_original_timezone = NULL;
@@ -108,7 +110,7 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
/**
* Tests Date::ampm()
*
*
* @test
* @covers Date::ampm
* @dataProvider provider_am_pm
@@ -225,11 +227,11 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
);
// This should be a mirrored array, days => days
for($i = 1; $i <= $expected; ++$i)
for ($i = 1; $i <= $expected; ++$i)
{
$this->assertArrayHasKey($i, $days);
// Combining the type check into this saves about 400-500 assertions!
$this->assertSame((string) $i, $days[$i]);
$this->assertSame( (string) $i, $days[$i]);
}
}
@@ -268,22 +270,98 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
}
/**
* Tests Date::months()
*
* Provider for test_months()
*
* @return array Test data
*/
public function provider_months()
{
return array(
array(
array(
1 => "1",
2 => "2",
3 => "3",
4 => "4",
5 => "5",
6 => "6",
7 => "7",
8 => "8",
9 => "9",
10 => "10",
11 => "11",
12 => "12"
),
NULL
),
array(
array(
1 => "1",
2 => "2",
3 => "3",
4 => "4",
5 => "5",
6 => "6",
7 => "7",
8 => "8",
9 => "9",
10 => "10",
11 => "11",
12 => "12"
),
'Guinness'
),
array(
array(
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "August",
9 => "September",
10 => "October",
11 => "November",
12 => "December"
),
Date::MONTHS_LONG
),
array(
array(
1 => "Jan",
2 => "Feb",
3 => "Mar",
4 => "Apr",
5 => "May",
6 => "Jun",
7 => "Jul",
8 => "Aug",
9 => "Sep",
10 => "Oct",
11 => "Nov",
12 => "Dec"
),
Date::MONTHS_SHORT
)
);
}
/**
* Date::months() should allow the user to specify different format types, defaulting
* to a mirrored month number => month number array if format is NULL or unrecognised
*
* @test
* @dataProvider provider_months
* @covers Date::months
*/
public function test_months()
public function test_months($expected, $format)
{
$months = Date::months();
$months = Date::months($format);
$this->assertSame(12, count($months));
for($i = 1; $i <= 12; ++$i)
{
$this->assertArrayHasKey($i, $months);
$this->assertSame((string) $i, $months[$i]);
}
$this->assertSame($expected, $months);
}
/**
@@ -360,9 +438,9 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
/**
* Provides test data to test_fuzzy_span
*
*
* This test data is provided on the assumption that it
* won't take phpunit more than 30 seconds to get the
* won't take phpunit more than 30 seconds to get the
* data from this provider to the test... ;)
*
* @return array Test Data
@@ -387,8 +465,8 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
array('less than a day ago', $now - 12*60*60, $now),
array('in less than a day', $now + 12*60*60, $now),
array('about a day ago', $now - 30*60*60, $now),
array('in about a day', $now + 30*60*60, $now),
array('about a day ago', $now - 30*60*60, $now),
array('in about a day', $now + 30*60*60, $now),
array('a couple of days ago', $now - 3*24*60*60, $now),
array('in a couple of days', $now + 3*24*60*60, $now),
@@ -451,7 +529,7 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
);
}
/**
/**
* Provides test data for test_years()
*
* @return array Test Data
@@ -461,15 +539,15 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
return array(
array(
array (
2005 => '2005',
2006 => '2006',
2005 => '2005',
2006 => '2006',
2007 => '2007',
2008 => '2008',
2009 => '2009',
2010 => '2010',
2011 => '2011',
2012 => '2012',
2013 => '2013',
2013 => '2013',
2014 => '2014',
2015 => '2015',
),
@@ -521,7 +599,7 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_hours
*/
public function test_hours($expected, $step = 1, $long = FALSE, $start = NULL)
public function test_hours($expected, $step = 1, $long = FALSE, $start = NULL)
{
$this->assertSame(
$expected,
@@ -539,19 +617,19 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
return array(
array(
// Thank god for var_export()
array (
0 => '00', 1 => '01', 2 => '02', 3 => '03', 4 => '04',
5 => '05', 6 => '06', 7 => '07', 8 => '08', 9 => '09',
10 => '10', 11 => '11', 12 => '12', 13 => '13', 14 => '14',
15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19',
20 => '20', 21 => '21', 22 => '22', 23 => '23', 24 => '24',
25 => '25', 26 => '26', 27 => '27', 28 => '28', 29 => '29',
30 => '30', 31 => '31', 32 => '32', 33 => '33', 34 => '34',
35 => '35', 36 => '36', 37 => '37', 38 => '38', 39 => '39',
40 => '40', 41 => '41', 42 => '42', 43 => '43', 44 => '44',
45 => '45', 46 => '46', 47 => '47', 48 => '48', 49 => '49',
50 => '50', 51 => '51', 52 => '52', 53 => '53', 54 => '54',
55 => '55', 56 => '56', 57 => '57', 58 => '58', 59 => '59',
array (
0 => '00', 1 => '01', 2 => '02', 3 => '03', 4 => '04',
5 => '05', 6 => '06', 7 => '07', 8 => '08', 9 => '09',
10 => '10', 11 => '11', 12 => '12', 13 => '13', 14 => '14',
15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19',
20 => '20', 21 => '21', 22 => '22', 23 => '23', 24 => '24',
25 => '25', 26 => '26', 27 => '27', 28 => '28', 29 => '29',
30 => '30', 31 => '31', 32 => '32', 33 => '33', 34 => '34',
35 => '35', 36 => '36', 37 => '37', 38 => '38', 39 => '39',
40 => '40', 41 => '41', 42 => '42', 43 => '43', 44 => '44',
45 => '45', 46 => '46', 47 => '47', 48 => '48', 49 => '49',
50 => '50', 51 => '51', 52 => '52', 53 => '53', 54 => '54',
55 => '55', 56 => '56', 57 => '57', 58 => '58', 59 => '59',
),
1,
0,
@@ -583,14 +661,14 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
{
return array(
array(
array(
0 => '00', 5 => '05', 10 => '10',
15 => '15', 20 => '20', 25 => '25',
30 => '30', 35 => '35', 40 => '40',
45 => '45', 50 => '50', 55 => '55',
array(
0 => '00', 5 => '05', 10 => '10',
15 => '15', 20 => '20', 25 => '25',
30 => '30', 35 => '35', 40 => '40',
45 => '45', 50 => '50', 55 => '55',
),
5,
),
),
);
}
@@ -616,11 +694,11 @@ class Kohana_DateTest extends Kohana_Unittest_TestCase
*/
public function test_minutes_defaults_to_using_step_of5()
{
$minutes = array(
0 => '00', 5 => '05', 10 => '10',
15 => '15', 20 => '20', 25 => '25',
30 => '30', 35 => '35', 40 => '40',
45 => '45', 50 => '50', 55 => '55',
$minutes = array(
0 => '00', 5 => '05', 10 => '10',
15 => '15', 20 => '20', 25 => '25',
30 => '30', 35 => '35', 40 => '40',
45 => '45', 50 => '50', 55 => '55',
);
$this->assertSame(

View File

@@ -0,0 +1,113 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests Kohana Core
*
* @TODO Use a virtual filesystem (see phpunit doc on mocking fs) for find_file etc.
*
* @group kohana
* @group kohana.debug
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_DebugTest extends Unittest_TestCase
{
/**
* Provides test data for test_debug()
*
* @return array
*/
public function provider_vars()
{
return array(
// $exception_type, $message, $is_cli, $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>"),
);
}
/**
* Tests Debug::vars()
*
* @test
* @dataProvider provider_vars
* @covers Debug::vars
* @param boolean $thing The thing to debug
* @param boolean $expected Output for Debug::vars
*/
public function test_var($thing, $expected)
{
$this->assertEquals($expected, Debug::vars($thing));
}
/**
* Provides test data for testDebugPath()
*
* @return array
*/
public function provider_debug_path()
{
return array(
array(
SYSPATH.'classes'.DIRECTORY_SEPARATOR.'kohana'.EXT,
'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
),
);
}
/**
* Tests Debug::path()
*
* @test
* @dataProvider provider_debug_path
* @covers Debug::path
* @param boolean $path Input for Debug::path
* @param boolean $expected Output for Debug::path
*/
public function test_debug_path($path, $expected)
{
$this->assertEquals($expected, Debug::path($path));
}
/**
* Provides test data for test_dump()
*
* @return array
*/
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"'),
);
}
/**
* Tests Debug::dump()
*
* @test
* @dataProvider provider_dump
* @covers Debug::dump
* @covers Debug::_dump
* @param object $exception exception to test
* @param string $expected expected output
*/
public function test_dump($input, $length, $expected)
{
$this->assertEquals($expected, Debug::dump($input, $length));
}
}

View File

@@ -0,0 +1,92 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests Kohana Exception Class
*
* @group kohana
* @group kohana.exception
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ExceptionTest extends Unittest_TestCase
{
/**
* Provides test data for test_constructor()
*
* @return array
*/
public function provider_constructor()
{
return array(
array(array(''), '', 0),
array(array(':a'), ':a', 0),
array(array(':a', NULL), ':a', 0),
array(array(':a', array()), ':a', 0),
array(array(':a', array(':a' => 'b')), 'b', 0),
array(array(':a :b', array(':a' => 'c', ':b' => 'd')), 'c d', 0),
array(array(':a', NULL, 5), ':a', 5),
// #3927
array(array(':a', NULL, 'b'), ':a', 'b'),
);
}
/**
* Tests Kohana_Kohana_Exception::__construct()
*
* @test
* @dataProvider provider_constructor
* @covers Kohana_Kohana_Exception::__construct
* @param array $arguments Arguments
* @param string $expected_message Value from getMessage()
* @param integer|string $expected_code Value from getCode()
*/
public function test_constructor($arguments, $expected_message, $expected_code)
{
switch (count($arguments))
{
case 1:
$exception = new Kohana_Exception(reset($arguments));
break;
case 2:
$exception = new Kohana_Exception(reset($arguments), next($arguments));
break;
default:
$exception = new Kohana_Exception(reset($arguments), next($arguments), next($arguments));
}
$this->assertSame($expected_code, $exception->getCode());
$this->assertSame($expected_message, $exception->getMessage());
}
/**
* Provides test data for test_text()
*
* @return array
*/
public function provider_text()
{
return array(
array(new Kohana_Exception('foobar'), $this->dirSeparator('Kohana_Exception [ 0 ]: foobar ~ SYSPATH/tests/kohana/ExceptionTest.php [ '.__LINE__.' ]')),
);
}
/**
* Tests Kohana_Exception::text()
*
* @test
* @dataProvider provider_text
* @covers Kohana_Exception::text
* @param object $exception exception to test
* @param string $expected expected output
*/
public function test_text($exception, $expected)
{
$this->assertEquals($expected, Kohana_Exception::text($exception));
}
}

View File

@@ -4,14 +4,16 @@
* Test for feed helper
*
* @group kohana
* @group kohana.feed
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_FeedTest extends Kohana_Unittest_TestCase
class Kohana_FeedTest extends Unittest_TestCase
{
/**
* Provides test data for test_parse()
@@ -38,8 +40,10 @@ class Kohana_FeedTest extends Kohana_Unittest_TestCase
public function test_parse($source, $expected)
{
if ( ! $this->hasInternet())
{
$this->markTestSkipped('An internet connection is required for this test');
}
$this->assertEquals($expected, count(feed::parse($source)));
}

View File

@@ -6,17 +6,18 @@
* @group kohana
* @group kohana.url
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_FileTest extends Kohana_Unittest_Testcase
class Kohana_FileTest extends Unittest_TestCase
{
/**
* Provides test data for test_sanitize()
*
*
* @return array
*/
public function provider_mime()
@@ -43,7 +44,7 @@ class Kohana_FileTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_split_join()
*
*
* @return array
*/
public function provider_split_join()
@@ -68,6 +69,9 @@ class Kohana_FileTest extends Kohana_Unittest_Testcase
$this->assertSame($expected, File::split($input, $peices));
$this->assertSame($expected, File::join($input));
foreach (glob(Kohana::find_file('tests', 'test_data/github', 'png') . '.*') as $file) unlink($file);
foreach (glob(Kohana::find_file('tests', 'test_data/github', 'png').'.*') as $file)
{
unlink($file);
}
}
}

View File

@@ -6,13 +6,14 @@
* @group kohana
* @group kohana.form
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_FormTest extends Kohana_Unittest_Testcase
class Kohana_FormTest extends Unittest_TestCase
{
/**
* Defaults for this test
@@ -25,14 +26,14 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_open()
*
*
* @return array
*/
public function provider_open()
{
return array(
// $value, $result
#array(NULL, NULL, '<form action="/" method="post" accept-charset="utf-8">'), // Fails because of Request::$current
array(NULL, NULL, '<form action="/" method="post" accept-charset="utf-8">'),
array('foo', NULL),
array('', NULL),
array('foo', array('method' => 'get')),
@@ -58,10 +59,12 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
'accept-charset' => 'utf-8',
),
);
if($attributes !== NULL)
if ($attributes !== NULL)
{
$matcher['attributes'] = $attributes + $matcher['attributes'];
}
$this->assertTag($matcher, $tag);
}
@@ -77,7 +80,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_input()
*
*
* @return array
*/
public function provider_input()
@@ -107,16 +110,22 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
);
// Form::input creates a text input
if($type === 'input')
if ($type === 'input')
{
$matcher['attributes']['type'] = 'text';
}
// NULL just means no value
if($value !== NULL)
if ($value !== NULL)
{
$matcher['attributes']['value'] = $value;
}
// Add on any attributes
if(is_array($attributes))
if (is_array($attributes))
{
$matcher['attributes'] = $attributes + $matcher['attributes'];
}
$tag = Form::$type($name, $value, $attributes);
@@ -125,7 +134,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_file()
*
*
* @return array
*/
public function provider_file()
@@ -151,7 +160,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_check()
*
*
* @return array
*/
public function provider_check()
@@ -161,7 +170,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
array('checkbox', 'foo', NULL, FALSE, NULL),
array('checkbox', 'foo', NULL, TRUE, NULL),
array('checkbox', 'foo', 'bar', TRUE, NULL),
array('radio', 'foo', NULL, FALSE, NULL),
array('radio', 'foo', NULL, TRUE, NULL),
array('radio', 'foo', 'bar', TRUE, NULL),
@@ -180,14 +189,20 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
{
$matcher = array('tag' => 'input', 'attributes' => array('name' => $name, 'type' => $type));
if($value !== NULL)
if ($value !== NULL)
{
$matcher['attributes']['value'] = $value;
}
if(is_array($attributes))
if (is_array($attributes))
{
$matcher['attributes'] = $attributes + $matcher['attributes'];
}
if($checked === TRUE)
if ($checked === TRUE)
{
$matcher['attributes']['checked'] = 'checked';
}
$tag = Form::$type($name, $value, $checked, $attributes);
$this->assertTag($matcher, $tag, $tag);
@@ -195,7 +210,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_text()
*
*
* @return array
*/
public function provider_text()
@@ -226,14 +241,20 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
'content' => $body,
);
if($type !== 'label')
if ($type !== 'label')
{
$matcher['attributes'] = array('name' => $name);
}
else
{
$matcher['attributes'] = array('for' => $name);
}
if(is_array($attributes))
if (is_array($attributes))
{
$matcher['attributes'] = $attributes + $matcher['attributes'];
}
$tag = Form::$type($name, $body, $attributes);
@@ -243,7 +264,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_select()
*
*
* @return array
*/
public function provider_select()
@@ -276,7 +297,7 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
/**
* Provides test data for test_submit()
*
*
* @return array
*/
public function provider_submit()
@@ -301,14 +322,14 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
'tag' => 'input',
'attributes' => array('name' => $name, 'type' => 'submit', 'value' => $value)
);
$this->assertTag($matcher, Form::submit($name, $value));
}
/**
* Provides test data for test_image()
*
*
* @return array
*/
public function provider_image()
@@ -335,11 +356,11 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
}
/**
* Provides test data for testLabel()
*
* Provides test data for test_label()
*
* @return array
*/
function providerLabel()
function provider_label()
{
return array(
// $value, $result
@@ -359,13 +380,13 @@ class Kohana_FormTest extends Kohana_Unittest_Testcase
* Tests Form::label()
*
* @test
* @dataProvider providerLabel
* @dataProvider provider_label
* @param boolean $for Input for Form::label
* @param boolean $text Input for Form::label
* @param boolean $attributes Input for Form::label
* @param boolean $expected Output for Form::label
*/
function testLabel($for, $text, $attributes, $expected)
function test_label($for, $text, $attributes, $expected)
{
$this->assertSame($expected, Form::label($for, $text, $attributes));
}

View File

@@ -2,25 +2,28 @@
/**
* Tests HTML
*
* @group kohana
*
* @package Unittest
* @group kohana
* @group kohana.html
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
{
class Kohana_HTMLTest extends Unittest_TestCase
{
protected $environmentDefault = array(
'Kohana::$base_url' => '/kohana/',
'Kohana::$base_url' => '/kohana/',
'Kohana::$index_file' => 'index.php',
'HTTP_HOST' => 'www.kohanaframework.org',
);
/**
* Provides test data for test_attributes()
*
*
* @return array
*/
public function provider_attributes()
@@ -73,6 +76,21 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
'<script type="text/javascript" src="http://google.com/script.js"></script>',
'http://google.com/script.js',
),
array(
'<script type="text/javascript" src="http://www.kohanaframework.org/kohana/index.php/my/script.js"></script>',
'my/script.js',
NULL,
'http',
TRUE
),
array(
'<script type="text/javascript" src="https://www.kohanaframework.org/kohana/my/script.js"></script>',
'my/script.js',
NULL,
'https',
FALSE
),
);
}
@@ -84,13 +102,14 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
* @param string $expected Expected output
* @param string $file URL to script
* @param array $attributes HTML attributes for the anchor
* @param string $protocol Protocol to use
* @param bool $index Should the index file be included in url?
*/
public function test_script($expected, $file, array $attributes = NULL, $index = FALSE)
public function test_script($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::script($file, $attributes, $index)
HTML::script($file, $attributes, $protocol, $index)
);
}
@@ -106,8 +125,30 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
'<link type="text/css" href="http://google.com/style.css" rel="stylesheet" />',
'http://google.com/style.css',
array(),
NULL,
FALSE
),
array(
'<link type="text/css" href="/kohana/my/style.css" rel="stylesheet" />',
'my/style.css',
array(),
NULL,
FALSE
),
array(
'<link type="text/css" href="https://www.kohanaframework.org/kohana/my/style.css" rel="stylesheet" />',
'my/style.css',
array(),
'https',
FALSE
),
array(
'<link type="text/css" href="https://www.kohanaframework.org/kohana/index.php/my/style.css" rel="stylesheet" />',
'my/style.css',
array(),
'https',
TRUE
),
);
}
@@ -119,13 +160,14 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
* @param string $expected The expected output
* @param string $file The file to link to
* @param array $attributes Any extra attributes for the link
* @param string $protocol Protocol to use
* @param bool $index Whether the index file should be added to the link
*/
public function test_style($expected, $file, array $attributes = NULL, $index = FALSE)
public function test_style($expected, $file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::style($file, $attributes, $index)
HTML::style($file, $attributes, $protocol, $index)
);
}
@@ -177,6 +219,25 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
'http://google.com',
'GOOGLE',
array('target' => '_blank'),
'http',
),
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/index.php/users/example">Kohana</a>',
array(),
'users/example',
'Kohana',
NULL,
'https',
TRUE,
),
);
}
@@ -187,13 +248,13 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_anchor
*/
public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL)
public function test_anchor($expected, array $options, $uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
//$this->setEnvironment($options);
// $this->setEnvironment($options);
$this->assertSame(
$expected,
HTML::anchor($uri, $title, $attributes, $protocol)
HTML::anchor($uri, $title, $attributes, $protocol, $index)
);
}
@@ -210,6 +271,22 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
array(),
'mypic.png',
'My picture file',
),
array(
'<a href="https://www.kohanaframework.org/kohana/index.php/mypic.png" attr="value">My picture file</a>',
array('attr' => 'value'),
'mypic.png',
'My picture file',
'https',
TRUE
),
array(
'<a href="ftp://www.kohanaframework.org/kohana/mypic.png">My picture file</a>',
array(),
'mypic.png',
'My picture file',
'ftp',
FALSE
)
);
}
@@ -221,11 +298,11 @@ Class Kohana_HTMLTest extends Kohana_Unittest_TestCase
* @covers HTML::file_anchor
* @dataProvider provider_file_anchor
*/
public function test_file_anchor($expected, array $options, $file, $title = NULL, array $attributes = NULL, $protocol = NULL)
public function test_file_anchor($expected, array $attributes, $file, $title = NULL, $protocol = NULL, $index = FALSE)
{
$this->assertSame(
$expected,
HTML::file_anchor($file, $title, $attributes, $protocol)
HTML::file_anchor($file, $title, $attributes, $protocol, $index)
);
}
}

View File

@@ -0,0 +1,164 @@
<?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);
}
}

View File

@@ -0,0 +1,129 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Unit Tests for Kohana_HTTP_Header
*
* @group kohana
* @group kohana.http
* @group kohana.http.header
* @group kohana.http.header
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_HTTP_HeaderTest extends Unittest_TestCase {
/**
* Provides test data for test_parse_header_values()
*
* @return array
*/
public function provider_parse_header_values()
{
return array(
array(
array(
'Date' => 'Sun, 13 Mar 2011 16:02:19 GMT',
'Expires' => '-1',
'Cache-Control' => 'private, max-age=0',
'Content-Type' => 'text/html; charset=ISO-8859-1',
'Server' => 'Apache'
),
array(
'date' => new HTTP_Header_Value('Sun, 13 Mar 2011 16:02:19 GMT'),
'expires' => new HTTP_Header_Value('-1'),
'cache-control' => array(new HTTP_Header_Value('private'), 'max-age' => new HTTP_Header_Value('max-age=0')),
'content-type' => new HTTP_Header_Value('text/html; charset=ISO-8859-1'),
'server' => new HTTP_Header_Value('Apache')
)
),
array(
array(
'Date' => 'Sun, 13 Mar 2011 16:02:19 GMT',
'Expires' => '-1',
'Cache-Control' => 'private, max-age=0',
'Content-Type' => 'text/html; charset=ISO-8859-1',
'Server' => 'Apache',
'Set-Cookie' => array(
'PREF=ID=dbfa7be8975d02f9:FF=0:TM=1300032139:LM=1300032139:S=ufhpKoOHVm55WY6v; expires=Tue, 12-Mar-2013 16:02:19 GMT; path=/; domain=.google.co.uk',
'NID=44=CxNpOQbQ7fFoxIDZWRHQJaKXgZvi76heU9OfsVi75gUH2Ik0p6tAuqW9AmTwvsE1oy0XBHDgIgMq-301hvAiyHC1sgI71pKcMUEf5VCRvCwHxJH9ZR-tlJdDD-df1vnz; expires=Mon, 12-Sep-2011 16:02:19 GMT; path=/; domain=.google.co.uk; HttpOnly'
)
),
array(
'date' => new HTTP_Header_Value('Sun, 13 Mar 2011 16:02:19 GMT'),
'expires' => new HTTP_Header_Value('-1'),
'cache-control' => array(
new HTTP_Header_Value('private'),
'max-age' => new HTTP_Header_Value('max-age=0'
)),
'content-type' => new HTTP_Header_Value('text/html; charset=ISO-8859-1'),
'server' => new HTTP_Header_Value('Apache'),
'set-cookie' => array(
new HTTP_Header_Value(array(
'key' => 'PREF',
'value' => 'ID=dbfa7be8975d02f9:FF=0:TM=1300032139:LM=1300032139:S=ufhpKoOHVm55WY6v',
'properties' => array(
'expires' => 'Tue, 12-Mar-2013 16:02:19 GMT',
'path' => '/',
'domain' => '.google.co.uk'
)
)),
new HTTP_Header_Value(array(
'key' => 'NID',
'value' => '44=CxNpOQbQ7fFoxIDZWRHQJaKXgZvi76heU9OfsVi75gUH2Ik0p6tAuqW9AmTwvsE1oy0XBHDgIgMq-301hvAiyHC1sgI71pKcMUEf5VCRvCwHxJH9ZR-tlJdDD-df1vnz',
'properties' => array(
'expires' => 'Mon, 12-Sep-2011 16:02:19 GMT',
'path' => '/',
'domain' => '.google.co.uk',
'HttpOnly'
)
))
)
)
),
);
}
/**
* Tests the parse_header_values() method.
*
* @dataProvider provider_parse_header_values
*
* @param array header array to parse
* @param array expected result
* @return void
*/
public function test_parse_header_values($header_array, $expected)
{
$header = HTTP_Header::parse_header_values($header_array);
// Test the correct type is returned
$this->assertTrue(is_array($header));
foreach ($header as $key => $value)
{
if ($value instanceof HTTP_Header_Value)
{
$this->assertSame($value->value(), $expected[$key]->value());
$this->assertSame($value->key(), $expected[$key]->key());
$this->assertSame($value->properties(), $expected[$key]->properties());
}
elseif (is_array($value))
{
foreach ($value as $k => $v)
{
$this->assertSame($v->value(), $expected[$key][$k]->value());
$this->assertSame($v->key(), $expected[$key][$k]->key());
$this->assertSame($v->properties(), $expected[$key][$k]->properties());
}
}
else
{
$this->fail('Unexpected value in HTTP_Header::parse_header_values() return value.');
}
}
}
} // End Kohana_HTTP_HeaderTest

View File

@@ -6,13 +6,14 @@
* @group kohana
* @group kohana.i18n
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_I18nTest extends Kohana_Unittest_TestCase
class Kohana_I18nTest extends Unittest_TestCase
{
/**

View File

@@ -6,13 +6,14 @@
* @group kohana
* @group kohana.inflector
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_InflectorTest extends Kohana_Unittest_TestCase
class Kohana_InflectorTest extends Unittest_TestCase
{
/**
* Provides test data for test_lang()
@@ -25,6 +26,9 @@ class Kohana_InflectorTest extends Kohana_Unittest_TestCase
// $value, $result
array('fish', TRUE),
array('cat', FALSE),
array('deer', TRUE),
array('bison', TRUE),
array('friend', FALSE),
);
}

View File

@@ -6,13 +6,14 @@
* @group kohana
* @group kohana.logging
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Matt Button <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_LogTest extends Kohana_Unittest_TestCase
class Kohana_LogTest extends Unittest_TestCase
{
/**
@@ -20,11 +21,11 @@ Class Kohana_LogTest extends Kohana_Unittest_TestCase
* empty
*
* @test
* @covers Kohana_Log
* @covers Log
*/
public function test_messages_is_initially_empty()
{
$logger = new Kohana_Log;
$logger = new Log;
$this->assertAttributeSame(array(), '_messages', $logger);
}
@@ -34,32 +35,54 @@ Class Kohana_LogTest extends Kohana_Unittest_TestCase
* empty
*
* @test
* @covers Kohana_Log
* @covers Log
*/
public function test_writers_is_initially_empty()
{
$logger = new Kohana_Log;
$logger = new Log;
$this->assertAttributeSame(array(), '_writers', $logger);
}
/**
* Test that attaching a log writer adds it to the array of log writers
* Test that attaching a log writer using an array of levels adds it to the array of log writers
*
* @TODO Is this test too specific?
*
* @test
* @covers Kohana_Log::attach
* @covers Log::attach
*/
public function test_attach_attaches_log_writer_and_returns_this()
{
$logger = new Kohana_Log;
$writer = $this->getMockForAbstractClass('Kohana_Log_Writer');
$logger = new Log;
$writer = $this->getMockForAbstractClass('Log_Writer');
$this->assertSame($logger, $logger->attach($writer));
$this->assertAttributeSame(
array(spl_object_hash($writer) => array('object' => $writer, 'types' => NULL)),
array(spl_object_hash($writer) => array('object' => $writer, 'levels' => array())),
'_writers',
$logger
);
}
/**
* Test that attaching a log writer using a min/max level adds it to the array of log writers
*
* @TODO Is this test too specific?
*
* @test
* @covers Log::attach
*/
public function test_attach_attaches_log_writer_min_max_and_returns_this()
{
$logger = new Log;
$writer = $this->getMockForAbstractClass('Log_Writer');
$this->assertSame($logger, $logger->attach($writer, Log::NOTICE, Log::CRITICAL));
$this->assertAttributeSame(
array(spl_object_hash($writer) => array('object' => $writer, 'levels' => array(Log::CRITICAL, Log::ERROR, Log::WARNING, Log::NOTICE))),
'_writers',
$logger
);
@@ -69,12 +92,12 @@ Class Kohana_LogTest extends Kohana_Unittest_TestCase
* When we call detach() we expect the specified log writer to be removed
*
* @test
* @covers Kohana_Log::detach
* @covers Log::detach
*/
public function test_detach_removes_log_writer_and_returns_this()
{
$logger = new Kohana_Log;
$writer = $this->getMockForAbstractClass('Kohana_Log_Writer');
$logger = new Log;
$writer = $this->getMockForAbstractClass('Log_Writer');
$logger->attach($writer);

View File

@@ -1,30 +1,35 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* This test only really exists for code coverage. No tests really apply to base model.
* We can't even test Model because database doesn't exist!
* This test only really exists for code coverage.
*
* @group kohana
* @group kohana.model
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ModelTest extends Kohana_Unittest_TestCase
class Kohana_ModelTest extends Unittest_TestCase
{
/**
* Test the model's factory.
*
* @test
* @covers Model::factory
*/
public function test_construct()
public function test_create()
{
#$model = new Model_Foobar('foo');
#$model = Model::factory('Foobar', 'foo');
$foobar = Model::factory('Foobar');
$this->assertEquals(TRUE, $foobar instanceof Model);
}
}
class Model_Foobar extends Model
{
}

View File

@@ -4,13 +4,15 @@
* Tests Num
*
* @group kohana
* @package Unittest
* @group kohana.num
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_NumTest extends Kohana_Unittest_TestCase
class Kohana_NumTest extends Unittest_TestCase
{
protected $default_locale;
@@ -33,6 +35,35 @@ Class Kohana_NumTest extends Kohana_Unittest_TestCase
setlocale(LC_ALL, $this->default_locale);
}
/**
* Provides test data for test_bytes()
*
* @return array
*/
public function provider_bytes()
{
return array(
array(204800.0, '200K'),
array(5242880.0, '5MiB'),
array(1000.0, 1000),
array(2684354560.0, '2.5GB'),
);
}
/**
* Tests Num::bytes()
*
* @test
* @covers Num::bytes
* @dataProvider provider_bytes
* @param integer Expected Value
* @param string Input value
*/
public function test_bytes($expected, $size)
{
$this->assertSame($expected, Num::bytes($size));
}
/**
* Provides test data for test_ordinal()
@@ -92,4 +123,78 @@ Class Kohana_NumTest extends Kohana_Unittest_TestCase
{
$this->assertSame($expected, Num::format($number, $places, $monetary));
}
/**
* Provides data for test_round()
* @return array
*/
function provider_round()
{
return array(
array(5.5, 0, array(
6.0,
5.0,
6.0,
5.0,
)),
array(42.5, 0, array(
43.0,
42.0,
42.0,
43.0,
)),
array(10.4, 0, array(
10.0,
10.0,
10.0,
10.0,
)),
array(10.8, 0, array(
11.0,
11.0,
11.0,
11.0,
)),
array(-5.5, 0, array(
-6.0,
-5.0,
-6.0,
-5.0,
)),
array(-10.5, 0, array(
-11.0,
-10.0,
-10.0,
-11.0,
)),
array(26.12375, 4, array(
26.1238,
26.1237,
26.1238,
26.1237,
)),
array(26.12325, 4, array(
26.1233,
26.1232,
26.1232,
26.1233,
)),
);
}
/**
* @test
* @dataProvider provider_round
* @param number $input
* @param integer $precision
* @param integer $mode
* @param number $expected
*/
function test_round($input, $precision, $expected)
{
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));
}
}
}

View File

@@ -1,77 +0,0 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests Kohana remote class
*
* @group kohana
* @group kohana.remote
*
* @package Unittest
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_RemoteTest extends Kohana_Unittest_TestCase
{
/**
* Provides test data for test_get()
*
* @return array
*/
public function provider_get()
{
return array(
// $value, $result
array('', TRUE),
array('cat', FALSE),
);
}
/**
* Tests Remote::get
*
* @test
* @dataProvider provider_get
* @param boolean $input Input for File::mime
* @param boolean $expected Output for File::mime
*/
public function test_get($input, $expected)
{
if ( ! $this->hasInternet())
$this->markTestSkipped('An internet connection is required for this test');
#$this->assertSame($expected, Remote::get($input));
}
/**
* Provides test data for test_status()
*
* @return array
*/
public function provider_status()
{
return array(
// $value, $result
array('http://kohanaframework.org/', 200),
array('http://kohanaframework.org', 200),
array('http://kohanaframework.org/foobar', 500),
);
}
/**
* Tests Remote::status
*
* @test
* @dataProvider provider_status
* @param boolean $input Input for File::mime
* @param boolean $expected Output for File::mime
*/
public function test_status($input, $expected)
{
if ( ! $this->hasInternet())
$this->markTestSkipped('An internet connection is required for this test');
$this->assertSame($expected, Remote::status($input));
}
}

View File

@@ -4,37 +4,231 @@
* Unit tests for request class
*
* @group kohana
* @group kohana.request
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_RequestTest extends Kohana_Unittest_TestCase
class Kohana_RequestTest extends Unittest_TestCase
{
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,
'_SERVER' => array(
'HTTPS' => NULL,
'PATH_INFO' => '/',
'HTTP_REFERER' => 'http://example.com/',
'HTTP_USER_AGENT' => 'whatever (Mozilla 5.0/compatible)',
'REMOTE_ADDR' => '127.0.0.1',
'REQUEST_METHOD' => 'GET',
'HTTP_X_REQUESTED_WITH' => 'ajax-or-something',
),
'_GET' => array(),
'_POST' => array(),
));
$request = Request::factory();
$this->assertEquals(Request::$initial, $request);
$this->assertEquals(Request::$client_ip, '127.0.0.1');
$this->assertEquals(Request::$user_agent, 'whatever (Mozilla 5.0/compatible)');
$this->assertEquals($request->protocol(), 'http');
$this->assertEquals($request->referrer(), 'http://example.com/');
$this->assertEquals($request->requested_with(), 'ajax-or-something');
$this->assertEquals($request->query(), array());
$this->assertEquals($request->post(), array());
$this->setEnvironment($original);
}
/**
* Route::matches() should return false if the route doesn't match against a uri
* Tests that an initial request won't use an external client
*
* @expectedException HTTP_Exception_404
*
* @return null
*/
public function test_initial_request_only_loads_internal()
{
$this->setEnvironment(
array(
'Kohana::$is_cli' => FALSE,
'Request::$initial' => NULL,
)
);
$request = new Request('http://www.google.com/');
}
/**
* 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();
}
/**
* Provides the data for test_create()
* @return array
*/
public function provider_create()
{
return array(
array('foo/bar', 'Request_Client_Internal'),
array('http://google.com', 'Request_Client_External'),
);
}
/**
* Ensures the create class is created with the correct client
*
* @test
* @dataProvider provider_create
*/
public function test_create($uri, $client_class)
{
$request = Request::factory($uri);
$this->assertInstanceOf($client_class, $request->get_client());
}
/**
* Ensure that parameters can be read
*
* @test
*/
public function test_create()
public function test_param()
{
$request = Request::factory('foo/bar')->execute();
$uri = 'foo/bar/id';
$request = Request::factory($uri);
$this->assertEquals(200, $request->status);
$this->assertEquals('foo', $request->response);
$this->assertArrayHasKey('id', $request->param());
$this->assertArrayNotHasKey('foo', $request->param());
$this->assertEquals($request->uri(), $uri);
try
{
$request = new Request('bar/foo');
$request->execute();
}
catch (Exception $e)
{
$this->assertEquals(TRUE, $e instanceof ReflectionException);
$this->assertEquals('404', $request->status);
}
// Ensure the params do not contain contamination from controller, action, route, uri etc etc
$params = $request->param();
// Test for illegal components
$this->assertArrayNotHasKey('controller', $params);
$this->assertArrayNotHasKey('action', $params);
$this->assertArrayNotHasKey('directory', $params);
$this->assertArrayNotHasKey('uri', $params);
$this->assertArrayNotHasKey('route', $params);
$route = new Route('(<uri>)', array('uri' => '.+'));
$route->defaults(array('controller' => 'foobar', 'action' => 'index'));
$request = Request::factory('foobar', NULL, array($route));
$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()
*
* @test
*/
public function test_method()
{
$request = Request::factory('foo/bar');
$this->assertEquals($request->method(), 'GET');
$this->assertEquals(($request->method('post') === $request), TRUE);
$this->assertEquals(($request->method() === 'POST'), TRUE);
}
/**
* Tests Request::route()
*
* @test
*/
public function test_route()
{
$request = Request::factory(''); // This should always match something, no matter what changes people make
$this->assertInstanceOf('Route', $request->route());
}
/**
@@ -48,60 +242,6 @@ class Kohana_RequestTest extends Kohana_Unittest_TestCase
$this->assertEquals(array('*/*' => 1), Request::accept_type());
}
/**
* Provides test data for test_instance()
*
* @return array
*/
public function provider_instance()
{
return array(
// $route, $is_cli, $_server, $status, $response
array('foo/bar', TRUE, array(), 200, ''), // Shouldn't this be 'foo' ?
array('foo/foo', TRUE, array(), 200, ''), // Shouldn't this be a 404?
array(
'foo/bar',
FALSE,
array(
'REQUEST_METHOD' => 'get',
'HTTP_REFERER' => 'http://www.kohanaframework.org',
'HTTP_USER_AGENT' => 'Kohana Unit Test',
'REMOTE_ADDR' => '127.0.0.1',
), 200, ''), // Shouldn't this be 'foo' ?
);
}
/**
* Tests Request::instance()
*
* @test
* @dataProvider provider_instance
* @covers Request::instance
* @param boolean $value Input for Kohana::sanitize
* @param boolean $result Output for Kohana::sanitize
*/
public function test_instance($route, $is_cli, $server, $status, $response)
{
$this->setEnvironment(array(
'_SERVER' => $server+array('argc' => $_SERVER['argc']),
'Kohana::$is_cli' => $is_cli,
'Request::$instance' => NULL
));
$request = Request::instance($route);
$this->assertEquals($status, $request->status);
$this->assertEquals($response, $request->response);
$this->assertEquals($route, $request->uri);
if ( ! $is_cli)
{
$this->assertEquals($server['REQUEST_METHOD'], Request::$method);
$this->assertEquals($server['HTTP_REFERER'], Request::$referrer);
$this->assertEquals($server['HTTP_USER_AGENT'], Request::$user_agent);
}
}
/**
* Provides test data for Request::accept_lang()
* @return array
@@ -179,13 +319,535 @@ class Kohana_RequestTest extends Kohana_Unittest_TestCase
'Kohana::$is_cli' => $is_cli,
));
$this->assertEquals(Request::instance($uri)->url($params, $protocol), $expected);
$this->assertEquals(Request::factory($uri)->url($params, $protocol), $expected);
}
}
class Controller_Foo extends Controller {
public function action_bar()
/**
* Tests that request caching works
*
* @return null
*/
public function test_cache()
{
$this->request->response = 'foo';
/**
* 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);
}
/**
* Data provider for test_set_protocol() test
*
* @return array
*/
public function provider_set_protocol()
{
return array(
array(
'http',
'http',
),
array(
'FTP',
'ftp',
),
array(
'hTTps',
'https',
),
);
}
/**
* Tests the protocol() method
*
* @dataProvider provider_set_protocol
*
* @return null
*/
public function test_set_protocol($protocol, $expected)
{
$request = Request::factory();
// Set the supplied protocol
$result = $request->protocol($protocol);
// Test the set value
$this->assertSame($request->protocol(), $expected);
// Test the return value
$this->assertTrue($request instanceof $result);
}
/**
* Provides data for test_post_max_size_exceeded()
*
* @return array
*/
public function provider_post_max_size_exceeded()
{
// Get the post max size
$post_max_size = Num::bytes(ini_get('post_max_size'));
return array(
array(
$post_max_size+200000,
TRUE
),
array(
$post_max_size-20,
FALSE
),
array(
$post_max_size,
FALSE
)
);
}
/**
* Tests the post_max_size_exceeded() method
*
* @dataProvider provider_post_max_size_exceeded
*
* @param int content_length
* @param bool expected
* @return void
*/
public function test_post_max_size_exceeded($content_length, $expected)
{
// Ensure the request method is set to POST
Request::$initial->method(HTTP_Request::POST);
// Set the content length
$_SERVER['CONTENT_LENGTH'] = $content_length;
// Test the post_max_size_exceeded() method
$this->assertSame(Request::post_max_size_exceeded(), $expected);
}
/**
* Provides data for test_uri_only_trimed_on_internal()
*
* @return array
*/
public function provider_uri_only_trimed_on_internal()
{
$old_request = Request::$initial;
Request::$initial = new Request('foo/bar');
$result = array(
array(
new Request('http://www.google.com'),
'http://www.google.com'
),
array(
new Request('http://www.google.com/'),
'http://www.google.com/'
),
array(
new Request('foo/bar/'),
'foo/bar'
),
array(
new Request('foo/bar'),
'foo/bar'
)
);
Request::$initial = $old_request;
return $result;
}
/**
* Tests that the uri supplied to Request is only trimed
* for internal requests.
*
* @dataProvider provider_uri_only_trimed_on_internal
*
* @return void
*/
public function test_uri_only_trimed_on_internal(Request $request, $expected)
{
$this->assertSame($request->uri(), $expected);
}
/**
* Data provider for test_options_set_to_external_client()
*
* @return array
*/
public function provider_options_set_to_external_client()
{
$provider = array(
array(
array(
CURLOPT_PROXYPORT => 8080,
CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
CURLOPT_VERBOSE => TRUE
),
array(
CURLOPT_PROXYPORT => 8080,
CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
CURLOPT_VERBOSE => TRUE
)
)
);
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;
}
/**
* Test for Request_Client_External::options() to ensure options
* can be set to the external client (for cURL and PECL_HTTP)
*
* @dataProvider provider_options_set_to_external_client
*
* @param array settings
* @param array expected
* @return void
*/
public function test_options_set_to_external_client($settings, $expected)
{
$request = Request::factory('http://www.kohanaframework.org');
$request_client = $request->get_client();
// Test for empty array
$this->assertSame($request_client->options(), array());
// Test that set works as expected
$this->assertSame($request_client->options($settings), $request_client);
// Test that each setting is present and returned
foreach ($expected as $key => $value)
{
$this->assertSame($request_client->options($key), $value);
}
}
/**
* Provides data for test_headers_get()
*
* @return array
*/
public function provider_headers_get()
{
$x_powered_by = 'Kohana Unit Test';
$content_type = 'application/x-www-form-urlencoded';
return array(
array(
$request = Request::factory('foo/bar')
->headers(array(
'x-powered-by' => $x_powered_by,
'content-type' => $content_type
)
),
array(
'x-powered-by' => $x_powered_by,
'content-type' => $content_type
)
)
);
}
/**
* Tests getting headers from the Request object
*
* @dataProvider provider_headers_get
*
* @param Request request to test
* @param array headers to test against
* @return void
*/
public function test_headers_get($request, $headers)
{
foreach ($headers as $key => $expected_value)
{
$this->assertSame((string) $request->headers($key), $expected_value);
}
}
/**
* Provides data for test_headers_set
*
* @return array
*/
public function provider_headers_set()
{
return array(
array(
new Request('foo/bar'),
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"
),
array(
new Request('foo/bar'),
array(
'content-type' => 'application/json',
'x-powered-by' => 'kohana'
),
"content-type: application/json\r\nx-powered-by: kohana\r\n\n"
)
);
}
/**
* Tests the setting of headers to the request object
*
* @dataProvider provider_headers_set
*
* @param Request request object
* @param array header(s) to set to the request object
* @param string expected http header
* @return void
*/
public function test_headers_set(Request $request, $headers, $expected)
{
$request->headers($headers);
$this->assertSame($expected, (string) $request->headers());
}
/**
* Provides test data for test_query_parameter_parsing()
*
* @return array
*/
public function provider_query_parameter_parsing()
{
return array(
array(
new Request('foo/bar'),
array(
'foo' => 'bar',
'sna' => 'fu'
),
array(
'foo' => 'bar',
'sna' => 'fu'
),
),
array(
new Request('foo/bar?john=wayne&peggy=sue'),
array(
'foo' => 'bar',
'sna' => 'fu'
),
array(
'john' => 'wayne',
'peggy' => 'sue',
'foo' => 'bar',
'sna' => 'fu'
),
),
array(
new Request('http://host.tld/foo/bar?john=wayne&peggy=sue'),
array(
'foo' => 'bar',
'sna' => 'fu'
),
array(
'john' => 'wayne',
'peggy' => 'sue',
'foo' => 'bar',
'sna' => 'fu'
),
),
);
}
/**
* Tests that query parameters are parsed correctly
*
* @dataProvider provider_query_parameter_parsing
*
* @param Request request
* @param array query
* @param array expected
* @return void
*/
public function test_query_parameter_parsing(Request $request, $query, $expected)
{
foreach ($query as $key => $value)
{
$request->query($key, $value);
}
$this->assertSame($expected, $request->query());
}
/**
* Provider for test_uri_without_query_parameters
*
* @return array
*/
public function provider_uri_without_query_parameters()
{
return array(
array(
new Request('foo/bar?foo=bar&bar=foo'),
array(),
'foo/bar'
),
array(
new Request('foo/bar'),
array('bar' => 'foo', 'foo' => 'bar'),
'foo/bar'
),
array(
new Request('foo/bar'),
array(),
'foo/bar'
)
);
}
/**
* Tests that the [Request::uri()] method does not return
* query parameters
*
* @dataProvider provider_uri_without_query_parameters
*
* @param Request request
* @param array query
* @param string expected
* @return void
*/
public function test_uri_without_query_parameters(Request $request, $query, $expected)
{
$request->query($query);
$this->assertSame($expected, $request->uri());
}
} // End Kohana_RequestTest

View File

@@ -0,0 +1,244 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for response class
*
* @group kohana
* @group kohana.response
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 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
*
* @return array
*/
public function provider_body()
{
$view = $this->getMock('View');
$view->expects($this->any())
->method('__toString')
->will($this->returnValue('foo'));
return array(
array('unit test', 'unit test'),
array($view, 'foo'),
);
}
/**
* Tests that we can set and read a body of a response
*
* @test
* @dataProvider provider_body
*
* @return null
*/
public function test_body($source, $expected)
{
$response = new Response;
$response->body($source);
$this->assertSame($response->body(), $expected);
$response = (string) $response;
$this->assertSame($response, $expected);
}
/**
* Provides data for test_body_string_zero()
*
* @return array
*/
public function provider_body_string_zero()
{
return array(
array('0', '0'),
array("0", '0'),
array(0, '0')
);
}
/**
* Test that Response::body() handles numerics correctly
*
* @test
* @dataProvider provider_body_string_zero
* @param string $string
* @param string $expected
* @return void
*/
public function test_body_string_zero($string, $expected)
{
$response = new Response;
$response->body($string);
$this->assertSame($expected, $response->body());
}
/**
* provider for test_cookie_set()
*
* @return array
*/
public function provider_cookie_set()
{
return array(
array(
'test1',
'foo',
array(
'test1' => array(
'value' => 'foo',
'expiration' => Cookie::$expiration
),
)
),
array(
array(
'test2' => 'stfu',
'test3' => array(
'value' => 'snafu',
'expiration' => 123456789
)
),
NULL,
array(
'test2' => array(
'value' => 'stfu',
'expiration' => Cookie::$expiration
),
'test3' => array(
'value' => 'snafu',
'expiration' => 123456789
)
)
)
);
}
/**
* Tests the Response::cookie() method, ensures
* correct values are set, including defaults
*
* @test
* @dataProvider provider_cookie_set
* @param string $key
* @param string $value
* @param string $expected
* @return void
*/
public function test_cookie_set($key, $value, $expected)
{
// Setup the Response and apply cookie
$response = new Response;
$response->cookie($key, $value);
foreach ($expected as $_key => $_value)
{
$cookie = $response->cookie($_key);
$this->assertSame($_value['value'], $cookie['value']);
$this->assertSame($_value['expiration'], $cookie['expiration']);
}
}
/**
* Tests the Response::cookie() get functionality
*
* @return void
*/
public function test_cookie_get()
{
$response = new Response;
// Test for empty cookies
$this->assertSame(array(), $response->cookie());
// Test for no specific cookie
$this->assertNull($response->cookie('foobar'));
$response->cookie('foo', 'bar');
$cookie = $response->cookie('foo');
$this->assertSame('bar', $cookie['value']);
$this->assertSame(Cookie::$expiration, $cookie['expiration']);
}
/**
* Tests that the headers are not sent by PHP in CLI mode
*
* @return void
*/
public function test_send_headers_cli()
{
if (Kohana::$is_cli)
{
$content_type = 'application/json';
$response = new Response;
$response->headers('content-type', $content_type)
->send_headers();
$this->assertFalse(headers_sent());
}
else
{
$this->markTestSkipped('Unable to perform test outside of CLI mode');
}
}
/**
* Test the content type is sent when set
*
* @test
*/
public function test_content_type_when_set()
{
$content_type = 'application/json';
$response = new Response;
$response->headers('content-type', $content_type);
$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,14 +4,19 @@
* Description of RouteTest
*
* @group kohana
* @group kohana.route
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_RouteTest extends Kohana_Unittest_TestCase
include Kohana::find_file('tests', 'test_data/callback_routes');
class Kohana_RouteTest extends Unittest_TestCase
{
/**
* Remove all caches
@@ -73,7 +78,7 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
public function test_name_returns_routes_name_or_false_if_dnx()
{
$route = Route::set('flamingo_people', 'flamingo/dance');
$this->assertSame('flamingo_people', Route::name($route));
$route = new Route('dance/dance');
@@ -101,6 +106,9 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
// Then try and load said cache
$this->assertTrue(Route::cache());
// Check the route cache flag
$this->assertTrue(Route::$cache);
// And if all went ok the nonsensical route should be gone...
$this->assertEquals($routes, Route::all());
}
@@ -108,15 +116,18 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
/**
* Route::cache() should return FALSE if cached routes could not be found
*
* The cache is cleared before and after each test in setUp tearDown
* The cache is cleared before and after each test in setUp tearDown
* by cleanCacheDir()
*
*
* @test
* @covers Route::cache
*/
public function test_cache_returns_false_if_cache_dnx()
{
$this->assertSame(FALSE, Route::cache(), 'Route cache was not empty');
// Check the route cache flag
$this->assertFalse(Route::$cache);
}
/**
@@ -139,37 +150,52 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
$this->assertAttributeSame('', '_uri', $route);
$this->assertAttributeSame(array(), '_regex', $route);
$this->assertAttributeSame(array('action' => 'index'), '_defaults', $route);
$this->assertAttributeSame(array('action' => 'index', 'host' => FALSE), '_defaults', $route);
$this->assertAttributeSame(NULL, '_route_regex', $route);
}
/**
* Provider for test_constructor_only_changes_custom_regex_if_passed
*
* @return array
*/
public function provider_constructor_only_changes_custom_regex_if_passed()
{
return array(
array('<controller>/<action>', '<controller>/<action>'),
array(array('Route_Holder', 'default_callback'), array('Route_Holder', 'default_callback')),
);
}
/**
* The constructor should only use custom regex if passed a non-empty array
*
* Technically we can't "test" this as the default regex is an empty array, this
* is purely for improving test coverage
*
* @dataProvider provider_constructor_only_changes_custom_regex_if_passed
*
* @test
* @covers Route::__construct
*/
public function test_constructor_only_changes_custom_regex_if_passed()
public function test_constructor_only_changes_custom_regex_if_passed($uri, $uri2)
{
$route = new Route('<controller>/<action>', array());
$route = new Route($uri, array());
$this->assertAttributeSame(array(), '_regex', $route);
$route = new Route('<controller>/<action>', NULL);
$route = new Route($uri2, NULL);
$this->assertAttributeSame(array(), '_regex', $route);
}
/**
* When we pass custom regex to the route's constructor it should it
* in leu of the default
* in leu of the default. This does not apply to callback/lambda routes
*
* @test
* @covers Route::__construct
* @covers Route::_compile
* @covers Route::compile
*/
public function test_route_uses_custom_regex_passed_to_constructor()
{
@@ -185,108 +211,268 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
);
}
/**
* Provider for test_matches_returns_false_on_failure
*
* @return array
*/
public function provider_matches_returns_false_on_failure()
{
return array(
array('projects/(<project_id>/(<controller>(/<action>(/<id>))))', 'apple/pie'),
array(array('Route_Holder', 'default_callback'), 'apple/pie'),
);
}
/**
* Route::matches() should return false if the route doesn't match against a uri
*
* @dataProvider provider_matches_returns_false_on_failure
*
* @test
* @covers Route::matches
*/
public function test_matches_returns_false_on_failure()
public function test_matches_returns_false_on_failure($uri, $match)
{
$route = new Route('projects/(<project_id>/(<controller>(/<action>(/<id>))))');
$route = new Route($uri);
$this->assertSame(FALSE, $route->matches('apple/pie'));
$this->assertSame(FALSE, $route->matches($match));
}
/**
* Provider for test_matches_returns_array_of_parameters_on_successful_match
*
* @return array
*/
public function provider_matches_returns_array_of_parameters_on_successful_match()
{
return array(
array(
'(<controller>(/<action>(/<id>)))',
'welcome/index',
'welcome',
'index',
),
array(
array('Route_Holder', 'matches_returns_array_of_parameters_on_successful_match'),
'apple/pie',
'welcome',
'index',
),
);
}
/**
* Route::matches() should return an array of parameters when a match is made
* An parameters that are not matched should not be present in the array of matches
*
* @dataProvider provider_matches_returns_array_of_parameters_on_successful_match
*
* @test
* @covers Route::matches
*/
public function test_matches_returns_array_of_parameters_on_successful_match()
public function test_matches_returns_array_of_parameters_on_successful_match($uri, $m, $c, $a)
{
$route = new Route('(<controller>(/<action>(/<id>)))');
$route = new Route($uri);
$matches = $route->matches('welcome/index');
$matches = $route->matches($m);
$this->assertInternalType('array', $matches);
$this->assertArrayHasKey('controller', $matches);
$this->assertArrayHasKey('action', $matches);
$this->assertArrayNotHasKey('id', $matches);
$this->assertSame(2, count($matches));
$this->assertSame('welcome', $matches['controller']);
$this->assertSame('index', $matches['action']);
// $this->assertSame(5, count($matches));
$this->assertSame($c, $matches['controller']);
$this->assertSame($a, $matches['action']);
}
/**
* Provider for test_matches_returns_array_of_parameters_on_successful_match
*
* @return array
*/
public function provider_defaults_are_used_if_params_arent_specified()
{
return array(
array(
'<controller>(/<action>(/<id>))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'index',
'unit/test/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'welcome',
),
array(
'(<controller>(/<action>(/<id>)))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'index',
'unit/test/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'',
),
array(
array('Route_Holder', 'default_return_callback'),
'(<controller>(/<action>(/<id>)))',
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'index',
'unit/test/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'',
),
);
}
/**
* Defaults specified with defaults() should be used if their values aren't
* present in the uri
*
* @dataProvider provider_defaults_are_used_if_params_arent_specified
*
* @test
* @covers Route::matches
*/
public function test_defaults_are_used_if_params_arent_specified()
public function test_defaults_are_used_if_params_arent_specified($uri, $regex, $defaults, $c, $a, $test_uri, $test_uri_array, $default_uri)
{
$route = new Route('(<controller>(/<action>(/<id>)))');
$route->defaults(array('controller' => 'welcome', 'action' => 'index'));
$route = new Route($uri, $regex);
$route->defaults($defaults);
$matches = $route->matches('');
$matches = $route->matches($default_uri);
$this->assertInternalType('array', $matches);
$this->assertArrayHasKey('controller', $matches);
$this->assertArrayHasKey('action', $matches);
$this->assertArrayNotHasKey('id', $matches);
$this->assertSame(2, count($matches));
$this->assertSame('welcome', $matches['controller']);
$this->assertSame('index', $matches['action']);
$this->assertSame('unit/test/1', $route->uri(array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
)));
$this->assertSame('welcome/index', $route->uri());
// $this->assertSame(4, count($matches));
$this->assertSame($c, $matches['controller']);
$this->assertSame($a, $matches['action']);
$this->assertSame($test_uri, $route->uri($test_uri_array));
$this->assertSame($default_uri, $route->uri());
}
/**
* Provider for test_required_parameters_are_needed
*
* @return array
*/
public function provider_required_parameters_are_needed()
{
return array(
array(
'admin(/<controller>(/<action>(/<id>)))',
'admin',
'admin/users/add',
),
array(
array('Route_Holder', 'required_parameters_are_needed'),
'admin',
'admin/users/add',
),
);
}
/**
* This tests that routes with required parameters will not match uris without them present
*
* @dataProvider provider_required_parameters_are_needed
*
* @test
* @covers Route::matches
*/
public function test_required_parameters_are_needed()
public function test_required_parameters_are_needed($uri, $matches_route1, $matches_route2)
{
$route = new Route('admin(/<controller>(/<action>(/<id>)))');
$route = new Route($uri);
$this->assertFalse($route->matches(''));
$matches = $route->matches('admin');
$matches = $route->matches($matches_route1);
$this->assertInternalType('array', $matches);
$matches = $route->matches('admin/users/add');
$matches = $route->matches($matches_route2);
$this->assertSame(2, count($matches));
$this->assertInternalType('array', $matches);
// $this->assertSame(5, count($matches));
$this->assertArrayHasKey('controller', $matches);
$this->assertArrayHasKey('action', $matches);
}
/**
* Provider for test_required_parameters_are_needed
*
* @return array
*/
public function provider_reverse_routing_returns_routes_uri_if_route_is_static()
{
return array(
array(
'info/about_us',
NULL,
'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'),
),
);
}
/**
* This tests the reverse routing returns the uri specified in the route
* if it's a static route
*
* A static route is a route without any parameters
*
* @dataProvider provider_reverse_routing_returns_routes_uri_if_route_is_static
*
* @test
* @covers Route::uri
*/
public function test_reverse_routing_returns_routes_uri_if_route_is_static()
public function test_reverse_routing_returns_routes_uri_if_route_is_static($uri, $regex, $target_uri, $uri_params)
{
$route = new Route('info/about_us');
$route = new Route($uri, $regex);
$this->assertSame('info/about_us', $route->uri(array('some' => 'random', 'params' => 'to confuse')));
$this->assertSame($target_uri, $route->uri($uri_params));
}
/**
* Provider for test_uri_throws_exception_if_required_params_are_missing
*
* @return array
*/
public function provider_uri_throws_exception_if_required_params_are_missing()
{
return array(
array(
'<controller>(/<action)',
NULL,
array('action' => 'awesome-action'),
),
array(
array('Route_Holder', 'default_return_callback'),
'<controller>(/<action)',
array('action' => 'awesome-action'),
),
);
}
/**
@@ -294,16 +480,18 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
* (i.e. <controller> in '<controller(/<action)') then it should throw an exception
* if the param was not provided
*
* @dataProvider provider_uri_throws_exception_if_required_params_are_missing
*
* @test
* @covers Route::uri
*/
public function test_uri_throws_exception_if_required_params_are_missing()
public function test_uri_throws_exception_if_required_params_are_missing($uri, $regex, $uri_array)
{
$route = new Route('<controller>(/<action)');
$route = new Route($uri, $regex);
try
try
{
$route->uri(array('action' => 'awesome-action'));
$route->uri($uri_array);
$this->fail('Route::uri should throw exception if required param is not provided');
}
@@ -315,6 +503,47 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
}
}
/**
* Provider for test_uri_fills_required_uri_segments_from_params
*
* @return array
*/
public function provider_uri_fills_required_uri_segments_from_params()
{
return array(
array(
'<controller>/<action>(/<id>)',
NULL,
'users/edit',
array(
'controller' => 'users',
'action' => 'edit',
),
'users/edit/god',
array(
'controller' => 'users',
'action' => 'edit',
'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',
),
),
);
}
/**
* The logic for replacing required segments is separate (but similar) to that for
* replacing optional segments.
@@ -322,28 +551,23 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
* This test asserts that Route::uri will replace required segments with provided
* params
*
* @dataProvider provider_uri_fills_required_uri_segments_from_params
*
* @test
* @covers Route::uri
*/
public function test_uri_fills_required_uri_segments_from_params()
public function test_uri_fills_required_uri_segments_from_params($uri, $regex, $uri_string1, $uri_array1, $uri_string2, $uri_array2)
{
$route = new Route('<controller>/<action>(/<id>)');
$route = new Route($uri, $regex);
$this->assertSame(
'users/edit',
$route->uri(array(
'controller' => 'users',
'action' => 'edit',
))
$uri_string1,
$route->uri($uri_array1)
);
$this->assertSame(
'users/edit/god',
$route->uri(array(
'controller' => 'users',
'action' => 'edit',
'id' => 'god',
))
$uri_string2,
$route->uri($uri_array2)
);
}
@@ -354,9 +578,9 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
public function provider_composing_url_from_route()
{
return array(
array('/welcome'),
array('/'),
array('/news/view/42', array('controller' => 'news', 'action' => 'view', 'id' => 42)),
array('http://kohanaframework.org/news', array('controller' => 'news'), true)
array('http://kohanaframework.org/news', array('controller' => 'news'), 'http')
);
}
@@ -382,7 +606,6 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
$this->setEnvironment(array(
'_SERVER' => array('HTTP_HOST' => 'kohanaframework.org'),
'Kohana::$base_url' => '/',
'Request::$protocol' => 'http',
'Kohana::$index_file' => '',
));
@@ -390,27 +613,103 @@ class Kohana_RouteTest extends Kohana_Unittest_TestCase
}
/**
* Tests Route::_compile()
* Tests Route::compile()
*
* Makes sure that compile will use custom regex if specified
*
* @test
* @covers Route::_compile
* @covers Route::compile
*/
public function test_compile_uses_custom_regex_if_specificed()
{
$route = new Route(
'<controller>(/<action>(/<id>))',
$compiled = Route::compile(
'<controller>(/<action>(/<id>))',
array(
'controller' => '[a-z]+',
'id' => '\d+',
)
);
$this->assertAttributeSame(
'#^(?P<controller>[a-z]+)(?:/(?P<action>[^/.,;?\n]++)(?:/(?P<id>\d+))?)?$#uD',
'_route_regex',
$route
$this->assertSame('#^(?P<controller>[a-z]+)(?:/(?P<action>[^/.,;?\n]++)(?:/(?P<id>\d+))?)?$#uD', $compiled);
}
/**
* Tests Route::is_external(), ensuring the host can return
* whether internal or external host
*/
public function test_is_external_route_from_host()
{
// Setup local route
Route::set('internal', 'local/test/route')
->defaults(array(
'controller' => 'foo',
'action' => 'bar'
)
);
// Setup external route
Route::set('external', 'local/test/route')
->defaults(array(
'controller' => 'foo',
'action' => 'bar',
'host' => 'http://kohanaframework.org'
)
);
// Test internal route
$this->assertFalse(Route::get('internal')->is_external());
// Test external route
$this->assertTrue(Route::get('external')->is_external());
}
/**
* Provider for test_external_route_includes_params_in_uri
*
* @return array
*/
public function provider_external_route_includes_params_in_uri()
{
return array(
array(
'<controller>/<action>',
array(
'controller' => 'foo',
'action' => 'bar',
'host' => 'kohanaframework.org'
),
'http://kohanaframework.org/foo/bar'
),
array(
'<controller>/<action>',
array(
'controller' => 'foo',
'action' => 'bar',
'host' => 'http://kohanaframework.org'
),
'http://kohanaframework.org/foo/bar'
),
array(
'foo/bar',
array(
'controller' => 'foo',
'host' => 'http://kohanaframework.org'
),
'http://kohanaframework.org/foo/bar'
),
);
}
/**
* Tests the external route include route parameters
*
* @dataProvider provider_external_route_includes_params_in_uri
*/
public function test_external_route_includes_params_in_uri($route, $defaults, $expected_uri)
{
Route::set('test', $route)
->defaults($defaults);
$this->assertSame($expected_uri, Route::get('test')->uri());
}
}

View File

@@ -4,9 +4,13 @@
* Tests Kohana_Security
*
* @group kohana
* @group kohana.security
*
* @package Kohana
* @category Tests
*/
Class Kohana_SecurityTest extends Kohana_Unittest_TestCase
class Kohana_SecurityTest extends Unittest_TestCase
{
/**
* Provides test data for test_envode_php_tags()
@@ -86,20 +90,4 @@ Class Kohana_SecurityTest extends Kohana_Unittest_TestCase
$this->assertSame($expected, Security::token(FALSE));
Session::instance()->delete(Security::$token_name);
}
/**
* Tests that Security::xss_clean() removes null bytes
*
*
* @test
* @covers Security::xss_clean
* @ticket 2676
* @see http://www.hakipedia.com/index.php/Poison_Null_Byte#Perl_PHP_Null_Byte_Injection
*/
public function test_xss_clean_removes_null_bytes()
{
$input = "<\0script>alert('XSS');<\0/script>";
$this->assertSame("alert('XSS');", Security::xss_clean($input));
}
}

View File

@@ -1,17 +1,19 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests the cookie class
* Tests the session class
*
* @group kohana
* @group kohana.session
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_SessionTest extends Kohana_Unittest_TestCase
class Kohana_SessionTest extends Unittest_TestCase
{
/**
@@ -25,7 +27,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
}
/**
* Provides test data for
* Provides test data for
*
* test_constructor_uses_name_from_config_and_casts()
*
@@ -41,7 +43,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
'name' => 'awesomeness',
'lifetime' => 1231456421,
'encrypted' => FALSE
),
),
array(
'name' => 'awesomeness',
'lifetime' => '1231456421',
@@ -53,7 +55,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
array(
'name' => '123',
'encrypted' => 'default',
),
),
array(
'name' => 123,
'encrypted' => TRUE,
@@ -74,7 +76,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
{
$session = $this->getMockForAbstractClass('Session', array($config));
foreach($expected as $var => $value)
foreach ($expected as $var => $value)
{
$this->assertAttributeSame($value, '_'.$var, $session);
}
@@ -134,7 +136,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
$this->assertSame('foobar', $session->get('our_var'));
}
/**
* When a session is initially created it should have no data
*
@@ -173,7 +175,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
public function test_default_session_is_unencrypted()
{
$session = $this->getMockSession();
$this->assertAttributeSame(FALSE, '_encrypted', $session);
}
@@ -205,7 +207,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
}
/**
* Make sure that get() is using the default value we provide and
* Make sure that get() is using the default value we provide and
* isn't tampering with it
*
* @test
@@ -244,7 +246,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
$session = $this->getMockSession();
$session->set('arkward', NULL);
$this->assertSame(NULL, $session->get('arkward', 'uh oh'));
}
@@ -288,7 +290,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
$this->assertSame($session, $session->set('pork', 'pie'));
$this->assertAttributeSame(
array('pork' => 'pie'),
array('pork' => 'pie'),
'_data',
$session
);
@@ -321,7 +323,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
'c' => 'C',
'easy' => '123'
);
// Make a copy of $data for testing purposes
$copy = $data;
@@ -353,8 +355,8 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
return array(
// If driver returns array then just load it up
array(
array(),
'wacka_wacka',
array(),
'wacka_wacka',
array()
),
array(
@@ -364,7 +366,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
),
// If it's a string an encrpytion is disabled (by default) base64decode and unserialize
array(
array('dead' => 'arrival'),
array('dead' => 'arrival'),
'lolums',
'YToxOntzOjQ6ImRlYWQiO3M6NzoiYXJyaXZhbCI7fQ=='
),
@@ -374,7 +376,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
/**
* This is one of the "big" tests for the session lib
*
* The test makes sure that
* The test makes sure that
*
* 1. Session asks the driver for the data relating to $session_id
* 2. That it will load the returned data into the session
@@ -405,7 +407,7 @@ Class Kohana_SessionTest extends Kohana_Unittest_TestCase
public function test_regenerate_tells_driver_to_regenerate()
{
$session = $this->getMockSession();
$new_session_id = 'asdnoawdnoainf';
$session->expects($this->once())

View File

@@ -4,14 +4,18 @@
* Tests the kohana text class (Kohana_Text)
*
* @group kohana
* @group kohana.text
*
* @package Kohana
* @category Tests
*/
Class Kohana_TextTest extends Kohana_Unittest_TestCase
class Kohana_TextTest extends Unittest_TestCase
{
/**
* Sets up the test enviroment
*/
public function setUp()
function setUp()
{
parent::setUp();
@@ -25,7 +29,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @covers Text::auto_p
*/
public function test_auto_para_returns_empty_string_on_empty_input()
function test_auto_para_returns_empty_string_on_empty_input()
{
$this->assertSame('', Text::auto_p(''));
}
@@ -34,7 +38,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array Test Data
*/
public function provider_auto_para_does_not_enclose_html_tags_in_paragraphs()
function provider_auto_para_does_not_enclose_html_tags_in_paragraphs()
{
return array(
array(
@@ -49,18 +53,18 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
}
/**
* This test makes sure that auto_p doesn't enclose HTML tags
* This test makes sure that auto_p doesn't enclose HTML tags
* in paragraphs
*
* @test
* @covers Text::auto_p
* @dataProvider provider_auto_para_does_not_enclose_html_tags_in_paragraphs
*/
public function test_auto_para_does_not_enclose_html_tags_in_paragraphs(array $tags, $text)
*/
function test_auto_para_does_not_enclose_html_tags_in_paragraphs(array $tags, $text)
{
$output = Text::auto_p($text);
foreach($tags as $tag)
foreach ($tags as $tag)
{
$this->assertNotTag(
array('tag' => $tag, 'ancestor' => array('tag' => 'p')),
@@ -76,7 +80,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @covers Text::auto_p
*/
public function test_auto_para_encloses_slot_in_paragraph()
function test_auto_para_encloses_slot_in_paragraph()
{
$text = 'Pick a pinch of purple pepper';
@@ -102,7 +106,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array Array of test data
*/
public function provider_limit_words()
function provider_limit_words()
{
return array
(
@@ -112,13 +116,13 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
array('The rain...', 'The rain in spain', 2, '...'),
);
}
/**
*
* @test
* @dataProvider provider_limit_words
*/
public function test_limit_words($expected, $str, $limit, $end_char)
function test_limit_words($expected, $str, $limit, $end_char)
{
$this->assertSame($expected, Text::limit_words($str, $limit, $end_char));
}
@@ -128,7 +132,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array Test data
*/
public function provider_limit_chars()
function provider_limit_chars()
{
return array
(
@@ -151,7 +155,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_limit_chars
*/
public function test_limit_chars($expected, $str, $limit, $end_char, $preserve_words)
function test_limit_chars($expected, $str, $limit, $end_char, $preserve_words)
{
$this->assertSame($expected, Text::limit_chars($str, $limit, $end_char, $preserve_words));
}
@@ -161,14 +165,14 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @test
*/
public function test_alternate_alternates_between_parameters()
function test_alternate_alternates_between_parameters()
{
list($val_a, $val_b, $val_c) = array('good', 'bad', 'ugly');
$this->assertSame('good', Text::alternate($val_a, $val_b, $val_c));
$this->assertSame('bad', Text::alternate($val_a, $val_b, $val_c));
$this->assertSame('ugly', Text::alternate($val_a, $val_b, $val_c));
$this->assertSame('good', Text::alternate($val_a, $val_b, $val_c));
}
@@ -178,13 +182,13 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @covers Text::alternate
*/
public function test_alternate_resets_when_called_with_no_params_and_returns_empty_string()
function test_alternate_resets_when_called_with_no_params_and_returns_empty_string()
{
list($val_a, $val_b, $val_c) = array('yes', 'no', 'maybe');
$this->assertSame('yes', Text::alternate($val_a, $val_b, $val_c));
$this->assertSame('', Text::alternate());
$this->assertSame('', Text::alternate());
$this->assertSame('yes', Text::alternate($val_a, $val_b, $val_c));
}
@@ -192,9 +196,9 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
/**
* Provides test data for test_reducde_slashes()
*
* @returns array Array of test data
* @returns array Array of test data
*/
public function provider_reduce_slashes()
function provider_reduce_slashes()
{
return array
(
@@ -209,7 +213,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_reduce_slashes
*/
public function test_reduce_slashes($expected, $str)
function test_reduce_slashes($expected, $str)
{
$this->assertSame($expected, Text::reduce_slashes($str));
}
@@ -219,7 +223,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array Test data
*/
public function provider_censor()
function provider_censor()
{
return array
@@ -240,7 +244,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_censor
*/
public function test_censor($expected, $str, $badwords, $replacement, $replace_partial_words)
function test_censor($expected, $str, $badwords, $replacement, $replace_partial_words)
{
$this->assertSame($expected, Text::censor($str, $badwords, $replacement, $replace_partial_words));
}
@@ -250,7 +254,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array Test Data
*/
public function provider_random()
function provider_random()
{
return array(
array('alnum', 8),
@@ -268,7 +272,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
/**
* Tests Text::random() as well as possible
*
* Obviously you can't compare a randomly generated string against a
* Obviously you can't compare a randomly generated string against a
* pre-generated one and check that they are the same as this goes
* against the whole ethos of random.
*
@@ -278,7 +282,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_random
*/
public function test_random($type, $length)
function test_random($type, $length)
{
if ($type === NULL)
{
@@ -308,7 +312,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
break;
}
$this->assertRegExp('/^['.$pool.']{'.$length.'}$/u', Text::random($type, $length));
}
@@ -317,7 +321,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array
*/
public function provider_similar()
function provider_similar()
{
return array
(
@@ -333,7 +337,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @dataProvider provider_similar
* @covers Text::similar
*/
public function test_similar($expected, $words)
function test_similar($expected, $words)
{
$this->assertSame($expected, Text::similar($words));
}
@@ -365,7 +369,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_bytes
*/
public function test_bytes($expected, $bytes, $force_unit, $format, $si)
function test_bytes($expected, $bytes, $force_unit, $format, $si)
{
$this->assertSame($expected, Text::bytes($bytes, $force_unit, $format, $si));
}
@@ -375,7 +379,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
*
* @return array Test data
*/
public function provider_widont()
function provider_widont()
{
return array
(
@@ -391,7 +395,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @test
* @dataProvider provider_widont
*/
public function test_widont($expected, $string)
function test_widont($expected, $string)
{
$this->assertSame($expected, Text::widont($string));
}
@@ -410,13 +414,13 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
* @covers Text::auto_link_emails
* @ticket 2772
*/
public function test_auto_link_emails_respects_word_boundaries()
function test_auto_link_emails_respects_word_boundaries()
{
$original = '<ul>
<li>voorzitter@xxxx.com</li>
<li>vicevoorzitter@xxxx.com</li>
</ul>';
$this->assertFalse(strpos('vice', Text::auto_link_emails($original)));
}
@@ -454,7 +458,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
/**
* Provides test data for test_auto_link_urls()
*
* @return array
* @return array
*/
public function provider_auto_link_urls()
{
@@ -508,7 +512,7 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
/**
* Provides test data for test_auto_link_emails()
*
* @return array
* @return array
*/
public function provider_auto_link_emails()
{
@@ -573,24 +577,24 @@ Class Kohana_TextTest extends Kohana_Unittest_TestCase
{
$linked_text = Text::auto_link($text);
if($urls === FALSE)
if ($urls === FALSE)
{
$this->assertNotContains('http://', $linked_text);
}
elseif(count($urls))
elseif (count($urls))
{
foreach($urls as $url)
foreach ($urls as $url)
{
// Assert that all the urls have been caught by text auto_link_urls()
$this->assertContains(Text::auto_link_urls($url), $linked_text);
}
}
foreach($emails as $email)
foreach ($emails as $email)
{
$this->assertNotContains($email, $linked_text);
}
}
}

View File

@@ -6,13 +6,14 @@
* @group kohana
* @group kohana.url
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
Class Kohana_URLTest extends Kohana_Unittest_TestCase
class Kohana_URLTest extends Unittest_TestCase
{
/**
* Default values for the environment, see setEnvironment
@@ -21,50 +22,46 @@ Class Kohana_URLTest extends Kohana_Unittest_TestCase
protected $environmentDefault = array(
'Kohana::$base_url' => '/kohana/',
'Kohana::$index_file'=> 'index.php',
'Request::$protocol' => 'http',
'HTTP_HOST' => 'example.com',
'_GET' => array(),
);
/**
* Provides test data for test_base()
*
*
* @return array
*/
public function provider_base()
{
return array(
// $index, $protocol, $expected, $enviroment
//
// Test with different combinations of parameters for max code coverage
array(FALSE, FALSE, '/kohana/'),
array(FALSE, TRUE, 'http://example.com/kohana/'),
array(TRUE, FALSE, '/kohana/index.php/'),
array(TRUE, FALSE, '/kohana/index.php/'),
array(TRUE, TRUE, 'http://example.com/kohana/index.php/'),
array(TRUE, 'http', 'http://example.com/kohana/index.php/'),
array(TRUE, 'https','https://example.com/kohana/index.php/'),
array(TRUE, 'ftp', 'ftp://example.com/kohana/index.php/'),
// $protocol, $index, $expected, $enviroment
//
// These tests make sure that the protocol changes when the global setting changes
array(TRUE, TRUE, 'https://example.com/kohana/index.php/', array('Request::$protocol' => 'https')),
array(FALSE, TRUE, 'https://example.com/kohana/', array('Request::$protocol' => 'https')),
// Test with different combinations of parameters for max code coverage
array(NULL, FALSE, '/kohana/'),
array('http', FALSE, 'http://example.com/kohana/'),
array(NULL, TRUE, '/kohana/index.php/'),
array(NULL, TRUE, '/kohana/index.php/'),
array('http', TRUE, 'http://example.com/kohana/index.php/'),
array('https', TRUE, 'https://example.com/kohana/index.php/'),
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)),
// Change base url'
array(FALSE, 'https', 'https://example.com/kohana/', array('Kohana::$base_url' => 'omglol://example.com/kohana/')),
array('https', FALSE, 'https://example.com/kohana/', array('Kohana::$base_url' => 'omglol://example.com/kohana/')),
// Use port in base url, issue #3307
array(FALSE, TRUE, 'http://example.com:8080/', array('Kohana::$base_url' => 'example.com:8080/')),
array('http', FALSE, 'http://example.com:8080/', array('Kohana::$base_url' => 'example.com:8080/')),
// Use protocol from base url if none specified
array(FALSE, FALSE, 'http://www.example.com/', array('Kohana::$base_url' => 'http://www.example.com/')),
array(NULL, FALSE, 'http://www.example.com/', array('Kohana::$base_url' => 'http://www.example.com/')),
// Use HTTP_HOST before SERVER_NAME
array(FALSE, 'http', 'http://example.com/kohana/', array('HTTP_HOST' => 'example.com', 'SERVER_NAME' => 'example.org')),
array('http', FALSE, 'http://example.com/kohana/', array('HTTP_HOST' => 'example.com', 'SERVER_NAME' => 'example.org')),
// Use SERVER_NAME if HTTP_HOST DNX
array(FALSE, 'http', 'http://example.org/kohana/', array('HTTP_HOST' => NULL, 'SERVER_NAME' => 'example.org')),
array('http', FALSE, 'http://example.org/kohana/', array('HTTP_HOST' => NULL, 'SERVER_NAME' => 'example.org')),
);
}
@@ -73,50 +70,50 @@ Class Kohana_URLTest extends Kohana_Unittest_TestCase
*
* @test
* @dataProvider provider_base
* @param boolean $index Parameter for Url::base()
* @param boolean $protocol Parameter for Url::base()
* @param boolean $index Parameter for Url::base()
* @param string $expected Expected url
* @param array $enviroment Array of enviroment vars to change @see Kohana_URLTest::setEnvironment()
*/
public function test_base($index, $protocol, $expected, array $enviroment = array())
public function test_base($protocol, $index, $expected, array $enviroment = array())
{
$this->setEnvironment($enviroment);
$this->assertSame(
$expected,
URL::base($index, $protocol)
URL::base($protocol, $index)
);
}
/**
* Provides test data for test_site()
*
*
* @return array
*/
public function provider_site()
{
return array(
array('', FALSE, '/kohana/index.php/'),
array('', TRUE, 'http://example.com/kohana/index.php/'),
array('', NULL, '/kohana/index.php/'),
array('', 'http', 'http://example.com/kohana/index.php/'),
array('my/site', FALSE, '/kohana/index.php/my/site'),
array('my/site', TRUE, 'http://example.com/kohana/index.php/my/site'),
array('my/site', NULL, '/kohana/index.php/my/site'),
array('my/site', 'http', 'http://example.com/kohana/index.php/my/site'),
// @ticket #3110
array('my/site/page:5', FALSE, '/kohana/index.php/my/site/page:5'),
array('my/site/page:5', TRUE, 'http://example.com/kohana/index.php/my/site/page:5'),
array('my/site/page:5', NULL, '/kohana/index.php/my/site/page:5'),
array('my/site/page:5', 'http', 'http://example.com/kohana/index.php/my/site/page:5'),
array('my/site?var=asd&kohana=awesome', FALSE, '/kohana/index.php/my/site?var=asd&kohana=awesome'),
array('my/site?var=asd&kohana=awesome', TRUE, 'http://example.com/kohana/index.php/my/site?var=asd&kohana=awesome'),
array('my/site?var=asd&kohana=awesome', NULL, '/kohana/index.php/my/site?var=asd&kohana=awesome'),
array('my/site?var=asd&kohana=awesome', 'http', 'http://example.com/kohana/index.php/my/site?var=asd&kohana=awesome'),
array('?kohana=awesome&life=good', FALSE, '/kohana/index.php/?kohana=awesome&life=good'),
array('?kohana=awesome&life=good', TRUE, 'http://example.com/kohana/index.php/?kohana=awesome&life=good'),
array('?kohana=awesome&life=good', NULL, '/kohana/index.php/?kohana=awesome&life=good'),
array('?kohana=awesome&life=good', 'http', 'http://example.com/kohana/index.php/?kohana=awesome&life=good'),
array('?kohana=awesome&life=good#fact', FALSE, '/kohana/index.php/?kohana=awesome&life=good#fact'),
array('?kohana=awesome&life=good#fact', TRUE, 'http://example.com/kohana/index.php/?kohana=awesome&life=good#fact'),
array('?kohana=awesome&life=good#fact', NULL, '/kohana/index.php/?kohana=awesome&life=good#fact'),
array('?kohana=awesome&life=good#fact', 'http', 'http://example.com/kohana/index.php/?kohana=awesome&life=good#fact'),
array('some/long/route/goes/here?kohana=awesome&life=good#fact', FALSE, '/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'),
array('some/long/route/goes/here?kohana=awesome&life=good#fact', TRUE, 'http://example.com/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'),
array('some/long/route/goes/here?kohana=awesome&life=good#fact', NULL, '/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'),
array('some/long/route/goes/here?kohana=awesome&life=good#fact', 'http', 'http://example.com/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'),
array('/route/goes/here?kohana=awesome&life=good#fact', 'https', 'https://example.com/kohana/index.php/route/goes/here?kohana=awesome&life=good#fact'),
array('/route/goes/here?kohana=awesome&life=good#fact', 'ftp', 'ftp://example.com/kohana/index.php/route/goes/here?kohana=awesome&life=good#fact'),
@@ -223,7 +220,7 @@ Class Kohana_URLTest extends Kohana_Unittest_TestCase
* @param string $separator Seperate to replace invalid characters with
* @param string $expected Expected result
*/
public function test_Title($expected, $title, $separator, $ascii_only = FALSE)
public function test_title($expected, $title, $separator, $ascii_only = FALSE)
{
$this->assertSame(
$expected,
@@ -235,7 +232,7 @@ Class Kohana_URLTest extends Kohana_Unittest_TestCase
* Provides test data for URL::query()
* @return array
*/
public function provider_Query()
public function provider_query()
{
return array(
array(array(), '', NULL),

View File

@@ -5,12 +5,13 @@
* @group kohana
* @group kohana.utf8
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_UTF8Test extends Kohana_Unittest_TestCase
class Kohana_UTF8Test extends Unittest_TestCase
{
/**
@@ -157,9 +158,6 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
public function test_strlen($input, $expected)
{
$this->assertSame($expected, UTF8::strlen($input));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
$this->assertSame($expected, UTF8::strlen($input));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
}
/**
@@ -182,9 +180,6 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
public function test_strpos($input, $str, $offset, $expected)
{
$this->assertSame($expected, UTF8::strpos($input, $str, $offset));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
$this->assertSame($expected, UTF8::strpos($input, $str, $offset));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
}
/**
@@ -207,9 +202,6 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
public function test_strrpos($input, $str, $offset, $expected)
{
$this->assertSame($expected, UTF8::strrpos($input, $str, $offset));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
$this->assertSame($expected, UTF8::strrpos($input, $str, $offset));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
}
/**
@@ -234,9 +226,6 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
public function test_substr($input, $offset, $length, $expected)
{
$this->assertSame($expected, UTF8::substr($input, $offset, $length));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
$this->assertSame($expected, UTF8::substr($input, $offset, $length));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
}
/**
@@ -281,9 +270,6 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
public function test_strtolower($input, $expected)
{
$this->assertSame($expected, UTF8::strtolower($input));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
$this->assertSame($expected, UTF8::strtolower($input));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
}
/**
@@ -306,9 +292,6 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
public function test_strtoupper($input, $expected)
{
$this->assertSame($expected, UTF8::strtoupper($input));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
$this->assertSame($expected, UTF8::strtoupper($input));
UTF8::$server_utf8 = ! UTF8::$server_utf8;
}
/**
@@ -644,4 +627,4 @@ class Kohana_UTF8Test extends Kohana_Unittest_TestCase
{
$this->assertSame($expected, UTF8::ord($input));
}
}
}

View File

@@ -6,13 +6,14 @@
* @group kohana
* @group kohana.upload
*
* @package Unittest
* @package Kohana
* @category Tests
* @author Kohana Team
* @author Jeremy Bush <contractfrombelow@gmail.com>
* @copyright (c) 2008-2010 Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_UploadTest extends Kohana_Unittest_TestCase
class Kohana_UploadTest extends Unittest_TestCase
{
/**
* Provides test data for test_size()

View File

@@ -0,0 +1,495 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests the Validation lib that's shipped with Kohana
*
* @group kohana
* @group kohana.validation
*
* @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_ValidationTest extends Unittest_TestCase
{
/**
* Tests Validation::factory()
*
* Makes sure that the factory method returns an instance of Validation lib
* and that it uses the variables passed
*
* @test
*/
public function test_factory_method_returns_instance_with_values()
{
$values = array(
'this' => 'something else',
'writing tests' => 'sucks',
'why the hell' => 'amIDoingThis',
);
$instance = Validation::factory($values);
$this->assertTrue($instance instanceof Validation);
$this->assertSame(
$values,
$instance->as_array()
);
}
/**
* When we copy() a validation object, we should have a new validation object
* with the exact same attributes, apart from the data, which should be the
* same as the array we pass to copy()
*
* @test
* @covers Validation::copy
*/
public function test_copy_copies_all_attributes_except_data()
{
$validation = new Validation(array('foo' => 'bar', 'fud' => 'fear, uncertainty, doubt', 'num' => 9));
$validation->rule('num', 'is_int')->rule('foo', 'is_string');
$copy_data = array('foo' => 'no', 'fud' => 'maybe', 'num' => 42);
$copy = $validation->copy($copy_data);
$this->assertNotSame($validation, $copy);
foreach (array('_rules', '_bound', '_labels', '_empty_rules', '_errors') as $attribute)
{
// This is just an easy way to check that the attributes are identical
// Without hardcoding the expected values
$this->assertAttributeSame(
self::readAttribute($validation, $attribute),
$attribute,
$copy
);
}
$this->assertSame($copy_data, $copy->as_array());
}
/**
* When the validation object is initially created there should be no labels
* specified
*
* @test
*/
public function test_initially_there_are_no_labels()
{
$validation = new Validation(array());
$this->assertAttributeSame(array(), '_labels', $validation);
}
/**
* Adding a label to a field should set it in the labels array
* If the label already exists it should overwrite it
*
* In both cases thefunction should return a reference to $this
*
* @test
* @covers Validation::label
*/
public function test_label_adds_and_overwrites_label_and_returns_this()
{
$validation = new Validation(array());
$this->assertSame($validation, $validation->label('email', 'Email Address'));
$this->assertAttributeSame(array('email' => 'Email Address'), '_labels', $validation);
$this->assertSame($validation, $validation->label('email', 'Your Email'));
$validation->label('name', 'Your Name');
$this->assertAttributeSame(
array('email' => 'Your Email', 'name' => 'Your Name'),
'_labels',
$validation
);
}
/**
* Using labels() we should be able to add / overwrite multiple labels
*
* The function should also return $this for chaining purposes
*
* @test
* @covers Validation::labels
*/
public function test_labels_adds_and_overwrites_multiple_labels_and_returns_this()
{
$validation = new Validation(array());
$initial_data = array('kung fu' => 'fighting', 'fast' => 'cheetah');
$this->assertSame($validation, $validation->labels($initial_data));
$this->assertAttributeSame($initial_data, '_labels', $validation);
$this->assertSame($validation, $validation->labels(array('fast' => 'lightning')));
$this->assertAttributeSame(
array('fast' => 'lightning', 'kung fu' => 'fighting'),
'_labels',
$validation
);
}
/**
* Using bind() we should be able to add / overwrite multiple bound variables
*
* The function should also return $this for chaining purposes
*
* @test
* @covers Validation::bind
*/
public function test_bind_adds_and_overwrites_multiple_variables_and_returns_this()
{
$validation = new Validation(array());
$data = array('kung fu' => 'fighting', 'fast' => 'cheetah');
$bound = array(':foo' => 'some value');
// Test binding an array of values
$this->assertSame($validation, $validation->bind($bound));
$this->assertAttributeSame($bound, '_bound', $validation);
// Test binding one value
$this->assertSame($validation, $validation->bind(':foo', 'some other value'));
$this->assertAttributeSame(array(':foo' => 'some other value'), '_bound', $validation);
}
/**
* Provides test data for test_check
*
* @return array
*/
public function provider_check()
{
// $data_array, $rules, $labels, $first_expected, $expected_error
return array(
array(
array('foo' => 'bar'),
array('foo' => array(array('not_empty', NULL))),
array(),
TRUE,
array(),
),
array(
array('unit' => 'test'),
array(
'foo' => array(array('not_empty', NULL)),
'unit' => array(array('min_length', array(':value', 6))
),
),
array(),
FALSE,
array(
'foo' => 'foo must not be empty',
'unit' => 'unit must be at least 6 characters long'
),
),
array(
array('foo' => 'bar'),
array(
// Tests wildcard rules
TRUE => array(array('min_length', array(':value', 4))),
'foo' => array(
array('not_empty', NULL),
// Tests the array syntax for callbacks
array(array('Valid', 'exact_length'), array(':value', 3)),
// Tests the Class::method syntax for callbacks
array('Valid::exact_length', array(':value', 3)),
// Tests the lambda function syntax for callbacks
// Commented out for PHP 5.2 support
// array(function($value){return TRUE;}, array(':value')),
// Tests using a function as a rule
array('is_string', array(':value')),
),
// Tests that rules do not run on empty fields unless they are in _empty_rules
'unit' => array(array('exact_length', array(':value', 4))),
),
array(),
FALSE,
array('foo' => 'foo must be at least 4 characters long'),
),
// Switch things around and make :value an array
array(
array('foo' => array('test', 'data')),
array('foo' => array(array('in_array', array('kohana', ':value')))),
array(),
FALSE,
array('foo' => 'foo must be one of the available options'),
),
// Test wildcard rules with no other rules
array(
array('foo' => array('test')),
array(TRUE => array(array('is_string', array(':value')))),
array('foo' => 'foo'),
FALSE,
array('foo' => '1.foo.is_string'),
),
// Test array rules use method as error name
array(
array('foo' => 'test'),
array('foo' => array(array(array('Valid', 'min_length'), array(':value', 10)))),
array(),
FALSE,
array('foo' => 'foo must be at least 10 characters long'),
),
);
}
/**
* Tests Validation::check()
*
* @test
* @covers Validation::check
* @covers Validation::rule
* @covers Validation::rules
* @covers Validation::errors
* @covers Validation::error
* @dataProvider provider_check
* @param array $array The array of data
* @param array $rules The array of rules
* @param array $labels The array of labels
* @param boolean $expected Is it valid?
* @param boolean $expected_errors Array of expected errors
*/
public function test_check($array, $rules, $labels, $expected, $expected_errors)
{
$validation = new Validation($array);
foreach ($labels as $field => $label)
{
$validation->label($field, $label);
}
foreach ($rules as $field => $field_rules)
{
foreach ($field_rules as $rule)
$validation->rule($field, $rule[0], $rule[1]);
}
$status = $validation->check();
$errors = $validation->errors(TRUE);
$this->assertSame($expected, $status);
$this->assertSame($expected_errors, $errors);
$validation = new validation($array);
foreach ($rules as $field => $rules)
{
$validation->rules($field, $rules);
}
$validation->labels($labels);
$this->assertSame($expected, $validation->check());
}
/**
* Provides test data for test_errors()
*
* @return array
*/
public function provider_errors()
{
// [data, rules, expected], ...
return array(
// No Error
array(
array('username' => 'frank'),
array('username' => array(array('not_empty', NULL))),
array(),
),
// Error from message file
array(
array('username' => ''),
array('username' => array(array('not_empty', NULL))),
array('username' => 'username must not be empty'),
),
// No error message exists, display the path expected
array(
array('username' => 'John'),
array('username' => array(array('strpos', array(':value', 'Kohana')))),
array('username' => 'Validation.username.strpos'),
),
);
}
/**
* Tests Validation::errors()
*
* @test
* @covers Validation::errors
* @dataProvider provider_errors
* @param array $array The array of data
* @param array $rules The array of rules
* @param array $expected Array of expected errors
*/
public function test_errors($array, $rules, $expected)
{
$validation = Validation::factory($array);
foreach ($rules as $field => $field_rules)
{
$validation->rules($field, $field_rules);
}
$validation->check();
$this->assertSame($expected, $validation->errors('Validation', FALSE));
// Should be able to get raw errors array
$this->assertAttributeSame($validation->errors(NULL), '_errors', $validation);
}
/**
* Provides test data for test_translated_errors()
*
* @return array
*/
public function provider_translated_errors()
{
// [data, rules, expected], ...
return array(
array(
array('Spanish' => ''),
array('Spanish' => array(array('not_empty', NULL))),
// Errors are not translated yet so only the label will translate
array('Spanish' => 'Español must not be empty'),
array('Spanish' => 'Spanish must not be empty'),
),
);
}
/**
* Tests Validation::errors()
*
* @test
* @covers Validation::errors
* @dataProvider provider_translated_errors
* @param array $data The array of data to test
* @param array $rules The array of rules to add
* @param array $translated_expected The array of expected errors when translated
* @param array $untranslated_expected The array of expected errors when not translated
*/
public function test_translated_errors($data, $rules, $translated_expected, $untranslated_expected)
{
$validation = Validation::factory($data);
$current = i18n::lang();
i18n::lang('es');
foreach($rules as $field => $field_rules)
{
$validation->rules($field, $field_rules);
}
$validation->check();
$result_1 = $validation->errors('Validation', TRUE);
$result_2 = $validation->errors('Validation', 'en');
$result_3 = $validation->errors('Validation', FALSE);
// Restore the current language
i18n::lang($current);
$this->assertSame($translated_expected, $result_1);
$this->assertSame($translated_expected, $result_2);
$this->assertSame($untranslated_expected, $result_3);
}
/**
* Tests Validation::errors()
*
* @test
* @covers Validation::errors
*/
public function test_parameter_labels()
{
$validation = Validation::factory(array('foo' => 'bar'))
->rule('foo', 'equals', array(':value', 'something'))
->label('something', 'Spanish');
$current = i18n::lang();
i18n::lang('es');
$validation->check();
$translated_expected = array('foo' => 'foo must equal Español');
$untranslated_expected = array('foo' => 'foo must equal Spanish');
$result_1 = $validation->errors('Validation', TRUE);
$result_2 = $validation->errors('Validation', 'en');
$result_3 = $validation->errors('Validation', FALSE);
// Restore the current language
i18n::lang($current);
$this->assertSame($translated_expected, $result_1);
$this->assertSame($translated_expected, $result_2);
$this->assertSame($untranslated_expected, $result_3);
}
/**
* Tests Validation::errors()
*
* @test
* @covers Validation::errors
*/
public function test_arrays_in_parameters()
{
$validation = Validation::factory(array('foo' => 'bar'))
->rule('foo', 'equals', array(':value', array('one', 'two')));
$validation->check();
$expected = array('foo' => 'foo must equal one, two');
$this->assertSame($expected, $validation->errors('Validation', FALSE));
}
/**
* Tests Validation::check()
*
* @test
* @covers Validation::check
*/
public function test_data_stays_unaltered()
{
$validation = Validation::factory(array('foo' => 'bar'))
->rule('something', 'not_empty');
$before = $validation->as_array();
$validation->check();
$after = $validation->as_array();
$expected = array('foo' => 'bar');
$this->assertSame($expected, $before);
$this->assertSame($expected, $after);
}
/**
* Tests Validation::errors()
*
* @test
* @covers Validation::errors
*/
public function test_object_parameters_not_in_messages()
{
$validation = Validation::factory(array('foo' => 'foo'))
->rule('bar', 'matches', array(':validation', 'foo', ':field'));
$validation->check();
$errors = $validation->errors('validation');
$expected = array('bar' => 'bar must be the same as foo');
$this->assertSame($expected, $errors);
}
}

View File

@@ -0,0 +1,78 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Tests the View class
*
* @group kohana
* @group kohana.view
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_ViewTest extends Unittest_TestCase
{
protected static $old_modules = array();
/**
* Setups the filesystem for test view files
*
* @return null
*/
public static function setupBeforeClass()
{
self::$old_modules = Kohana::modules();
$new_modules = self::$old_modules+array(
'test_views' => realpath(__DIR__.'/../test_data/')
);
Kohana::modules($new_modules);
}
/**
* Restores the module list
*
* @return null
*/
public static function teardownAfterClass()
{
Kohana::modules(self::$old_modules);
}
/**
* Provider for test_instaniate
*
* @return array
*/
public function provider_instantiate()
{
return array(
array('kohana/error', FALSE),
array('test.css', FALSE),
array('doesnt_exist', TRUE),
);
}
/**
* Tests that we can instantiate a view file
*
* @test
* @dataProvider provider_instantiate
*
* @return null
*/
public function test_instantiate($path, $expects_exception)
{
try
{
$view = new View($path);
$this->assertSame(FALSE, $expects_exception);
}
catch(Kohana_View_Exception $e)
{
$this->assertSame(TRUE, $expects_exception);
}
}
}

View File

@@ -0,0 +1,89 @@
<?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

@@ -0,0 +1,78 @@
<?php
/**
* A holding class for route callback tests
*
* @group kohana
*
* @package Unittest
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Route_Holder
{
/**
* Just an empty callback that doesn't match anything
*/
public static function default_callback($uri)
{
}
/**
* Just an empty callback that matches everything
*
* @return array
*/
public static function default_return_callback($uri)
{
return array(
);
}
/**
* Route callback for test_matches_returns_array_of_parameters_on_successful_match
*
* @return array
*/
public static function matches_returns_array_of_parameters_on_successful_match($uri)
{
return array(
'controller' => 'welcome',
'action' => 'index',
);
}
/**
* Route callback for test_required_parameters_are_needed
*
* @return array
*/
public static function required_parameters_are_needed($uri)
{
if (substr($uri, 0, 5) == 'admin')
{
return array(
'controller' => 'foo',
'action' => 'bar',
);
}
}
/**
* Route callback for test reverse_routing_returns_routes_uri_if_route_is_static
*
* @return array
*/
public static function reverse_routing_returns_routes_uri_if_route_is_static($uri)
{
if ($uri == 'info/about_us')
{
return array(
);
}
}
}

View File

@@ -0,0 +1 @@
This is a view with a dot in the filename.