| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115 |
- <?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.6
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Console\Command\Task;
- use Cake\Console\Command\Task\ModelTask;
- use Cake\Console\Command\Task\TemplateTask;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Model\Model;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\ClassRegistry;
- use Cake\Utility\Inflector;
- /**
- * ModelTaskTest class
- */
- class ModelTaskTest extends TestCase {
- /**
- * fixtures
- *
- * @var array
- */
- public $fixtures = array(
- 'core.bake_article', 'core.bake_comment', 'core.bake_articles_bake_tag',
- 'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree',
- 'core.counter_cache_user', 'core.counter_cache_post'
- );
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
- $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
- array('in', 'err', 'createFile', '_stop', '_checkUnitTest'),
- array($io)
- );
- $this->Task->connection = 'test';
- $this->_setupOtherMocks();
- TableRegistry::clear();
- }
- /**
- * Setup a mock that has out mocked. Normally this is not used as it makes $this->at() really tricky.
- *
- * @return void
- */
- protected function _useMockedOut() {
- $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
- $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
- array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
- array($io)
- );
- $this->_setupOtherMocks();
- }
- /**
- * sets up the rest of the dependencies for Model Task
- *
- * @return void
- */
- protected function _setupOtherMocks() {
- $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
- $this->Task->Fixture = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
- $this->Task->Test = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
- $this->Task->Template = new TemplateTask($io);
- $this->Task->Template->interactive = false;
- $this->Task->name = 'Model';
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- unset($this->Task);
- $this->fixtureManager->shutDown();
- }
- /**
- * Test that listAll uses the connection property
- *
- * @return void
- */
- public function testListAllConnection() {
- $this->_useMockedOut();
- $this->Task->connection = 'test';
- $result = $this->Task->listAll();
- $this->assertContains('bake_articles', $result);
- $this->assertContains('bake_articles_bake_tags', $result);
- $this->assertContains('bake_tags', $result);
- $this->assertContains('bake_comments', $result);
- $this->assertContains('category_threads', $result);
- }
- /**
- * Test getName() method.
- *
- * @return void
- */
- public function testGetTable() {
- $result = $this->Task->getTable('BakeArticle');
- $this->assertEquals('bake_articles', $result);
- $result = $this->Task->getTable('BakeArticles');
- $this->assertEquals('bake_articles', $result);
- $this->Task->params['table'] = 'bake_articles';
- $result = $this->Task->getTable('Article');
- $this->assertEquals('bake_articles', $result);
- }
- /**
- * Test getting the a table class.
- *
- * @return void
- */
- public function testGetTableObject() {
- $result = $this->Task->getTableObject('Article', 'bake_articles');
- $this->assertInstanceOf('Cake\ORM\Table', $result);
- $this->assertEquals('bake_articles', $result->table());
- $this->assertEquals('Article', $result->alias());
- }
- /**
- * Test getAssociations with off flag.
- *
- * @return void
- */
- public function testGetAssociationsNoFlag() {
- $this->Task->params['no-associations'] = true;
- $articles = TableRegistry::get('BakeArticle');
- $this->assertEquals([], $this->Task->getAssociations($articles));
- }
- /**
- * Test applying associations.
- *
- * @return void
- */
- public function testApplyAssociations() {
- $articles = TableRegistry::get('BakeArticles');
- $assocs = [
- 'belongsTo' => [
- [
- 'alias' => 'BakeUsers',
- 'foreignKey' => 'bake_user_id',
- ],
- ],
- 'hasMany' => [
- [
- 'alias' => 'BakeComments',
- 'foreignKey' => 'bake_article_id',
- ],
- ],
- 'belongsToMany' => [
- [
- 'alias' => 'BakeTags',
- 'foreignKey' => 'bake_article_id',
- 'joinTable' => 'bake_articles_bake_tags',
- 'targetForeignKey' => 'bake_tag_id',
- ],
- ],
- ];
- $original = $articles->associations()->keys();
- $this->assertEquals([], $original);
- $this->Task->applyAssociations($articles, $assocs);
- $new = $articles->associations()->keys();
- $expected = ['bakeusers', 'bakecomments', 'baketags'];
- $this->assertEquals($expected, $new);
- }
- /**
- * Test applying associations does nothing on a concrete class
- *
- * @return void
- */
- public function testApplyAssociationsConcreteClass() {
- Configure::write('App.namespace', 'TestApp');
- $articles = TableRegistry::get('Articles');
- $assocs = [
- 'belongsTo' => [
- [
- 'alias' => 'BakeUsers',
- 'foreignKey' => 'bake_user_id',
- ],
- ],
- 'hasMany' => [
- [
- 'alias' => 'BakeComments',
- 'foreignKey' => 'bake_article_id',
- ],
- ],
- 'belongsToMany' => [
- [
- 'alias' => 'BakeTags',
- 'foreignKey' => 'bake_article_id',
- 'joinTable' => 'bake_articles_bake_tags',
- 'targetForeignKey' => 'bake_tag_id',
- ],
- ],
- ];
- $original = $articles->associations()->keys();
- $this->Task->applyAssociations($articles, $assocs);
- $new = $articles->associations()->keys();
- $this->assertEquals($original, $new);
- }
- /**
- * Test getAssociations
- *
- * @return void
- */
- public function testGetAssociations() {
- $articles = TableRegistry::get('BakeArticles');
- $result = $this->Task->getAssociations($articles);
- $expected = [
- 'belongsTo' => [
- [
- 'alias' => 'BakeUsers',
- 'foreignKey' => 'bake_user_id'
- ],
- ],
- 'hasMany' => [
- [
- 'alias' => 'BakeComments',
- 'foreignKey' => 'bake_article_id',
- ],
- ],
- 'belongsToMany' => [
- [
- 'alias' => 'BakeTags',
- 'foreignKey' => 'bake_article_id',
- 'joinTable' => 'bake_articles_bake_tags',
- 'targetForeignKey' => 'bake_tag_id',
- ],
- ],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getAssociations in a plugin
- *
- * @return void
- */
- public function testGetAssociationsPlugin() {
- $articles = TableRegistry::get('BakeArticles');
- $this->Task->plugin = 'TestPlugin';
- $result = $this->Task->getAssociations($articles);
- $expected = [
- 'belongsTo' => [
- [
- 'alias' => 'BakeUsers',
- 'className' => 'TestPlugin.BakeUsers',
- 'foreignKey' => 'bake_user_id'
- ],
- ],
- 'hasMany' => [
- [
- 'alias' => 'BakeComments',
- 'className' => 'TestPlugin.BakeComments',
- 'foreignKey' => 'bake_article_id',
- ],
- ],
- 'belongsToMany' => [
- [
- 'alias' => 'BakeTags',
- 'className' => 'TestPlugin.BakeTags',
- 'foreignKey' => 'bake_article_id',
- 'joinTable' => 'bake_articles_bake_tags',
- 'targetForeignKey' => 'bake_tag_id',
- ],
- ],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test that belongsTo generation works.
- *
- * @return void
- */
- public function testBelongsToGeneration() {
- $model = TableRegistry::get('BakeComments');
- $result = $this->Task->findBelongsTo($model, []);
- $expected = [
- 'belongsTo' => [
- [
- 'alias' => 'BakeArticles',
- 'foreignKey' => 'bake_article_id'
- ],
- [
- 'alias' => 'BakeUsers',
- 'foreignKey' => 'bake_user_id'
- ],
- ]
- ];
- $this->assertEquals($expected, $result);
- $model = TableRegistry::get('CategoryThreads');
- $result = $this->Task->findBelongsTo($model, array());
- $expected = [
- 'belongsTo' => [
- [
- 'alias' => 'ParentCategoryThreads',
- 'className' => 'CategoryThreads',
- 'foreignKey' => 'parent_id'
- ],
- ]
- ];
- $this->assertEquals($expected, $result);
- $this->Task->plugin = 'Blog';
- $result = $this->Task->findBelongsTo($model, array());
- $expected = [
- 'belongsTo' => [
- [
- 'alias' => 'ParentCategoryThreads',
- 'className' => 'Blog.CategoryThreads',
- 'foreignKey' => 'parent_id'
- ],
- ]
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test that hasOne and/or hasMany relations are generated properly.
- *
- * @return void
- */
- public function testHasManyGeneration() {
- $this->Task->connection = 'test';
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->findHasMany($model, []);
- $expected = [
- 'hasMany' => [
- [
- 'alias' => 'BakeComments',
- 'foreignKey' => 'bake_article_id',
- ],
- ],
- ];
- $this->assertEquals($expected, $result);
- $model = TableRegistry::get('CategoryThreads');
- $result = $this->Task->findHasMany($model, []);
- $expected = [
- 'hasMany' => [
- [
- 'alias' => 'ChildCategoryThreads',
- 'className' => 'CategoryThreads',
- 'foreignKey' => 'parent_id',
- ],
- ]
- ];
- $this->assertEquals($expected, $result);
- $this->Task->plugin = 'Blog';
- $result = $this->Task->findHasMany($model, array());
- $expected = [
- 'hasMany' => [
- [
- 'alias' => 'ChildCategoryThreads',
- 'className' => 'Blog.CategoryThreads',
- 'foreignKey' => 'parent_id'
- ],
- ]
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that HABTM generation works
- *
- * @return void
- */
- public function testHasAndBelongsToManyGeneration() {
- $this->Task->connection = 'test';
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->findBelongsToMany($model, []);
- $expected = [
- 'belongsToMany' => [
- [
- 'alias' => 'BakeTags',
- 'foreignKey' => 'bake_article_id',
- 'joinTable' => 'bake_articles_bake_tags',
- 'targetForeignKey' => 'bake_tag_id',
- ],
- ],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getting accessible fields.
- *
- * @return void
- */
- public function testGetFields() {
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->getFields($model);
- $expected = [
- 'bake_user_id',
- 'title',
- 'body',
- 'published',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getting accessible fields includes associations.
- *
- * @return void
- */
- public function testGetFieldsAssociations() {
- $model = TableRegistry::get('BakeArticles');
- $model->belongsToMany('BakeTags');
- $model->belongsTo('BakeAuthors');
- $model->hasMany('BakeComments');
- $result = $this->Task->getFields($model);
- $this->assertContains('bake_tags', $result);
- $this->assertContains('bake_comments', $result);
- $this->assertContains('bake_author', $result);
- }
- /**
- * Test getting field with the no- option
- *
- * @return void
- */
- public function testGetFieldsDisabled() {
- $model = TableRegistry::get('BakeArticles');
- $this->Task->params['no-fields'] = true;
- $result = $this->Task->getFields($model);
- $this->assertEquals([], $result);
- }
- /**
- * Test getting field with a whitelist
- *
- * @return void
- */
- public function testGetFieldsWhiteList() {
- $model = TableRegistry::get('BakeArticles');
- $this->Task->params['fields'] = 'id, title , , body , created';
- $result = $this->Task->getFields($model);
- $expected = [
- 'id',
- 'title',
- 'body',
- 'created',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getting hidden fields.
- *
- * @return void
- */
- public function testGetHiddenFields() {
- $model = TableRegistry::get('Users');
- $result = $this->Task->getHiddenFields($model);
- $expected = [
- 'password',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getting hidden field with the no- option
- *
- * @return void
- */
- public function testGetHiddenFieldsDisabled() {
- $model = TableRegistry::get('Users');
- $this->Task->params['no-hidden'] = true;
- $result = $this->Task->getHiddenFields($model);
- $this->assertEquals([], $result);
- }
- /**
- * Test getting hidden field with a whitelist
- *
- * @return void
- */
- public function testGetHiddenFieldsWhiteList() {
- $model = TableRegistry::get('Users');
- $this->Task->params['hidden'] = 'id, title , , body , created';
- $result = $this->Task->getHiddenFields($model);
- $expected = [
- 'id',
- 'title',
- 'body',
- 'created',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getting primary key
- *
- * @return void
- */
- public function testGetPrimaryKey() {
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->getPrimaryKey($model);
- $expected = ['id'];
- $this->assertEquals($expected, $result);
- $this->Task->params['primary-key'] = 'id, , account_id';
- $result = $this->Task->getPrimaryKey($model);
- $expected = ['id', 'account_id'];
- $this->assertEquals($expected, $result);
- }
- /**
- * test getting validation rules with the no-validation rule.
- *
- * @return void
- */
- public function testGetValidationDisabled() {
- $model = TableRegistry::get('BakeArticles');
- $this->Task->params['no-validation'] = true;
- $result = $this->Task->getValidation($model);
- $this->assertEquals([], $result);
- }
- /**
- * test getting validation rules.
- *
- * @return void
- */
- public function testGetValidation() {
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->getValidation($model);
- $expected = [
- 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
- 'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
- 'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
- 'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
- 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
- ];
- $this->assertEquals($expected, $result);
- $model = TableRegistry::get('BakeComments');
- $result = $this->Task->getValidation($model);
- $expected = [
- 'bake_article_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
- 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
- 'comment' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
- 'published' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
- 'otherid' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests that a username column will get a validateUnique rule applied
- *
- * @return void
- */
- public function testGetValidationWithUnique() {
- $model = TableRegistry::get('Users');
- $result = $this->Task->getValidation($model);
- $expected = [
- 'password' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
- 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']],
- 'username' => [
- 'valid' => [
- 'rule' => false,
- 'allowEmpty' => true
- ],
- 'unique' => [
- 'rule' => 'validateUnique',
- 'provider' => 'table'
- ]
- ]
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test non interactive doActsAs
- *
- * @return void
- */
- public function testGetBehaviors() {
- $model = TableRegistry::get('NumberTrees');
- $result = $this->Task->getBehaviors($model);
- $this->assertEquals(['Tree' => []], $result);
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->getBehaviors($model);
- $this->assertEquals(['Timestamp' => []], $result);
- TableRegistry::clear();
- TableRegistry::get('Users', [
- 'table' => 'counter_cache_users'
- ]);
- $model = TableRegistry::get('Posts', [
- 'table' => 'counter_cache_posts'
- ]);
- $result = $this->Task->getBehaviors($model);
- $expected = [
- 'CounterCache' => ["'Users' => ['post_count']"]
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test getDisplayField() method.
- *
- * @return void
- */
- public function testGetDisplayField() {
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->getDisplayField($model);
- $this->assertEquals('title', $result);
- $this->Task->params['display-field'] = 'custom';
- $result = $this->Task->getDisplayField($model);
- $this->assertEquals('custom', $result);
- }
- /**
- * Ensure that the fixture object is correctly called.
- *
- * @return void
- */
- public function testBakeFixture() {
- $this->Task->plugin = 'TestPlugin';
- $this->Task->Fixture->expects($this->at(0))
- ->method('bake')
- ->with('BakeArticle', 'bake_articles');
- $this->Task->bakeFixture('BakeArticle', 'bake_articles');
- $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
- $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
- $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
- }
- /**
- * Ensure that the fixture baking can be disabled
- *
- * @return void
- */
- public function testBakeFixtureDisabled() {
- $this->Task->params['no-fixture'] = true;
- $this->Task->plugin = 'TestPlugin';
- $this->Task->Fixture->expects($this->never())
- ->method('bake');
- $this->Task->bakeFixture('BakeArticle', 'bake_articles');
- }
- /**
- * Ensure that the test object is correctly called.
- *
- * @return void
- */
- public function testBakeTest() {
- $this->Task->plugin = 'TestPlugin';
- $this->Task->Test->expects($this->at(0))
- ->method('bake')
- ->with('Table', 'BakeArticle');
- $this->Task->bakeTest('BakeArticle');
- $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
- $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
- $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
- }
- /**
- * Ensure that test baking can be disabled.
- *
- * @return void
- */
- public function testBakeTestDisabled() {
- $this->Task->params['no-test'] = true;
- $this->Task->plugin = 'TestPlugin';
- $this->Task->Test->expects($this->never())
- ->method('bake');
- $this->Task->bakeTest('BakeArticle');
- }
- /**
- * test baking validation
- *
- * @return void
- */
- public function testBakeTableValidation() {
- $validation = [
- 'id' => [
- 'valid' => array(
- 'allowEmpty' => 'create',
- 'rule' => 'numeric',
- )
- ],
- 'name' => [
- 'valid' => [
- 'allowEmpty' => false,
- 'rule' => false,
- ]
- ],
- 'email' => [
- 'valid' => [
- 'allowEmpty' => true,
- 'rule' => 'email'
- ],
- 'unique' => [
- 'rule' => 'validateUnique',
- 'provider' => 'table'
- ]
- ]
- ];
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->bakeTable($model, compact('validation'));
- $this->assertContains('namespace App\Model\Table;', $result);
- $this->assertContains('use Cake\ORM\Table;', $result);
- $this->assertContains('use Cake\Validation\Validator;', $result);
- $this->assertContains('class BakeArticlesTable extends Table {', $result);
- $this->assertContains('public function validationDefault(Validator $validator) {', $result);
- $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
- $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
- $this->assertContains(
- "->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
- $result);
- $this->assertContains("->allowEmpty('id', 'create')", $result);
- $this->assertContains("->allowEmpty('email')", $result);
- $this->assertContains("->validatePresence('name', 'create')", $result);
- }
- /**
- * test baking
- *
- * @return void
- */
- public function testBakeTableConfig() {
- $config = [
- 'table' => 'articles',
- 'primaryKey' => ['id'],
- 'displayField' => 'title',
- 'behaviors' => ['Timestamp' => ''],
- ];
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->bakeTable($model, $config);
- $this->assertContains('public function initialize(array $config) {', $result);
- $this->assertContains("this->primaryKey('id');\n", $result);
- $this->assertContains("this->displayField('title');\n", $result);
- $this->assertContains("this->addBehavior('Timestamp');\n", $result);
- $this->assertContains("this->table('articles');\n", $result);
- $this->assertContains('use Cake\Validation\Validator;', $result);
- }
- /**
- * test baking relations
- *
- * @return void
- */
- public function testBakeTableRelations() {
- $associations = [
- 'belongsTo' => [
- [
- 'alias' => 'SomethingElse',
- 'foreignKey' => 'something_else_id',
- ],
- [
- 'alias' => 'BakeUser',
- 'foreignKey' => 'bake_user_id',
- ],
- ],
- 'hasMany' => [
- [
- 'alias' => 'BakeComment',
- 'foreignKey' => 'parent_id',
- ],
- ],
- 'belongsToMany' => [
- [
- 'alias' => 'BakeTag',
- 'foreignKey' => 'bake_article_id',
- 'joinTable' => 'bake_articles_bake_tags',
- 'targetForeignKey' => 'bake_tag_id',
- ],
- ]
- ];
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->bakeTable($model, compact('associations'));
- $this->assertContains("this->hasMany('BakeComment', [", $result);
- $this->assertContains("this->belongsTo('SomethingElse', [", $result);
- $this->assertContains("this->belongsTo('BakeUser', [", $result);
- $this->assertContains("this->belongsToMany('BakeTag', [", $result);
- $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
- }
- /**
- * test baking an entity class
- *
- * @return void
- */
- public function testBakeEntity() {
- $config = [
- 'fields' => []
- ];
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->bakeEntity($model, $config);
- $this->assertContains('namespace App\Model\Entity;', $result);
- $this->assertContains('use Cake\ORM\Entity;', $result);
- $this->assertContains('class BakeArticle extends Entity {', $result);
- $this->assertNotContains('$_accessible', $result);
- }
- /**
- * test baking an entity class
- *
- * @return void
- */
- public function testBakeEntityFields() {
- $config = [
- 'fields' => ['title', 'body', 'published']
- ];
- $model = TableRegistry::get('BakeArticles');
- $result = $this->Task->bakeEntity($model, $config);
- $this->assertContains("protected \$_accessible = [", $result);
- $this->assertContains("'title' => true,", $result);
- $this->assertContains("'body' => true,", $result);
- $this->assertContains("'published' => true", $result);
- $this->assertNotContains("protected \$_hidden", $result);
- }
- /**
- * test baking an entity class sets hidden fields.
- *
- * @return void
- */
- public function testBakeEntityHidden() {
- $model = TableRegistry::get('BakeUsers');
- $config = [
- 'hidden' => ['password'],
- ];
- $result = $this->Task->bakeEntity($model, $config);
- $this->assertContains("protected \$_hidden = [", $result);
- $this->assertContains("'password'", $result);
- $this->assertNotContains("protected \$_accessible", $result);
- }
- /**
- * test bake() with a -plugin param
- *
- * @return void
- */
- public function testBakeTableWithPlugin() {
- $this->Task->plugin = 'ControllerTest';
- // fake plugin path
- Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
- $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
- $this->Task->expects($this->once())->method('createFile')
- ->with($path, $this->logicalAnd(
- $this->stringContains('namespace ControllerTest\\Model\\Table;'),
- $this->stringContains('use Cake\\ORM\\Table;'),
- $this->stringContains('class BakeArticlesTable extends Table {')
- ));
- $model = TableRegistry::get('BakeArticles');
- $this->Task->bakeTable($model);
- }
- /**
- * test bake() with a -plugin param
- *
- * @return void
- */
- public function testBakeEntityWithPlugin() {
- $this->Task->plugin = 'ControllerTest';
- // fake plugin path
- Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
- $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
- $path = $this->_normalizePath($path);
- $this->Task->expects($this->once())->method('createFile')
- ->with($path, $this->logicalAnd(
- $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
- $this->stringContains('use Cake\\ORM\\Entity;'),
- $this->stringContains('class BakeArticle extends Entity {')
- ));
- $model = TableRegistry::get('BakeArticles');
- $this->Task->bakeEntity($model);
- }
- /**
- * test that execute with no args
- *
- * @return void
- */
- public function testMainNoArgs() {
- $this->_useMockedOut();
- $this->Task->connection = 'test';
- $this->Task->path = '/my/path/';
- $this->Task->expects($this->at(0))
- ->method('out')
- ->with($this->stringContains('Choose a model to bake from the following:'));
- $this->Task->main();
- }
- /**
- * test that execute passes runs bake depending with named model.
- *
- * @return void
- */
- public function testMainWithNamedModel() {
- $this->Task->connection = 'test';
- $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
- $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
- $this->Task->expects($this->at(1))
- ->method('createFile')
- ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
- $this->Task->main('BakeArticles');
- }
- /**
- * data provider for testMainWithNamedModelVariations
- *
- * @return void
- */
- public static function nameVariations() {
- return array(
- array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
- );
- }
- /**
- * test that execute passes with different inflections of the same name.
- *
- * @dataProvider nameVariations
- * @return void
- */
- public function testMainWithNamedModelVariations($name) {
- $this->Task->connection = 'test';
- $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
- $this->Task->main($name);
- }
- /**
- * test that execute runs all() when args[0] = all
- *
- * @return void
- */
- public function testMainIntoAll() {
- $count = count($this->Task->listAll());
- if ($count != count($this->fixtures)) {
- $this->markTestSkipped('Additional tables detected.');
- }
- $this->Task->connection = 'test';
- $this->Task->Fixture->expects($this->exactly($count))
- ->method('bake');
- $this->Task->Test->expects($this->exactly($count))
- ->method('bake');
- $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
- $this->Task->expects($this->at(0))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
- $this->Task->expects($this->at(1))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeArticle extends'));
- $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
- $this->Task->expects($this->at(2))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
- $this->Task->expects($this->at(3))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
- $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
- $this->Task->expects($this->at(4))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
- $this->Task->expects($this->at(5))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeComment extends'));
- $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
- $this->Task->expects($this->at(6))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeTagsTable extends'));
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
- $this->Task->expects($this->at(7))
- ->method('createFile')
- ->with($filename, $this->stringContains('class BakeTag extends'));
- $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
- $this->Task->expects($this->at(8))
- ->method('createFile')
- ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
- $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
- $this->Task->expects($this->at(9))
- ->method('createFile')
- ->with($filename, $this->stringContains('class CategoryThread extends'));
- $this->Task->all();
- }
- /**
- * test that skipTables changes how all() works.
- *
- * @return void
- */
- public function testSkipTablesAndAll() {
- $count = count($this->Task->listAll('test'));
- if ($count != count($this->fixtures)) {
- $this->markTestSkipped('Additional tables detected.');
- }
- $this->Task->connection = 'test';
- $this->Task->skipTables = ['bake_tags', 'counter_cache_posts'];
- $this->Task->Fixture->expects($this->exactly(7))
- ->method('bake');
- $this->Task->Test->expects($this->exactly(7))
- ->method('bake');
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
- $this->Task->expects($this->at(1))
- ->method('createFile')
- ->with($filename);
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
- $this->Task->expects($this->at(3))
- ->method('createFile')
- ->with($filename);
- $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
- $this->Task->expects($this->at(5))
- ->method('createFile')
- ->with($filename);
- $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
- $this->Task->expects($this->at(7))
- ->method('createFile')
- ->with($filename);
- $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
- $this->Task->expects($this->at(9))
- ->method('createFile')
- ->with($filename);
- $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
- $this->Task->expects($this->at(11))
- ->method('createFile')
- ->with($filename);
- $this->Task->all();
- }
- }
|