cakephp2-php8/lib/Cake/Test/Case/Console/Command/Task/TestTaskTest.php

665 lines
18 KiB
PHP
Raw Normal View History

<?php
/**
2009-03-19 21:10:13 +00:00
* TestTaskTest file
*
2009-03-19 21:10:13 +00:00
* Test Case for test generation shell task
*
* PHP 5
*
2010-01-26 22:15:15 +00:00
* CakePHP : Rapid Development Framework (http://cakephp.org)
2011-05-29 21:31:39 +00:00
* Copyright 2005-2011, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2011-05-29 21:31:39 +00:00
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP Project
* @package Cake.Test.Case.Console.Command.Task
* @since CakePHP v 1.2.0.7726
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('ShellDispatcher', 'Console');
2011-03-07 05:27:21 +00:00
App::uses('ConsoleOutput', 'Console');
App::uses('ConsoleInput', 'Console');
App::uses('Shell', 'Console');
App::uses('TestTask', 'Console/Command/Task');
App::uses('TemplateTask', 'Console/Command/Task');
App::uses('Controller', 'Controller');
App::uses('Model', 'Model');
/**
* Test Article model
*
* @package Cake.Test.Case.Console.Command.Task
* @package Cake.Test.Case.Console.Command.Task
*/
class TestTaskArticle extends Model {
/**
* Model name
*
* @var string
*/
public $name = 'TestTaskArticle';
/**
* Table name to use
*
* @var string
*/
public $useTable = 'articles';
/**
* HasMany Associations
*
* @var array
*/
public $hasMany = array(
'Comment' => array(
'className' => 'TestTask.TestTaskComment',
'foreignKey' => 'article_id',
)
);
/**
* Has and Belongs To Many Associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'TestTaskTag',
'joinTable' => 'articles_tags',
'foreignKey' => 'article_id',
'associationForeignKey' => 'tag_id'
)
);
/**
* Example public method
*
* @return void
*/
public function doSomething() {
}
/**
* Example Secondary public method
*
* @return void
*/
public function doSomethingElse() {
}
/**
* Example protected method
*
* @return void
*/
protected function _innerMethod() {
}
}
/**
* Tag Testing Model
*
* @package Cake.Test.Case.Console.Command.Task
* @package Cake.Test.Case.Console.Command.Task
*/
class TestTaskTag extends Model {
/**
* Model name
*
* @var string
*/
public $name = 'TestTaskTag';
/**
* Table name
*
* @var string
*/
public $useTable = 'tags';
/**
* Has and Belongs To Many Associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Article' => array(
'className' => 'TestTaskArticle',
'joinTable' => 'articles_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'article_id'
)
);
}
2009-07-26 09:59:51 +00:00
/**
* Simulated plugin
*
* @package Cake.Test.Case.Console.Command.Task
* @package Cake.Test.Case.Console.Command.Task
*/
class TestTaskAppModel extends Model {
}
/**
* Testing AppMode (TaskComment)
*
* @package Cake.Test.Case.Console.Command.Task
* @package Cake.Test.Case.Console.Command.Task
*/
class TestTaskComment extends TestTaskAppModel {
/**
* Model name
*
* @var string
*/
public $name = 'TestTaskComment';
/**
* Table name
*
* @var string
*/
public $useTable = 'comments';
/**
* Belongs To Associations
*
* @var array
*/
public $belongsTo = array(
'Article' => array(
'className' => 'TestTaskArticle',
'foreignKey' => 'article_id',
)
);
}
/**
* Test Task Comments Controller
*
* @package Cake.Test.Case.Console.Command.Task
* @package Cake.Test.Case.Console.Command.Task
*/
class TestTaskCommentsController extends Controller {
/**
* Controller Name
*
* @var string
*/
public $name = 'TestTaskComments';
/**
* Models to use
*
* @var array
*/
public $uses = array('TestTaskComment', 'TestTaskTag');
}
/**
* TestTaskTest class
*
* @package Cake.Test.Case.Console.Command.Task
*/
class TestTaskTest extends CakeTestCase {
/**
* Fixtures
*
* @var string
*/
public $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
2009-07-26 09:59:51 +00:00
/**
* setup method
*
* @return void
*/
public function setup() {
parent::setup();
$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('TestTask',
array('in', 'err', 'createFile', '_stop', 'isLoadableClass'),
array($out, $out, $in)
);
$this->Task->name = 'Test';
$this->Task->Template = new TemplateTask($out, $out, $in);
}
2009-07-26 09:59:51 +00:00
/**
* endTest method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Task);
2011-05-12 04:22:09 +00:00
CakePlugin::unload();
}
2009-07-26 09:59:51 +00:00
/**
* Test that file path generation doesn't continuously append paths.
*
* @return void
*/
public function testFilePathGenerationModelRepeated() {
$this->Task->expects($this->never())->method('err');
$this->Task->expects($this->never())->method('_stop');
$file = TESTS . 'Case' . DS . 'Model' . DS . 'MyClassTest.php';
$this->Task->expects($this->at(1))->method('createFile')
->with($file, $this->anything());
$this->Task->expects($this->at(3))->method('createFile')
->with($file, $this->anything());
2009-05-26 03:57:15 +00:00
$file = TESTS . 'Case' . DS . 'Controller' . DS . 'CommentsControllerTest.php';
$this->Task->expects($this->at(5))->method('createFile')
->with($file, $this->anything());
$this->Task->bake('Model', 'MyClass');
$this->Task->bake('Model', 'MyClass');
2009-05-26 03:57:15 +00:00
$this->Task->bake('Controller', 'Comments');
}
2009-07-26 09:59:51 +00:00
/**
2009-07-26 09:59:51 +00:00
* Test that method introspection pulls all relevant non parent class
* methods into the test case.
*
* @return void
*/
public function testMethodIntrospection() {
$result = $this->Task->getTestableMethods('TestTaskArticle');
$expected = array('dosomething', 'dosomethingelse');
$this->assertEqual(array_map('strtolower', $result), $expected);
}
2009-07-26 09:59:51 +00:00
/**
* test that the generation of fixtures works correctly.
*
* @return void
*/
public function testFixtureArrayGenerationFromModel() {
$subject = ClassRegistry::init('TestTaskArticle');
$result = $this->Task->generateFixtureList($subject);
2009-07-26 09:59:51 +00:00
$expected = array('plugin.test_task.test_task_comment', 'app.articles_tags',
'app.test_task_article', 'app.test_task_tag');
$this->assertEqual(sort($result), sort($expected));
}
2009-07-26 09:59:51 +00:00
/**
* test that the generation of fixtures works correctly.
*
* @return void
*/
public function testFixtureArrayGenerationFromController() {
$subject = new TestTaskCommentsController();
$result = $this->Task->generateFixtureList($subject);
2009-07-26 09:59:51 +00:00
$expected = array('plugin.test_task.test_task_comment', 'app.articles_tags',
'app.test_task_article', 'app.test_task_tag');
$this->assertEqual(sort($result), sort($expected));
}
2009-07-26 09:59:51 +00:00
/**
* test user interaction to get object type
*
* @return void
*/
public function testGetObjectType() {
$this->Task->expects($this->once())->method('_stop');
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('q'));
$this->Task->expects($this->at(2))->method('in')->will($this->returnValue(2));
$this->Task->getObjectType();
$result = $this->Task->getObjectType();
$this->assertEqual($result, $this->Task->classTypes['Controller']);
}
2009-07-26 09:59:51 +00:00
/**
* creating test subjects should clear the registry so the registry is always fresh
*
* @return void
*/
public function testRegistryClearWhenBuildingTestObjects() {
ClassRegistry::flush();
$model = ClassRegistry::init('TestTaskComment');
$model->bindModel(array(
'belongsTo' => array(
'Random' => array(
'className' => 'TestTaskArticle',
'foreignKey' => 'article_id',
)
)
));
$keys = ClassRegistry::keys();
$this->assertTrue(in_array('test_task_comment', $keys));
$object = $this->Task->buildTestSubject('Model', 'TestTaskComment');
$keys = ClassRegistry::keys();
$this->assertFalse(in_array('random', $keys));
}
2009-07-26 09:59:51 +00:00
/**
* test that getClassName returns the user choice as a classname.
*
* @return void
*/
public function testGetClassName() {
$objects = App::objects('model');
2011-05-31 00:49:46 +00:00
$this->skipIf(empty($objects), 'No models in app.');
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('MyCustomClass'));
$this->Task->expects($this->at(1))->method('in')->will($this->returnValue(1));
$result = $this->Task->getClassName('Model');
$this->assertEqual($result, 'MyCustomClass');
$result = $this->Task->getClassName('Model');
$options = App::objects('model');
$this->assertEqual($result, $options[0]);
}
2009-07-26 09:59:51 +00:00
/**
* Test the user interaction for defining additional fixtures.
*
* @return void
*/
public function testGetUserFixtures() {
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
$this->Task->expects($this->at(1))->method('in')
->will($this->returnValue('app.pizza, app.topping, app.side_dish'));
$result = $this->Task->getUserFixtures();
$expected = array('app.pizza', 'app.topping', 'app.side_dish');
$this->assertEqual($expected, $result);
}
2009-07-26 09:59:51 +00:00
/**
* test that resolving classnames works
*
* @return void
*/
public function testGetRealClassname() {
$result = $this->Task->getRealClassname('Model', 'Post');
$this->assertEqual($result, 'Post');
$result = $this->Task->getRealClassname('Controller', 'Posts');
$this->assertEqual($result, 'PostsController');
$result = $this->Task->getRealClassname('Controller', 'PostsController');
$this->assertEqual($result, 'PostsController');
$result = $this->Task->getRealClassname('Helper', 'Form');
$this->assertEqual($result, 'FormHelper');
$result = $this->Task->getRealClassname('Helper', 'FormHelper');
$this->assertEqual($result, 'FormHelper');
$result = $this->Task->getRealClassname('Behavior', 'Containable');
$this->assertEqual($result, 'ContainableBehavior');
$result = $this->Task->getRealClassname('Behavior', 'ContainableBehavior');
$this->assertEqual($result, 'ContainableBehavior');
$result = $this->Task->getRealClassname('Component', 'Auth');
$this->assertEqual($result, 'AuthComponent');
}
2009-07-26 09:59:51 +00:00
/**
* test baking files. The conditionally run tests are known to fail in PHP4
* as PHP4 classnames are all lower case, breaking the plugin path inflection.
*
* @return void
*/
public function testBakeModelTest() {
$this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
$this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
$result = $this->Task->bake('Model', 'TestTaskArticle');
2011-03-16 02:59:18 +00:00
$this->assertContains("App::uses('TestTaskArticle', 'Model')", $result);
$this->assertContains('class TestTaskArticleTestCase extends CakeTestCase', $result);
2011-02-05 06:40:14 +00:00
$this->assertContains('function setUp()', $result);
$this->assertContains("\$this->TestTaskArticle = ClassRegistry::init('TestTaskArticle')", $result);
2011-02-05 06:40:14 +00:00
$this->assertContains('function tearDown()', $result);
$this->assertContains('unset($this->TestTaskArticle)', $result);
$this->assertContains('function testDoSomething()', $result);
$this->assertContains('function testDoSomethingElse()', $result);
$this->assertContains("'app.test_task_article'", $result);
$this->assertContains("'plugin.test_task.test_task_comment'", $result);
$this->assertContains("'app.test_task_tag'", $result);
$this->assertContains("'app.articles_tag'", $result);
}
2009-07-26 09:59:51 +00:00
/**
* test baking controller test files, ensure that the stub class is generated.
* Conditional assertion is known to fail on PHP4 as classnames are all lower case
* causing issues with inflection of path name from classname.
*
* @return void
*/
public function testBakeControllerTest() {
$this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true));
$this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
$result = $this->Task->bake('Controller', 'TestTaskComments');
2011-03-16 02:59:18 +00:00
$this->assertContains("App::uses('TestTaskCommentsController', 'Controller')", $result);
$this->assertContains('class TestTaskCommentsControllerTestCase extends CakeTestCase', $result);
$this->assertContains('class TestTestTaskCommentsController extends TestTaskCommentsController', $result);
$this->assertContains('public $autoRender = false', $result);
$this->assertContains('function redirect($url, $status = null, $exit = true)', $result);
2011-02-05 06:40:14 +00:00
$this->assertContains('function setUp()', $result);
$this->assertContains("\$this->TestTaskComments = new TestTestTaskCommentsController()", $result);
$this->assertContains("\$this->TestTaskComments->constructClasses()", $result);
2011-02-05 06:40:14 +00:00
$this->assertContains('function tearDown()', $result);
$this->assertContains('unset($this->TestTaskComments)', $result);
$this->assertContains("'app.test_task_article'", $result);
$this->assertContains("'plugin.test_task.test_task_comment'", $result);
$this->assertContains("'app.test_task_tag'", $result);
$this->assertContains("'app.articles_tag'", $result);
}
2009-07-26 09:59:51 +00:00
/**
* test Constructor generation ensure that constructClasses is called for controllers
*
* @return void
*/
public function testGenerateConstructor() {
$result = $this->Task->generateConstructor('controller', 'PostsController');
$expected = "new TestPostsController();\n\t\t\$this->Posts->constructClasses();\n";
$this->assertEqual($expected, $result);
2009-05-25 05:02:59 +00:00
$result = $this->Task->generateConstructor('model', 'Post');
$expected = "ClassRegistry::init('Post');\n";
$this->assertEqual($expected, $result);
2009-05-25 05:02:59 +00:00
$result = $this->Task->generateConstructor('helper', 'FormHelper');
$expected = "new FormHelper();\n";
$this->assertEqual($expected, $result);
}
2009-07-26 09:59:51 +00:00
/**
* Test that mock class generation works for the appropriate classes
*
* @return void
*/
public function testMockClassGeneration() {
$result = $this->Task->hasMockClass('controller');
$this->assertTrue($result);
}
2009-07-26 09:59:51 +00:00
/**
* test bake() with a -plugin param
*
* @return void
*/
public function testBakeWithPlugin() {
$this->Task->plugin = 'TestTest';
2011-05-12 04:22:09 +00:00
//fake plugin path
2011-05-13 07:53:53 +00:00
CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
$path = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS .'FormHelperTest.php';
$this->Task->expects($this->once())->method('createFile')
->with($path, $this->anything());
$this->Task->bake('Helper', 'Form');
2011-05-12 04:22:09 +00:00
CakePlugin::unload();
}
2009-07-26 09:59:51 +00:00
/**
* test interactive with plugins lists from the plugin
*
* @return void
*/
public function testInteractiveWithPlugin() {
$testApp = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
App::build(array(
'plugins' => array($testApp)
), true);
2011-05-12 04:22:09 +00:00
CakePlugin::load('TestPlugin');
$this->Task->plugin = 'TestPlugin';
$path = $testApp . 'TestPlugin' . DS . 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'OtherHelperHelperTest.php';
$this->Task->expects($this->any())
->method('in')
->will($this->onConsecutiveCalls(
5, //helper
1 //OtherHelper
));
$this->Task->expects($this->once())
->method('createFile')
->with($path, $this->anything());
$this->Task->stdout->expects($this->at(21))
->method('write')
2011-03-07 05:27:21 +00:00
->with('1. OtherHelperHelper');
$this->Task->execute();
}
/**
* Test filename generation for each type + plugins
*
* @return void
*/
public function testTestCaseFileName() {
$this->Task->path = DS . 'my' . DS . 'path' . DS . 'tests' . DS;
$result = $this->Task->testCaseFileName('Model', 'Post');
$expected = $this->Task->path . 'Case' . DS . 'Model' . DS . 'PostTest.php';
$this->assertEqual($expected, $result);
$result = $this->Task->testCaseFileName('Helper', 'Form');
$expected = $this->Task->path . 'Case' . DS . 'View' . DS . 'Helper' . DS . 'FormHelperTest.php';
$this->assertEqual($expected, $result);
$result = $this->Task->testCaseFileName('Controller', 'Posts');
$expected = $this->Task->path . 'Case' . DS . 'Controller' . DS . 'PostsControllerTest.php';
$this->assertEqual($expected, $result);
$result = $this->Task->testCaseFileName('Behavior', 'Containable');
$expected = $this->Task->path . 'Case' . DS . 'Model' . DS . 'Behavior' . DS . 'ContainableBehaviorTest.php';
$this->assertEqual($expected, $result);
$result = $this->Task->testCaseFileName('Component', 'Auth');
$expected = $this->Task->path . 'Case' . DS . 'Controller' . DS . 'Component' . DS . 'AuthComponentTest.php';
$this->assertEqual($expected, $result);
2011-05-13 07:53:53 +00:00
CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS ));
$this->Task->plugin = 'TestTest';
$result = $this->Task->testCaseFileName('Model', 'Post');
$expected = APP . 'Plugin' . DS . 'TestTest' . DS . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'PostTest.php';
$this->assertEqual($expected, $result);
}
2009-07-26 09:59:51 +00:00
2009-05-30 04:14:57 +00:00
/**
* test execute with a type defined
*
* @return void
*/
public function testExecuteWithOneArg() {
2009-05-30 04:14:57 +00:00
$this->Task->args[0] = 'Model';
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
$this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true));
$this->Task->expects($this->once())->method('createFile')
->with(
$this->anything(),
$this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
);
2009-05-30 04:14:57 +00:00
$this->Task->execute();
}
2009-07-26 09:59:51 +00:00
2009-05-30 04:14:57 +00:00
/**
* test execute with type and class name defined
*
* @return void
*/
public function testExecuteWithTwoArgs() {
2009-05-30 04:14:57 +00:00
$this->Task->args = array('Model', 'TestTaskTag');
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag'));
$this->Task->expects($this->once())->method('createFile')
->with(
$this->anything(),
$this->stringContains('class TestTaskTagTestCase extends CakeTestCase')
);
$this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true));
2009-05-30 04:14:57 +00:00
$this->Task->execute();
}
/**
* Data provider for mapType() tests.
*
* @return array
*/
public static function mapTypeProvider() {
return array(
array('controller', null, 'Controller'),
array('Controller', null, 'Controller'),
array('component', null, 'Controller/Component'),
array('Component', null, 'Controller/Component'),
array('model', null, 'Model'),
array('Model', null, 'Model'),
array('behavior', null, 'Model/Behavior'),
array('Behavior', null, 'Model/Behavior'),
array('helper', null, 'View/Helper'),
array('Helper', null, 'View/Helper'),
array('Helper', 'DebugKit', 'DebugKit.View/Helper'),
);
}
/**
* Test that mapType returns the correct package names.
*
* @dataProvider mapTypeProvider
* @return void
*/
public function testMapType($original, $plugin, $expected) {
$this->assertEquals($expected, $this->Task->mapType($original, $plugin));
}
}