mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 07:04:07 +01:00
* Refactor integration test setup * Refactor application lock process Signed-off-by: Rubén D <nuxsmin@syspass.org>
281 lines
7.8 KiB
PHP
281 lines
7.8 KiB
PHP
<?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/>.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace SP\Modules\Web\Controllers\Client;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\MockObject\Exception;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use SP\Domain\Client\Models\Client;
|
|
use SP\Infrastructure\Database\QueryResult;
|
|
use SP\Tests\BodyChecker;
|
|
use SP\Tests\Generators\ClientGenerator;
|
|
use SP\Tests\IntegrationTestCase;
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
/**
|
|
* Class ClientTest
|
|
*/
|
|
#[Group('integration')]
|
|
class ClientTest extends IntegrationTestCase
|
|
{
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
#[BodyChecker('outputCheckerCreate')]
|
|
public function create()
|
|
{
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/create'])
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
public function deleteMultiple()
|
|
{
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/delete', 'items' => [100, 200, 300]])
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
|
|
$this->expectOutputString('{"status":"OK","description":"Clients deleted","data":null}');
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
public function deleteSingle()
|
|
{
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/delete/100'])
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
|
|
$this->expectOutputString('{"status":"OK","description":"Client deleted","data":null}');
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
#[BodyChecker('outputCheckerEdit')]
|
|
public function edit()
|
|
{
|
|
$this->addDatabaseMapperResolver(
|
|
Client::class,
|
|
new QueryResult([ClientGenerator::factory()->buildClient()])
|
|
);
|
|
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/edit/100'])
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
public function saveCreate()
|
|
{
|
|
$data = [
|
|
'name' => self::$faker->name(),
|
|
'description' => self::$faker->text(),
|
|
'isglobal' => self::$faker->boolean(),
|
|
];
|
|
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'client/saveCreate'], $data)
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
|
|
$this->expectOutputString('{"status":"OK","description":"Client added","data":null}');
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
public function saveEdit()
|
|
{
|
|
$data = [
|
|
'name' => self::$faker->name(),
|
|
'description' => self::$faker->text(),
|
|
'isglobal' => self::$faker->boolean(),
|
|
];
|
|
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'client/saveEdit/100'], $data)
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
|
|
$this->expectOutputString('{"status":"OK","description":"Client updated","data":null}');
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
#[BodyChecker('outputCheckerSearch')]
|
|
public function search()
|
|
{
|
|
$clientGenerator = ClientGenerator::factory();
|
|
|
|
$this->addDatabaseMapperResolver(
|
|
Client::class,
|
|
QueryResult::withTotalNumRows(
|
|
[
|
|
$clientGenerator->buildClient(),
|
|
$clientGenerator->buildClient()
|
|
],
|
|
2
|
|
)
|
|
);
|
|
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/search', 'search' => 'test'])
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws Exception
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
#[Test]
|
|
#[BodyChecker('outputCheckerView')]
|
|
public function view()
|
|
{
|
|
$this->addDatabaseMapperResolver(
|
|
Client::class,
|
|
new QueryResult([ClientGenerator::factory()->buildClient()])
|
|
);
|
|
|
|
$container = $this->buildContainer(
|
|
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'client/view/100'])
|
|
);
|
|
|
|
IntegrationTestCase::runApp($container);
|
|
}
|
|
|
|
/**
|
|
* @param string $output
|
|
* @return void
|
|
*/
|
|
private function outputCheckerCreate(string $output): void
|
|
{
|
|
$json = json_decode($output);
|
|
|
|
$crawler = new Crawler($json->data->html);
|
|
$filter = $crawler->filterXPath(
|
|
'//div[@id="box-popup"]//form[@name="frmClients"]//select|//input'
|
|
)->extract(['_name']);
|
|
|
|
self::assertCount(4, $filter);
|
|
}
|
|
|
|
/**
|
|
* @param string $output
|
|
* @return void
|
|
*/
|
|
private function outputCheckerEdit(string $output): void
|
|
{
|
|
$json = json_decode($output);
|
|
|
|
$crawler = new Crawler($json->data->html);
|
|
$filter = $crawler->filterXPath(
|
|
'//div[@id="box-popup"]//form[@name="frmClients"]//select|//input'
|
|
)->extract(['_name']);
|
|
|
|
self::assertCount(4, $filter);
|
|
self::assertEquals('OK', $json->status);
|
|
}
|
|
|
|
/**
|
|
* @param string $output
|
|
* @return void
|
|
*/
|
|
private function outputCheckerSearch(string $output): void
|
|
{
|
|
$json = json_decode($output);
|
|
|
|
$crawler = new Crawler($json->data->html);
|
|
$filter = $crawler->filterXPath(
|
|
'//table/tbody[@id="data-rows-tblClients"]//tr[string-length(@data-item-id) > 0]'
|
|
)->extract(['data-item-id']);
|
|
|
|
self::assertCount(2, $filter);
|
|
self::assertEquals('OK', $json->status);
|
|
}
|
|
|
|
/**
|
|
* @param string $output
|
|
* @return void
|
|
*/
|
|
private function outputCheckerView(string $output): void
|
|
{
|
|
$json = json_decode($output);
|
|
|
|
$crawler = new Crawler($json->data->html);
|
|
$filter = $crawler->filterXPath(
|
|
'//div[@id="box-popup"]//form[@name="frmClients"]//select|//input'
|
|
)->extract(['_name']);
|
|
|
|
self::assertCount(4, $filter);
|
|
self::assertEquals('OK', $json->status);
|
|
}
|
|
}
|