Skip to content

Commit

Permalink
make it possible to test for files that don't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Oct 18, 2011
1 parent d492c86 commit 6ab5f1f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/Cake/Console/Command/TestShell.php
Expand Up @@ -337,10 +337,13 @@ public function available() {
* Find the test case for the passed file. The file could itself be a test.
*
* @param mixed $file
* @param mixed $category
* @param mixed $throwOnMissingFile
* @access protected
* @return array(type, case)
* @throws Exception
*/
protected function _mapFileToCase($file, $category) {
protected function _mapFileToCase($file, $category, $throwOnMissingFile = true) {
if (!$category || (substr($file, -4) !== '.php')) {
return false;
}
Expand Down Expand Up @@ -381,12 +384,11 @@ protected function _mapFileToCase($file, $category) {
$testCase[0] = strtoupper($testCase[0]);
$testFile = CAKE . 'Test/Case/' . $testCase . 'Test.php';

if (file_exists($testFile)) {
return $testCase;
if (!file_exists($testFile) && $throwOnMissingFile) {
throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
}

throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
return false;

return $testCase;
}

if ($category === 'app') {
Expand All @@ -399,9 +401,8 @@ protected function _mapFileToCase($file, $category) {
);
}

if (!file_exists($testFile)) {
if (!file_exists($testFile) && $throwOnMissingFile) {
throw new Exception(__d('cake_dev', 'Test case %s not found', $testFile));
return false;
}

$testCase = substr($file, 0, -8);
Expand Down
52 changes: 51 additions & 1 deletion lib/Cake/Test/Case/Console/Command/TestShellTest.php
Expand Up @@ -20,6 +20,17 @@
App::uses('ShellDispatcher', 'Console');
App::uses('TestShell', 'Console/Command');

class TestTestShell extends TestShell {

public function mapFileToCase($file, $category, $throwOnMissingFile = true) {
return $this->_mapFileToCase($file, $category, $throwOnMissingFile);
}

public function mapFileToCategory($file) {
return $this->_mapFileToCategory($file);
}
}

class TestShellTest extends CakeTestCase {


Expand All @@ -33,7 +44,7 @@ public function setUp() {
$in = $this->getMock('ConsoleInput', array(), array(), '', false);

$this->Shell = $this->getMock(
'TestShell',
'TestTestShell',
array('in', 'out', 'hr', 'help', 'error', 'err', '_stop', 'initialize', '_run', 'clear'),
array($out, $out, $in)
);
Expand All @@ -49,6 +60,45 @@ public function tearDown() {
unset($this->Dispatch, $this->Shell);
}

/**
* testMapCoreFileToCategory
*
*
* @return void
*/
public function testMapCoreFileToCategory() {
$this->Shell->startup();

$return = $this->Shell->mapFileToCategory('lib/Cake/basics.php');
$this->assertSame('core', $return);

$return = $this->Shell->mapFileToCategory('lib/Cake/Core/App.php');
$this->assertSame('core', $return);

$return = $this->Shell->mapFileToCategory('lib/Cake/Some/Deeply/Nested/Structure.php');
$this->assertSame('core', $return);
}

/**
* testMapCoreFileToCase
*
* basics.php is a slightly special case - it's the only file in the core with a test that isn't Capitalized
*
* @return void
*/
public function testMapCoreFileToCase() {
$this->Shell->startup();

$return = $this->Shell->mapFileToCase('lib/Cake/basics.php', 'core');
$this->assertSame('Basics', $return);

$return = $this->Shell->mapFileToCase('lib/Cake/Core/App.php', 'core');
$this->assertSame('Core/App', $return);

$return = $this->Shell->mapFileToCase('lib/Cake/Some/Deeply/Nested/Structure.php', 'core', false);
$this->assertSame('Some/Deeply/Nested/Structure', $return);
}

/**
* test available list of test cases for an empty category
*
Expand Down

0 comments on commit 6ab5f1f

Please sign in to comment.