Fix risky tests

- Fix CakeTestCase::run() to restore original output buffering level
- Fix an undefined variable warning in missing_controller.ctp
- Fix ViewTest (backport of 92bd86274b64c31ef6034caa74bb168be041b5e0)
- Fix ThemeViewTest
This commit is contained in:
chinpei215 2017-03-25 20:18:12 +09:00
parent 19e95e2fb2
commit 3b75bd2dea
4 changed files with 78 additions and 34 deletions

View file

@ -198,7 +198,6 @@ class ThemeViewTest extends CakeTestCase {
/**
* testMissingView method
*
* @expectedException MissingViewException
* @return void
*/
public function testMissingView() {
@ -211,17 +210,18 @@ class ThemeViewTest extends CakeTestCase {
$this->Controller->params['pass'] = array('home');
$View = new TestTheme2View($this->Controller);
ob_start();
$View->getViewFileName('does_not_exist');
$expected = ob_get_clean();
$this->assertRegExp("/PagesController::/", $expected);
$this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
try {
$View->getViewFileName('does_not_exist');
$this->fail('No exception');
} catch (MissingViewException $e) {
$this->assertContains('Pages' . DS . 'does_not_exist.ctp', $e->getMessage());
}
}
/**
* testMissingLayout method
*
* @expectedException MissingLayoutException
* @return void
*/
public function testMissingLayout() {
@ -232,11 +232,13 @@ class ThemeViewTest extends CakeTestCase {
$this->Controller->theme = 'my_theme';
$View = new TestTheme2View($this->Controller);
ob_start();
$View->getLayoutFileName();
$expected = ob_get_clean();
$this->assertRegExp("/Missing Layout/", $expected);
$this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
try {
$View->getLayoutFileName();
$this->fail('No exception');
} catch (MissingLayoutException $e) {
$this->assertContains('Layouts' . DS . 'whatever.ctp', $e->getMessage());
}
}
/**

View file

@ -625,9 +625,16 @@ class ViewTest extends CakeTestCase {
$this->Controller->params['pass'] = array('home');
$View = new TestView($this->Controller);
ob_start();
$View->getViewFileName('does_not_exist');
}
/**
* Test for missing theme views
*
* @expectedException MissingViewException
* @return void
*/
public function testMissingThemeView() {
$this->ThemeController->plugin = null;
$this->ThemeController->name = 'Pages';
$this->ThemeController->viewPath = 'Pages';
@ -653,10 +660,16 @@ class ViewTest extends CakeTestCase {
$this->Controller->layout = 'whatever';
$View = new TestView($this->Controller);
ob_start();
$View->getLayoutFileName();
ob_get_clean();
}
/**
* Test for missing theme layouts
*
* @expectedException MissingLayoutException
* @return void
*/
public function testMissingThemeLayout() {
$this->ThemeController->plugin = null;
$this->ThemeController->name = 'Posts';
$this->ThemeController->viewPath = 'posts';
@ -1512,7 +1525,6 @@ class ViewTest extends CakeTestCase {
public static function blockValueProvider() {
return array(
'string' => array('A string value'),
'null' => array(null),
'decimal' => array(1.23456),
'object with __toString' => array(new TestObjectWithToString()),
);
@ -1631,26 +1643,33 @@ class ViewTest extends CakeTestCase {
/**
* Test that starting the same block twice throws an exception
*
* @expectedException CakeException
* @return void
*/
public function testStartBlocksTwice() {
$this->View->start('first');
echo 'In first ';
$this->View->start('second');
echo 'In second';
$this->View->start('first');
try {
$this->View->start('first');
$this->View->start('first');
$this->fail('No exception');
} catch (CakeException $e) {
ob_end_clean();
$this->assertTrue(true);
}
}
/**
* Test that an exception gets thrown when you leave a block open at the end
* of a view.
*
* @expectedException CakeException
* @return void
*/
public function testExceptionOnOpenBlock() {
$this->View->render('open_block');
try {
$this->View->render('open_block');
$this->fail('No exception');
} catch (CakeException $e) {
ob_end_clean();
$this->assertContains('The "no_close" block was left open', $e->getMessage());
}
}
/**
@ -1673,23 +1692,33 @@ TEXT;
/**
* Make sure that extending the current view with itself causes an exception
*
* @expectedException LogicException
* @return void
*/
public function testExtendSelf() {
$this->View->layout = false;
$this->View->render('extend_self');
try {
$this->View->layout = false;
$this->View->render('extend_self');
$this->fail('No exception');
} catch (LogicException $e) {
ob_end_clean();
$this->assertContains('cannot have views extend themselves', $e->getMessage());
}
}
/**
* Make sure that extending in a loop causes an exception
*
* @expectedException LogicException
* @return void
*/
public function testExtendLoop() {
$this->View->layout = false;
$this->View->render('extend_loop');
try {
$this->View->layout = false;
$this->View->render('extend_loop');
$this->fail('No exception');
} catch (LogicException $e) {
ob_end_clean();
$this->assertContains('cannot have views extend in a loop', $e->getMessage());
}
}
/**
@ -1713,12 +1742,18 @@ TEXT;
/**
* Extending an element which doesn't exist should throw a missing view exception
*
* @expectedException LogicException
* @return void
*/
public function testExtendMissingElement() {
$this->View->layout = false;
$this->View->render('extend_missing_element');
try {
$this->View->layout = false;
$this->View->render('extend_missing_element');
$this->fail('No exception');
} catch (LogicException $e) {
ob_end_clean();
ob_end_clean();
$this->assertContains('element', $e->getMessage());
}
}
/**

View file

@ -76,6 +76,8 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
* @throws InvalidArgumentException
*/
public function run(PHPUnit_Framework_TestResult $result = null) {
$level = ob_get_level();
if (!empty($this->fixtureManager)) {
$this->fixtureManager->load($this);
}
@ -83,6 +85,11 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
if (!empty($this->fixtureManager)) {
$this->fixtureManager->unload($this);
}
for ($i = ob_get_level(); $i < $level; ++$i) {
ob_start();
}
return $result;
}

View file

@ -27,7 +27,7 @@ $pluginDot = empty($plugin) ? null : $plugin . '.';
</p>
<pre>
&lt;?php
class <?php echo h($class . ' extends ' . $plugin); ?>AppController {
class <?php echo h($class . ' extends ' . (empty($plugin) ? '' : $plugin)); ?>AppController {
}
</pre>