module/Application/test/Controller/Frontend/FactoryControllerTest.php
<?php
namespace ApplicationTest\Controller\Frontend;
use Application\Controller\Api\ItemController;
use Application\Controller\Api\ItemParentController;
use Application\Test\AbstractHttpControllerTestCase;
use ApplicationTest\Data;
use Exception;
use Laminas\Http\Header\Location;
use Laminas\Http\Request;
use Laminas\Http\Response;
use function array_replace;
use function count;
use function explode;
class FactoryControllerTest extends AbstractHttpControllerTestCase
{
protected string $applicationConfigPath = __DIR__ . '/../../../../../config/application.config.php';
/**
* @throws Exception
*/
private function createItem(array $params): int
{
$this->reset();
/** @var Request $request */
$request = $this->getRequest();
$request->getHeaders()->addHeader(Data::getAdminAuthHeader(
$this->getApplicationServiceLocator()->get('Config')['keycloak']
));
$this->dispatch('https://www.autowp.ru/api/item', Request::METHOD_POST, $params);
$this->assertResponseStatusCode(201);
$this->assertModuleName('application');
$this->assertControllerName(ItemController::class);
$this->assertMatchedRouteName('api/item/post');
$this->assertActionName('post');
/** @var Response $response */
$response = $this->getResponse();
/** @var Location $location */
$location = $response->getHeaders()->get('Location');
$uri = $location->uri();
$parts = explode('/', $uri->getPath());
return (int) $parts[count($parts) - 1];
}
/**
* @throws Exception
*/
private function addItemParent(int $itemId, int $parentId, array $params = []): void
{
$this->reset();
/** @var Request $request */
$request = $this->getRequest();
$request->getHeaders()->addHeader(Data::getAdminAuthHeader(
$this->getApplicationServiceLocator()->get('Config')['keycloak']
));
$this->dispatch(
'https://www.autowp.ru/api/item-parent',
Request::METHOD_POST,
array_replace([
'item_id' => $itemId,
'parent_id' => $parentId,
], $params)
);
$this->assertResponseStatusCode(201);
$this->assertModuleName('application');
$this->assertControllerName(ItemParentController::class);
$this->assertMatchedRouteName('api/item-parent/post');
$this->assertActionName('post');
}
public function testIndex(): void
{
$factoryId = $this->createItem([
'item_type_id' => 6,
'name' => 'Factory',
]);
$vehcileId = $this->createItem([
'item_type_id' => 1,
'name' => 'Vehicle on factory',
]);
$this->addItemParent($vehcileId, $factoryId);
$this->reset();
$this->dispatch('https://www.autowp.ru/api/item/' . $factoryId, Request::METHOD_GET, [
'fields' => 'related_group_pictures',
]);
$this->assertResponseStatusCode(200);
$this->assertModuleName('application');
$this->assertControllerName(ItemController::class);
$this->assertMatchedRouteName('api/item/item/get');
$this->assertActionName('item');
}
}