Files
gorilla/tests/Feature/ParseEntriesTest.php
2026-01-12 12:37:16 +01:00

87 lines
2.7 KiB
PHP

<?php
namespace Tests\Feature;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
class ParseEntriesTest extends TestCase
{
private string $filePath;
private string $outputDir;
public function setUp(): void {
parent::setUp();
$this->filePath = base_path('./test_data.json');
$this->outputDir = base_path('./output');
if(File::exists($this->outputDir)) {
File::deleteDirectory($this->outputDir);
}
}
public function tearDown(): void
{
parent::tearDown();
if(File::exists($this->outputDir)) {
File::deleteDirectory($this->outputDir);
}
if(File::exists($this->filePath)) {
File::delete($this->filePath);
}
}
public function testCommandFailOnNonexistentFile()
{
$this->artisan('app:parse-entries', ['file' => 'nonexistent.csv'])
->expectsOutput('File not found')
->assertExitCode(0);
}
public function testCommandSuccessOnExistingFile()
{
$data = [
[
'number' => 1,
'description' => 'Test zgłoszenie awarii pilne',
'dueDate' => '2020-01-01',
'phone' => '123-456-789',
],
[
'number' => 2,
'description' => 'Test przegląd okresowy',
'dueDate' => '2020-01-02',
'phone' => '123-456-789',
],
[
'number' => 3,
'description' => 'Test przegląd okresowy',
'dueDate' => '2020-01-02',
'phone' => '123-456-789',
],
];
File::put($this->filePath, json_encode($data));
$this->artisan('app:parse-entries', ['file' => $this->filePath])
->expectsOutput('File loaded')
->expectsOutput('Start parsing...')
->expectsOutput('Parsing finished.')
->expectsOutput('Total entries: 3')
->expectsOutput('Success entries: 2')
->expectsOutput('Fail entries: 1');
$this->assertTrue(File::exists($this->outputDir.'/inspections.json'));
$this->assertTrue(File::exists($this->outputDir.'/failureReports.json'));
$this->assertTrue(File::exists($this->outputDir.'/unprocessable.json'));
$inspections = json_decode(File::get($this->outputDir.'/inspections.json'), true);
$this->assertCount(1, $inspections);
$failureReports = json_decode(File::get($this->outputDir.'/failureReports.json'), true);
$this->assertCount(1, $failureReports);
$unprocessable = json_decode(File::get($this->outputDir.'/unprocessable.json'), true);
$this->assertCount(1, $unprocessable);
}
}