chore(tests): UT for adapters

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-04-29 07:55:38 +02:00
parent 602e054179
commit 5e968200bb
9 changed files with 329 additions and 34 deletions

View File

@@ -91,7 +91,7 @@ use SP\Domain\Install\Adapters\InstallDataFactory;
use SP\Domain\Install\Services\DatabaseSetupInterface;
use SP\Domain\Install\Services\MysqlSetupBuilder;
use SP\Domain\Log\Providers\DatabaseHandler;
use SP\Domain\Log\Providers\FileHandler;
use SP\Domain\Log\Providers\FileHandler as LogFileHandler;
use SP\Domain\Log\Providers\RemoteSyslogHandler;
use SP\Domain\Log\Providers\SyslogHandler;
use SP\Domain\Notification\Ports\MailerInterface;
@@ -134,7 +134,7 @@ final class CoreDefinitions
ConfigFileService::class => create(ConfigFile::class)
->constructor(
create(XmlFileStorage::class)
->constructor(create(FileHandler::class)->constructor(CONFIG_FILE)),
->constructor(create(LogFileHandler::class)->constructor(CONFIG_FILE)),
create(FileCache::class)->constructor(ConfigFile::CONFIG_CACHE_FILE),
get(Context::class),
autowire(ConfigBackup::class)
@@ -218,17 +218,17 @@ final class CoreDefinitions
return MysqlSetupBuilder::build($installData);
}
throw new SPException(__u('Unimplemented'), SPException::ERROR, __u('Wrong backend type'));
throw SPException::error(__u('Unimplemented'), __u('Wrong backend type'));
},
ProvidersHelper::class => factory(static function (ContainerInterface $c) {
$configData = $c->get(ConfigDataInterface::class);
if (!$configData->isInstalled()) {
return new ProvidersHelper($c->get(FileHandler::class));
return new ProvidersHelper($c->get(LogFileHandler::class));
}
return new ProvidersHelper(
$c->get(FileHandler::class),
$c->get(LogFileHandler::class),
$c->get(DatabaseHandler::class),
$c->get(MailHandler::class),
$c->get(SyslogHandler::class),

View File

@@ -53,8 +53,8 @@ final class Category extends Adapter implements CategoryAdapter
public function __construct(
ConfigDataInterface $configData,
string $baseUrl,
private readonly CustomFieldDataService $customFieldService,
string $baseUrl,
private readonly CustomFieldDataService $customFieldDataService,
private readonly ActionsInterface $actions
) {
parent::__construct($configData, $baseUrl);
@@ -69,7 +69,7 @@ final class Category extends Adapter implements CategoryAdapter
public function includeCustomFields(CategoryModel $data): Collection
{
return $this->collection(
$this->getCustomFieldsForItem(AclActionsInterface::CATEGORY, $data->id, $this->customFieldService),
$this->getCustomFieldsForItem(AclActionsInterface::CATEGORY, $data->getId(), $this->customFieldDataService),
new CustomField($this->configData, $this->baseUrl)
);
}

View File

@@ -33,8 +33,6 @@ use SP\Domain\Core\Exceptions\SPException;
/**
* Class CategoryAdapter
*
* @package SP\Adapters
*/
interface CategoryAdapter
{

View File

@@ -31,7 +31,10 @@ use SP\Domain\Common\Adapters\Adapter;
use SP\Domain\Common\Models\Model;
use SP\Domain\Common\Providers\Link;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\ActionNotFoundException;
use SP\Domain\Core\Acl\ActionsInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
@@ -48,22 +51,36 @@ final class Client extends Adapter implements ClientAdapter
protected array $availableIncludes = ['customFields'];
public function __construct(
ConfigDataInterface $configData,
string $baseUrl,
private readonly CustomFieldDataService $customFieldDataService,
private readonly ActionsInterface $actions
) {
parent::__construct($configData, $baseUrl);
}
/**
* @throws ConstraintException
* @throws QueryException
* @throws SPException
* @throws ServiceException
*/
public function includeCustomFields(ClientModel $client, CustomFieldDataService $customFieldService): Collection
public function includeCustomFields(ClientModel $client): Collection
{
return $this->collection(
$this->getCustomFieldsForItem(AclActionsInterface::CLIENT, $client->getId(), $customFieldService),
$this->getCustomFieldsForItem(AclActionsInterface::CLIENT, $client->getId(), $this->customFieldDataService),
new CustomField($this->configData, $this->baseUrl)
);
}
/**
* @throws ActionNotFoundException
*/
public function transform(Model|ClientModel $data): array
{
$actionRoute = $this->actions->getActionById(AclActionsInterface::CLIENT_VIEW)->getRoute();
return [
'id' => $data->getId(),
'name' => $data->getName(),
@@ -75,7 +92,7 @@ final class Client extends Adapter implements ClientAdapter
'rel' => 'self',
'uri' => Link::getDeepLink(
$data->getId(),
AclActionsInterface::CLIENT_VIEW,
$actionRoute,
$this->configData,
$this->baseUrl
),

View File

@@ -30,12 +30,9 @@ use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
/**
* Class ClientAdapter
*
* @package SP\Adapters
*/
interface ClientAdapter
{
@@ -45,7 +42,7 @@ interface ClientAdapter
* @throws SPException
* @throws ServiceException
*/
public function includeCustomFields(Client $client, CustomFieldDataService $customFieldService): Collection;
public function includeCustomFields(Client $client): Collection;
public function transform(Client $data): array;
}

View File

@@ -40,11 +40,11 @@ use SPT\Generators\CustomFieldGenerator;
use SPT\UnitaryTestCase;
/**
* Class AccountAdapterTest
* Class AccountTest
*
*/
#[Group('unitary')]
class AccountAdapterTest extends UnitaryTestCase
class AccountTest extends UnitaryTestCase
{
/**
* @throws Exception

View File

@@ -0,0 +1,139 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SPT\Domain\Category\Adapters;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Category\Adapters\Category;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\ActionNotFoundException;
use SP\Domain\Core\Acl\ActionsInterface;
use SP\Domain\Core\Models\Action;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SPT\Generators\CategoryGenerator;
use SPT\Generators\CustomFieldGenerator;
use SPT\UnitaryTestCase;
/**
* Class CategoryTest
*/
#[Group('unitary')]
class CategoryTest extends UnitaryTestCase
{
private MockObject|ActionsInterface $actions;
private MockObject|CustomFieldDataService $customFieldDataService;
/**
* @throws ActionNotFoundException
*/
public function testAdapt(): void
{
$category = CategoryGenerator::factory()->buildCategory();
$adapter = new Category(
$this->config->getConfigData(),
'testUrl',
$this->customFieldDataService,
$this->actions
);
$out = $adapter->transform($category);
$this->assertEquals($category->getId(), $out['id']);
$this->assertEquals($category->getName(), $out['name']);
$this->assertEquals($category->getDescription(), $out['description']);
$this->assertNull($out['customFields']);
$this->assertEquals('self', $out['links'][0]['rel']);
$this->assertNotEmpty($out['links'][0]['uri']);
}
/**
* @throws Exception
*/
public function testIncludeCustomFields(): void
{
$category = CategoryGenerator::factory()->buildCategory();
$customFieldData = CustomFieldGenerator::factory()->buildSimpleModel();
$this->customFieldDataService
->expects(self::once())
->method('getBy')
->with(AclActionsInterface::CATEGORY, $category->getId())
->willReturn([$customFieldData]);
$adapter = new Category(
$this->config->getConfigData(),
'testUrl',
$this->customFieldDataService,
$this->actions
);
$fractal = new Manager();
$fractal->parseIncludes('customFields');
$out = $fractal->createData(new Item($category, $adapter))->toArray();
$data = $out['data'];
$this->assertEquals($category->getId(), $data['id']);
$this->assertEquals($category->getName(), $data['name']);
$this->assertEquals($category->getDescription(), $data['description']);
$this->assertArrayHasKey('customFields', $data);
$this->assertEquals($customFieldData['typeName'], $data['customFields']['data'][0]['type']);
$this->assertEquals($customFieldData['typeText'], $data['customFields']['data'][0]['typeText']);
$this->assertEquals($customFieldData['definitionId'], $data['customFields']['data'][0]['definitionId']);
$this->assertEquals(
$customFieldData['definitionName'],
$data['customFields']['data'][0]['definitionName']
);
$this->assertEquals($customFieldData['help'], $data['customFields']['data'][0]['help']);
$this->assertEquals($customFieldData['value'], $data['customFields']['data'][0]['value']);
$this->assertEquals($customFieldData['encrypted'], $data['customFields']['data'][0]['isEncrypted']);
$this->assertEquals($customFieldData['required'], $data['customFields']['data'][0]['required']);
}
protected function setUp(): void
{
parent::setUp();
$this->actions = $this->createMock(ActionsInterface::class);
$this->actions->expects(self::once())
->method('getActionById')
->with(AclActionsInterface::CATEGORY_VIEW)
->willReturn(
new Action(
self::$faker->randomNumber(),
self::$faker->colorName,
self::$faker->sentence,
self::$faker->colorName
)
);
$this->customFieldDataService = $this->createMock(CustomFieldDataService::class);
}
}

View File

@@ -0,0 +1,142 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SPT\Domain\Client\Adapters;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Client\Adapters\Client;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\ActionNotFoundException;
use SP\Domain\Core\Acl\ActionsInterface;
use SP\Domain\Core\Models\Action;
use SP\Domain\CustomField\Ports\CustomFieldDataService;
use SPT\Generators\ClientGenerator;
use SPT\Generators\CustomFieldGenerator;
use SPT\UnitaryTestCase;
/**
* Class ClientTest
*/
#[Group('unitary')]
class ClientTest extends UnitaryTestCase
{
private MockObject|ActionsInterface $actions;
private MockObject|CustomFieldDataService $customFieldDataService;
/**
* @throws ActionNotFoundException
*/
public function testAdapt(): void
{
$client = ClientGenerator::factory()->buildClient();
$adapter = new Client(
$this->config->getConfigData(),
'testUrl',
$this->customFieldDataService,
$this->actions
);
$out = $adapter->transform($client);
$this->assertEquals($client->getId(), $out['id']);
$this->assertEquals($client->getName(), $out['name']);
$this->assertEquals($client->getDescription(), $out['description']);
$this->assertEquals($client->getIsGlobal(), $out['isGlobal']);
$this->assertNull($out['customFields']);
$this->assertEquals('self', $out['links'][0]['rel']);
$this->assertNotEmpty($out['links'][0]['uri']);
}
/**
* @throws Exception
*/
public function testIncludeCustomFields(): void
{
$client = ClientGenerator::factory()->buildClient();
$customFieldData = CustomFieldGenerator::factory()->buildSimpleModel();
$this->customFieldDataService
->expects(self::once())
->method('getBy')
->with(AclActionsInterface::CLIENT, $client->getId())
->willReturn([$customFieldData]);
$adapter = new Client(
$this->config->getConfigData(),
'testUrl',
$this->customFieldDataService,
$this->actions
);
$fractal = new Manager();
$fractal->parseIncludes('customFields');
$out = $fractal->createData(new Item($client, $adapter))->toArray();
$data = $out['data'];
$this->assertEquals($client->getId(), $data['id']);
$this->assertEquals($client->getName(), $data['name']);
$this->assertEquals($client->getDescription(), $data['description']);
$this->assertEquals($client->getIsGlobal(), $data['isGlobal']);
$this->assertArrayHasKey('customFields', $data);
$this->assertEquals($customFieldData['typeName'], $data['customFields']['data'][0]['type']);
$this->assertEquals($customFieldData['typeText'], $data['customFields']['data'][0]['typeText']);
$this->assertEquals($customFieldData['definitionId'], $data['customFields']['data'][0]['definitionId']);
$this->assertEquals(
$customFieldData['definitionName'],
$data['customFields']['data'][0]['definitionName']
);
$this->assertEquals($customFieldData['help'], $data['customFields']['data'][0]['help']);
$this->assertEquals($customFieldData['value'], $data['customFields']['data'][0]['value']);
$this->assertEquals($customFieldData['encrypted'], $data['customFields']['data'][0]['isEncrypted']);
$this->assertEquals($customFieldData['required'], $data['customFields']['data'][0]['required']);
}
protected function setUp(): void
{
parent::setUp();
$this->actions = $this->createMock(ActionsInterface::class);
$this->actions->expects(self::once())
->method('getActionById')
->with(AclActionsInterface::CLIENT_VIEW)
->willReturn(
new Action(
self::$faker->randomNumber(),
self::$faker->colorName,
self::$faker->sentence,
self::$faker->colorName
)
);
$this->customFieldDataService = $this->createMock(CustomFieldDataService::class);
}
}

View File

@@ -41,20 +41,22 @@ final class CustomFieldGenerator extends DataGenerator
$key = $this->faker->sha1();
}
return new Simple([
'required' => $this->faker->boolean(),
'showInList' => $this->faker->boolean(),
'help' => $this->faker->text(),
'definitionId' => $this->faker->randomNumber(3),
'definitionName' => $this->faker->name(),
'typeId' => $this->faker->randomNumber(3),
'typeName' => $this->faker->name(),
'typeText' => $this->faker->text(),
'moduleId' => $this->faker->randomNumber(3),
'formId' => $this->faker->randomNumber(3),
'isEncrypted' => $this->faker->boolean(),
'data' => $data,
'key' => $key,
]);
return new Simple(
[
'required' => $this->faker->boolean(),
'showInList' => $this->faker->boolean(),
'help' => $this->faker->text(),
'definitionId' => $this->faker->randomNumber(3),
'definitionName' => $this->faker->name(),
'typeId' => $this->faker->randomNumber(3),
'typeName' => $this->faker->name(),
'typeText' => $this->faker->text(),
'moduleId' => $this->faker->randomNumber(3),
'formId' => $this->faker->randomNumber(3),
'isEncrypted' => $this->faker->boolean(),
'data' => $data,
'key' => $key,
]
);
}
}