Part-DB-server/tests/Entity/BulkInfoProviderImportJobPartTest.php

302 lines
11 KiB
PHP
Raw Normal View History

2025-08-05 03:17:55 +02:00
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\Entity;
use App\Entity\InfoProviderSystem\BulkImportPartStatus;
use App\Entity\InfoProviderSystem\BulkInfoProviderImportJob;
use App\Entity\InfoProviderSystem\BulkInfoProviderImportJobPart;
2025-08-05 03:17:55 +02:00
use App\Entity\Parts\Part;
use PHPUnit\Framework\TestCase;
class BulkInfoProviderImportJobPartTest extends TestCase
{
private BulkInfoProviderImportJob $job;
private Part $part;
private BulkInfoProviderImportJobPart $jobPart;
protected function setUp(): void
{
$this->job = $this->createMock(BulkInfoProviderImportJob::class);
$this->part = $this->createMock(Part::class);
2025-08-05 03:17:55 +02:00
$this->jobPart = new BulkInfoProviderImportJobPart($this->job, $this->part);
}
public function testConstructor(): void
{
$this->assertSame($this->job, $this->jobPart->getJob());
$this->assertSame($this->part, $this->jobPart->getPart());
$this->assertEquals(BulkImportPartStatus::PENDING, $this->jobPart->getStatus());
$this->assertNull($this->jobPart->getReason());
$this->assertNull($this->jobPart->getCompletedAt());
}
public function testGetAndSetJob(): void
{
$newJob = $this->createMock(BulkInfoProviderImportJob::class);
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->setJob($newJob);
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertSame($newJob, $this->jobPart->getJob());
}
public function testGetAndSetPart(): void
{
$newPart = $this->createMock(Part::class);
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->setPart($newPart);
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertSame($newPart, $this->jobPart->getPart());
}
public function testGetAndSetStatus(): void
{
$result = $this->jobPart->setStatus(BulkImportPartStatus::COMPLETED);
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::COMPLETED, $this->jobPart->getStatus());
}
public function testGetAndSetReason(): void
{
$reason = 'Test reason';
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->setReason($reason);
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals($reason, $this->jobPart->getReason());
}
public function testGetAndSetCompletedAt(): void
{
$completedAt = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->setCompletedAt($completedAt);
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertSame($completedAt, $this->jobPart->getCompletedAt());
}
public function testMarkAsCompleted(): void
{
$beforeTime = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->markAsCompleted();
2025-08-05 03:17:55 +02:00
$afterTime = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::COMPLETED, $this->jobPart->getStatus());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
$this->assertGreaterThanOrEqual($beforeTime, $this->jobPart->getCompletedAt());
$this->assertLessThanOrEqual($afterTime, $this->jobPart->getCompletedAt());
}
public function testMarkAsSkipped(): void
{
$reason = 'Skipped for testing';
$beforeTime = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->markAsSkipped($reason);
2025-08-05 03:17:55 +02:00
$afterTime = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::SKIPPED, $this->jobPart->getStatus());
$this->assertEquals($reason, $this->jobPart->getReason());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
$this->assertGreaterThanOrEqual($beforeTime, $this->jobPart->getCompletedAt());
$this->assertLessThanOrEqual($afterTime, $this->jobPart->getCompletedAt());
}
public function testMarkAsSkippedWithoutReason(): void
{
$result = $this->jobPart->markAsSkipped();
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::SKIPPED, $this->jobPart->getStatus());
$this->assertEquals('', $this->jobPart->getReason());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
}
public function testMarkAsFailed(): void
{
$reason = 'Failed for testing';
$beforeTime = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->markAsFailed($reason);
2025-08-05 03:17:55 +02:00
$afterTime = new \DateTimeImmutable();
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::FAILED, $this->jobPart->getStatus());
$this->assertEquals($reason, $this->jobPart->getReason());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
$this->assertGreaterThanOrEqual($beforeTime, $this->jobPart->getCompletedAt());
$this->assertLessThanOrEqual($afterTime, $this->jobPart->getCompletedAt());
}
public function testMarkAsFailedWithoutReason(): void
{
$result = $this->jobPart->markAsFailed();
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::FAILED, $this->jobPart->getStatus());
$this->assertEquals('', $this->jobPart->getReason());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
}
public function testMarkAsPending(): void
{
// First mark as completed to have something to reset
$this->jobPart->markAsCompleted();
2025-08-05 03:17:55 +02:00
$result = $this->jobPart->markAsPending();
2025-08-05 03:17:55 +02:00
$this->assertSame($this->jobPart, $result);
$this->assertEquals(BulkImportPartStatus::PENDING, $this->jobPart->getStatus());
$this->assertNull($this->jobPart->getReason());
$this->assertNull($this->jobPart->getCompletedAt());
}
public function testIsPending(): void
{
$this->assertTrue($this->jobPart->isPending());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::COMPLETED);
$this->assertFalse($this->jobPart->isPending());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::SKIPPED);
$this->assertFalse($this->jobPart->isPending());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::FAILED);
$this->assertFalse($this->jobPart->isPending());
}
public function testIsCompleted(): void
{
$this->assertFalse($this->jobPart->isCompleted());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::COMPLETED);
$this->assertTrue($this->jobPart->isCompleted());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::SKIPPED);
$this->assertFalse($this->jobPart->isCompleted());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::FAILED);
$this->assertFalse($this->jobPart->isCompleted());
}
public function testIsSkipped(): void
{
$this->assertFalse($this->jobPart->isSkipped());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::SKIPPED);
$this->assertTrue($this->jobPart->isSkipped());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::COMPLETED);
$this->assertFalse($this->jobPart->isSkipped());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::FAILED);
$this->assertFalse($this->jobPart->isSkipped());
}
public function testIsFailed(): void
{
$this->assertFalse($this->jobPart->isFailed());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::FAILED);
$this->assertTrue($this->jobPart->isFailed());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::COMPLETED);
$this->assertFalse($this->jobPart->isFailed());
2025-08-05 03:17:55 +02:00
$this->jobPart->setStatus(BulkImportPartStatus::SKIPPED);
$this->assertFalse($this->jobPart->isFailed());
}
public function testBulkImportPartStatusEnum(): void
{
$this->assertEquals('pending', BulkImportPartStatus::PENDING->value);
$this->assertEquals('completed', BulkImportPartStatus::COMPLETED->value);
$this->assertEquals('skipped', BulkImportPartStatus::SKIPPED->value);
$this->assertEquals('failed', BulkImportPartStatus::FAILED->value);
}
public function testStatusTransitions(): void
{
// Test pending -> completed
$this->assertTrue($this->jobPart->isPending());
$this->jobPart->markAsCompleted();
$this->assertTrue($this->jobPart->isCompleted());
2025-08-05 03:17:55 +02:00
// Test completed -> pending
$this->jobPart->markAsPending();
$this->assertTrue($this->jobPart->isPending());
2025-08-05 03:17:55 +02:00
// Test pending -> skipped
$this->jobPart->markAsSkipped('Test reason');
$this->assertTrue($this->jobPart->isSkipped());
2025-08-05 03:17:55 +02:00
// Test skipped -> pending
$this->jobPart->markAsPending();
$this->assertTrue($this->jobPart->isPending());
2025-08-05 03:17:55 +02:00
// Test pending -> failed
$this->jobPart->markAsFailed('Test error');
$this->assertTrue($this->jobPart->isFailed());
2025-08-05 03:17:55 +02:00
// Test failed -> pending
$this->jobPart->markAsPending();
$this->assertTrue($this->jobPart->isPending());
}
public function testReasonAndCompletedAtConsistency(): void
{
// Initially no reason or completion time
$this->assertNull($this->jobPart->getReason());
$this->assertNull($this->jobPart->getCompletedAt());
2025-08-05 03:17:55 +02:00
// After marking as skipped, should have reason and completion time
$this->jobPart->markAsSkipped('Skipped reason');
$this->assertEquals('Skipped reason', $this->jobPart->getReason());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
2025-08-05 03:17:55 +02:00
// After marking as pending, reason and completion time should be cleared
$this->jobPart->markAsPending();
$this->assertNull($this->jobPart->getReason());
$this->assertNull($this->jobPart->getCompletedAt());
2025-08-05 03:17:55 +02:00
// After marking as failed, should have reason and completion time
$this->jobPart->markAsFailed('Failed reason');
$this->assertEquals('Failed reason', $this->jobPart->getReason());
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
2025-08-05 03:17:55 +02:00
// After marking as completed, should have completion time (reason may remain from previous state)
$this->jobPart->markAsCompleted();
$this->assertInstanceOf(\DateTimeImmutable::class, $this->jobPart->getCompletedAt());
}
}