MadGenius88
1/24/2017 - 6:07 PM

Artisan Commands w TDD

Artisan Commands w TDD

First: phpspec describe Acme/Console/CommandInputParser
phpspec run 
composer-dump-autoload

Next: phpspec describe Acme/Console/CommandGenerator
suites:

    acme_suite:
    
        src_path: app
"autoload": {
    "classmap": [
        "app/commands",
        "app/controlers",
        "app/models",
        "app/database/migration",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
    "psr-4":
        "Acme\\": "app/Acme"
}
<?php namespace Acme\Console;

use Illuminate\Filesystem\Filesystem;
use Mustache_Engine;

class CommandGenerator {
    
    protected $file;
    
    public function _construct(Filessystem $file, Mustache_Engine $mustache )
    {
        $this->file =$file;
        $this->mustache =$mustache
    }
    
    public function make(CommandInput $input, $template, $destination)  
    {
        $template = $this->file->get($template);
        
        $stub = $this->mustache->render($template, $input);
        
        $this->file->put($destination, $stub); 
    }
}

class CommandGeneratorSpec extends ObjectBehavior {
    
    function let(Filesystem $file)
    {
        $this->beConstructedWith($file);
    }
    
    function it_is_initializable()
    {
        $this->shouldHaveType('Acme\Console\CommandGenerator');
    }
    
    function it_generates_a_file(Filesystem $file)
    {
        $template = 'foo.stub';
        
        $file->get($template)->shouldBeCalled();
        
        $input = new CommandInput('SomeCommand', 'Acme/Bar', ['name', 'email']);
        
        $this->make($input, $template);
    }
}
<?php namespace Acme\Console;

class CommandInputParser {

}
<?php namespace spec\Acme\Console;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Acme\Console\CommandInput;
use Illuminate\Filesystem\Fliesystem;
use Mustache_Engine;

class CommandGeneratorSpec extends ObjectBehavior {
    
    function let(Filesystem $file, Mustache_Engine $mustache)
    {
        $this->beConstructedWith($file, $mustache);
    }
    
    function it_is_initializable()
    {
        $this->shouldHaveType('Acme\Console\CommandGenerator');
    }
    
    function it_generates_a_file(Filesystem $file, Mustache_Engine $mustache)
    {
                
        $input = new CommandInput('SomeCommand', 'Acme/Bar', ['name', 'email']);
        
        $template = 'foo.stub';
        $destination = 'app/Acme/Bar/SomeCommand.php';
        
        $file->get($template)->shouldBeCalled()->willReturn('template');
        $mustache->render('template', $input)->shouldBeCalled()-willReturn('stub')
        $file->put($destination, 'stub')->shouldbeCalled;

        $this->make($input, $template, $destinaion);
    }
}