Skip to content

Commit

Permalink
Rewrite Kohana::message tests for fragility and coverage
Browse files Browse the repository at this point in the history
These tests were vulnerable to breakage if modules provide
their own validation message files, so were being skipped.

Fixed to use (hopefully) collision-proof message files with
known values and to cover a broader range of cases
including missing file and missing key.

Would ideally be further refactored to mock/have complete
control of the module search path outside global state.
  • Loading branch information
acoulton committed Sep 23, 2014
1 parent 5a24c6e commit c7bc251
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
82 changes: 47 additions & 35 deletions tests/kohana/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@
*/
class Kohana_CoreTest extends Unittest_TestCase
{
protected $old_modules = array();

/**
* Captures the module list as it was before this test
*
* @return null
*/
// @codingStandardsIgnoreStart
public function setUp()
// @codingStandardsIgnoreEnd
{
parent::setUp();
$this->old_modules = Kohana::modules();
}

/**
* Restores the module list
*
* @return null
*/
// @codingStandardsIgnoreStart
public function tearDown()
// @codingStandardsIgnoreEnd
{
Kohana::modules($this->old_modules);
}

/**
* Provides test data for test_sanitize()
Expand Down Expand Up @@ -157,35 +183,18 @@ public function test_cache($key, $value, $lifetime)
public function provider_message()
{
return array(
// $value, $result
array(':field must not be empty', 'validation', 'not_empty'),
array(
array('no_message_file', 'anything', 'default', 'default'),
array('no_message_file', NULL, 'anything', array()),
array('kohana_core_message_tests', 'bottom_only', 'anything', 'inherited bottom message'),
array('kohana_core_message_tests', 'cfs_replaced', 'anything', 'overriding cfs_replaced message'),
array('kohana_core_message_tests', 'top_only', 'anything', 'top only message'),
array('kohana_core_message_tests', 'missing', 'default', 'default'),
array('kohana_core_message_tests', NULL, 'anything',
array(
'alpha' => ':field must contain only letters',
'alpha_dash' => ':field must contain only numbers, letters and dashes',
'alpha_numeric' => ':field must contain only letters and numbers',
'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 :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 :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 :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 :param2 to :param3',
'regex' => ':field does not match the required format',
'url' => ':field must be a url',
),
'validation', NULL,
'bottom_only' => 'inherited bottom message',
'cfs_replaced' => 'overriding cfs_replaced message',
'top_only' => 'top only message'
)
),
);
}
Expand All @@ -195,15 +204,18 @@ public function provider_message()
*
* @test
* @dataProvider provider_message
* @covers Kohana::message
* @param boolean $expected Output for Kohana::message
* @param boolean $file File to look in for Kohana::message
* @param boolean $key Key for Kohana::message
* @covers Kohana::message
* @param string $file to pass to Kohana::message
* @param string $key to pass to Kohana::message
* @param string $default to pass to Kohana::message
* @param string $expected Output for Kohana::message
*/
public function test_message($expected, $file, $key)
public function test_message($file, $key, $default, $expected)
{
$this->markTestSkipped('This test is incredibly fragile and needs to be re-done');
$this->assertEquals($expected, Kohana::message($file, $key));
$test_path = realpath(dirname(__FILE__).'/../test_data/message_tests');
Kohana::modules(array('top' => "$test_path/top_module", 'bottom' => "$test_path/bottom_module"));

$this->assertEquals($expected, Kohana::message($file, $key, $default, $expected));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// See the Kohana_CoreTest tests for Kohana::message
return array(
'bottom_only' => 'inherited bottom message',
'cfs_replaced' => 'inherited cfs_replaced message',
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// See the Kohana_CoreTest tests for Kohana::message
return array(
'top_only' => 'top only message',
'cfs_replaced' => 'overriding cfs_replaced message',
);

0 comments on commit c7bc251

Please sign in to comment.