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' => '',
],
];
}
}

10
tests/TestCase.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
//
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\Unit;
use App\Service\Entities\FailureReport;
use App\Service\Entities\InputEntity;
use PHPUnit\Framework\TestCase;
class FailureReportTest extends TestCase
{
public function testPriorityIsKrytyczny() {
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test Bardzo pilne zgłoszenie',
]);
$inspection = new FailureReport($inputEntity);
$this->assertEquals('krytyczny', $inspection->priority->value);
}
public function testPriorityIsWysoki() {
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test pilne zgłoszenie',
]);
$inspection = new FailureReport($inputEntity);
$this->assertEquals('wysoki', $inspection->priority->value);
}
public function testPriorityIsNormalny() {
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test zwykłego zgłoszenia',
]);
$inspection = new FailureReport($inputEntity);
$this->assertEquals('normalny', $inspection->priority->value);
}
public function testStatusIsTermin()
{
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-01',
]);
$inspection = new FailureReport($inputEntity);
$this->assertEquals('termin', $inspection->status->value);
}
public function testStatusIsNowy()
{
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test',
'dueDate' => '',
]);
$inspection = new FailureReport($inputEntity);
$this->assertEquals('nowy', $inspection->status->value);
}
}

View File

@@ -0,0 +1,227 @@
<?php
namespace Tests\Unit;
use App\Service\Entities\InputEntity;
use PHPUnit\Framework\TestCase;
class InputEntityTest extends TestCase
{
public function testValidatesCorrectNumber()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '',
];
$entity = new InputEntity($data);
$this->assertEquals(1, $entity->number);
}
public function testThrowsExceptionOnInvalidNumber()
{
$data = [
'number' => 'abc',
'description' => 'Test',
'dueDate' => '',
'phone' => '',
];
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid number');
new InputEntity($data);
}
public function testThrowsExceptionOnMissedNumberField()
{
$data = [
'description' => 'Test',
'dueDate' => '',
'phone' => '',
];
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid number');
new InputEntity($data);
}
public function testValidatesCorrectDescription()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '',
];
$entity = new InputEntity($data);
$this->assertEquals('Test', $entity->description);
}
public function testThrowsExceptionOnEmptyDescription()
{
$data = [
'number' => 1,
'description' => '',
'dueDate' => '',
'phone' => '',
];
$this->expectException(\InvalidArgumentException::class);
new InputEntity($data);
}
public function testThrowsExceptionOnMissedDescriptionField()
{
$data = [
'number' => 1,
'dueDate' => '',
'phone' => '',
];
$this->expectException(\InvalidArgumentException::class);
new InputEntity($data);
}
public function testValidatesCorrectDate()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-01',
'phone' => '',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('2020-01-01', $inputEntity->date->format('Y-m-d'));
}
public function testValidatesCorrectDateHour()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-01 12:00',
'phone' => '',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('2020-01-01 12:00', $inputEntity->date->format('Y-m-d H:i'));
}
public function testValidatesCorrectDateHourSecond()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-01 12:00:01',
'phone' => '',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('2020-01-01 12:00:01', $inputEntity->date->format('Y-m-d H:i:s'));
}
public function testAllowNullDate()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => null,
'phone' => '',
];
$inputEntity = new InputEntity($data);
$this->assertNull($inputEntity->date);
}
public function testAllowEmptyDate()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '',
];
$inputEntity = new InputEntity($data);
$this->assertNull($inputEntity->date);
}
public function testThrowsExceptionOnInvalidDate()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-01 12:00:aa',
'phone' => '',
];
$this->expectException(\InvalidArgumentException::class);
new InputEntity($data);
}
public function testValidatesCorrectPhone()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '123456789',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('123456789', $inputEntity->phone);
}
public function testValidatesCorrectPhoneWithDashes()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '123-456-789',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('123-456-789', $inputEntity->phone);
}
public function testValidatesCorrectPhoneWithSpaces()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '123 456 789',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('123 456 789', $inputEntity->phone);
}
public function testValidatesCorrectPhoneWithContryCode()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '+48 123 456 789',
];
$inputEntity = new InputEntity($data);
$this->assertEquals('+48 123 456 789', $inputEntity->phone);
}
public function testThrowsExceptionOnInvalidPhone()
{
$data = [
'number' => 1,
'description' => 'Test',
'dueDate' => '',
'phone' => '4',
];
$this->expectException(\InvalidArgumentException::class);
new InputEntity($data);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\Unit;
use App\Service\Entities\InputEntity;
use App\Service\Entities\Inspection;
use Tests\TestCase;
class InspectionTest extends TestCase
{
public function testStatusIsNowyWhenDateIsEmpty()
{
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test',
'dueDate' => '',
]);
$inspection = new Inspection($inputEntity);
$this->assertEquals('nowy', $inspection->status->value);
}
public function testStatusIsZaplanowanyWhenDateIsNotEmpty()
{
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-01',
]);
$inspection = new Inspection($inputEntity);
$this->assertEquals('zaplanowany', $inspection->status->value);
}
public function testThereIsWeekOfYearWhenDateIsSet()
{
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test',
'dueDate' => '2020-01-07',
]);
$inspection = new Inspection($inputEntity);
$this->assertNotNull($inspection->serviceWeek);
$this->assertEquals(2, $inspection->serviceWeek);
}
public function testThereIsNoWeekOfYearWhenDateIsNotSet()
{
$inputEntity = new InputEntity([
'number' => 1,
'description' => 'Test',
'dueDate' => '',
]);
$inspection = new Inspection($inputEntity);
$this->assertNull($inspection->serviceWeek);
}
}