Skip to content

Commit aaa40d8

Browse files
committed
Write initial tests for export command
1 parent 7442266 commit aaa40d8

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
composer.phar
33
composer.lock
4+
.idea

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"phpunit/phpunit" : "^4.8 || ^5.0",
2323
"orchestra/testbench": "3.3.x-dev",
2424
"orchestra/database": "3.3.x-dev",
25-
"mockery/mockery": "~0.9.4"
25+
"mockery/mockery": "~0.9.4",
26+
"mikey179/vfsStream": "1.*"
2627
},
2728
"autoload": {
2829
"psr-4": {

tests/ExportCommandTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
class ExportCommandTest extends TestCase
4+
{
5+
public function testCreatesExcelFile()
6+
{
7+
$this->createTempFiles([
8+
'en' => ['user' => "<?php\n return['address' => 'Address', 'contact' => ['cellphone' => 'Mobile']];"],
9+
'es' => ['user' => "<?php\n return['address' => 'Dirección', 'contact' => ['cellphone' => 'Movil']];"],
10+
]);
11+
12+
$this->artisan('langman:export');
13+
14+
$exportedFilePath = $this->app['config']['langman.exports_path'] . '/' . date('Y_m_d_His') . '_langman.xlsx';
15+
16+
$excelRows = $this->getExcelFileContents($exportedFilePath);
17+
18+
$headerRow = $excelRows[1];
19+
$contentRows = [$excelRows[2], $excelRows[3]];
20+
21+
$this->assertFileExists($exportedFilePath);
22+
$this->assertHeaderRow($headerRow);
23+
$this->assertContentRows($contentRows);
24+
}
25+
26+
/**
27+
* @param $exportedFilePath
28+
* @return array
29+
*/
30+
protected function getExcelFileContents($exportedFilePath)
31+
{
32+
$excelObj = \PHPExcel_IOFactory::load($exportedFilePath);
33+
$rows = $excelObj->getActiveSheet()->toArray('', true, true, true);
34+
35+
return $rows;
36+
}
37+
38+
/**
39+
* @param $headerRow
40+
*/
41+
protected function assertHeaderRow($headerRow)
42+
{
43+
$this->assertEquals($headerRow['A'], 'Language File');
44+
$this->assertEquals($headerRow['B'], 'Key');
45+
$this->assertEquals($headerRow['C'], 'en');
46+
$this->assertEquals($headerRow['D'], 'es');
47+
}
48+
49+
/**
50+
* @param $row1
51+
* @param $row2
52+
*/
53+
protected function assertContentRows($contentRows)
54+
{
55+
$row1 = $contentRows[0];
56+
$this->assertEquals($row1['A'], 'user');
57+
$this->assertEquals($row1['B'], 'address');
58+
$this->assertEquals($row1['C'], 'Address');
59+
$this->assertEquals($row1['D'], 'Dirección');
60+
61+
$row2 = $contentRows[1];
62+
$this->assertEquals($row2['A'], 'user');
63+
$this->assertEquals($row2['B'], 'contact.cellphone');
64+
$this->assertEquals($row2['C'], 'Mobile');
65+
$this->assertEquals($row2['D'], 'Movil');
66+
}
67+
}

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ protected function getPackageProviders($app)
1212
protected function getEnvironmentSetUp($app)
1313
{
1414
$app['config']->set('langman.path', __DIR__.'/temp');
15+
$app['config']->set('langman.exports_path', __DIR__.'/temp_exports_path');
1516
$app['config']->set('view.paths', [__DIR__.'/views_temp']);
1617
}
1718

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

2223
exec('rm -rf '.__DIR__.'/temp/*');
24+
exec('rm -rf '.__DIR__.'/temp_exports_path/*');
2325
}
2426

2527
public function tearDown()
2628
{
2729
parent::tearDown();
2830

2931
exec('rm -rf '.__DIR__.'/temp/*');
32+
exec('rm -rf '.__DIR__.'/temp_exports_path/*');
3033

3134
$this->consoleOutput = '';
3235
}

0 commit comments

Comments
 (0)