Skip to content

Commit

Permalink
Behat tests for generators
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 3, 2014
1 parent f5ab2b7 commit 648968e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 25 deletions.
9 changes: 9 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default:
paths:
features: tests/features
bootstrap: %behat.paths.features%/bootstrap
extensions:
Behat\MinkExtension\Extension:
base_url: http://localhost:8000
goutte: ~
selenium2: ~
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"illuminate/support": "4.1.*"
},
"require-dev": {
Expand All @@ -17,7 +17,8 @@
"behat/mink": "v1.5.0",
"behat/mink-extension": "*",
"behat/mink-goutte-driver": "*",
"behat/mink-selenium2-driver": "*"
"behat/mink-selenium2-driver": "*",
"phpunit/phpunit": "3.*"
},
"autoload": {
"psr-0": {
Expand Down
91 changes: 68 additions & 23 deletions tests/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,84 @@
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\Console\Tester\CommandTester;
use Way\Generators\Filesystem\Filesystem;
use Way\Generators\Generator;
use Way\Generators\Laravel\ModelGeneratorCommand;
use Symfony\Component\Console\Application;

//
// Require 3rd-party libraries here:
//
// require_once 'PHPUnit/Autoload.php';
// require_once 'PHPUnit/Framework/Assert/Functions.php';
//
require_once __DIR__.'/../../../vendor/phpunit/phpunit/PHPUnit/Framework/Assert/Functions.php';

/**
* Features context.
*/
class FeatureContext extends BehatContext
{
protected $tester;

/**
* @beforeSuite
*/
public static function bootstrapLaravel()
{
require __DIR__.'/../../../../../../vendor/autoload.php';
require __DIR__.'/../../../../../../bootstrap/start.php';
}

/**
* @AfterScenario
*/
public function tearDown()
{
@unlink(app_path('models/Order.php'));
@unlink(app_path('database/seeds/OrdersTableSeeder.php'));

$this->tester = null;
}

/**
* @When /^I generate a model with "([^"]*)"$/
*/
public function iGenerateAModelWith($name)
{
$this->tester = new CommandTester(App::make('Way\Generators\Laravel\ModelGeneratorCommand'));
$this->tester->execute(['modelName' => $name]);
}

/**
* @When /^I generate a seed with "([^"]*)"$/
*/
public function iGenerateASeedWith($tableName)
{
$this->tester = new CommandTester(App::make('Way\Generators\Laravel\SeederGeneratorCommand'));
$this->tester->execute(['tableName' => $tableName]);
}

/**
* Initializes context.
* Every scenario gets its own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
* @Then /^I should see "([^"]*)"$/
*/
public function __construct(array $parameters)
public function iShouldSee($output)
{
// Initialize your context here
$display = $this->tester->getDisplay();

assertContains($output, $display);
}

/**
* @Given /^"([^"]*)" should match my stub$/
*/
public function shouldMatchMyStub($generatedFilePath)
{
// We'll use the name of the generated file as
// the basic for our stub lookup.
$stubName = pathinfo($generatedFilePath)['filename'];

$expected = file_get_contents(__DIR__."/../../stubs/{$stubName}.txt");
$actual = file_get_contents(base_path($generatedFilePath));

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

//
// Place your definition and hook methods here:
//
// /**
// * @Given /^I have done something with "([^"]*)"$/
// */
// public function iHaveDoneSomethingWith($argument)
// {
// doSomethingWith($argument);
// }
//
}
11 changes: 11 additions & 0 deletions tests/features/generators.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Generators

Scenario Outline: Generation
When I generate a <command> with "<argument>"
Then I should see "Created:"
And "<generatedFilePath>" should match my stub

Examples:
| command | argument | generatedFilePath |
| model | Order | app/models/Order.php |
| seed | orders | app/database/seeds/OrdersTableSeeder.php |
5 changes: 5 additions & 0 deletions tests/stubs/Order.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class Order extends \Eloquent {
protected $fillable = [];
}
20 changes: 20 additions & 0 deletions tests/stubs/OrdersTableSeeder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;

class OrdersTableSeeder extends Seeder {

public function run()
{
$faker = Faker::create();

foreach(range(1, 10) as $index)
{
Order::create([

]);
}
}

}

0 comments on commit 648968e

Please sign in to comment.