Skip to content

Commit

Permalink
convert all bake tests to be file comparisons
Browse files Browse the repository at this point in the history
This makes it much easier to see what is expected, and what is wrong
when the output changes
  • Loading branch information
AD7six committed Nov 25, 2014
1 parent 2535a03 commit 830f702
Show file tree
Hide file tree
Showing 39 changed files with 783 additions and 211 deletions.
51 changes: 51 additions & 0 deletions src/TestSuite/StringCompareTrait.php
@@ -0,0 +1,51 @@
<?php
/**
* CakePHP(tm) : 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(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite;

/**
* Compare a string to the contents of a file
*
* Implementing objects are expected to declare a `$_compareBasePath` property.
*/
trait StringCompareTrait {

/**
* The base path for output comparisons
*
* Must be initialized before use
*
* @var string
*/
protected $_compareBasePath = '';

/**
* Compare the result to the contents of the file
*
* @param string $path partial path to test comparison file
* @param string $result test result as a string
* @return void
*/
public function assertSameAsFile($path, $result) {
$path = $this->_compareBasePath . $path;
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
file_put_contents($path, $result);

$expected = file_get_contents($path);
$this->assertTextEquals($expected, $result);
}

}
8 changes: 5 additions & 3 deletions tests/TestCase/Shell/Task/CellTaskTest.php
Expand Up @@ -17,20 +17,24 @@
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Shell\Task\TemplateTask;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;

