Skip to content

Commit

Permalink
Removing code from CakeTestCase around the method "testAction" which …
Browse files Browse the repository at this point in the history
…was a bad way of testing controllers, often making it more difficult.

This also removes class contamination in newly created test cases
  • Loading branch information
lorenzo committed May 6, 2010
1 parent b53d21f commit bed87b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 425 deletions.
194 changes: 12 additions & 182 deletions cake/tests/cases/libs/cake_test_case.test.php
Expand Up @@ -27,11 +27,11 @@
define('APP_CONTROLLER_EXISTS', true);
}

Mock::generate('CakeHtmlReporter');
Mock::generate('CakeTestCase', 'CakeDispatcherMockTestCase');
//Mock::generate('CakeHtmlReporter');
//Mock::generate('CakeTestCase', 'CakeDispatcherMockTestCase');

SimpleTest::ignore('SubjectCakeTestCase');
SimpleTest::ignore('CakeDispatcherMockTestCase');
//SimpleTest::ignore('SubjectCakeTestCase');
//SimpleTest::ignore('CakeDispatcherMockTestCase');

/**
* SubjectCakeTestCase
Expand All @@ -41,18 +41,6 @@
*/
class SubjectCakeTestCase extends CakeTestCase {

/**
* Feed a Mocked Reporter to the subject case
* prevents its pass/fails from affecting the real test
*
* @param string $reporter
* @access public
* @return void
*/
function setReporter(&$reporter) {
$this->_reporter = &$reporter;
}

/**
* testDummy method
*
Expand All @@ -78,10 +66,9 @@ class CakeTestCaseTest extends CakeTestCase {
*/
function setUp() {
$this->_debug = Configure::read('debug');
$this->Case =& new SubjectCakeTestCase();
$reporter =& new MockCakeHtmlReporter();
$this->Case->setReporter($reporter);
$this->Reporter = $reporter;
$this->Case = new SubjectCakeTestCase();
$this->Result = new PHPUnit_Framework_TestResult;
$this->Reporter = $this->getMock('CakeHtmlReporter');
}

/**
Expand All @@ -93,6 +80,7 @@ function setUp() {
function tearDown() {
Configure::write('debug', $this->_debug);
unset($this->Case);
unset($this->Result);
unset($this->Reporter);
}

Expand All @@ -113,16 +101,15 @@ function endTest() {
* @return void
*/
function testAssertGoodTags() {
$this->Reporter->expectAtLeastOnce('paintPass');
$this->Reporter->expectNever('paintFail');
$this->Reporter->expects($this->atLeastOnce())->method('paintPass');

$input = '<p>Text</p>';
$pattern = array(
'<p',
'Text',
'/p',
);
$this->assertTrue($this->Case->assertTags($input, $pattern));
$this->Case->assertTags($input, $pattern);

$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
Expand Down Expand Up @@ -250,8 +237,8 @@ function testNumericValuesInExpectationForAssertTags() {
* @return void
*/
function testBadAssertTags() {
$this->Reporter->expectAtLeastOnce('paintFail');
$this->Reporter->expectNever('paintPass');
// $this->Reporter->expectAtLeastOnce('paintFail');
// $this->Reporter->expectNever('paintPass');

$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
Expand Down Expand Up @@ -331,137 +318,6 @@ public function testGetTests() {
$this->assertEqual(array_slice($result, -2), array('endCase', 'end'));
}

/**
* TestTestAction
*
* @access public
* @return void
*/
function testTestAction() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS),
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
'controllers' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS)
), true);

$result = $this->Case->testAction('/tests_apps/index', array('return' => 'view'));
$this->assertPattern('/This is the TestsAppsController index view/', $result);

$result = $this->Case->testAction('/tests_apps/index', array('return' => 'contents'));
$this->assertPattern('/This is the TestsAppsController index view/', $result);
$this->assertPattern('/<html/', $result);
$this->assertPattern('/<\/html>/', $result);

$result = $this->Case->testAction('/tests_apps/some_method', array('return' => 'result'));
$this->assertEqual($result, 5);

