mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-03 15:14:08 +01:00
test(IT): Test Client use cases
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
303
tests/SP/Modules/Web/Controllers/Client/ClientTest.php
Normal file
303
tests/SP/Modules/Web/Controllers/Client/ClientTest.php
Normal file
@@ -0,0 +1,303 @@
|
||||
<?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\Domain\Core\Exceptions\InvalidClassException;
|
||||
use SP\Infrastructure\Database\QueryResult;
|
||||
use SP\Infrastructure\File\FileException;
|
||||
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
|
||||
{
|
||||
private array $definitions;
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws Exception
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
#[Test]
|
||||
#[BodyChecker('outputCheckerCreate')]
|
||||
public function create()
|
||||
{
|
||||
$container = $this->buildContainer(
|
||||
$this->definitions,
|
||||
$this->buildRequest('get', 'index.php', ['r' => 'client/create'])
|
||||
);
|
||||
|
||||
$this->runApp($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws Exception
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
#[Test]
|
||||
public function deleteMultiple()
|
||||
{
|
||||
$container = $this->buildContainer(
|
||||
$this->definitions,
|
||||
$this->buildRequest('get', 'index.php', ['r' => 'client/delete', 'items' => [100, 200, 300]])
|
||||
);
|
||||
|
||||
$this->runApp($container);
|
||||
|
||||
$this->expectOutputString('{"status":"OK","description":"Clients deleted","data":null}');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws Exception
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
#[Test]
|
||||
public function deleteSingle()
|
||||
{
|
||||
$container = $this->buildContainer(
|
||||
$this->definitions,
|
||||
$this->buildRequest('get', 'index.php', ['r' => 'client/delete/100'])
|
||||
);
|
||||
|
||||
$this->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(
|
||||
$this->definitions,
|
||||
$this->buildRequest('get', 'index.php', ['r' => 'client/edit/100'])
|
||||
);
|
||||
|
||||
$this->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(
|
||||
$this->definitions,
|
||||
$this->buildRequest('post', 'index.php', ['r' => 'client/saveCreate'], $data)
|
||||
);
|
||||
|
||||
$this->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(
|
||||
$this->definitions,
|
||||
$this->buildRequest('post', 'index.php', ['r' => 'client/saveEdit/100'], $data)
|
||||
);
|
||||
|
||||
$this->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(
|
||||
$this->definitions,
|
||||
$this->buildRequest('get', 'index.php', ['r' => 'client/search', 'search' => 'test'])
|
||||
);
|
||||
|
||||
$this->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(
|
||||
$this->definitions,
|
||||
$this->buildRequest('get', 'index.php', ['r' => 'client/view/100'])
|
||||
);
|
||||
|
||||
$this->runApp($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FileException
|
||||
* @throws InvalidClassException
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->definitions = $this->getModuleDefinitions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user