Skip to content

Commit

Permalink
PHP_EOL Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tihomiro committed Dec 21, 2016
1 parent aeb1453 commit 4fa66df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/Orangehill/Iseed/Iseed.php
Expand Up @@ -20,7 +20,7 @@ class Iseed
*
* @var string
*/
private $newLineCharacter = "\r\n";
private $newLineCharacter = PHP_EOL;

/**
* Desired indent for the code.
Expand All @@ -36,6 +36,12 @@ public function __construct(Filesystem $filesystem = null)
$this->files = $filesystem ?: new Filesystem;
}

public function readStubFile($file)
{
$buffer = file($file, FILE_IGNORE_NEW_LINES);
return implode(PHP_EOL, $buffer);
}

/**
* Generates a seed file.
* @param string $table
Expand Down Expand Up @@ -69,7 +75,8 @@ public function generateSeed($table, $database = null, $max = 0, $prerunEvent =
$className = $this->generateClassName($table);

// Get template for a seed file contents
$stub = $this->files->get($this->getStubPath() . '/seed.stub');
// $stub = $this->files->get($this->getStubPath() . '/seed.stub');
$stub = $this->readStubFile($this->getStubPath() . '/seed.stub');

// Get a seed folder path
$seedPath = $this->getSeedPath();
Expand Down
52 changes: 29 additions & 23 deletions tests/IseedTest.php
Expand Up @@ -10,26 +10,32 @@ class IseedTest extends PHPUnit_Framework_TestCase

public function __construct()
{
static::$stubsDir = __DIR__.'/../src/Orangehill/Iseed/Stubs';
static::$testStubsDir = __DIR__.'/Stubs';
static::$stubsDir = __DIR__ . '/../src/Orangehill/Iseed/Stubs';
static::$testStubsDir = __DIR__ . '/Stubs';
}

public function tearDown()
{
m::close();
}

public function readStubFile($file)
{
$buffer = file($file, FILE_IGNORE_NEW_LINES);
return implode(PHP_EOL, $buffer);
}

public function testPopulatesStub()
{
$productionStub = file_get_contents(static::$stubsDir.'/seed.stub');
$productionStub = $this->readStubFile(static::$stubsDir . '/seed.stub');

$testStubs = array(
'blank' => array(
'content' => file_get_contents(static::$testStubsDir.'/seed_blank.stub'),
'data' => array()
'content' => $this->readStubFile(static::$testStubsDir . '/seed_blank.stub'),
'data' => array(),
),
'entries_5' => array(
'content' => file_get_contents(static::$testStubsDir.'/seed_5.stub'),
'content' => $this->readStubFile(static::$testStubsDir . '/seed_5.stub'),
'data' => array(
array(
'id' => '1',
Expand All @@ -50,11 +56,11 @@ public function testPopulatesStub()
array(
'id' => '5',
'time' => '2013-10-18 14:28:51',
)
)
),
),
),
'entries_505' => array(
'content' => file_get_contents(static::$testStubsDir.'/seed_505.stub'),
'content' => $this->readStubFile(static::$testStubsDir . '/seed_505.stub'),
'data' => array(
array(
'id' => '1',
Expand Down Expand Up @@ -2075,8 +2081,8 @@ public function testPopulatesStub()
array(
'id' => '505',
'time' => '2013-10-18 14:31:24',
)
)));
),
)));

$iSeed = new Orangehill\Iseed\Iseed();
foreach ($testStubs as $key => $stub) {
Expand All @@ -2098,40 +2104,40 @@ public function testTableNotFoundException()

public function testRepacksSeedData()
{
$data = array(
$data = array(
array('id' => '1', 'name' => 'one'),
array('id' => '2', 'name' => 'two')
array('id' => '2', 'name' => 'two'),
);
$iseed = new Orangehill\Iseed\Iseed();
$iseed = new Orangehill\Iseed\Iseed();
$output = $iseed->repackSeedData($data);
$this->assertEquals(json_encode($data), json_encode($output));
}

public function testCanGenerateClassName()
{
$iseed = new Orangehill\Iseed\Iseed();
$iseed = new Orangehill\Iseed\Iseed();
$output = $iseed->generateClassName('tablename');
$this->assertEquals('TablenameTableSeeder', $output);
}

public function testCanGetStubPath()
{
$iseed = new Orangehill\Iseed\Iseed();
$output = $iseed->getStubPath();
$expected = substr(__DIR__, 0, -5).'src'.DIRECTORY_SEPARATOR.'Orangehill'.DIRECTORY_SEPARATOR.'Iseed'.DIRECTORY_SEPARATOR.'Stubs';
$iseed = new Orangehill\Iseed\Iseed();
$output = $iseed->getStubPath();
$expected = substr(__DIR__, 0, -5) . 'src' . DIRECTORY_SEPARATOR . 'Orangehill' . DIRECTORY_SEPARATOR . 'Iseed' . DIRECTORY_SEPARATOR . 'Stubs';
$this->assertEquals($expected, $output);
}

public function testCanGenerateSeed()
{
$file = m::mock('Illuminate\Filesystem\Filesystem')->makePartial();
$file->shouldReceive('get')
$file = m::mock('Illuminate\Filesystem\Filesystem')->makePartial();
$mocked = m::mock('Orangehill\Iseed\Iseed', array($file))->makePartial();
$mocked->shouldReceive('readStubFile')
->once()
->with(substr(__DIR__, 0, -5).'src'.DIRECTORY_SEPARATOR.'Orangehill'.DIRECTORY_SEPARATOR.'Iseed'.DIRECTORY_SEPARATOR.'Stubs'.DIRECTORY_SEPARATOR.'seed.stub');
->with(substr(__DIR__, 0, -5) . 'src' . DIRECTORY_SEPARATOR . 'Orangehill' . DIRECTORY_SEPARATOR . 'Iseed' . DIRECTORY_SEPARATOR . 'Stubs' . DIRECTORY_SEPARATOR . 'seed.stub');
$file->shouldReceive('put')
->once()
->with('seedPath', 'populatedStub');
$mocked = m::mock('Orangehill\Iseed\Iseed', array($file))->makePartial();
$mocked->shouldReceive('hasTable')->once()->andReturn(true);
$mocked->shouldReceive('getData')->once()->andReturn(array());
$mocked->shouldReceive('generateClassName')->once()->andReturn('ClassName');
Expand All @@ -2141,4 +2147,4 @@ public function testCanGenerateSeed()
$mocked->shouldReceive('updateDatabaseSeederRunMethod')->once()->with('ClassName')->andReturn(true);
$mocked->generateSeed('tablename', 'database', 'numOfRows');
}
}
}

0 comments on commit 4fa66df

Please sign in to comment.