Skip to content

Commit

Permalink
Write initial tests for export command
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaaloko committed Sep 9, 2016
1 parent 7442266 commit aaa40d8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.phar
composer.lock
.idea
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"phpunit/phpunit" : "^4.8 || ^5.0",
"orchestra/testbench": "3.3.x-dev",
"orchestra/database": "3.3.x-dev",
"mockery/mockery": "~0.9.4"
"mockery/mockery": "~0.9.4",
"mikey179/vfsStream": "1.*"
},
"autoload": {
"psr-4": {
Expand Down
67 changes: 67 additions & 0 deletions tests/ExportCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

class ExportCommandTest extends TestCase
{
public function testCreatesExcelFile()
{
$this->createTempFiles([
'en' => ['user' => "<?php\n return['address' => 'Address', 'contact' => ['cellphone' => 'Mobile']];"],
'es' => ['user' => "<?php\n return['address' => 'Dirección', 'contact' => ['cellphone' => 'Movil']];"],
]);

$this->artisan('langman:export');

$exportedFilePath = $this->app['config']['langman.exports_path'] . '/' . date('Y_m_d_His') . '_langman.xlsx';

$excelRows = $this->getExcelFileContents($exportedFilePath);

$headerRow = $excelRows[1];
$contentRows = [$excelRows[2], $excelRows[3]];

$this->assertFileExists($exportedFilePath);
$this->assertHeaderRow($headerRow);
$this->assertContentRows($contentRows);
}

/**
* @param $exportedFilePath
* @return array
*/
protected function getExcelFileContents($exportedFilePath)
{
$excelObj = \PHPExcel_IOFactory::load($exportedFilePath);
$rows = $excelObj->getActiveSheet()->toArray('', true, true, true);

return $rows;
}

/**
* @param $headerRow
*/
protected function assertHeaderRow($headerRow)
{
$this->assertEquals($headerRow['A'], 'Language File');
$this->assertEquals($headerRow['B'], 'Key');
$this->assertEquals($headerRow['C'], 'en');
$this->assertEquals($headerRow['D'], 'es');
}

/**
* @param $row1
* @param $row2
*/
protected function assertContentRows($contentRows)
{
$row1 = $contentRows[0];
$this->assertEquals($row1['A'], 'user');
$this->assertEquals($row1['B'], 'address');
$this->assertEquals($row1['C'], 'Address');
$this->assertEquals($row1['D'], 'Dirección');

$row2 = $contentRows[1];
$this->assertEquals($row2['A'], 'user');
$this->assertEquals($row2['B'], 'contact.cellphone');
$this->assertEquals($row2['C'], 'Mobile');
$this->assertEquals($row2['D'], 'Movil');
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protected function getPackageProviders($app)
protected function getEnvironmentSetUp($app)
{
$app['config']->set('langman.path', __DIR__.'/temp');
$app['config']->set('langman.exports_path', __DIR__.'/temp_exports_path');
$app['config']->set('view.paths', [__DIR__.'/views_temp']);
}

Expand All @@ -20,13 +21,15 @@ public function setUp()
parent::setUp();

exec('rm -rf '.__DIR__.'/temp/*');
exec('rm -rf '.__DIR__.'/temp_exports_path/*');
}

public function tearDown()
{
parent::tearDown();

exec('rm -rf '.__DIR__.'/temp/*');
exec('rm -rf '.__DIR__.'/temp_exports_path/*');

$this->consoleOutput = '';
}
Expand Down

0 comments on commit aaa40d8

Please sign in to comment.