Files
shopPRO/tests/Unit/api/Controllers/DictionariesApiControllerTest.php
Jacek Pyziak 8e2e070eb7 ver. 0.320: API słowniki — ensure_producer; ProductRepository — producer_name w odpowiedzi
- DictionariesApiController: nowy endpoint POST ensure_producer (znajdź lub utwórz producenta)
- ProducerRepository: metoda ensureProducerForApi()
- ProductRepository: pole producer_name w odpowiedzi GET product
- ApiRouter: wstrzyknięto ProducerRepository do DictionariesApiController
- Zaktualizowano docs/API.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 13:32:25 +01:00

242 lines
7.2 KiB
PHP

<?php
namespace Tests\Unit\api\Controllers;
use PHPUnit\Framework\TestCase;
use api\Controllers\DictionariesApiController;
use Domain\Attribute\AttributeRepository;
use Domain\Producer\ProducerRepository;
use Domain\ShopStatus\ShopStatusRepository;
use Domain\Transport\TransportRepository;
use Domain\PaymentMethod\PaymentMethodRepository;
class DictionariesApiControllerTest extends TestCase
{
private $mockStatusRepo;
private $mockTransportRepo;
private $mockPaymentRepo;
private $mockAttrRepo;
private $mockProducerRepo;
private $controller;
protected function setUp(): void
{
$this->mockStatusRepo = $this->createMock(ShopStatusRepository::class);
$this->mockTransportRepo = $this->createMock(TransportRepository::class);
$this->mockPaymentRepo = $this->createMock(PaymentMethodRepository::class);
$this->mockAttrRepo = $this->createMock(AttributeRepository::class);
$this->mockProducerRepo = $this->createMock(ProducerRepository::class);
$this->controller = new DictionariesApiController(
$this->mockStatusRepo,
$this->mockTransportRepo,
$this->mockPaymentRepo,
$this->mockAttrRepo,
$this->mockProducerRepo
);
$_SERVER['REQUEST_METHOD'] = 'GET';
}
protected function tearDown(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
http_response_code(200);
}
// --- statuses ---
public function testStatusesReturnsFormattedList(): void
{
$this->mockStatusRepo->method('allStatuses')
->willReturn([
0 => 'Nowe',
1 => 'Opłacone',
4 => 'W realizacji',
6 => 'Wysłane',
]);
ob_start();
$this->controller->statuses();
$output = ob_get_clean();
$json = json_decode($output, true);
$this->assertSame('ok', $json['status']);
$this->assertCount(4, $json['data']);
$this->assertSame(0, $json['data'][0]['id']);
$this->assertSame('Nowe', $json['data'][0]['name']);
$this->assertSame(6, $json['data'][3]['id']);
$this->assertSame('Wysłane', $json['data'][3]['name']);
}
public function testStatusesRejectsPostMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
$this->controller->statuses();
$output = ob_get_clean();
$this->assertSame(405, http_response_code());
}
// --- transports ---
public function testTransportsReturnsFormattedList(): void
{
$this->mockTransportRepo->method('allActive')
->willReturn([
['id' => 1, 'name_visible' => 'InPost Paczkomat', 'cost' => '12.99'],
['id' => 2, 'name_visible' => 'Kurier DPD', 'cost' => '15.00'],
]);
ob_start();
$this->controller->transports();
$output = ob_get_clean();
$json = json_decode($output, true);
$this->assertSame('ok', $json['status']);
$this->assertCount(2, $json['data']);
$this->assertSame(1, $json['data'][0]['id']);
$this->assertSame('InPost Paczkomat', $json['data'][0]['name']);
$this->assertSame(12.99, $json['data'][0]['cost']);
}
public function testTransportsRejectsPostMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
$this->controller->transports();
$output = ob_get_clean();
$this->assertSame(405, http_response_code());
}
// --- payment_methods ---
public function testPaymentMethodsReturnsFormattedList(): void
{
$this->mockPaymentRepo->method('allActive')
->willReturn([
['id' => 1, 'name' => 'Przelew bankowy'],
['id' => 2, 'name' => 'Przelewy24'],
['id' => 3, 'name' => 'Przy odbiorze'],
]);
ob_start();
$this->controller->payment_methods();
$output = ob_get_clean();
$json = json_decode($output, true);
$this->assertSame('ok', $json['status']);
$this->assertCount(3, $json['data']);
$this->assertSame(1, $json['data'][0]['id']);
$this->assertSame('Przelew bankowy', $json['data'][0]['name']);
}
public function testPaymentMethodsRejectsPostMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
$this->controller->payment_methods();
$output = ob_get_clean();
$this->assertSame(405, http_response_code());
}
// --- attributes ---
public function testAttributesReturnsFormattedList(): void
{
$this->mockAttrRepo->method('listForApi')
->willReturn([
[
'id' => 5,
'type' => 0,
'status' => 1,
'names' => ['pl' => 'Rozmiar', 'en' => 'Size'],
'values' => [
[
'id' => 12,
'names' => ['pl' => 'M', 'en' => 'M'],
'is_default' => 1,
'impact_on_the_price' => null,
],
],
],
]);
ob_start();
$this->controller->attributes();
$output = ob_get_clean();
$json = json_decode($output, true);
$this->assertSame('ok', $json['status']);
$this->assertCount(1, $json['data']);
$this->assertSame(5, $json['data'][0]['id']);
$this->assertSame('Rozmiar', $json['data'][0]['names']['pl']);
$this->assertCount(1, $json['data'][0]['values']);
$this->assertSame(12, $json['data'][0]['values'][0]['id']);
}
public function testAttributesRejectsPostMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
$this->controller->attributes();
$output = ob_get_clean();
$this->assertSame(405, http_response_code());
}
public function testEnsureAttributeRejectsGetMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
ob_start();
$this->controller->ensure_attribute();
ob_get_clean();
$this->assertSame(405, http_response_code());
}
public function testEnsureAttributeReturns400WhenNoBody(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
$this->controller->ensure_attribute();
$output = ob_get_clean();
$this->assertSame(400, http_response_code());
$json = json_decode($output, true);
$this->assertSame('BAD_REQUEST', $json['code']);
}
public function testEnsureAttributeValueRejectsGetMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
ob_start();
$this->controller->ensure_attribute_value();
ob_get_clean();
$this->assertSame(405, http_response_code());
}
public function testEnsureAttributeValueReturns400WhenNoBody(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
$this->controller->ensure_attribute_value();
$output = ob_get_clean();
$this->assertSame(400, http_response_code());
$json = json_decode($output, true);
$this->assertSame('BAD_REQUEST', $json['code']);
}
}