$result = $this->Case->testAction('/tests_apps/set_action', array('return' => 'vars'));
$this->assertEqual($result, array('var' => 'string'));

$db =& ConnectionManager::getDataSource('test_suite');
$fixture =& new PostFixture();
$fixture->create($db);

$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
$this->assertTrue(array_key_exists('posts', $result));
$this->assertEqual(count($result['posts']), 1);

$result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
'return' => 'vars',
'method' => 'get',
));
$this->assertTrue(isset($result['params']['url']['url']));
$this->assertEqual(array_keys($result['params']['named']), array('var1', 'var2'));

$result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
'return' => 'vars',
'method' => 'get',
));
$this->assertEqual($result['params']['pass'], array('gogo', 'val2'));


$result = $this->Case->testAction('/tests_apps_posts/url_var', array(
'return' => 'vars',
'method' => 'get',
'data' => array(
'red' => 'health',
'blue' => 'mana'
)
));
$this->assertTrue(isset($result['params']['url']['red']));
$this->assertTrue(isset($result['params']['url']['blue']));
$this->assertTrue(isset($result['params']['url']['url']));

$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
'return' => 'vars',
'method' => 'post',
'data' => array(
'name' => 'is jonas',
'pork' => 'and beans',
)
));
$this->assertEqual(array_keys($result['data']), array('name', 'pork'));
$fixture->drop($db);

$db =& ConnectionManager::getDataSource('test_suite');
$_backPrefix = $db->config['prefix'];
$db->config['prefix'] = 'cake_testaction_test_suite_';

$config = $db->config;
$config['prefix'] = 'cake_testcase_test_';

ConnectionManager::create('cake_test_case', $config);
$db2 =& ConnectionManager::getDataSource('cake_test_case');

$fixture =& new PostFixture($db2);
$fixture->create($db2);
$fixture->insert($db2);

$result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
'return' => 'vars',
'fixturize' => true,
'connection' => 'cake_test_case',
));
$this->assertTrue(isset($result['posts']));
$this->assertEqual(count($result['posts']), 3);
$tables = $db2->listSources();
$this->assertFalse(in_array('cake_testaction_test_suite_posts', $tables));

$fixture->drop($db2);

$db =& ConnectionManager::getDataSource('test_suite');

//test that drop tables behaves as exepected with testAction
$db =& ConnectionManager::getDataSource('test_suite');
$_backPrefix = $db->config['prefix'];
$db->config['prefix'] = 'cake_testaction_test_suite_';

$config = $db->config;
$config['prefix'] = 'cake_testcase_test_';

ConnectionManager::create('cake_test_case', $config);
$db =& ConnectionManager::getDataSource('cake_test_case');
$fixture =& new PostFixture($db);
$fixture->create($db);
$fixture->insert($db);

$this->Case->dropTables = false;
$result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
'return' => 'vars',
'fixturize' => true,
'connection' => 'cake_test_case',
));

$tables = $db->listSources();
$this->assertTrue(in_array('cake_testaction_test_suite_posts', $tables));

$fixture->drop($db);
$db =& ConnectionManager::getDataSource('test_suite');
$db->config['prefix'] = $_backPrefix;
$fixture->drop($db);
}

/**
* testSkipIf
*
Expand All @@ -471,31 +327,5 @@ function testSkipIf() {
$this->assertTrue($this->Case->skipIf(true));
$this->assertFalse($this->Case->skipIf(false));
}

/**
* testTestDispatcher
*
* @access public
* @return void
*/
function testTestDispatcher() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS),
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
'controllers' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS)
), true);

$Dispatcher =& new CakeTestDispatcher();
$Case =& new CakeDispatcherMockTestCase();

$Case->expectOnce('startController');
$Case->expectOnce('endController');

$Dispatcher->testCase($Case);
$this->assertTrue(isset($Dispatcher->testCase));

$return = $Dispatcher->dispatch('/tests_apps/index', array('autoRender' => 0, 'return' => 1, 'requested' => 1));
}
}
?>

0 comments on commit bed87b8

Please sign in to comment.