86 lines
3.1 KiB
PHP
86 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Tests\Unit\Infrastructure\Messenger\Handler;
|
||
|
|
|
||
|
|
use App\Domain\Article\ArticleType;
|
||
|
|
use App\Domain\Article\AttributeDefinition;
|
||
|
|
use App\Domain\Article\AttributeType;
|
||
|
|
use App\Domain\Pipeline\AIPipelineJob;
|
||
|
|
use App\Domain\Pipeline\AIPipelineJobType;
|
||
|
|
use App\Domain\Pipeline\Repository\AIPipelineJobRepositoryInterface;
|
||
|
|
use App\Infrastructure\Messenger\Handler\ValidationHandler;
|
||
|
|
use App\Infrastructure\Messenger\Message\DraftArticleMessage;
|
||
|
|
use App\Infrastructure\Messenger\Message\JsonCodingMessage;
|
||
|
|
use App\Infrastructure\Messenger\Message\ValidationMessage;
|
||
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use Symfony\Component\Messenger\Envelope;
|
||
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
||
|
|
|
||
|
|
final class ValidationHandlerTest extends TestCase
|
||
|
|
{
|
||
|
|
private AIPipelineJobRepositoryInterface&MockObject $jobRepo;
|
||
|
|
private MessageBusInterface&MockObject $bus;
|
||
|
|
private AIPipelineJob $job;
|
||
|
|
private ValidationHandler $handler;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
$this->jobRepo = $this->createMock(AIPipelineJobRepositoryInterface::class);
|
||
|
|
$this->bus = $this->createMock(MessageBusInterface::class);
|
||
|
|
$this->handler = new ValidationHandler($this->jobRepo, $this->bus);
|
||
|
|
|
||
|
|
$this->job = new AIPipelineJob(AIPipelineJobType::Photo, ['test' => true]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testDispatchesDraftMessageWhenAllAttributesPresent(): void
|
||
|
|
{
|
||
|
|
$this->jobRepo->method('findById')->willReturn($this->job);
|
||
|
|
|
||
|
|
$type = new ArticleType('Notebook');
|
||
|
|
$ramDef = new AttributeDefinition('RAM', AttributeType::String);
|
||
|
|
$cpuDef = new AttributeDefinition('CPU', AttributeType::String);
|
||
|
|
$type->addAttributeDefinition($ramDef);
|
||
|
|
$type->addAttributeDefinition($cpuDef);
|
||
|
|
|
||
|
|
$attributes = [
|
||
|
|
$ramDef->getId()->toRfc4122() => '16 GB',
|
||
|
|
$cpuDef->getId()->toRfc4122() => 'Intel i7',
|
||
|
|
];
|
||
|
|
|
||
|
|
$this->bus->expects(self::once())
|
||
|
|
->method('dispatch')
|
||
|
|
->with(self::isInstanceOf(DraftArticleMessage::class))
|
||
|
|
->willReturn(new Envelope(new \stdClass()));
|
||
|
|
|
||
|
|
($this->handler)(new ValidationMessage(
|
||
|
|
jobId: $this->job->getId()->toRfc4122(),
|
||
|
|
articleTypeId: $type->getId()->toRfc4122(),
|
||
|
|
specsText: 'some specs',
|
||
|
|
attributes: $attributes,
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testRetriesJsonCodingWhenFieldsMissingAndUnderLimit(): void
|
||
|
|
{
|
||
|
|
$this->jobRepo->method('findById')->willReturn($this->job);
|
||
|
|
|
||
|
|
$type = new ArticleType('Notebook');
|
||
|
|
$type->addAttributeDefinition(new AttributeDefinition('RAM', AttributeType::String));
|
||
|
|
|
||
|
|
$this->bus->expects(self::once())
|
||
|
|
->method('dispatch')
|
||
|
|
->with(self::isInstanceOf(JsonCodingMessage::class))
|
||
|
|
->willReturn(new Envelope(new \stdClass()));
|
||
|
|
|
||
|
|
($this->handler)(new ValidationMessage(
|
||
|
|
jobId: $this->job->getId()->toRfc4122(),
|
||
|
|
articleTypeId: $type->getId()->toRfc4122(),
|
||
|
|
specsText: 'some specs',
|
||
|
|
attributes: [],
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|