107 lines
3.4 KiB
PHP
107 lines
3.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Tests\Controller;
|
||
|
|
|
||
|
|
use App\Controller\ReserverController;
|
||
|
|
use App\Entity\Product;
|
||
|
|
use App\Entity\ProductReserve;
|
||
|
|
use App\Repository\ProductRepository;
|
||
|
|
use App\Repository\ProductReserveRepository;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
|
|
||
|
|
class ReserverControllerTest extends TestCase
|
||
|
|
{
|
||
|
|
public function testProductCheckAvailable()
|
||
|
|
{
|
||
|
|
// 1. Mock Repositories
|
||
|
|
$productRepository = $this->createMock(ProductRepository::class);
|
||
|
|
$productReserveRepository = $this->createMock(ProductReserveRepository::class);
|
||
|
|
|
||
|
|
// 2. Setup Data
|
||
|
|
$productId = "1";
|
||
|
|
$startStr = "2026-02-07";
|
||
|
|
$endStr = "2026-02-14";
|
||
|
|
|
||
|
|
$product = new Product();
|
||
|
|
// We can't set ID easily if it's generated, but the repo find returning it is enough.
|
||
|
|
|
||
|
|
$productRepository->expects($this->once())
|
||
|
|
->method('find')
|
||
|
|
->with($productId)
|
||
|
|
->willReturn($product);
|
||
|
|
|
||
|
|
$productReserveRepository->expects($this->once())
|
||
|
|
->method('checkAvailability')
|
||
|
|
->willReturn(true);
|
||
|
|
|
||
|
|
// 3. Create Request
|
||
|
|
$request = new Request([], [], [], [], [], [], json_encode([
|
||
|
|
'id' => $productId,
|
||
|
|
'start' => $startStr,
|
||
|
|
'end' => $endStr
|
||
|
|
]));
|
||
|
|
$request->setMethod('POST');
|
||
|
|
$request->headers->set('Content-Type', 'application/json');
|
||
|
|
|
||
|
|
// 4. Instantiate Controller
|
||
|
|
$controller = new ReserverController();
|
||
|
|
|
||
|
|
// 5. Call Method
|
||
|
|
$response = $controller->productCheck($request, $productReserveRepository, $productRepository);
|
||
|
|
|
||
|
|
// 6. Assertions
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$content = json_decode($response->getContent(), true);
|
||
|
|
|
||
|
|
$this->assertArrayHasKey('dispo', $content);
|
||
|
|
$this->assertTrue($content['dispo']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testProductCheckNotAvailable()
|
||
|
|
{
|
||
|
|
// 1. Mock Repositories
|
||
|
|
$productRepository = $this->createMock(ProductRepository::class);
|
||
|
|
$productReserveRepository = $this->createMock(ProductReserveRepository::class);
|
||
|
|
|
||
|
|
// 2. Setup Data
|
||
|
|
$productId = "1";
|
||
|
|
$startStr = "2026-02-07";
|
||
|
|
$endStr = "2026-02-14";
|
||
|
|
|
||
|
|
$product = new Product();
|
||
|
|
|
||
|
|
$productRepository->expects($this->once())
|
||
|
|
->method('find')
|
||
|
|
->with($productId)
|
||
|
|
->willReturn($product);
|
||
|
|
|
||
|
|
$productReserveRepository->expects($this->once())
|
||
|
|
->method('checkAvailability')
|
||
|
|
->willReturn(false);
|
||
|
|
|
||
|
|
// 3. Create Request
|
||
|
|
$request = new Request([], [], [], [], [], [], json_encode([
|
||
|
|
'id' => $productId,
|
||
|
|
'start' => $startStr,
|
||
|
|
'end' => $endStr
|
||
|
|
]));
|
||
|
|
$request->setMethod('POST');
|
||
|
|
$request->headers->set('Content-Type', 'application/json');
|
||
|
|
|
||
|
|
// 4. Instantiate Controller
|
||
|
|
$controller = new ReserverController();
|
||
|
|
|
||
|
|
// 5. Call Method
|
||
|
|
$response = $controller->productCheck($request, $productReserveRepository, $productRepository);
|
||
|
|
|
||
|
|
// 6. Assertions
|
||
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
||
|
|
$content = json_decode($response->getContent(), true);
|
||
|
|
|
||
|
|
$this->assertArrayHasKey('dispo', $content);
|
||
|
|
$this->assertFalse($content['dispo']);
|
||
|
|
}
|
||
|
|
}
|