| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- <?php
- /**
- * CakePHP : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP Project
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Console\Command\Task;
- use Cake\Console\Command\Task\TemplateTask;
- use Cake\Console\Command\Task\TestTask;
- use Cake\Controller\Controller;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Network\Request;
- use Cake\Network\Response;
- use Cake\ORM\Table;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- use TestApp\Controller\PostsController;
- use TestApp\Model\Table\ArticlesTable;
- /**
- * TestTaskTest class
- *
- */
- class TestTaskTest extends TestCase {
- /**
- * Fixtures
- *
- * @var string
- */
- public $fixtures = ['core.article', 'core.author',
- 'core.comment', 'core.articles_tag', 'core.tag'];
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
- $this->Task = $this->getMock('Cake\Console\Command\Task\TestTask',
- array('in', 'err', 'createFile', '_stop', 'isLoadableClass'),
- array($this->io)
- );
- $this->Task->name = 'Test';
- $this->Task->Template = new TemplateTask($this->io);
- $this->Task->Template->interactive = false;
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- unset($this->Task);
- Plugin::unload();
- }
- /**
- * Test that with no args execute() outputs the types you can generate
- * tests for.
- *
- * @return void
- */
- public function testExecuteNoArgsPrintsTypeOptions() {
- $this->Task = $this->getMockBuilder('Cake\Console\Command\Task\TestTask')
- ->disableOriginalConstructor()
- ->setMethods(['outputTypeChoices'])
- ->getMock();
- $this->Task->expects($this->once())
- ->method('outputTypeChoices');
- $this->Task->main();
- }
- /**
- * Test outputTypeChoices method
- *
- * @return void
- */
- public function testOutputTypeChoices() {
- $this->io->expects($this->at(0))
- ->method('out')
- ->with($this->stringContains('You must provide'));
- $this->io->expects($this->at(1))
- ->method('out')
- ->with($this->stringContains('1. Entity'));
- $this->io->expects($this->at(2))
- ->method('out')
- ->with($this->stringContains('2. Table'));
- $this->io->expects($this->at(3))
- ->method('out')
- ->with($this->stringContains('3. Controller'));
- $this->Task->outputTypeChoices();
- }
- /**
- * Test that with no args execute() outputs the types you can generate
- * tests for.
- *
- * @return void
- */
- public function testExecuteOneArgPrintsClassOptions() {
- $this->Task = $this->getMockBuilder('Cake\Console\Command\Task\TestTask')
- ->disableOriginalConstructor()
- ->setMethods(['outputClassChoices'])
- ->getMock();
- $this->Task->expects($this->once())
- ->method('outputClassChoices');
- $this->Task->main('Entity');
- }
- /**
- * test execute with type and class name defined
- *
- * @return void
- */
- public function testExecuteWithTwoArgs() {
- $this->Task->expects($this->once())->method('createFile')
- ->with(
- $this->stringContains('TestCase' . DS . 'Model' . DS . 'Table' . DS . 'TestTaskTagTableTest.php'),
- $this->stringContains('class TestTaskTagTableTest extends TestCase')
- );
- $this->Task->main('Table', 'TestTaskTag');
- }
- /**
- * Test generating class options for table.
- *
- * @return void
- */
- public function testOutputClassOptionsForTable() {
- $this->io->expects($this->at(0))
- ->method('out')
- ->with($this->stringContains('You must provide'));
- $this->io->expects($this->at(1))
- ->method('out')
- ->with($this->stringContains('1. ArticlesTable'));
- $this->io->expects($this->at(2))
- ->method('out')
- ->with($this->stringContains('2. ArticlesTagsTable'));
- $this->io->expects($this->at(3))
- ->method('out')
- ->with($this->stringContains('3. AuthUsersTable'));
- $this->io->expects($this->at(4))
- ->method('out')
- ->with($this->stringContains('4. AuthorsTable'));
- $this->Task->outputClassChoices('Table');
- }
- /**
- * Test generating class options for table.
- *
- * @return void
- */
- public function testOutputClassOptionsForTablePlugin() {
- Plugin::load('TestPlugin');
- $this->Task->plugin = 'TestPlugin';
- $this->io->expects($this->at(0))
- ->method('out')
- ->with($this->stringContains('You must provide'));
- $this->io->expects($this->at(1))
- ->method('out')
- ->with($this->stringContains('1. AuthorsTable'));
- $this->io->expects($this->at(2))
- ->method('out')
- ->with($this->stringContains('2. CommentsTable'));
- $this->io->expects($this->at(3))
- ->method('out')
- ->with($this->stringContains('3. TestPluginCommentsTable'));
- $this->Task->outputClassChoices('Table');
- }
- /**
- * Test that method introspection pulls all relevant non parent class
- * methods into the test case.
- *
- * @return void
- */
- public function testMethodIntrospection() {
- $result = $this->Task->getTestableMethods('TestApp\Model\Table\ArticlesTable');
- $expected = array('dosomething', 'dosomethingelse');
- $this->assertEquals($expected, array_map('strtolower', $result));
- }
- /**
- * test that the generation of fixtures works correctly.
- *
- * @return void
- */
- public function testFixtureArrayGenerationFromModel() {
- $subject = new ArticlesTable();
- $result = $this->Task->generateFixtureList($subject);
- $expected = [
- 'app.article',
- 'app.author',
- 'app.tag',
- 'app.articles_tag'
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test that the generation of fixtures works correctly.
- *
- * @return void
- */
- public function testFixtureArrayGenerationFromController() {
- $subject = new PostsController(new Request(), new Response());
- $result = $this->Task->generateFixtureList($subject);
- $expected = [
- 'app.post',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Dataprovider for class name generation.
- *
- * @return array
- */
- public static function realClassProvider() {
- return [
- ['Entity', 'Article', 'App\Model\Entity\Article'],
- ['entity', 'ArticleEntity', 'App\Model\Entity\ArticleEntity'],
- ['Table', 'Posts', 'App\Model\Table\PostsTable'],
- ['table', 'PostsTable', 'App\Model\Table\PostsTable'],
- ['Controller', 'Posts', 'App\Controller\PostsController'],
- ['controller', 'PostsController', 'App\Controller\PostsController'],
- ['Behavior', 'Timestamp', 'App\Model\Behavior\TimestampBehavior'],
- ['behavior', 'TimestampBehavior', 'App\Model\Behavior\TimestampBehavior'],
- ['Helper', 'Form', 'App\View\Helper\FormHelper'],
- ['helper', 'FormHelper', 'App\View\Helper\FormHelper'],
- ['Component', 'Auth', 'App\Controller\Component\AuthComponent'],
- ['component', 'AuthComponent', 'App\Controller\Component\AuthComponent'],
- ['Shell', 'Example', 'App\Console\Command\ExampleShell'],
- ['shell', 'Example', 'App\Console\Command\ExampleShell'],
- ['Cell', 'Example', 'App\View\Cell\ExampleCell'],
- ['cell', 'Example', 'App\View\Cell\ExampleCell'],
- ];
- }
- /**
- * test that resolving class names works
- *
- * @dataProvider realClassProvider
- * @return void
- */
- public function testGetRealClassname($type, $name, $expected) {
- $result = $this->Task->getRealClassname($type, $name);
- $this->assertEquals($expected, $result);
- }
- /**
- * test resolving class names with plugins
- *
- * @return void
- */
- public function testGetRealClassnamePlugin() {
- Plugin::load('TestPlugin');
- $this->Task->plugin = 'TestPlugin';
- $result = $this->Task->getRealClassname('Helper', 'Asset');
- $expected = 'TestPlugin\View\Helper\AssetHelper';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test baking a test for a concrete model with fixtures arg
- *
- * @return void
- */
- public function testBakeFixturesParam() {
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $this->Task->params['fixtures'] = 'app.post, app.comments , app.user ,';
- $result = $this->Task->bake('Table', 'Articles');
- $this->assertContains('public $fixtures = [', $result);
- $this->assertContains('app.post', $result);
- $this->assertContains('app.comments', $result);
- $this->assertContains('app.user', $result);
- $this->assertNotContains("''", $result);
- }
- /**
- * Test baking a test for a cell.
- *
- * @return void
- */
- public function testBakeCellTest() {
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Cell', 'Articles');
- $this->assertContains("use App\View\Cell\ArticlesCell", $result);
- $this->assertContains('class ArticlesCellTest extends TestCase', $result);
- $this->assertContains('function setUp()', $result);
- $this->assertContains("\$this->request = \$this->getMock('Cake\Network\Request')", $result);
- $this->assertContains("\$this->response = \$this->getMock('Cake\Network\Response')", $result);
- $this->assertContains("\$this->Articles = new ArticlesCell(\$this->request, \$this->response", $result);
- }
- /**
- * Test baking a test for a concrete model.
- *
- * @return void
- */
- public function testBakeModelTest() {
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Table', 'Articles');
- $this->assertContains("use App\Model\Table\ArticlesTable", $result);
- $this->assertContains('class ArticlesTableTest extends TestCase', $result);
- $this->assertContains('function setUp()', $result);
- $this->assertContains("\$config = TableRegistry::exists('Articles') ?", $result);
- $this->assertContains("\$this->Articles = TableRegistry::get('Articles', \$config", $result);
- $this->assertContains('function tearDown()', $result);
- $this->assertContains('unset($this->Articles)', $result);
- }
- /**
- * test baking controller test files
- *
- * @return void
- */
- public function testBakeControllerTest() {
- Configure::write('App.namespace', 'TestApp');
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Controller', 'PostsController');
- $this->assertContains("use TestApp\Controller\PostsController", $result);
- $this->assertContains('class PostsControllerTest extends ControllerTestCase', $result);
- $this->assertNotContains('function setUp()', $result);
- $this->assertNotContains("\$this->Posts = new PostsController()", $result);
- $this->assertNotContains("\$this->Posts->constructClasses()", $result);
- $this->assertNotContains('function tearDown()', $result);
- $this->assertNotContains('unset($this->Posts)', $result);
- $this->assertContains("'app.post'", $result);
- }
- /**
- * test baking controller test files
- *
- * @return void
- */
- public function testBakePrefixControllerTest() {
- Configure::write('App.namespace', 'TestApp');
- $this->Task->expects($this->once())
- ->method('createFile')
- ->with($this->stringContains('Controller' . DS . 'Admin' . DS . 'PostsControllerTest.php'))
- ->will($this->returnValue(true));
- $result = $this->Task->bake('controller', 'Admin\Posts');
- $this->assertContains("use TestApp\Controller\Admin\PostsController", $result);
- $this->assertContains('class PostsControllerTest extends ControllerTestCase', $result);
- $this->assertNotContains('function setUp()', $result);
- $this->assertNotContains("\$this->Posts = new PostsController()", $result);
- $this->assertNotContains("\$this->Posts->constructClasses()", $result);
- $this->assertNotContains('function tearDown()', $result);
- $this->assertNotContains('unset($this->Posts)', $result);
- }
- /**
- * test baking component test files,
- *
- * @return void
- */
- public function testBakeComponentTest() {
- Configure::write('App.namespace', 'TestApp');
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Component', 'Apple');
- $this->assertContains('use Cake\Controller\ComponentRegistry', $result);
- $this->assertContains('use TestApp\Controller\Component\AppleComponent', $result);
- $this->assertContains('class AppleComponentTest extends TestCase', $result);
- $this->assertContains('function setUp()', $result);
- $this->assertContains("\$registry = new ComponentRegistry()", $result);
- $this->assertContains("\$this->Apple = new AppleComponent(\$registry)", $result);
- $this->assertContains('function testStartup()', $result);
- $this->assertContains('$this->markTestIncomplete(\'testStartup not implemented.\')', $result);
- $this->assertContains('function tearDown()', $result);
- $this->assertContains('unset($this->Apple)', $result);
- }
- /**
- * test baking behavior test files,
- *
- * @return void
- */
- public function testBakeBehaviorTest() {
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Behavior', 'Example');
- $this->assertContains("use App\Model\Behavior\ExampleBehavior;", $result);
- $this->assertContains('class ExampleBehaviorTest extends TestCase', $result);
- $this->assertContains('function setUp()', $result);
- $this->assertContains("\$this->Example = new ExampleBehavior()", $result);
- $this->assertContains('function tearDown()', $result);
- $this->assertContains('unset($this->Example)', $result);
- }
- /**
- * test baking helper test files,
- *
- * @return void
- */
- public function testBakeHelperTest() {
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Helper', 'Example');
- $this->assertContains("use Cake\View\View;", $result);
- $this->assertContains("use App\View\Helper\ExampleHelper;", $result);
- $this->assertContains('class ExampleHelperTest extends TestCase', $result);
- $this->assertContains('function setUp()', $result);
- $this->assertContains("\$view = new View()", $result);
- $this->assertContains("\$this->Example = new ExampleHelper(\$view)", $result);
- $this->assertContains('function tearDown()', $result);
- $this->assertContains('unset($this->Example)', $result);
- }
- /**
- * Test baking a test for a concrete model.
- *
- * @return void
- */
- public function testBakeShellTest() {
- $this->Task->expects($this->once())
- ->method('createFile')
- ->will($this->returnValue(true));
- $result = $this->Task->bake('Shell', 'Articles');
- $this->assertContains("use App\Console\Command\ArticlesShell", $result);
- $this->assertContains('class ArticlesShellTest extends TestCase', $result);
- $this->assertContains('function setUp()', $result);
- $this->assertContains("\$this->io = \$this->getMock('Cake\Console\ConsoleIo');", $result);
- $this->assertContains("\$this->Articles = new ArticlesShell(\$this->io);", $result);
- $this->assertContains('function tearDown()', $result);
- $this->assertContains('unset($this->Articles)', $result);
- }
- /**
- * test Constructor generation ensure that constructClasses is called for controllers
- *
- * @return void
- */
- public function testGenerateConstructor() {
- $result = $this->Task->generateConstructor('controller', 'PostsController');
- $expected = ['', '', ''];
- $this->assertEquals($expected, $result);
- $result = $this->Task->generateConstructor('table', 'App\Model\\Table\PostsTable');
- $expected = [
- "\$config = TableRegistry::exists('Posts') ? [] : ['className' => 'App\Model\\Table\PostsTable'];\n",
- "TableRegistry::get('Posts', \$config);\n",
- ''
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Task->generateConstructor('helper', 'FormHelper');
- $expected = ["\$view = new View();\n", "new FormHelper(\$view);\n", ''];
- $this->assertEquals($expected, $result);
- $result = $this->Task->generateConstructor('entity', 'TestPlugin\Model\Entity\Article');
- $expected = ["", "new Article();\n", ''];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test generateUses()
- *
- * @return void
- */
- public function testGenerateUses() {
- $result = $this->Task->generateUses('table', 'App\Model\Table\PostsTable');
- $expected = array(
- 'Cake\ORM\TableRegistry',
- 'App\Model\Table\PostsTable',
- );
- $this->assertEquals($expected, $result);
- $result = $this->Task->generateUses('controller', 'App\Controller\PostsController');
- $expected = array(
- 'App\Controller\PostsController',
- );
- $this->assertEquals($expected, $result);
- $result = $this->Task->generateUses('helper', 'App\View\Helper\FormHelper');
- $expected = array(
- 'Cake\View\View',
- 'App\View\Helper\FormHelper',
- );
- $this->assertEquals($expected, $result);
- $result = $this->Task->generateUses('component', 'App\Controller\Component\AuthComponent');
- $expected = array(
- 'Cake\Controller\ComponentRegistry',
- 'App\Controller\Component\AuthComponent',
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that mock class generation works for the appropriate classes
- *
- * @return void
- */
- public function testMockClassGeneration() {
- $result = $this->Task->hasMockClass('controller');
- $this->assertTrue($result);
- }
- /**
- * test bake() with a -plugin param
- *
- * @return void
- */
- public function testBakeWithPlugin() {
- $this->Task->plugin = 'TestPlugin';
- Plugin::load('TestPlugin');
- $path = TEST_APP . 'Plugin/TestPlugin/tests/TestCase/View/Helper/FormHelperTest.php';
- $path = str_replace('/', DS, $path);
- $this->Task->expects($this->once())->method('createFile')
- ->with($path, $this->anything());
- $this->Task->bake('Helper', 'Form');
- }
- /**
- * Provider for test case file names.
- *
- * @return array
- */
- public static function caseFileNameProvider() {
- return array(
- array('Table', 'App\Model\Table\PostsTable', 'TestCase/Model/Table/PostsTableTest.php'),
- array('Entity', 'App\Model\Entity\Article', 'TestCase/Model/Entity/ArticleTest.php'),
- array('Helper', 'App\View\Helper\FormHelper', 'TestCase/View/Helper/FormHelperTest.php'),
- array('Controller', 'App\Controller\PostsController', 'TestCase/Controller/PostsControllerTest.php'),
- array('Controller', 'App\Controller\Admin\PostsController', 'TestCase/Controller/Admin/PostsControllerTest.php'),
- array('Behavior', 'App\Model\Behavior\TreeBehavior', 'TestCase/Model/Behavior/TreeBehaviorTest.php'),
- array('Component', 'App\Controller\Component\AuthComponent', 'TestCase/Controller/Component/AuthComponentTest.php'),
- array('entity', 'App\Model\Entity\Article', 'TestCase/Model/Entity/ArticleTest.php'),
- array('table', 'App\Model\Table\PostsTable', 'TestCase/Model/Table/PostsTableTest.php'),
- array('helper', 'App\View\Helper\FormHelper', 'TestCase/View/Helper/FormHelperTest.php'),
- array('controller', 'App\Controller\PostsController', 'TestCase/Controller/PostsControllerTest.php'),
- array('behavior', 'App\Model\Behavior\TreeBehavior', 'TestCase/Model/Behavior/TreeBehaviorTest.php'),
- array('component', 'App\Controller\Component\AuthComponent', 'TestCase/Controller/Component/AuthComponentTest.php'),
- ['Shell', 'App\Console\Command\ExampleShell', 'TestCase/Console/Command/ExampleShellTest.php'],
- ['shell', 'App\Console\Command\ExampleShell', 'TestCase/Console/Command/ExampleShellTest.php'],
- );
- }
- /**
- * Test filename generation for each type + plugins
- *
- * @dataProvider caseFileNameProvider
- * @return void
- */
- public function testTestCaseFileName($type, $class, $expected) {
- $result = $this->Task->testCaseFileName($type, $class);
- $expected = ROOT . DS . 'tests/' . $expected;
- $this->assertPathEquals($expected, $result);
- }
- /**
- * Test filename generation for plugins.
- *
- * @return void
- */
- public function testTestCaseFileNamePlugin() {
- $this->Task->path = DS . 'my/path/tests/';
- Plugin::load('TestPlugin');
- $this->Task->plugin = 'TestPlugin';
- $class = 'TestPlugin\Model\Entity\Post';
- $result = $this->Task->testCaseFileName('entity', $class);
- $expected = TEST_APP . 'Plugin/TestPlugin/tests/TestCase/Model/Entity/PostTest.php';
- $this->assertPathEquals($expected, $result);
- }
- /**
- * Data provider for mapType() tests.
- *
- * @return array
- */
- public static function mapTypeProvider() {
- return array(
- array('controller', 'Controller'),
- array('Controller', 'Controller'),
- array('component', 'Controller\Component'),
- array('Component', 'Controller\Component'),
- array('table', 'Model\Table'),
- array('Table', 'Model\Table'),
- array('entity', 'Model\Entity'),
- array('Entity', 'Model\Entity'),
- array('behavior', 'Model\Behavior'),
- array('Behavior', 'Model\Behavior'),
- array('helper', 'View\Helper'),
- array('Helper', 'View\Helper'),
- array('Helper', 'View\Helper'),
- );
- }
- /**
- * Test that mapType returns the correct package names.
- *
- * @dataProvider mapTypeProvider
- * @return void
- */
- public function testMapType($original, $expected) {
- $this->assertEquals($expected, $this->Task->mapType($original));
- }
- }
|