Skip to content

Commit

Permalink
Add test for Fixometer Export.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Dec 20, 2021
1 parent 9ad5139 commit 8597cf4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function devices(Request $request)
$uEmissionratio = \App\Helpers\LcaStats::getEmissionRatioUnpowered();

// Create CSV
$filename = 'devices.csv';
$filename = base_path() . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'devices.csv';
$file = fopen($filename, 'w+');

// Do not include model column
Expand Down
47 changes: 47 additions & 0 deletions tests/Feature/Fixometer/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Feature\Fixometer;

use App\Category;
use App\Device;
use DB;
use Hash;
use Mockery;
Expand Down Expand Up @@ -56,4 +58,49 @@ public function testPageLoads()
],
]);
}

public function testExport() {
$this->loginAsTestUser();

DB::statement('SET foreign_key_checks=0');
Category::truncate();
DB::statement('SET foreign_key_checks=1');
factory(Category::class)->create([
'idcategories' => 1,
'revision' => 1,
'name' => 'powered non-misc',
'powered' => 1,
'weight' => 4,
'footprint' => 14.4,
]);

factory(Device::class)->create([
'category' => 1,
'category_creation' => 1,
'repair_status' => 0
]);
factory(Device::class)->create([
'category' => 1,
'category_creation' => 1,
'repair_status' => env('DEVICE_FIXED')
]);

$response = $this->get('/export/devices');

$this->assertTrue($response->headers->get('content-disposition') == 'attachment; filename=devices.csv');

// Bit hacky, but grab the file that was created. Can't find a way to do this in Laravel easily, though it's
// probably possible using mocking.
$filename = base_path() . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'devices.csv';
$fh = fopen($filename, 'r');

# Skip headers.
fgetcsv($fh);
$row2 = fgetcsv($fh);
$row3 = fgetcsv($fh);
$this->assertEquals(0, $row2[9]);
$this->assertEquals(0, $row2[10]);
$this->assertEquals(4, $row3[9]);
$this->assertEquals(7.2, $row3[10]);
}
}

0 comments on commit 8597cf4

Please sign in to comment.