/**
* CellTaskTest class
*/
class CellTaskTest extends TestCase {

use StringCompareTrait;

/**
* setup method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_compareBasePath = CORE_TESTS . 'bake_compare' . DS . 'Cell' . DS;
$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock(
Expand Down Expand Up @@ -123,9 +127,7 @@ public function testBakePlugin() {
);

$result = $this->Task->bake('Example');

$expected = file_get_contents(CORE_TESTS . '/bake_compare/View/Cell/ExampleCell.php');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

}
11 changes: 6 additions & 5 deletions tests/TestCase/Shell/Task/ControllerTaskTest.php
Expand Up @@ -21,6 +21,7 @@
use Cake\ORM\TableRegistry;
use Cake\Shell\Task\ControllerTask;
use Cake\Shell\Task\TemplateTask;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;
use Cake\View\Helper;

Expand All @@ -43,6 +44,8 @@ public function initialize(array $config) {
*/
class ControllerTaskTest extends TestCase {

use StringCompareTrait;

/**
* fixtures
*
Expand All @@ -57,7 +60,7 @@ class ControllerTaskTest extends TestCase {
*/
public function setUp() {
parent::setUp();

$this->_compareBasePath = CORE_TESTS . 'bake_compare' . DS . 'Controller' . DS;
$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
$this->Task = $this->getMock('Cake\Shell\Task\ControllerTask',
array('in', 'out', 'err', 'hr', 'createFile', '_stop'),
Expand Down Expand Up @@ -153,8 +156,7 @@ public function testBakeNoActions() {
$this->Task->params['components'] = 'Csrf, Auth';

$result = $this->Task->bake('BakeArticles');
$expected = file_get_contents(CORE_TESTS . '/bake_compare/Controller/NoActions.ctp');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down Expand Up @@ -236,8 +238,7 @@ public function testBakeWithPlugin() {
*/
public function testBakeActionsContent() {
$result = $this->Task->bakeActions('BakeArticles');
$expected = file_get_contents(CORE_TESTS . 'bake_compare/Controller/Actions.ctp');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down
33 changes: 12 additions & 21 deletions tests/TestCase/Shell/Task/FixtureTaskTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\ORM\TableRegistry;
use Cake\Shell\Task\FixtureTask;
use Cake\Shell\Task\TemplateTask;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;

/**
Expand All @@ -27,6 +28,8 @@
*/
class FixtureTaskTest extends TestCase {

use StringCompareTrait;

/**
* fixtures
*
Expand All @@ -41,6 +44,7 @@ class FixtureTaskTest extends TestCase {
*/
public function setUp() {
parent::setUp();
$this->_compareBasePath = CORE_TESTS . 'bake_compare' . DS . 'Fixture' . DS;
$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock('Cake\Shell\Task\FixtureTask',
Expand Down Expand Up @@ -98,9 +102,7 @@ public function testImportRecordsFromDatabaseWithConditionsPoo() {
$this->Task->params = ['schema' => true, 'records' => true];

$result = $this->Task->bake('Articles');

$expected = file_get_contents(CORE_TESTS . '/bake_compare/test/Fixture/ArticleImportWithConditionsFixture.php');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -112,9 +114,7 @@ public function testImportOptionsAlternateConnection() {
$this->Task->connection = 'test';
$this->Task->params = ['schema' => true];
$result = $this->Task->bake('Article');

$expected = file_get_contents(CORE_TESTS . '/bake_compare/test/Fixture/ArticleAlternateConnectionFixture.php');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -129,9 +129,7 @@ public function testImportRecordsNoEscaping() {
$this->Task->connection = 'test';
$this->Task->params = ['schema' => 'true', 'records' => true];
$result = $this->Task->bake('Article');

$expected = file_get_contents(CORE_TESTS . '/bake_compare/test/Fixture/ArticleNoEscapingFixture.php');
$this->assertTextEquals($expected, $result, 'Data has bad escaping');
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down Expand Up @@ -318,9 +316,7 @@ public function testMainImportSchema() {
));

$result = $this->Task->bake('Article', 'comments');

$expected = file_get_contents(CORE_TESTS . '/bake_compare/test/Fixture/ArticleCommentsFixture.php');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -332,13 +328,10 @@ public function testRecordGenerationForBinaryAndFloat() {
$this->Task->connection = 'test';

$result = $this->Task->bake('Article', 'datatypes');

$expected = file_get_contents(CORE_TESTS . '/bake_compare/test/Fixture/ArticleDatatypesFixture.php');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '-datatypes.php', $result);

$result = $this->Task->bake('Article', 'binary_tests');
$expected = file_get_contents(CORE_TESTS . '/bake_compare/test/Fixture/ArticleBinaryTestFixture.php');
$this->assertTextEquals($expected, $result);
$this->assertSameAsFile(__FUNCTION__ . '-binary-tests.php', $result);
}

/**
Expand All @@ -355,8 +348,7 @@ public function testGenerateFixtureFile() {
->with($filename, $this->stringContains('ArticlesFixture'));

$result = $this->Task->generateFixtureFile('Articles', []);
$this->assertContains('<?php', $result);
$this->assertContains('namespace App\Test\Fixture;', $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -374,8 +366,7 @@ public function testGeneratePluginFixtureFile() {
->with($filename, $this->stringContains('class Articles'));

$result = $this->Task->generateFixtureFile('Articles', []);
$this->assertContains('<?php', $result);
$this->assertContains('namespace TestPlugin\Test\Fixture;', $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

}
69 changes: 16 additions & 53 deletions tests/TestCase/Shell/Task/ModelTaskTest.php
Expand Up @@ -20,6 +20,7 @@
use Cake\ORM\TableRegistry;
use Cake\Shell\Task\ModelTask;
use Cake\Shell\Task\TemplateTask;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;
use Cake\Utility\ClassRegistry;
use Cake\Utility\Inflector;
Expand All @@ -29,6 +30,8 @@
*/
class ModelTaskTest extends TestCase {

use StringCompareTrait;

/**
* fixtures
*
Expand All @@ -48,6 +51,7 @@ class ModelTaskTest extends TestCase {
*/
public function setUp() {
parent::setUp();
$this->_compareBasePath = CORE_TESTS . 'bake_compare' . DS . 'Model' . DS;
$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock('Cake\Shell\Task\ModelTask',
Expand Down Expand Up @@ -781,20 +785,7 @@ public function testBakeTableValidation() {
];
$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("->requirePresence('name', 'create')", $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -811,13 +802,7 @@ public function testBakeTableConfig() {
];
$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);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down Expand Up @@ -854,11 +839,7 @@ public function testBakeTableRelations() {
];
$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);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -872,11 +853,7 @@ public function testBakeEntity() {
];
$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);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -890,12 +867,7 @@ public function testBakeEntityFields() {
];
$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);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -909,10 +881,7 @@ public function testBakeEntityHidden() {
'hidden' => ['password'],
];
$result = $this->Task->bakeEntity($model, $config);

$this->assertContains("protected \$_hidden = [", $result);
$this->assertContains("'password'", $result);
$this->assertNotContains("protected \$_accessible", $result);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -927,14 +896,11 @@ public function testBakeTableWithPlugin() {
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 {')
));
->with($path);

$model = TableRegistry::get('BakeArticles');
$this->Task->bakeTable($model);
$result = $this->Task->bakeTable($model);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand All @@ -950,14 +916,11 @@ public function testBakeEntityWithPlugin() {
$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 {')
));
->with($path);

$model = TableRegistry::get('BakeArticles');
$this->Task->bakeEntity($model);
$result = $this->Task->bakeEntity($model);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down

0 comments on commit 830f702

Please sign in to comment.