2024-01-17 16:17:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
|
2024-01-18 11:01:43 +00:00
|
|
|
use App\Entity\Printer;
|
|
|
|
use App\Service\PrinterService;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2024-01-17 16:17:12 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-01-18 11:01:43 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2024-01-17 16:17:12 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-01-18 11:01:43 +00:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
use function PHPUnit\Framework\once;
|
2024-01-17 16:17:12 +00:00
|
|
|
|
|
|
|
class PrinterServiceTest extends TestCase
|
|
|
|
{
|
2024-01-18 11:01:43 +00:00
|
|
|
private readonly EntityManagerInterface&MockObject $entityManager;
|
|
|
|
private readonly SerializerInterface&MockObject $serializer;
|
|
|
|
private readonly PrinterService $printerService;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->entityManager = self::createMock(EntityManagerInterface::class);
|
|
|
|
$this->serializer = self::createMock(SerializerInterface::class);
|
|
|
|
$this->printerService = new PrinterService($this->entityManager, $this->serializer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function removeShouldInvokeRemoveOnPrinterAndFlush()
|
|
|
|
{
|
|
|
|
$printer = new Printer();
|
|
|
|
|
|
|
|
$this->entityManager->expects(self::once())->method('remove')->with($printer);
|
|
|
|
$this->entityManager->expects(self::once())->method('flush');
|
|
|
|
|
|
|
|
$this->printerService->deletePrinter($printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function validateJsonShouldReturnFalseOnInputIncompleteJson()
|
|
|
|
{
|
|
|
|
$invalidJson = '{
|
|
|
|
"name": "Bambu A1"
|
|
|
|
}';
|
|
|
|
|
|
|
|
self::assertFalse($this->printerService->validateJson($invalidJson));
|
|
|
|
}
|
|
|
|
|
2024-01-17 16:17:12 +00:00
|
|
|
#[Test]
|
2024-01-18 11:01:43 +00:00
|
|
|
public function validateJsonShouldReturnTrueOnInputCompleteJson()
|
|
|
|
{
|
|
|
|
$validJson = '{
|
|
|
|
"name": "Bambu A1 Mini",
|
|
|
|
"price": 10.50,
|
|
|
|
"build_volume": "180x180x180",
|
|
|
|
"max_speed": 1000
|
|
|
|
}';
|
|
|
|
|
|
|
|
self::assertTrue($this->printerService->validateJson($validJson));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function createPrinterShouldInvokePersistAndFlushOnCreatedPrinterObject(){
|
|
|
|
$json = '{
|
|
|
|
"name": "Bambu A1 Mini",
|
|
|
|
"price": 10.50,
|
|
|
|
"build_volume": "180x180x180",
|
|
|
|
"max_speed": 1000
|
|
|
|
}';
|
|
|
|
|
|
|
|
$printer = new Printer();
|
|
|
|
$printer
|
|
|
|
->setName('Bambu A1 Mini')
|
|
|
|
->setPrice(10.50)
|
|
|
|
->setBuildVolume('180x180x180')
|
|
|
|
->setMaxSpeed(1000);
|
|
|
|
|
|
|
|
$this->serializer->expects(self::once())->method('deserialize')->with($json)->willReturn($printer);
|
|
|
|
$this->entityManager->expects(self::once())->method('persist')->with($printer);
|
|
|
|
$this->entityManager->expects(self::once())->method('flush');
|
2024-01-17 16:17:12 +00:00
|
|
|
|
2024-01-18 11:01:43 +00:00
|
|
|
$this->printerService->createPrinter($json);
|
2024-01-17 16:17:12 +00:00
|
|
|
}
|
2024-01-18 12:30:39 +00:00
|
|
|
|
|
|
|
#[Test]
|
|
|
|
public function editPrinterShouldUpdatePrinterWithJsonContent(){
|
|
|
|
$json = '{
|
2024-03-13 05:43:01 +00:00
|
|
|
"name": "Bambu A1 Mini"
|
2024-01-18 12:30:39 +00:00
|
|
|
}';
|
|
|
|
|
|
|
|
$printer = new Printer();
|
|
|
|
$printer
|
|
|
|
->setName('Bambu A1')
|
|
|
|
->setPrice(10.50)
|
|
|
|
->setBuildVolume('180x180x180')
|
|
|
|
->setMaxSpeed(1000);
|
|
|
|
|
|
|
|
|
|
|
|
$expectedPrinter = new Printer();
|
|
|
|
$expectedPrinter
|
|
|
|
->setName('Bambu A1 Mini')
|
|
|
|
->setPrice(10.50)
|
|
|
|
->setBuildVolume('180x180x180')
|
|
|
|
->setMaxSpeed(1000);
|
|
|
|
|
|
|
|
$this->entityManager->expects(self::once())->method('flush');
|
|
|
|
|
|
|
|
self::assertEquals($expectedPrinter, $this->printerService->editPrinter($printer, $json));
|
|
|
|
}
|
2024-01-17 16:17:12 +00:00
|
|
|
}
|