Skip to content

Commit

Permalink
Add Behat tests for migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 3, 2014
1 parent d51d759 commit 77c8d95
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 8 deletions.
17 changes: 14 additions & 3 deletions src/Way/Generators/Commands/MigrationGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function fire()

// Now that the file has been generated,
// let's run dump-autoload to refresh everything
$this->call('dump-autoload');
if ( ! $this->option('testing'))
{
$this->call('dump-autoload');
}
}

/**
Expand Down Expand Up @@ -101,8 +104,15 @@ protected function getDatePrefix()
protected function getTemplateData()
{
$migrationName = $this->argument('migrationName');

// This will tell us the table name and action that we'll be performing
$migrationData = $this->migrationNameParser->parse($migrationName);
$fields = $this->migrationFieldsParser->parse($this->option('fields'));

// We also need to parse the migration fields, if provided
if ($fields = $this->option('fields'))
{
$fields = $this->migrationFieldsParser->parse($fields);
}

return [
'CLASS' => ucwords(camel_case($migrationName)),
Expand Down Expand Up @@ -133,7 +143,8 @@ protected function getOptions()
return array(
array('fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the migration'),
array('path', null, InputOption::VALUE_OPTIONAL, 'Where should the file be created?', app_path('database/migrations')),
array('templatePath', null, InputOption::VALUE_OPTIONAL, 'What is the path to the template for this generator?', __DIR__.'/../templates/migration.txt')
array('templatePath', null, InputOption::VALUE_OPTIONAL, 'What is the path to the template for this generator?', __DIR__.'/../templates/migration.txt'),
array('testing', null, InputOption::VALUE_OPTIONAL, 'For internal use only.')
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public function tearDown()
$this->tester = null;
}

/**
* @When /^I generate a migration with name \'([^\']*)\' and fields \'([^\']*)\'$/
*/
public function iGenerateAMigrationWithNameAndFields($migrationName, $fields)
{
$this->tester = new CommandTester(App::make('Way\Generators\Commands\MigrationGeneratorCommand'));
$this->tester->execute([
'migrationName' => $migrationName,
'--fields' => $fields,
'--testing' => true
]);
}

/**
* @When /^I generate a model with "([^"]*)"$/
*/
Expand Down Expand Up @@ -72,6 +85,18 @@ public function iGenerateAMigrationWith($migrationName)
$this->tester->execute(compact('migrationName'));
}

/**
* @Given /^the generated migration should match my \'([^\']*)\' stub$/
*/
public function theGeneratedMigrationShouldMatchMyStub($stubName)
{
$expected = file_get_contents(__DIR__."/../../stubs/{$stubName}.txt");
$actual = file_get_contents(glob(base_path('app/database/migrations/*'))[0]);

// Let's compare the stub against what was actually generated.
assertEquals($expected, $actual);
}

/**
* @Then /^I should see "([^"]*)"$/
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/features/migrations.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Feature: Migrations With Schema

Scenario: Creating a Table
When I generate a migration with name 'create_orders_table' and fields 'name:string'
Then I should see "Created:"
And the generated migration should match my 'CreateOrdersTable' stub

Scenario: Creating a Table With Complex Fields
When I generate a migration with name 'create_orders_table' and fields 'title:string(50):unique, body:text:unique:nullable'
Then I should see "Created:"
And the generated migration should match my 'CreateComplexOrdersTable' stub

Scenario: Dropping a Table
When I generate a migration with name 'drop_orders_table' and fields 'title:string'
Then I should see "Created:"
And the generated migration should match my 'DropOrdersTable' stub

Scenario: Adding to a Table
When I generate a migration with name 'add_title_to_orders_table' and fields 'title:string'
Then I should see "Created:"
And the generated migration should match my 'AddTitleToOrdersTable' stub

Scenario: Removing from a Table
When I generate a migration with name 'remove_title_from_orders_table' and fields 'title:string'
Then I should see "Created:"
And the generated migration should match my 'RemoveTitleFromOrdersTable' stub
4 changes: 2 additions & 2 deletions tests/stubs/AddTitleToOrdersTable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AddTitleToOrdersTable extends Migration {
public function up()
{
Schema::table('orders', function(Blueprint $table) {

$table->string('title');
});
}

Expand All @@ -26,7 +26,7 @@ class AddTitleToOrdersTable extends Migration {
public function down()
{
Schema::table('orders', function(Blueprint $table) {

$table->dropColumn('title');
});
}

Expand Down
33 changes: 33 additions & 0 deletions tests/stubs/CreateComplexOrdersTable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateOrdersTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 50)->unique();
$table->text('body')->unique()->nullable();
});
}


/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('orders');
}

}
3 changes: 2 additions & 1 deletion tests/stubs/CreateOrdersTable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class CreateOrdersTable extends Migration {
public function up()
{
Schema::create('orders', function(Blueprint $table) {

$table->increments('id');
$table->string('name');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class DeleteOrdersTable extends Migration {
class DropOrdersTable extends Migration {

/**
* Run the migrations.
Expand All @@ -24,7 +24,8 @@ class DeleteOrdersTable extends Migration {
public function down()
{
Schema::create('orders', function(Blueprint $table) {

$table->increments('id');
$table->string('title');
});
}

Expand Down
33 changes: 33 additions & 0 deletions tests/stubs/RemoveTitleFromOrdersTable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class RemoveTitleFromOrdersTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('orders', function(Blueprint $table) {
$table->dropColumn('title');
});
}


/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('orders', function(Blueprint $table) {
$table->string('title');
});
}

}

0 comments on commit 77c8d95

Please sign in to comment.