This commit is contained in:
johny
2026-01-12 12:37:16 +01:00
commit 516e51e0e9
76 changed files with 11962 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
<?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);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Tests\Feature;
use App\Service\Service;
use Tests\TestCase;
class ServiceTest extends TestCase
{
public function testEntriesAreClassifiedCorrectly()
{
$data = $this->getTestData();
$service = new Service();
foreach ($data as $entry) {
$service->processEntry($entry);
}
$this->assertEquals(2, $service->getInspectionsCount());
$this->assertEquals(2, $service->getFailureReportsCount());
}
public function testDuplicatedEntryThrowsException()
{
$data = $this->getTestData();
$service = new Service();
$this->expectException(\InvalidArgumentException::class);
$service->processEntry($data[0]);
$service->processEntry($data[0]);
}
public function testDuplicatedEntryIsAddedToUnprocessableList()
{
$data = $this->getTestData();
$service = new Service();
$service->processEntry($data[0]);
try {
$service->processEntry($data[0]);
} catch (\Exception $e) {}
$this->assertCount(1, $service->getUnprocessable());
}
private function getTestData()
{
return [
[
'number' => 1,
'description' => 'Test przegląd',
'dueDate' => '',
'phone' => '',
],
[
'number' => 2,
'description' => 'Awaria systemu',
'dueDate' => '',
'phone' => '',
],
[
'number' => 3,
'description' => 'Test przegląd 2',
'dueDate' => '',
'phone' => '',
],
[
'number' => 4,
'description' => 'Awaria systemu 2',
'dueDate' => '',
'phone' => '',
],
];
}
}