2023-12-15 17:51:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Tests\Helper;
|
|
|
|
|
|
|
|
use App\Entity\Product;
|
|
|
|
use App\Entity\Stock;
|
|
|
|
use App\Entity\Warehouse;
|
|
|
|
use App\Helper\HiltesImport;
|
|
|
|
use App\Repository\ProductRepository;
|
|
|
|
use App\Repository\StockRepository;
|
|
|
|
use App\Repository\WarehouseRepository;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class HiltesImportTest extends TestCase
|
|
|
|
{
|
|
|
|
private $productRepository;
|
|
|
|
private $stockRepository;
|
2024-03-01 10:15:12 +01:00
|
|
|
private $warehouseRepository;
|
2023-12-15 17:51:24 +01:00
|
|
|
private $logger;
|
|
|
|
private $hiltesImport;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->productRepository = $this->createMock(ProductRepository::class);
|
|
|
|
$this->stockRepository = $this->createMock(StockRepository::class);
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->warehouseRepository = $this->createMock(WarehouseRepository::class);
|
2023-12-15 17:51:24 +01:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
|
|
|
|
$this->hiltesImport = new HiltesImport(
|
|
|
|
$this->productRepository,
|
|
|
|
$this->warehouseRepository,
|
|
|
|
$this->stockRepository,
|
|
|
|
$this->logger,
|
2024-03-01 10:15:12 +01:00
|
|
|
'/path/to/root'
|
2023-12-15 17:51:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
public function testStartImportWithNoFiles(): void
|
2023-12-15 17:51:24 +01:00
|
|
|
{
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->assertEquals([], $this->hiltesImport->startImport());
|
2023-12-15 17:51:24 +01:00
|
|
|
}
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
public function testStartImportWithFiles(): void
|
2023-12-15 17:51:24 +01:00
|
|
|
{
|
|
|
|
// Mock the getFiles method to return true
|
|
|
|
$hiltesImport = $this->getMockBuilder(HiltesImport::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->productRepository,
|
|
|
|
$this->warehouseRepository,
|
|
|
|
$this->stockRepository,
|
|
|
|
$this->logger,
|
2024-03-01 10:15:12 +01:00
|
|
|
'/path/to/root'
|
2023-12-15 17:51:24 +01:00
|
|
|
])
|
|
|
|
->onlyMethods(['getFiles'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$hiltesImport->method('getFiles')->willReturn(true);
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->assertIsArray($hiltesImport->startImport());
|
2023-12-15 17:51:24 +01:00
|
|
|
}
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
public function testSaveDataWithInvalidData(): void
|
2023-12-15 17:51:24 +01:00
|
|
|
{
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->assertFalse($this->hiltesImport->saveData([]));
|
2023-12-15 17:51:24 +01:00
|
|
|
}
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
public function testSaveDataWithValidData(): void
|
2023-12-15 17:51:24 +01:00
|
|
|
{
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->productRepository->method('findOneBy')->willReturn(new Product());
|
|
|
|
$this->warehouseRepository->method('findOneBy')->willReturn(new Warehouse());
|
2023-12-15 17:51:24 +01:00
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->assertTrue($this->hiltesImport->saveData(['1234567890123', '100', '', 'Warehouse1']));
|
2023-12-15 17:51:24 +01:00
|
|
|
}
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
public function testCheckProductWithExistingProduct(): void
|
2023-12-15 17:51:24 +01:00
|
|
|
{
|
|
|
|
$product = new Product();
|
2024-03-01 10:15:12 +01:00
|
|
|
$product->setGtin('1234567890123');
|
|
|
|
$this->productRepository->method('findOneBy')->willReturn($product);
|
|
|
|
|
|
|
|
$this->assertEquals($product->getId(), $this->hiltesImport->checkProduct('1234567890123'));
|
|
|
|
}
|
2023-12-15 17:51:24 +01:00
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
public function testCheckProductWithNewProduct(): void
|
|
|
|
{
|
|
|
|
$this->productRepository->method('findOneBy')->willReturn(null);
|
|
|
|
|
|
|
|
$this->assertNull($this->hiltesImport->checkProduct('1234567890123'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckWarehouseNameWithExistingWarehouse(): void
|
|
|
|
{
|
|
|
|
$warehouse = new Warehouse();
|
|
|
|
$warehouse->setName('Warehouse1');
|
2023-12-15 17:51:24 +01:00
|
|
|
$this->warehouseRepository->method('findOneBy')->willReturn($warehouse);
|
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->assertEquals($warehouse, $this->hiltesImport->checkWarehouseName('Warehouse1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckWarehouseNameWithNewWarehouse(): void
|
|
|
|
{
|
|
|
|
$this->warehouseRepository->method('findOneBy')->willReturn(null);
|
2023-12-15 17:51:24 +01:00
|
|
|
|
2024-03-01 10:15:12 +01:00
|
|
|
$this->assertInstanceOf(Warehouse::class, $this->hiltesImport->checkWarehouseName('Warehouse1'));
|
2023-12-15 17:51:24 +01:00
|
|
|
}
|
|
|
|
}
|