Files
kupshop/tests/functional/ApiTest.php
2025-08-02 16:30:27 +02:00

200 lines
4.0 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Honza
* Date: 21/7/16
* Time: 9:47 AM.
*/
class ApiTest extends DatabaseTestCase
{
public static $printBody = true;
public static $url = 'https://www.kupshop.local/api/v1';
public static function setUpBeforeClass(): void
{
if (!findModule('api')) {
self::markTestSkipped('Modul api je vypnutý.');
}
}
public function getPest()
{
$pest = new PestJSON(self::$url);
$pest->curl_opts[CURLOPT_SSL_VERIFYHOST] = 0;
$pest->setupAuth('wpj@wpj.cz', 'HzSlterxIZf22KLBFSw6');
$pest->throw_exceptions = true;
return $pest;
}
/*
* Test vlozeni polozky do kosiku
*/
public function testInsertIntoCart()
{
$body = $this->insertCartItem();
$this->printBody($body);
$this->assertNotNull($body);
}
/*
* Test smazani celeho kosiku
*/
public function testDeleteCart()
{
$url = $this->makeURL('/cart');
$pest = $this->getPest();
$order = [];
$body = $pest->delete($url, $order);
$this->printBody($body);
$this->assertNotNull($body);
}
/*
* Test dotazani na vsechny polozky v kosiku
*/
public function testGetAllCart()
{
$url = $this->makeURL('/cart');
$pest = $this->getPest();
$order = [];
$body = $pest->get($url, $order);
$this->printBody($body);
$this->assertNotNull($body);
}
/*
* Test smazani konkretni polozky v kosiku - nejdrive vlozi polozku, pak ji smaze
*/
public function testDeleteCartItem()
{
$response = $this->insertCartItem();
$url = $this->makeURL('/cart/'.$response[2]['id_cart_item']);
$pest = $this->getPest();
$order = [];
$body = $pest->delete($url, $order);
$this->printBody($body);
$this->assertNotNull($body);
}
/*
* Test ziskani konkretni polozky v kosiku - nejdrive vlozi polozku, pak ji nacte
*/
public function testGetCartItem()
{
$response = $this->insertCartItem();
$this->printBody($response);
$url = $this->makeURL('/cart/'.$response[2]['id_cart_item']);
$pest = $this->getPest();
$body = $pest->get($url);
$this->printBody($body);
$this->assertNotNull($body);
}
/*
* Test vytvoreni potvrzeni objednavky
*/
public function testSubmitCart()
{
$this->insertCartItem();
$url = $this->makeURL('/cart/submit');
$pest = $this->getPest();
$order = [];
$body = $pest->post($url, $order);
$this->printBody($body);
$this->assertNotNull($body);
}
public function testGetOrders()
{
$url = $this->makeURL('/order');
$pest = $this->getPest();
$order = [];
$body = $pest->get($url, $order);
$order_code = $body['order_numbers'][0];
$this->printBody($body);
$this->assertNotNull($body);
return $order_code;
}
public function testGetOrderById()
{
$order_code = $this->testGetOrders();
$url = $this->makeURL('/order/'.$order_code);
$pest = $this->getPest();
$order = [];
$body = $pest->get($url, $order);
$this->printBody($body);
$this->assertNotNull($body);
}
/*
* Pomocna funkce pro vkladani do kosiku
*/
private function insertCartItem()
{
$url = $this->makeURL('/cart');
$pest = $this->getPest();
$order = [
['code' => 3,
'quantity' => 4,
],
];
$response = $pest->post($url, $order);
return $response['items'];
}
private function makeURL($object)
{
return self::$url.$object.'?XDEBUG_SESSION_START=10312';
}
private function printBody($body)
{
if (self::$printBody) {
echo '<pre>';
var_dump($body);
echo '</pre>';
}
}
}