<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('Generate the full structure of a new module for Backbone / RequireJS');
// Make sure that we're dealing with a clean directory
$I->cleanDir('tests/tmp');
$I->runShellCommand('php ../../../artisan modules:generate taco --path=tests/tmp/modules');
$I->seeInShellOutput('Successfully created 4 files');
$I->openFile('tests/tmp/modules/tacos/models/taco.js');
$I->seeFileContentsEqual(file_get_Contents('tests/acceptance/stubs/model.stub'));
$I->openFile('tests/tmp/modules/tacos/collections/tacos.js');
$I->seeFileContentsEqual(file_get_Contents('tests/acceptance/stubs/collection.stub'));
$I->openFile('tests/tmp/modules/tacos/views/taco_view.js');
$I->seeFileContentsEqual(file_get_Contents('tests/acceptance/stubs/view.stub'));
$I->openFile('tests/tmp/modules/tacos/index.js');
$I->seeFileContentsEqual(file_get_Contents('tests/acceptance/stubs/index.stub'));
$I->cleanDir('tests/tmp');
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate a model without fillable fields or dates');
$I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp');
$I->seeInShellOutput('TestingModel model generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\\Tmp;

use Illuminate\\Database\\Eloquent\\Model;

class TestingModel extends Model {

	protected $fillable = [];

	protected $dates = [];

	public static $rules = [
		// Validation rules
	];

	// Relationships

}
');
$I->wantTo('generate a model with fillable fields');
$I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --path=tests/tmp');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeInThisFile('protected $fillable = ["name", "title"];');
<?php

/** @var \Codeception\Scenario $scenario */
$I = new AcceptanceTester($scenario);
$inputPath = __DIR__ . '/inputs';
$outputPath = __DIR__ . '/outputs';
$I->runShellCommand('rm -rf ' . $outputPath);
$I->runShellCommand('mkdir ' . $outputPath);
$I->runShellCommand('php extract_markdown_codes.php ' . $inputPath . ' list ' . $outputPath);
// assertions
$I->amInPath($outputPath);
// list1
(function () use($I) {
    $I->openFile('list001.txt');
    $contents = <<<EOT
// list1
require_once __DIR__ . '/vendor/autoload.php';

\$foo = new Foo();
\$foo->execute();

EOT;
    $I->seeFileContentsEqual($contents);
})();
// list2
(function () use($I) {
    $I->openFile('list002.txt');
    $contents = <<<EOT
// list2
\$i = 2;
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate a RESTful resource');
$I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable descr;text:nullable;;fillable project_id;integer;required;key,fillable due;timestamp;;fillable,date" --has-many="tags,tasks" --belongs-to="project,creator:User" --migration-file=create_task_categories');
// Checking the model
$I->seeInShellOutput('TaskCategory model generated');
$I->seeFileFound('./app/TaskCategory.php');
$I->openFile('./app/TaskCategory.php');
$I->seeInThisFile('namespace App;');
$I->seeInThisFile('class TaskCategory extends Model');
$I->seeInThisFile('protected $fillable = ["name", "descr", "project_id", "due"];');
$I->seeInThisFile('protected $dates = ["due"];');
$I->seeInThisFile('public static $rules = [
		"name" => "requied",
		"project_id" => "required",
	];');
$I->seeInThisFile('
	public function tags()
	{
		return $this->hasMany("App\\Tag");
	}

	public function tasks()
	{
		return $this->hasMany("App\\Task");
	}

	public function project()
	{
		return $this->belongsTo("App\\Project");
<?php

$saveDir = './tests/acceptance/tmp';
$stubDir = './tests/acceptance/stubs';
$queryToGenerate = 'FooQuery';
$I = new AcceptanceTester($scenario);
$I->wantTo('generate a query and handler class');
$I->runShellCommand("php ../../../artisan querier:generate {$queryToGenerate} --properties='bar, baz' --base='{$saveDir}'");
$I->seeInShellOutput('All done!');
// My Command stub should match the generated class.
$I->openFile("{$saveDir}/{$queryToGenerate}.php");
$I->seeFileContentsEqual(file_get_contents("{$stubDir}/{$queryToGenerate}.stub"));
// And my QueryHandler stub should match its generated counterpart, as well.
$I->openFile("{$saveDir}/{$queryToGenerate}Handler.php");
$I->seeFileContentsEqual(file_get_contents("{$stubDir}/{$queryToGenerate}Handler.stub"));
$I->cleanDir($saveDir);
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate a pivot table');
$I->runShellCommand('php artisan wn:pivot-table Tag Project --file=pivot_table');
$I->seeInShellOutput('project_tag migration generated');
$I->seeFileFound('./database/migrations/pivot_table.php');
$I->openFile('./database/migrations/pivot_table.php');
$I->seeFileContentsEqual('<?php

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

class CreateProjectTagTable extends Migration
{
    
    public function up()
    {
        Schema::create(\'project_tag\', function(Blueprint $table) {
            $table->increments(\'id\');
            $table->integer(\'project_id\')->unsigned()->index();
            $table->integer(\'tag_id\')->unsigned()->index();
            $table->foreign(\'project_id\')
                ->references(\'id\')
                ->on(\'projects\');
            $table->foreign(\'tag_id\')
                ->references(\'id\')
                ->on(\'tags\');
            $table->timestamps();
        });
    }
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate RESTful routes for a resource with default controller');
$I->runShellCommand('php artisan wn:route project-type');
$I->seeInShellOutput('project-type routes generated');
$I->openFile('./app/Http/routes.php');
$I->seeInThisFile("\n\$app->get('project-type', 'ProjectTypesController@all');\n\$app->get('project-type/{id}', 'ProjectTypesController@get');\n\$app->post('project-type', 'ProjectTypesController@add');\n\$app->put('project-type/{id}', 'ProjectTypesController@put');\n\$app->delete('project-type/{id}', 'ProjectTypesController@remove');\n");
$I->writeToFile('./app/Http/routes.php', '<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$app->get("/", function () use ($app) {
    return $app->welcome();
});
');
$I->wantTo('generate RESTful routes for a resource with custom controller');
$I->runShellCommand('php artisan wn:route foo --controller=customController');
$I->seeInShellOutput('foo routes generated');
$I->openFile('./app/Http/routes.php');
$I->seeInThisFile("\n\$app->get('foo', 'customController@all');\n\$app->get('foo/{id}', 'customController@get');\n\$app->post('foo', 'customController@add');\n\$app->put('foo/{id}', 'customController@put');\n\$app->delete('foo/{id}', 'customController@remove');\n");
$I->writeToFile('./app/Http/routes.php', '<?php
<?php

$saveDir = './tests/acceptance/tmp';
$stubDir = './tests/acceptance/stubs';
$commandToGenerate = 'FooCommand';
$I = new AcceptanceTester($scenario);
$I->wantTo('generate a command and handler class');
$I->runShellCommand("php ../../../artisan commander:generate {$commandToGenerate} --properties='bar, baz' --base='{$saveDir}'");
$I->seeInShellOutput('All done!');
// My Command stub should match the generated class.
$I->openFile("{$saveDir}/{$commandToGenerate}.php");
$I->seeFileContentsEqual(file_get_contents("{$stubDir}/{$commandToGenerate}.stub"));
// And my CommandHandler stub should match its generated counterpart, as well.
$I->openFile("{$saveDir}/{$commandToGenerate}Handler.php");
$I->seeFileContentsEqual(file_get_contents("{$stubDir}/{$commandToGenerate}Handler.stub"));
$I->cleanDir($saveDir);
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate a migration without schema');
$I->runShellCommand('php artisan wn:migration tasks --file=create_tasks');
$I->seeInShellOutput('tasks migration generated');
$I->seeFileFound('./database/migrations/create_tasks.php');
$I->openFile('./database/migrations/create_tasks.php');
$I->seeFileContentsEqual('<?php

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

class CreateTasksMigration extends Migration
{
    
    public function up()
    {
        Schema::create(\'tasks\', function(Blueprint $table) {
            $table->increments(\'id\');
            // Schema declaration
            // Constraints declaration
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop(\'tasks\');
    }
}
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate the REST actions trait');
$I->runShellCommand('php artisan wn:controller:rest-actions');
$I->seeInShellOutput('REST actions trait generated');
$I->seeFileFound('./app/Http/Controllers/RESTActions.php');
$I->openFile('./app/Http/Controllers/RESTActions.php');
$I->seeInThisFile('trait RESTActions {');
$I->deleteFile('./app/Http/Controllers/RESTActions.php');
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate both a command and a hndler class');
$I->runShellCommand('php artisan commander:generate Acme/Bar/FooCommand --properties="bar, baz" --base="tests/tmp"');
$I->seeInShellOutput('All done!');
$I->openFile('tests/tmp/Acme/Bar/FooCommand.php');
$I->seeFileContentsEqual(file_get_contents('tests/acceptance/stub/FooCommand.stub'));
//$I->seeFileFound('tests/tmp/FooCommandHandler.php');
<?php

$I = new AcceptanceTester($scenario);
$I->wantTo('generate a RESTful controller with short model name');
$I->runShellCommand('php artisan wn:controller Test --no-routes');
$I->seeInShellOutput('TestsController generated');
$I->seeFileFound('./app/Http/Controllers/TestsController.php');
$I->openFile('./app/Http/Controllers/TestsController.php');
$I->seeFileContentsEqual('<?php namespace App\\Http\\Controllers;


class TestsController extends Controller {

	const MODEL = "App\\Test";

	use RESTActions;

}
');
$I->deleteFile('./app/Http/Controllers/TestsController.php');
$I->wantTo('generate a RESTful controller with full model name and routes');
$I->runShellCommand('php artisan wn:controller "App\\Models\\Category"');
$I->seeInShellOutput('CategoriesController generated');
$I->seeFileFound('./app/Http/Controllers/CategoriesController.php');
$I->openFile('./app/Http/Controllers/CategoriesController.php');
$I->seeFileContentsEqual('<?php namespace App\\Http\\Controllers;


class CategoriesController extends Controller {

	const MODEL = "App\\Models\\Category";
    unlink('tests/codeception/actualTestResults/FooForm.php');
}
if (false !== file_exists('app/Forms/FooForm.php')) {
    unlink('app/Forms/FooForm.php');
}
$I = new AcceptanceTester($scenario);
$I->wantTo('create a form taking into account all valid parameters');
$baseDir = '../../../';
$vendorDir = './vendor/bin/';
/*
 * Test all options
 */
$command = 'php ' . $baseDir . 'artisan generate:form Foo --dir="tests/codeception/actualTestResults" --namespace="Bar" --rules="baz|required|email & qux|between:3,6"';
$I->runShellCommand($command);
$I->seeInShellOutput('Form has been saved to');
$I->openFile('tests/codeception/actualTestResults/FooForm.php');
$I->canSeeFileContentsEqual(file_get_contents('tests/codeception/expectedTestResults/FooForm-with-all-options.php'));
unlink('tests/codeception/actualTestResults/FooForm.php');
/*
 * Test no namespace and no rules
 */
$command = 'php ' . $baseDir . 'artisan generate:form Foo --dir="tests/codeception/actualTestResults"';
$I->runShellCommand($command);
$I->seeInShellOutput('Form has been saved to');
$I->openFile('tests/codeception/actualTestResults/FooForm.php');
$I->canSeeFileContentsEqual(file_get_contents('tests/codeception/expectedTestResults/FooForm-with-no-namespace-and-no-rules.php'));
unlink('tests/codeception/actualTestResults/FooForm.php');
/*
 * Test no namespace and no rules or a directory
 */
$command = 'php ' . $baseDir . 'artisan generate:form Foo';