Added tests for the remaining API endpoints

This commit is contained in:
Jan Böhmer 2024-03-03 23:07:55 +01:00
parent 3d67ad870a
commit af98842090
10 changed files with 503 additions and 1 deletions

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
use App\Tests\API\AuthenticatedApiTestCase;
class ApiTokenEnpointTest extends AuthenticatedApiTestCase
{
public function testGetCurrentToken(): void
{
$response = self::createAuthenticatedClient()->request('GET', '/api/tokens/current');
self::assertResponseIsSuccessful();
self::assertJsonContains([
'name' => 'admin',
'level' => 3,
]);
}
}

View file

@ -21,7 +21,7 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Tests\Endpoints; namespace App\Tests\API\Endpoints;
use App\Tests\API\Endpoints\CrudEndpointTestCase; use App\Tests\API\Endpoints\CrudEndpointTestCase;

View file

@ -44,6 +44,16 @@ abstract class CrudEndpointTestCase extends AuthenticatedApiTestCase
return $basePath . $id; return $basePath . $id;
} }
/**
* Returns the id of the created element from the response.
* @param ResponseInterface $response
* @return int
*/
protected function getIdOfCreatedElement(ResponseInterface $response): int
{
return $response->toArray(true)['id'];
}
protected function _testGetCollection(): ResponseInterface protected function _testGetCollection(): ResponseInterface
{ {
$response = self::createAuthenticatedClient()->request('GET', $this->getBasePath()); $response = self::createAuthenticatedClient()->request('GET', $this->getBasePath());

View file

@ -0,0 +1,76 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
use App\Tests\API\Endpoints\CrudEndpointTestCase;
class OrderdetailsEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/orderdetails';
}
public function testGetCollection(): void
{
$this->_testGetCollection();
self::assertJsonContains([
'hydra:totalItems' => 2,
]);
}
public function testGetItem(): void
{
$this->_testGetItem(1);
$this->_testGetItem(2);
}
public function testCreateItem(): void
{
$this->_testPostItem([
'supplier' => '/api/suppliers/1',
'part' => '/api/parts/2',
]);
}
public function testUpdateItem(): void
{
$response = $this->_testPostItem([
'supplier' => '/api/suppliers/1',
'part' => '/api/parts/2',
]);
$id = $this->getIdOfCreatedElement($response);
$this->_testPatchItem($id, [
'supplierpartnr' => 'API test',
]);
}
public function testDeleteItem(): void
{
$this->_testDeleteItem(2);
}
}

View file

@ -0,0 +1,62 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
class ParametersEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/parameters';
}
public function testElementLifecycle(): void
{
//Type should be automatically guessed from the element
$this->_testPostItem([
'name' => 'test',
'element' => '/api/parts/1',
]);
//Or manually set
$response = $this->_testPostItem([
'name' => 'test',
'element' => '/api/footprints/1',
'_type' => 'Footprint'
]);
$id = $this->getIdOfCreatedElement($response);
//Check if the new item is in the database
$this->_testGetItem($id);
//Check if we can change the item
$this->_testPatchItem($id, [
'name' => 'test2',
]);
//Check if we can delete the item
$this->_testDeleteItem($id);
}
}

View file

@ -0,0 +1,62 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
use App\Tests\API\Endpoints\CrudEndpointTestCase;
class PartAssociationsEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/part_associations';
}
public function testGetCollection(): void
{
$this->_testGetCollection();
self::assertJsonContains([
'hydra:totalItems' => 0,
]);
}
public function testLifeCycle(): void
{
$response = $this->_testPostItem([
"owner" => '/api/parts/1',
"other" => '/api/parts/2',
'type' => 1,
]);
$id = $this->getIdOfCreatedElement($response);
$this->_testGetItem($id);
$this->_testPatchItem($id, [
'comment' => 'Test comment'
]);
$this->_testDeleteItem($id);
}
}

View file

@ -0,0 +1,71 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
use App\Tests\API\Endpoints\CrudEndpointTestCase;
class PartLotsEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/part_lots';
}
public function testGetCollection(): void
{
$this->_testGetCollection();
self::assertJsonContains([
'hydra:totalItems' => 2,
]);
}
public function testGetItem(): void
{
$this->_testGetItem(1);
$this->_testGetItem(2);
}
public function testCreateItem(): void
{
$this->_testPostItem([
'name' => 'API test',
'part' => '/api/parts/1',
'storage_location' => '/api/storage_locations/1',
'amount' => 100,
]);
}
public function testUpdateItem(): void
{
$this->_testPatchItem(1, [
'amount' => 220
]);
}
public function testDeleteItem(): void
{
$this->_testDeleteItem(1);
}
}

View file

@ -0,0 +1,72 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
use App\Tests\API\Endpoints\CrudEndpointTestCase;
class PricedetailsEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/pricedetails';
}
public function testGetCollection(): void
{
$this->_testGetCollection();
self::assertJsonContains([
'hydra:totalItems' => 4,
]);
}
public function testGetItem(): void
{
$this->_testGetItem(1);
$this->_testGetItem(2);
$this->_testGetItem(3);
}
public function testCreateItem(): void
{
$this->_testPostItem([
'price' => '2.0',
'orderdetail' => '/api/orderdetails/1',
'min_discount_quantity' => 1000,
]);
}
public function testUpdateItem(): void
{
$this->_testPatchItem(1, [
'price' => '3.5',
'min_discount_quantity' => 10,
]);
}
public function testDeleteItem(): void
{
$this->_testDeleteItem(1);
}
}

View file

@ -0,0 +1,66 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
class ProjectBOMEntriesEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/project_bom_entries';
}
public function testGetCollection(): void
{
$this->_testGetCollection();
}
public function testItemLifecycle(): void
{
$response = $this->_testPostItem([
'project' => '/api/projects/1',
'part' => '/api/parts/1',
'quantity' => 1,
]);
$new_id = $this->getIdOfCreatedElement($response);
//Check if the new item is in the database
$this->_testGetItem($new_id);
//Check if we can change the item
$this->_testPatchItem($new_id, [
'quantity' => 2,
]);
//Check if we can delete the item
$this->_testDeleteItem($new_id);
}
public function testGetBomOfProject(): void
{
$response = self::createAuthenticatedClient()->request('GET', '/api/projects/1/bom');
self::assertResponseIsSuccessful();
}
}

View file

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\API\Endpoints;
class UsersEndpointTest extends CrudEndpointTestCase
{
protected function getBasePath(): string
{
return '/api/users';
}
public function testGetCollection(): void
{
$this->_testGetCollection();
}
public function testGetItem(): void
{
$this->_testGetItem(1);
}
}