Skip to content
Permalink
Browse files

Implemented a composer dump-autoload feature

  • Loading branch information...
tihomiro committed Dec 21, 2016
1 parent 668fbde commit 677bc868ce29b355ab067f1cd631cabcb04fa462
Showing with 43 additions and 8 deletions.
  1. +8 −0 README.md
  2. +6 −1 composer.json
  3. +2 −1 phpunit.xml
  4. +15 −3 src/Orangehill/Iseed/Iseed.php
  5. +6 −2 src/Orangehill/Iseed/IseedCommand.php
  6. +6 −1 tests/IseedTest.php
@@ -53,6 +53,14 @@ The following command will overwrite `UsersTableSeeder.php` if it already exists
php artisan iseed users --force
```

### dumpauto
Optional boolean parameter that controls the execution of `composer dump-autoload` command. Defaults to true.

Example that will stop `composer dump-autoload` from execution:
```
php artisan iseed users --dumpauto=false
```

### clean
Optional parameter which will clean `app/database/seeds/DatabaseSeeder.php` before creating new seed class.

@@ -24,5 +24,10 @@
"src/Orangehill/Iseed/Exceptions.php"
]
},
"autoload-dev": {
"psr-4": {
"Orangehill\\Iseed\\Tests\\": "tests"
}
},
"minimum-stability": "dev"
}
}
@@ -9,10 +9,11 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
@@ -3,6 +3,7 @@
namespace Orangehill\Iseed;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Support\Facades\Config;
class Iseed
@@ -31,9 +32,16 @@ class Iseed
*/
private $indentCharacter = " ";
public function __construct(Filesystem $filesystem = null)
/**
* @var Composer
*/
private $composer;
public function __construct(Filesystem $filesystem = null, Composer $composer = null)
{
$this->files = $filesystem ?: new Filesystem;
$this->composer = $composer ?: new Composer($this->files);
// $this->composer = app()['composer'];
}
public function readStubFile($file)
@@ -52,7 +60,7 @@ public function readStubFile($file)
* @return bool
* @throws Orangehill\Iseed\TableNotFoundException
*/
public function generateSeed($table, $database = null, $max = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null)
public function generateSeed($table, $database = null, $max = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true)
{
if (!$database) {
$database = config('database.default');
@@ -75,7 +83,6 @@ public function generateSeed($table, $database = null, $max = 0, $exclude = null
$className = $this->generateClassName($table);
// Get template for a seed file contents
// $stub = $this->files->get($this->getStubPath() . '/seed.stub');
$stub = $this->readStubFile($this->getStubPath() . '/seed.stub');
// Get a seed folder path
@@ -98,6 +105,11 @@ public function generateSeed($table, $database = null, $max = 0, $exclude = null
// Save a populated stub
$this->files->put($seedsPath, $seedContent);
// Run composer dump-auto
if ($dumpAuto) {
$this->composer->dumpAutoloads();
}
// Update the DatabaseSeeder.php file
return $this->updateDatabaseSeederRunMethod($className) !== false;
}
@@ -49,6 +49,7 @@ public function fire()
$exclude = explode(",", $this->option('exclude'));
$prerunEvents = explode(",", $this->option('prerun'));
$postrunEvents = explode(",", $this->option('postrun'));
$dumpAuto = intval($this->option('dumpauto'));
if ($chunkSize < 1) {
$chunkSize = null;
@@ -79,7 +80,8 @@ public function fire()
$chunkSize,
$exclude,
$prerunEvent,
$postrunEvent
$postrunEvent,
$dumpAuto
),
$table
);
@@ -95,7 +97,8 @@ public function fire()
$chunkSize,
$exclude,
$prerunEvent,
$postrunEvent
$postrunEvent,
$dumpAuto
),
$table
);
@@ -132,6 +135,7 @@ protected function getOptions()
array('exclude', null, InputOption::VALUE_OPTIONAL, 'exclude columns', null),
array('prerun', null, InputOption::VALUE_OPTIONAL, 'prerun event name', null),
array('postrun', null, InputOption::VALUE_OPTIONAL, 'postrun event name', null),
array('dumpauto', null, InputOption::VALUE_OPTIONAL, 'run composer dump-autoload', true),
);
}
@@ -27,6 +27,8 @@ public function readStubFile($file)
public function testPopulatesStub()
{
$composer = m::mock('Composer')->makePartial();
$productionStub = $this->readStubFile(static::$stubsDir . '/seed.stub');
$testStubs = array(
@@ -2089,6 +2091,7 @@ public function testPopulatesStub()
$output = $iSeed->populateStub('test_class', $productionStub, 'test_table', $stub['data'], 500);
$this->assertEquals($stub['content'], $output, "Stub {$key} is not what it's expected to be.");
}
}
/**
@@ -2131,7 +2134,8 @@ public function testCanGetStubPath()
public function testCanGenerateSeed()
{
$file = m::mock('Illuminate\Filesystem\Filesystem')->makePartial();
$mocked = m::mock('Orangehill\Iseed\Iseed', array($file))->makePartial();
$composer = m::mock('Illuminate\Support\Composer', array($file))->makePartial();
$mocked = m::mock('Orangehill\Iseed\Iseed', array($file, $composer))->makePartial();
$mocked->shouldReceive('readStubFile')
->once()
->with(substr(__DIR__, 0, -5) . 'src' . DIRECTORY_SEPARATOR . 'Orangehill' . DIRECTORY_SEPARATOR . 'Iseed' . DIRECTORY_SEPARATOR . 'Stubs' . DIRECTORY_SEPARATOR . 'seed.stub');
@@ -2145,6 +2149,7 @@ public function testCanGenerateSeed()
$mocked->shouldReceive('getPath')->once()->with('ClassName', 'seedPath')->andReturn('seedPath');
$mocked->shouldReceive('populateStub')->once()->andReturn('populatedStub');
$mocked->shouldReceive('updateDatabaseSeederRunMethod')->once()->with('ClassName')->andReturn(true);
$composer->shouldReceive('dumpAutoloads')->once();
$mocked->generateSeed('tablename', 'database', 'numOfRows');
}
}

0 comments on commit 677bc86

Please sign in to comment.
You can’t perform that action at this time.